You are here

function search_api_location_plugin_exposed_form::exposed_form_alter in Search API Location 7

Overrides views_plugin_exposed_form::exposed_form_alter

File

includes/search_api_location_plugin_exposed_form.inc, line 42

Class

search_api_location_plugin_exposed_form
The search_api_location plugin to handle radius exposed filter forms.

Code

function exposed_form_alter(&$form, &$form_state) {
  parent::exposed_form_alter($form, $form_state);
  $view = $this->view;
  $filter = $view->filter['radius'];
  $identifier = $filter->options['expose']['identifier'];
  if (isset($view->exposed_input[$identifier]) && !empty($view->exposed_input[$identifier]['locpick']['latitude']) && !empty($view->exposed_input[$identifier]['locpick']['latitude'])) {
    $exposed_input = $view->exposed_input[$identifier];
  }
  elseif (isset($filter) && is_object($filter)) {
    $exposed_input = $filter->value;
  }
  else {
    return;
  }
  if (module_exists('gmap') && $this->options['use_gmap'] && isset($exposed_input) && isset($identifier)) {

    // Bad things happen if we try to show a gmap in the views live preview.
    if (isset($form_state['post']['live_preview'])) {
      $form['gmap']['proximity_map'] = array(
        '#value' => t('Gmap location selection is not available during live preview.'),
        '#weight' => 0,
      );
    }
    else {
      $map = array_merge(gmap_defaults(), gmap_parse_macro($this->options['gmap_macro']));
      $map['points'] = array();
      $map['pointsOverlays'] = array();
      $map['lines'] = array();
      $map['behavior']['locpick'] = TRUE;
      $map['behavior']['collapsehack'] = TRUE;
      $map['latitude'] = (double) $exposed_input['locpick']['latitude'];
      $map['longitude'] = (double) $exposed_input['locpick']['longitude'];
      $map['markers'][] = array(
        'latitude' => (double) $exposed_input['locpick']['latitude'],
        'longitude' => (double) $exposed_input['locpick']['longitude'],
        'text' => '<div class="gmap-popup">' . t('Your current search position') . '</div>',
        'markername' => 'treasure',
        'offset' => 0,
        'opts' => array(
          'clickable' => TRUE,
        ),
      );
      $map["shapes"][] = array(
        "type" => "circle",
        "style" => array(
          "000000",
          "1",
          "50",
          "0000ff",
          "25",
        ),
        "radius" => (double) $exposed_input['distance']['search_distance'],
        "center" => array(
          (double) $exposed_input['locpick']['latitude'],
          (double) $exposed_input['locpick']['longitude'],
        ),
        'opts' => array(
          'clickable' => FALSE,
        ),
      );
      $mapWidth = str_replace('px', '', $map['width']);
      $mapHeight = str_replace('px', '', $map['height']);
      $map['zoom'] = floor(17 - log(3300 * (double) $exposed_input['distance']['search_distance'] / min($mapWidth, $mapHeight), 2));

      //hide lockpick container
      $form[$identifier]['locpick']['#attributes'] = array(
        'style' => array(
          'display:none;',
        ),
      );
      $form[$identifier]['locpick']['latitude']['#map'] = $map['id'];
      gmap_widget_setup($form[$identifier]['locpick']['latitude'], 'locpick_latitude');
      $form[$identifier]['locpick']['longitude']['#map'] = $map['id'];
      gmap_widget_setup($form[$identifier]['locpick']['longitude'], 'locpick_longitude');
      $form[$identifier]['current_map'] = array(
        '#type' => 'value',
        '#value' => $map,
      );
      $form['current_mapid'] = array(
        '#type' => 'hidden',
        '#value' => $map['id'],
      );
      $form[$identifier]['gmap']['proximity_map']['locpick']['map'] = array(
        '#type' => 'gmap',
        '#weight' => -1,
        '#gmap_settings' => $map,
      );

      //part for no JS
      $form[$identifier]['address'] = array(
        '#type' => 'textfield',
        '#title' => t('Address:'),
        '#description' => t('Enter search address.'),
        '#prefix' => '<div id="search_address_field">',
        '#suffix' => '</div>',
        '#default' => isset($exposed_input['address']) ? $exposed_input['address'] : '',
        '#weight' => 3,
      );
    }
  }
}