<?php
/**
 * @file
 * Functions to expose SuiteDesk expense data to views
 */
function stormexpense_views_data() {
  $data['stormexpense']['table']['group']  = t('SuiteDesk Expense');
  $data['stormexpense']['table']['join'] = array(
    'node' => array(
      'left_field' => 'vid',
      'field' => 'vid',
    ),
  );

  $data['stormexpense']['organization_nid'] = array(
    'title' => t('Organization'),
    'help' => t('Expense -> Organization'),
    'relationship' => array(
      'base' => 'node',
      'field' => 'nid',
      'handler' => 'views_handler_relationship',
      'label' => t('Expense -> Organization'),
    ),
  );

  $data['stormexpense']['organization_title'] = array(
    'title' => t('Organization'),
    'help' => t('SuiteDesk Expense: Organization (title only)'),
    'field' => array(
      'click sortable' => TRUE,
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );

  $data['stormexpense']['project_nid'] = array(
    'title' => t('Project'),
    'help' => t('Expense -> Project'),
    'relationship' => array(
      'base' => 'node',
      'field' => 'nid',
      'handler' => 'views_handler_relationship',
      'label' => t('Expense -> Project'),
    ),
  );

  $data['stormexpense']['project_title'] = array(
    'title' => t('Project'),
    'help' => t('SuiteDesk Expense Project (title only)'),
    'field' => array(
      'click sortable' => TRUE,
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );

  $data['stormexpense']['task_nid'] = array(
    'title' => t('Task'),
    'help' => t('Expense -> Task'),
    'relationship' => array(
      'base' => 'node',
      'field' => 'nid',
      'handler' => 'views_handler_relationship',
      'label' => t('Expense -> Task'),
    ),
  );

  $data['stormexpense']['task_stepno'] = array(
    'title' => t('Task step number'),
    'help' => t('SuiteDesk Expense Task Step Number'),
    'field' => array(
      'click sortable' => TRUE,
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_numeric',
    ),
  );

  $data['stormexpense']['task_title'] = array(
    'title' => t('Task'),
    'help' => t('SuiteDesk Expense Task (title only)'),
    'field' => array(
      'click sortable' => TRUE,
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );

  $data['stormexpense']['ticket_nid'] = array(
    'title' => t('Ticket'),
    'help' => t('Expense -> Ticket'),
    'relationship' => array(
      'base' => 'node',
      'field' => 'nid',
      'handler' => 'views_handler_relationship',
      'label' => t('Expense -> Ticket'),
    ),
  );

  $data['stormexpense']['ticket_title'] = array(
    'title' => t('Ticket'),
    'help' => t('SuiteDesk Expense Ticket (title only)'),
    'field' => array(
      'click sortable' => TRUE,
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );

  $data['stormexpense']['provider_nid'] = array(
    'title' => t('Provider'),
    'help' => t('Expense -> Provider'),
    'relationship' => array(
      'base' => 'node',
      'field' => 'nid',
      'handler' => 'views_handler_relationship',
      'label' => t('Expense -> Provider'),
    ),
  );

  $data['stormexpense']['provider_title'] = array(
    'title' => t('Provider'),
    'help' => t('SuiteDesk Expense Provider (title only)'),
    'field' => array(
      'click sortable' => TRUE,
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );

  $data['stormexpense']['expensedate'] = array(
    'title' => t('Date'),
    'help' => t('SuiteDesk Expense Date'),
    'field' => array(
      'click sortable' => TRUE,
      'handler' => 'views_handler_field_date',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort_date',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_date',
    ),
  );

  $data['stormexpense']['amount'] = array(
    'title' => t('Amount'),
    'help' => t('SuiteDesk Expense Amount'),
    'field' => array(
      'click sortable' => TRUE,
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_numeric',
    ),
  );

  $data['stormexpense']['tax'] = array(
    'title' => t('Tax'),
    'help' => t('SuiteDesk Expense Tax'),
    'field' => array(
      'click sortable' => TRUE,
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_numeric',
    ),
  );

  $data['stormexpense']['total'] = array(
    'title' => t('Total'),
    'help' => t('SuiteDesk Expense Total'),
    'field' => array(
      'click sortable' => TRUE,
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_numeric',
    ),
  );

  $data['stormexpense']['operation'] = array(
    'field' => array(
      'title' => t('Edit/Delete link'),
      'help' => t('Provide a simple link to edit and delete the node.'),
      'handler' => 'storm_handler_field_operation',
      'type' => 'stormexpense',
    ),
  );

  return $data;
}

function stormexpense_views_handlers() {
  return array(
    'info' => array(
      'path' => drupal_get_path('module', 'storm'),
    ),
    'handlers' => array(
      'storm_handler_filter_attributes_domain' => array(
        'parent' => 'views_handler_filter_in_operator',
      ),
      'storm_handler_field_operation' => array(
        'parent' => 'views_handler_field_node_link',
        'path' => drupal_get_path('module', 'storm'),
      ),
    ),
  );
}

function stormexpense_date_api_fields($field) {
  $values = array(
    'sql_type' => DATE_UNIX,
    'tz_handling' => 'site',
    'timezone_field' => '',
    'offset_field' => '',
    'related_fields' => array(),
    'granularity' => array('year', 'month', 'day'),
  );

  switch ($field) {
    case 'stormexpense.expensedate':
      return $values;
  }

}

function stormexpense_date_api_tables() {
  return array('stormexpense');
}

