You are here

class search_api_location_plugin_exposed_form in Search API Location 7

The search_api_location plugin to handle radius exposed filter forms.

Hierarchy

Expanded class hierarchy of search_api_location_plugin_exposed_form

1 string reference to 'search_api_location_plugin_exposed_form'
search_api_location_views_plugins in ./search_api_location.module
Implements hook_views_plugins

File

includes/search_api_location_plugin_exposed_form.inc, line 7

View source
class search_api_location_plugin_exposed_form extends views_plugin_exposed_form {
  function summary_title() {
    return t('Search API Location');
  }
  function option_definition() {
    $options = parent::option_definition();
    $options['use_gmap'] = array(
      'default' => TRUE,
      'bool' => TRUE,
    );
    $options['gmap_macro'] = array(
      'default' => '[gmap ]',
      'translatable' => FALSE,
    );
    return $options;
  }
  function options_form(&$form, &$form_state) {
    $form['use_gmap'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use gmap'),
      '#description' => t('Use gmap to pick location with radius filter'),
      '#default_value' => $this->options['use_gmap'],
    );
    $form['gmap_macro'] = array(
      '#type' => 'textfield',
      '#title' => t('Gmap macro'),
      '#description' => t('The macro to use for the gmap. If you not specify gmap macro it will use default gmap settings.'),
      '#default_value' => $this->options['gmap_macro'],
      '#required' => TRUE,
      '#dependency' => array(
        'edit-exposed-form-options-use-gmap' => array(
          1,
        ),
      ),
      '#process' => array(
        'ctools_dependent_process',
      ),
    );
  }
  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,
        );
      }
    }
  }
  function exposed_form_submit(&$form, &$form_state, &$exclude) {
    $view = $this->view;
    $filter = $view->filter['radius'];
    $identifier = $filter->options['expose']['identifier'];
    $values = $form_state['values'];
    $gmap_key = variable_get('googlemap_api_key', '');
    if ($values[$identifier]['address'] && !empty($gmap_key)) {
      $url = url('http://maps.google.com/maps/geo', array(
        'query' => array(
          'q' => $values[$identifier]['address'],
          'key' => $gmap_key,
        ),
      ));
      $request = drupal_http_request($url);
      if ($request->code == '200') {
        $data = drupal_json_decode($request->data);
        if ($data['Status']['code'] == '200' && !empty($data['Placemark'])) {
          $form_state['view']->exposed_data[$identifier]['locpick']['longitude'] = $data['Placemark'][0]['Point']['coordinates'][0];
          $form_state['view']->exposed_data[$identifier]['locpick']['latitude'] = $data['Placemark'][0]['Point']['coordinates'][1];
        }
      }
    }
  }
  function pre_render($values) {
    $view = $this->view;
    if (isset($view->filter['radius'])) {
      $filter = $view->filter['radius'];
      if ($filter
        ->is_exposed()) {
        $identifier = $filter->options['expose']['identifier'];
        if (isset($view->exposed_data[$identifier])) {
          $exposed_data = $view->exposed_data[$identifier];
        }
      }
    }
    if (!isset($exposed_data['current_map'])) {
      return;
    }
    foreach ($values as $marker) {
      $marker = (array) $marker;
      if (isset($marker['location:latitude']) && isset($marker['location:longitude']) && isset($marker['title'])) {
        $exposed_data['current_map']['markers'][] = array(
          'latitude' => $marker['location:latitude'],
          'longitude' => $marker['location:longitude'],
          'text' => '<div class="gmap-popup">' . $marker['title'] . '</div>',
          'markername' => 'drupal',
          'offset' => 0,
          'opts' => array(
            'clickable' => TRUE,
          ),
        );
      }
    }
    $mapdefaults = gmap_defaults();
    $map = array_merge($mapdefaults, $exposed_data['current_map']);

    // Styles is a subarray.
    if (isset($exposed_data['current_map']['styles'])) {
      $map['styles'] = array_merge($mapdefaults['styles'], $exposed_data['current_map']['styles']);
    }
    gmap_map_cleanup($map);
    gmap_module_invoke('pre_theme_map', $map);
    unset($map["shapes"]);
    drupal_add_js(array(
      'gmap' => array(
        $exposed_data['current_map']['id'] => $map,
      ),
    ), 'setting');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
search_api_location_plugin_exposed_form::exposed_form_alter function Overrides views_plugin_exposed_form::exposed_form_alter
search_api_location_plugin_exposed_form::exposed_form_submit function This function is executed when exposed form is submited. Overrides views_plugin_exposed_form::exposed_form_submit
search_api_location_plugin_exposed_form::options_form function Provide a form to edit options for this plugin. Overrides views_plugin_exposed_form::options_form
search_api_location_plugin_exposed_form::option_definition function Information about options for all kinds of purposes will be held here. Overrides views_plugin_exposed_form::option_definition
search_api_location_plugin_exposed_form::pre_render function Overrides views_plugin_exposed_form::pre_render
search_api_location_plugin_exposed_form::summary_title function Returns the summary of the settings in the display. Overrides views_plugin::summary_title
views_object::$definition public property Handler's definition.
views_object::$options public property Except for displays, options for the object will be held here. 1
views_object::altered_option_definition function Collect this handler's option definition and alter them, ready for use.
views_object::construct public function Views handlers use a special construct function. 4
views_object::destroy public function Destructor. 2
views_object::export_option public function 1
views_object::export_options public function
views_object::export_option_always public function Always exports the option, regardless of the default value.
views_object::options Deprecated public function Set default options on this object. 1
views_object::set_default_options public function Set default options.
views_object::set_definition public function Let the handler know what its full definition is.
views_object::unpack_options public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
views_object::unpack_translatable public function Unpack a single option definition.
views_object::unpack_translatables public function Unpacks each handler to store translatable texts.
views_object::_set_option_defaults public function
views_plugin::$display public property The current used views display.
views_plugin::$plugin_name public property The plugin name of this plugin, for example table or full.
views_plugin::$plugin_type public property The plugin type of this plugin, for example style or query.
views_plugin::$view public property The top object of a view. Overrides views_object::$view 1
views_plugin::additional_theme_functions public function Provide a list of additional theme functions for the theme info page.
views_plugin::options_submit public function Handle any special handling on the validate form. 9
views_plugin::options_validate public function Validate the options form. 10
views_plugin::plugin_title public function Return the human readable name of the display.
views_plugin::theme_functions public function Provide a full list of possible theme templates used by this style.
views_plugin::validate public function Validate that the plugin is correct and can be saved. 3
views_plugin_exposed_form::exposed_form_validate public function
views_plugin_exposed_form::init public function Initialize the plugin.
views_plugin_exposed_form::post_execute public function
views_plugin_exposed_form::post_render public function
views_plugin_exposed_form::pre_execute public function
views_plugin_exposed_form::query public function Add anything to the query that we might need to. Overrides views_plugin::query 1
views_plugin_exposed_form::render_exposed_form public function Render the exposed filter form.
views_plugin_exposed_form::reset_form public function