You are here

function location_locationapi in Location 5.3

Same name and namespace in other branches
  1. 6.3 location.module \location_locationapi()
  2. 7.5 location.module \location_locationapi()
  3. 7.3 location.module \location_locationapi()
  4. 7.4 location.module \location_locationapi()

Implementation of hook_locationapi().

File

./location.module, line 481
Location module main routines. An implementation of a universal API for location manipulation. Provides functions for postal_code proximity searching, deep-linking into online mapping services. Currently, some options are configured through an…

Code

function location_locationapi(&$obj, $op, $a3 = NULL, $a4 = NULL, $a5 = NULL) {
  switch ($op) {
    case 'fields':
      return array(
        'name' => t('Location name'),
        'street' => t('Street location'),
        'additional' => t('Additional'),
        'city' => t('City'),
        'province' => t('State/Province'),
        'postal_code' => t('Postal code'),
        'country' => t('Country'),
        'locpick' => t('Coordinate Chooser'),
      );
    case 'virtual fields':
      return array(
        'province_name' => t('Province name'),
        'country_name' => t('Country name'),
        'map_link' => t('Map link'),
        'coords' => t('Coordinates'),
      );
    case 'defaults':
      return array(
        'lid' => array(
          'default' => FALSE,
        ),
        'name' => array(
          'default' => '',
          'collect' => 1,
          'weight' => 2,
        ),
        'street' => array(
          'default' => '',
          'collect' => 1,
          'weight' => 4,
        ),
        'additional' => array(
          'default' => '',
          'collect' => 1,
          'weight' => 6,
        ),
        'city' => array(
          'default' => '',
          'collect' => 0,
          'weight' => 8,
        ),
        'province' => array(
          'default' => '',
          'collect' => 0,
          'weight' => 10,
        ),
        'postal_code' => array(
          'default' => '',
          'collect' => 0,
          'weight' => 12,
        ),
        'country' => array(
          'default' => variable_get('location_default_country', 'us'),
          'collect' => 1,
          'weight' => 14,
        ),
        // @@@ Fix weight?
        'locpick' => array(
          'default' => FALSE,
          'collect' => 1,
          'weight' => 20,
          'nodiff' => TRUE,
        ),
        'latitude' => array(
          'default' => 0,
        ),
        'longitude' => array(
          'default' => 0,
        ),
        'source' => array(
          'default' => LOCATION_LATLON_UNDEFINED,
        ),
        'is_primary' => array(
          'default' => 0,
        ),
      );
    case 'validate':
      if (!empty($obj['country'])) {
        if (!empty($obj['province'])) {
          $provinces = location_get_provinces($obj['country']);
          $found = FALSE;
          $p = strtoupper($obj['province']);
          foreach ($provinces as $k => $v) {
            if ($p == strtoupper($k) || $p == strtoupper($v)) {
              $found = TRUE;
              break;
            }
          }
          if (!$found) {
            form_error($a3['province'], t('The specified province was not found in the specified country.'));
          }
        }
      }
      if (!empty($obj['locpick']) && is_array($obj['locpick'])) {

        // Can't specify just latitude or just longitude.
        if (_location_floats_are_equal($obj['locpick']['user_latitude'], 0) xor _location_floats_are_equal($obj['locpick']['user_longitude'], 0)) {
          $ref =& $a3['locpick']['user_latitude'];
          if (_location_floats_are_equal($obj['locpick']['user_longitude'], 0)) {
            $ref =& $a3['locpick']['user_longitude'];
          }
          form_error($ref, t('You must fill out both latitude and longitude or you must leave them both blank.'));
        }
      }
      break;
    case 'field_expand':
      switch ($a3) {
        case 'name':
          return array(
            '#type' => 'textfield',
            '#title' => t('Location name'),
            '#default_value' => $obj,
            '#size' => 64,
            '#maxlength' => 255,
            '#description' => t('e.g. a place of business, venue, meeting point'),
            '#attributes' => NULL,
            '#required' => $a4 == 2,
          );
        case 'street':
          return array(
            '#type' => 'textfield',
            '#title' => t('Street'),
            '#default_value' => $obj,
            '#size' => 64,
            '#maxlength' => 255,
            '#required' => $a4 == 2,
          );

        // Additional is linked to street.
        case 'additional':
          return array(
            '#type' => 'textfield',
            '#title' => t('Additional'),
            '#default_value' => $obj,
            '#size' => 64,
            '#maxlength' => 255,
          );
        case 'city':
          return array(
            '#type' => 'textfield',
            '#title' => t('City'),
            '#default_value' => $obj,
            '#size' => 64,
            '#maxlength' => 255,
            '#description' => NULL,
            '#attributes' => NULL,
            '#required' => $a4 == 2,
          );
        case 'province':
          drupal_add_js(drupal_get_path('module', 'location') . '/location_autocomplete.js');
          if (isset($a5['country']) && is_string($a5['country'])) {
            $country = $a5['country'];
          }
          elseif (isset($a5['country']['default']) && is_string($a5['country']['default'])) {
            $country = $a5['country']['default'];
          }
          else {
            $country = variable_get('site_default_country', 'us');
          }
          return array(
            '#type' => 'textfield',
            '#title' => t('State/Province'),
            '#autocomplete_path' => 'location/autocomplete/' . $country,
            '#default_value' => variable_get('location_use_province_abbreviation', 1) ? $obj : location_province_name($country, $obj),
            '#size' => 64,
            '#maxlength' => 64,
            '#description' => NULL,
            // Used by province autocompletion js.
            '#attributes' => array(
              'class' => 'location_auto_province',
            ),
            '#required' => $a4 == 2,
          );
        case 'country':

          // Force default.
          if ($a4 == 4) {
            return array(
              '#type' => 'value',
              '#value' => $obj,
            );
          }
          else {
            $options = array_merge(array(
              '' => t('Please select'),
              'xx' => t('NOT LISTED'),
            ), location_get_iso3166_list());
            return array(
              '#type' => 'select',
              '#title' => t('Country'),
              '#default_value' => $obj,
              '#options' => $options,
              '#description' => NULL,
              '#required' => $a4 == 2,
              // Used by province autocompletion js.
              '#attributes' => array(
                'class' => 'location_auto_country',
              ),
            );
          }
          break;
        case 'postal_code':
          return array(
            '#type' => 'textfield',
            '#title' => t('Postal code'),
            '#default_value' => $obj,
            '#size' => 16,
            '#maxlength' => 16,
            '#required' => $a4 == 2,
          );
      }
      break;
    case 'isunchanged':
      switch ($a3) {
        case 'lid':

          // Consider 0, NULL, and FALSE to be equivilent.
          if (empty($obj[$a3]) && empty($a4)) {
            return TRUE;
          }
          break;
        case 'latitude':
        case 'longitude':
          if (_location_floats_are_equal($obj[$a3], $a4)) {
            return TRUE;
          }
          break;
        case 'country':

          // Consider '  ' and '' to be equivilent, due to us storing country
          // as char(2) in the database.
          if (trim($obj[$a3]) == trim($a4)) {
            return TRUE;
          }
          break;
        case 'province_name':
        case 'country_name':
        case 'map_link':
        case 'coords':
        case 'locpick':

          // Always considered unchanged.
          return TRUE;
      }
      break;
  }
}