You are here

search_api_location.module in Search API Location 7

Same filename and directory in other branches
  1. 8 search_api_location.module
  2. 7.2 search_api_location.module

Search API Location module author: dolarchik (d.olaresko@madcap.nl)

File

search_api_location.module
View source
<?php

// $Id$

/**
 * @file
 *  Search API Location module
 *  author: dolarchik (d.olaresko@madcap.nl)
 */

// IMPLEMENTATIONS

/**
 * Implements hook_entity_property_info_alter()
 * @see entity_metadata_entity_property_info_alter()
 */
function search_api_location_entity_property_info_alter(&$info) {

  // Move the location property to the node by default, as its usually there this
  // makes dealing with it more convenient.
  $info['node']['properties']['location'] = array(
    'type' => 'struct',
    'label' => t('The location'),
    'property info' => search_api_location_entity_struct_info(),
    'field' => TRUE,
  );
}

/**
 * Defines info for the properties of the struct data structure for location.
 */
function search_api_location_entity_struct_info() {
  $result = array();
  $location_key_map = _search_api_location_key_map();
  foreach ($location_key_map as $key => $value) {
    $result[$key] = array(
      'type' => $value['type'],
      'label' => $value['label'],
      'sanitized' => TRUE,
      'getter callback' => 'search_api_location_field_verbatim_get',
    );
  }
  return $result;
}

/**
 * Implements hook_search_api_solr_index_items_alter()
 * adds/updates the location fields to the solr index.
 */
function search_api_location_search_api_solr_field_mapping_alter(SearchApiIndex $index, array &$fields) {
  if (isset($fields['location:latitude'])) {
    $fields['location:latitude'] = 'lat';
  }
  if (isset($fields['location:longitude'])) {
    $fields['location:longitude'] = 'lng';
  }
}

/**
 * Implements hook_search_api_solr_query_alter()
 */
function search_api_location_search_api_solr_query_alter(array &$call_args, SearchApiQueryInterface $query) {
  if ($query
    ->getOption('query_type') == 'geo') {
    $params['qt'] = 'geo';
    $params['lat'] = $query
      ->getOption('lat');
    $params['long'] = $query
      ->getOption('lng');
    $params['radius'] = $query
      ->getOption('radius');
    $params['sort'] = 'geo_distance ' . $query
      ->getOption('sort');
    if (!is_null($call_args['query']) && !empty($call_args['params']['qf'])) {
      $call_args['params']['fq'][] = '{!dismax qf="' . implode(" ", $call_args['params']['qf']) . '"}' . $call_args['query'];
      $call_args['query'] = NULL;
    }
    unset($call_args['params']['qf']);
    $call_args['params'] = array_merge($params, $call_args['params']);
  }
}

/**
 * Callback to get the data structure of a field. Useful for fields
 * that add metadata for their own data structure.
 */
function search_api_location_field_verbatim_get($item, array $options, $name, $entity_type) {
  return isset($item[$name]) && !empty($item[$name]) ? $item[$name] : NULL;
}

// HELPER FUNCTIONS

/**
 * Helper function that returns location key map array
 */
function _search_api_location_key_map() {
  return array(
    'lid' => array(
      'type' => 'integer',
      'label' => t('Lid'),
    ),
    'latitude' => array(
      'type' => 'decimal',
      'label' => t('Latitude'),
    ),
    'longitude' => array(
      'type' => 'decimal',
      'label' => t('Longitude'),
    ),
    'name' => array(
      'type' => 'text',
      'label' => t('Name'),
    ),
    'street' => array(
      'type' => 'text',
      'label' => t('Street'),
    ),
    'additional' => array(
      'type' => 'text',
      'label' => t('Additional'),
    ),
    'city' => array(
      'type' => 'text',
      'label' => t('City'),
    ),
    'province' => array(
      'type' => 'text',
      'label' => t('Province'),
    ),
    'postal_code' => array(
      'type' => 'text',
      'label' => t('Postal code'),
    ),
    'country' => array(
      'type' => 'text',
      'label' => t('Country'),
    ),
    'province_name' => array(
      'type' => 'text',
      'label' => t('Province name'),
    ),
    'country_name' => array(
      'type' => 'text',
      'label' => t('Country name'),
    ),
  );
}

//VIEWS PART

/**
 * Implements hook_views_api().
 */
function search_api_location_views_api() {
  return array(
    'api' => '3.0-alpha1',
  );
}

/**
 * Implements hook_views_data().
 */
function search_api_location_views_data() {
  $data = array();
  foreach (search_api_index_load_multiple(FALSE) as $index) {
    $data['search_api_index_' . $index->machine_name]['radius'] = array(
      'group' => t('Search'),
      'title' => t('Radius'),
      // The item it appears as on the UI,
      'help' => t('Location radius for @name index', array(
        '@name' => $index->name,
      )),
      // The help that appears on the UI,
      'filter' => array(
        'handler' => 'handler_filter_radius',
        'label' => t('Radius'),
      ),
    );
  }
  return $data;
}

/**
 * Implementation of hook_views_handlers().
 */
function search_api_location_views_handlers() {
  return array(
    'info' => array(
      'path' => drupal_get_path('module', 'search_api_location') . '/includes',
    ),
    'handlers' => array(
      'handler_filter_radius' => array(
        'parent' => 'views_handler_filter',
      ),
    ),
  );
}

/**
 * Implements hook_views_plugins
 */
function search_api_location_views_plugins() {
  return array(
    'exposed_form' => array(
      'search_api_location' => array(
        'title' => t('Search API location'),
        'help' => t('Test plugin'),
        'handler' => 'search_api_location_plugin_exposed_form',
        'uses row plugin' => FALSE,
        'uses fields' => TRUE,
        'uses options' => TRUE,
        'type' => 'normal',
        'parent' => 'parent',
      ),
    ),
  );
}

Functions

Namesort descending Description
search_api_location_entity_property_info_alter Implements hook_entity_property_info_alter()
search_api_location_entity_struct_info Defines info for the properties of the struct data structure for location.
search_api_location_field_verbatim_get Callback to get the data structure of a field. Useful for fields that add metadata for their own data structure.
search_api_location_search_api_solr_field_mapping_alter Implements hook_search_api_solr_index_items_alter() adds/updates the location fields to the solr index.
search_api_location_search_api_solr_query_alter Implements hook_search_api_solr_query_alter()
search_api_location_views_api Implements hook_views_api().
search_api_location_views_data Implements hook_views_data().
search_api_location_views_handlers Implementation of hook_views_handlers().
search_api_location_views_plugins Implements hook_views_plugins
_search_api_location_key_map Helper function that returns location key map array