You are here

function _location_process_location in Location 7.5

Same name and namespace in other branches
  1. 7.3 location.module \_location_process_location()

Process a location element.

1 string reference to '_location_process_location'
location_element_info in ./location.module
Implements hook_element_info().

File

./location.module, line 241
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_process_location($element, $form_state) {

  // this is TRUE if we are processing a form that already contains values, such as during an AJAX call
  drupal_add_css(drupal_get_path('module', 'location') . '/location.css');
  $element['#tree'] = TRUE;
  if (!isset($element['#title'])) {
    $element['#title'] = t('Location');
  }
  if (empty($element['#location_settings'])) {
    $element['#location_settings'] = array();
  }
  if (!isset($element['#default_value']) || $element['#default_value'] == 0) {
    $element['#default_value'] = array();
  }
  $element['location_settings'] = array(
    '#type' => 'value',
    '#value' => $element['#location_settings'],
  );

  // Ensure this isn't accidentally used later.
  unset($element['#location_settings']);

  // Make a reference to the settings.
  $settings =& $element['location_settings']['#value'];
  if (isset($element['#default_value']['lid']) && $element['#default_value']['lid']) {

    // Keep track of the old LID.
    $element['lid'] = array(
      '#type' => 'value',
      '#value' => $element['#default_value']['lid'],
    );
  }

  // Fill in missing defaults, etc.
  location_normalize_settings($settings, $element['#required']);
  $defaults = location_empty_location($settings);
  if (isset($element['lid']['#value']) && $element['lid']['#value']) {
    $defaults = location_load_location($element['lid']['#value']);
  }
  $fsettings =& $settings['form']['fields'];

  // $settings -> $settings['form']['fields']
  // $defaults is not necessarily what we want.
  // If #default_value was already specified, we want to use that, because
  // otherwise we will lose our values on preview!
  $fdefaults = $defaults;
  foreach ($element['#default_value'] as $k => $v) {
    $fdefaults[$k] = $v;
  }
  $fields = location_field_names();
  foreach ($fields as $field => $title) {
    if (!isset($element[$field])) {

      // @@@ Permission check hook?
      if ($fsettings[$field]['collect'] != 0) {
        $fsettings[$field]['#parents'] = $element['#parents'];
        $element[$field] = location_invoke_locationapi($fdefaults[$field], 'field_expand', $field, $fsettings[$field], $fdefaults);
        $element[$field]['#weight'] = (int) $fsettings[$field]['weight'];
      }
    }

    // If State/Province is using the select widget, update the element's options.
    if ($field == 'province' && $fsettings[$field]['widget'] == 'select') {

      // We are building the element for the first time
      if (!isset($element['value']['country'])) {
        $country = $fdefaults['country'];
      }
      else {
        $country = $element['#value']['country'];
      }
      $provinces = location_get_provinces($country);

      // The submit handler expects to find the full province name, not the
      // abbreviation. The select options should reflect this expectation.
      $element[$field]['#options'] = array(
        '' => t('Please select'),
        'xx' => t('NOT LISTED'),
      ) + $provinces;
    }
  }

  // Only include 'Street Additional' if 'Street' is 'allowed' or 'required'
  if (!isset($element['street'])) {
    unset($element['additional']);
  }

  // @@@ Split into submit and view permissions?
  if (user_access('submit latitude/longitude') && $fsettings['locpick']['collect']) {
    $element['locpick'] = array(
      '#weight' => $fsettings['locpick']['weight'],
    );
    if (location_has_coordinates($defaults, FALSE)) {
      $element['locpick']['current'] = array(
        '#type' => 'fieldset',
        '#title' => t('Current coordinates'),
        '#attributes' => array(
          'class' => array(
            'location-current-coordinates-fieldset',
          ),
        ),
      );
      $element['locpick']['current']['current_latitude'] = array(
        '#type' => 'item',
        '#title' => t('Latitude'),
        '#markup' => $defaults['latitude'],
      );
      $element['locpick']['current']['current_longitude'] = array(
        '#type' => 'item',
        '#title' => t('Longitude'),
        '#markup' => $defaults['longitude'],
      );
      $source = t('Unknown');
      switch ($defaults['source']) {
        case LOCATION_LATLON_USER_SUBMITTED:
          $source = t('User-submitted');
          break;
        case LOCATION_LATLON_GEOCODED_APPROX:
          $source = t('Geocoded (Postal code level)');
          break;
        case LOCATION_LATLON_GEOCODED_EXACT:
          $source = t('Geocoded (Exact)');
      }
      $element['locpick']['current']['current_source'] = array(
        '#type' => 'item',
        '#title' => t('Source'),
        '#markup' => $source,
      );
    }
    $element['locpick']['user_latitude'] = array(
      '#type' => 'textfield',
      '#title' => t('Latitude'),
      '#default_value' => isset($element['#default_value']['locpick']['user_latitude']) ? $element['#default_value']['locpick']['user_latitude'] : '',
      '#size' => 16,
      '#attributes' => array(
        'class' => array(
          'container-inline',
        ),
      ),
      '#maxlength' => 20,
      '#required' => $fsettings['locpick']['collect'] == 2,
    );
    $element['locpick']['user_longitude'] = array(
      '#type' => 'textfield',
      '#title' => t('Longitude'),
      '#default_value' => isset($element['#default_value']['locpick']['user_longitude']) ? $element['#default_value']['locpick']['user_longitude'] : '',
      '#size' => 16,
      '#maxlength' => 20,
      '#required' => $fsettings['locpick']['collect'] == 2,
    );
    $element['locpick']['instructions'] = array(
      '#type' => 'markup',
      '#weight' => 1,
      '#prefix' => '<div class=\'description\'>',
      '#markup' => '<br /><br />' . t('If you wish to supply your own latitude and longitude, you may enter them above.  If you leave these fields blank, the system will attempt to determine a latitude and longitude for you from the entered address.  To have the system recalculate your location from the address, for example if you change the address, delete the values for these fields.'),
      '#suffix' => '</div>',
    );
    if (function_exists('gmap_get_auto_mapid') && variable_get('location_usegmap', FALSE)) {
      $mapid = gmap_get_auto_mapid();
      $map = array_merge(gmap_defaults(), gmap_parse_macro(variable_get('location_locpick_macro', '[gmap]')));
      $map['id'] = $mapid;
      $map['points'] = array();
      $map['pointsOverlays'] = array();
      $map['lines'] = array();
      $map['behavior']['locpick'] = TRUE;
      $map['behavior']['collapsehack'] = TRUE;

      // Use previous coordinates to center the map.
      if (location_has_coordinates($defaults, FALSE)) {
        $map['latitude'] = (double) $defaults['latitude'];
        $map['longitude'] = (double) $defaults['longitude'];
        $map['markers'][] = array(
          'latitude' => $defaults['latitude'],
          'longitude' => $defaults['longitude'],
          'markername' => 'small gray',
          // @@@ Settable?
          'offset' => 0,
          'opts' => array(
            'clickable' => FALSE,
          ),
        );
      }
      $element['locpick']['user_latitude']['#map'] = $mapid;
      gmap_widget_setup($element['locpick']['user_latitude'], 'locpick_latitude');
      $element['locpick']['user_longitude']['#map'] = $mapid;
      gmap_widget_setup($element['locpick']['user_longitude'], 'locpick_longitude');
      $element['locpick']['map'] = array(
        '#type' => 'gmap',
        '#weight' => -1,
        '#gmap_settings' => $map,
      );
      $element['locpick']['map_instructions'] = array(
        '#type' => 'markup',
        '#weight' => 2,
        '#prefix' => '<div class=\'description\'>',
        '#markup' => t('You may set the location by clicking on the map, or dragging the location marker.  To clear the location and cause it to be recalculated, click on the marker.'),
        '#suffix' => '</div>',
      );
    }
  }
  if (isset($defaults['lid']) && !empty($defaults['lid'])) {
    $element['delete_location'] = array(
      '#type' => 'checkbox',
      '#title' => t('Delete'),
      '#default_value' => isset($fdefaults['delete_location']) ? $fdefaults['delete_location'] : FALSE,
      '#description' => t('Check this box to delete this location.'),
    );
  }
  $element += element_info('fieldset');
  drupal_alter('location_element', $element);
  return $element;
}