You are here

function location_views_tables in Location 5.3

Same name and namespace in other branches
  1. 5 contrib/location_views/location_views.module \location_views_tables()

Implementation of hook_views_tables().

File

contrib/location_views/location_views.module, line 11
Views-enables the location module.

Code

function location_views_tables() {
  $tables['location'] = array(
    'name' => 'location',
    'join' => array(
      'left' => array(
        'table' => 'location_instance_node',
        'field' => 'lid',
      ),
      'right' => array(
        'field' => 'lid',
      ),
    ),
    'fields' => array(
      'name' => array(
        'name' => t('Name'),
        'sortable' => TRUE,
      ),
      'street' => array(
        'name' => t('Street'),
        'sortable' => TRUE,
      ),
      'additional' => array(
        'name' => t('Additional'),
        'sortable' => TRUE,
      ),
      'city' => array(
        'name' => t('City'),
        'sortable' => TRUE,
      ),
      // Should be province_name, but can't change history.
      'province' => array(
        'name' => t('Province name'),
        'handler' => 'location_views_province_handler',
        'addlfields' => array(
          'country',
        ),
        'sortable' => TRUE,
      ),
      'province_code' => array(
        'field' => 'province',
        'name' => t('Province code'),
        'sortable' => TRUE,
      ),
      'postal_code' => array(
        'name' => t('Postal Code'),
        'sortable' => TRUE,
      ),
      // Should be country_name, but can't change history.
      'country' => array(
        'name' => t('Country'),
        'handler' => 'location_views_country_handler',
        'sortable' => TRUE,
      ),
      'country_code' => array(
        'field' => 'country',
        'name' => t('Country code'),
        'sortable' => TRUE,
      ),
      'latitude' => array(
        'name' => t('Latitude'),
        'sortable' => TRUE,
      ),
      'longitude' => array(
        'name' => t('Longitude'),
        'sortable' => TRUE,
      ),
      // add a complete address in a single box as a field option
      'address' => array(
        'field' => 'lid',
        'name' => t('Address'),
        'handler' => 'location_views_field_handler_address',
        'sortable' => FALSE,
        'help' => t('Complete formatted address in a single cell, using the location theme.'),
      ),
      // @@@ This one DEFINATELY needs a sanity check.
      // add field showing distance from selected zip code
      'distance' => array(
        'name' => t('Distance'),
        'handler' => 'location_views_field_handler_distance',
        'notafield' => TRUE,
        // @@@
        'sortable' => FALSE,
        'addlfields' => array(
          'name',
          'street',
          'additional',
          'city',
          'province',
          'country',
          'postal_code',
          'longitude',
          'latitude',
        ),
        'help' => t('Combine with proximity filter. Displays distance from each node to the central location selected with the proximity filter.'),
      ),
    ),
    'sorts' => array(
      'name' => array(
        'name' => t('Name'),
      ),
      'street' => array(
        'name' => t('Street'),
      ),
      'additional' => array(
        'name' => t('Additional'),
      ),
      'city' => array(
        'name' => t('City'),
      ),
      'province' => array(
        'name' => t('Province'),
      ),
      'country' => array(
        'name' => t('Country'),
      ),
      'postal_code' => array(
        'name' => t('Postal Code'),
      ),
    ),
    'filters' => array(
      'name' => array(
        'field' => 'name',
        'name' => t('Name'),
        'operator' => 'views_handler_operator_like',
        'handler' => 'views_handler_filter_like',
      ),
      'additional' => array(
        'field' => 'additional',
        'name' => t('Additional'),
        'operator' => 'views_handler_operator_like',
        'handler' => 'views_handler_filter_like',
      ),
      'street' => array(
        'field' => 'street',
        'name' => t('Street'),
        'operator' => 'views_handler_operator_like',
        'handler' => 'views_handler_filter_like',
      ),
      'city' => array(
        'field' => 'city',
        'name' => t('City'),
        'operator' => 'views_handler_operator_like',
        'handler' => 'views_handler_filter_like',
      ),
      // Note: Only checks latitude for now.
      'has_coordinates' => array(
        'field' => 'latitude',
        'name' => t('Has coordinates'),
        'operator' => array(
          'IS NOT' => t('Has coordinates'),
          'IS' => t('No coordinates'),
        ),
        'handler' => 'views_handler_filter_null',
      ),
      'province_select' => array(
        'field' => 'province',
        'name' => t('Province Selector'),
        'operator' => 'location_handler_operator_eq',
        'handler' => 'location_handler_filter_eq',
        'value' => array(
          '#type' => 'textfield',
          // Used so we can find ourselves during alter.
          '#location_views_province_field' => TRUE,
          '#autocomplete_path' => 'location/autocomplete/' . variable_get('location_default_country', 'us'),
          '#size' => 64,
          '#maxlength' => 64,
          // Used by province autocompletion js.
          '#attributes' => array(
            'class' => 'location_views_auto_province',
          ),
          // Used to ensure the JS is loaded.
          '#theme' => 'location_views_province_textfield',
        ),
        'cacheable' => 'no',
      ),
      'province' => array(
        'field' => 'province',
        'name' => t('Province'),
        'operator' => 'views_handler_operator_like',
        'handler' => 'views_handler_filter_like',
      ),
      'postal_code' => array(
        'field' => 'postal_code',
        'name' => t('Postal Code'),
        'operator' => 'views_handler_operator_like',
        'handler' => 'views_handler_filter_like',
      ),
      'country' => array(
        'field' => 'country',
        'name' => t('Country'),
        'operator' => 'location_handler_operator_eq',
        'handler' => 'location_handler_filter_eq',
        'value' => array(
          '#type' => 'select',
          '#options' => array_merge(array(
            '' => t('Please select'),
            'xx' => 'NOT LISTED',
          ), location_get_iso3166_list()),
          // Used by province autocompletion js.
          '#attributes' => array(
            'class' => 'location_views_auto_country',
          ),
        ),
      ),
      'latitude' => array(
        'field' => 'latitude',
        'name' => t('Latitude'),
        'operator' => 'views_handler_operator_gtlt',
      ),
      'longitude' => array(
        'field' => 'longitude',
        'name' => t('Longitude'),
        'operator' => 'views_handler_operator_gtlt',
      ),
      'proximity' => array(
        'field' => 'distance',
        'name' => t('Proximity'),
        'operator' => location_views_proximity_operators(),
        'handler' => 'location_views_filter_handler_proximity',
        'value' => location_views_proximity_form(),
        'help' => t('Select the postal code and the distance units to be used for the proximity filter.'),
        'cacheable' => 'no',
      ),
    ),
  );
  if (module_exists('gmap')) {
    $tables['location']['filters']['proximity_map'] = array(
      'field' => 'distance',
      'name' => t('Proximity Map'),
      'operator' => location_views_proximity_operators(),
      'handler' => 'location_views_filter_handler_proximity',
      'value' => array(
        '#type' => 'location_views_map',
      ),
      'help' => t('Map the central point and the distance units to be used for the proximity filter.'),
      'cacheable' => 'no',
    );
  }

  // location <-> location_instance <-> node
  $tables['location_instance_node'] = array(
    'name' => 'location_instance',
    'join' => array(
      'left' => array(
        'table' => 'node',
        'field' => 'vid',
      ),
      'right' => array(
        'field' => 'vid',
      ),
    ),
    'filters' => array(
      'has_location' => array(
        'field' => 'vid',
        'name' => t('Location: Has location'),
        'operator' => array(
          'IS NOT' => t('Has location'),
          'IS' => t('No location'),
        ),
        'handler' => 'views_handler_filter_null',
      ),
    ),
  );
  if (module_exists('usernode')) {
    $tables['location_instance_user'] = array(
      'name' => 'location_instance',
      'join' => array(
        'left' => array(
          'table' => 'usernode',
          'field' => 'uid',
        ),
        'right' => array(
          'field' => 'uid',
        ),
      ),
      'filters' => array(
        'has_location' => array(
          'field' => 'uid',
          'name' => t('User Location: Has location'),
          'operator' => array(
            'IS NOT' => t('Has location'),
            'IS' => t('No location'),
          ),
          'handler' => 'views_handler_filter_null',
        ),
      ),
    );
    $tables['user_location'] = $tables['location'];

    // Change join for user_location.
    $tables['user_location']['join']['left']['table'] = 'location_instance_user';

    // @@@ Fix these stupid handlers.
    $tables['user_location']['filters']['proximity']['handler'] = 'location_views_filter_handler_user_proximity';
    $tables['user_location']['filters']['proximity_map']['handler'] = 'location_views_filter_handler_user_proximity';
    foreach (array(
      'fields',
      'sorts',
      'filters',
    ) as $key) {
      foreach ($tables['user_location'][$key] as $k => $v) {
        if (isset($tables['user_location'][$key][$k]['name'])) {
          $tables['user_location'][$key][$k]['name'] = t('User Location: !field', array(
            '!field' => $v['name'],
          ));
        }
      }
    }
  }
  foreach (array(
    'fields',
    'sorts',
    'filters',
  ) as $key) {
    foreach ($tables['location'][$key] as $k => $v) {
      if (isset($tables['location'][$key][$k]['name'])) {
        $tables['location'][$key][$k]['name'] = t('Location: !field', array(
          '!field' => $v['name'],
        ));
      }
    }
  }
  return $tables;
}