You are here

function location_locationapi in Location 6.3

Same name and namespace in other branches
  1. 5.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 577
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 'widget':
      switch ($a3) {
        case 'province':
          return array(
            'autocomplete' => 'Autocomplete',
            'select' => 'Dropdown',
          );
        default:
          return array();
      }
    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,
          'widget' => 'autocomplete',
        ),
        '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,
        ),
        // @@@
        'delete_location' => array(
          'default' => FALSE,
          'nodiff' => TRUE,
        ),
      );
    case 'validate':
      if (!empty($obj['country']) && $obj['country']['#value'] != 'xx') {
        if (!empty($obj['province']) && !empty($obj['province']['#value'])) {
          $provinces = location_get_provinces($obj['country']['#value']);
          $found = FALSE;
          $p = strtoupper($obj['province']['#value']);
          foreach ($provinces as $k => $v) {
            if ($p == strtoupper($k) || $p == strtoupper($v)) {
              $found = TRUE;
              break;
            }
          }
          if (!$found) {
            form_error($obj['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':
      if (is_array($a4)) {
        $settings = $a4;
      }
      else {

        // On this $op, $a4 is now expected to be an array,
        // but we make an exception for backwards compatibility.
        $settings = array(
          'default' => NULL,
          'weight' => NULL,
          'collect' => $a4,
          'widget' => NULL,
        );
      }
      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' => $settings['collect'] == 2,
          );
        case 'street':
          return array(
            '#type' => 'textfield',
            '#title' => t('Street'),
            '#default_value' => $obj,
            '#size' => 64,
            '#maxlength' => 255,
            '#required' => $settings['collect'] == 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' => $settings['collect'] == 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');
          }
          switch ($settings['widget']) {
            case 'select':
              static $js_set;
              if (empty($js_set)) {
                drupal_add_js(array(
                  'location_fetch_provinces_url' => url('location/fetch_provinces'),
                  'please_select' => t('Please Select'),
                  'not_listed' => t('NOT LISTED'),
                ), 'setting');
                $js_set = TRUE;
              }

              // Options are defined once during hook_element implementation.
              // @see _location_expand_location
              // $options = array_merge(array('' => t('Please select'), 'xx' => t('NOT LISTED')), location_get_provinces($country));
              return array(
                '#type' => 'select',
                '#title' => t('State/Province'),
                '#default_value' => $obj,
                '#description' => NULL,
                '#required' => $settings['collect'] == 2,
                '#attributes' => array(
                  'class' => 'location_dropdown_province',
                ),
              );
            case 'autocomplete':
            default:
              return array(
                '#type' => 'textfield',
                '#title' => t('State/Province'),
                '#autocomplete_path' => 'location/autocomplete/' . $country,
                '#default_value' => $obj,
                '#size' => 64,
                '#maxlength' => 64,
                '#description' => NULL,
                '#attributes' => array(
                  'class' => 'location_auto_province',
                ),
                '#required' => $settings['collect'] == 2,
              );
          }
        case 'country':

          // Force default.
          if ($settings['collect'] == 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' => $settings['collect'] == 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' => $settings['collect'] == 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':
        case 'delete_location':

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