You are here

function smart_ip_views_bridge_views_pre_execute in Smart IP 6

Same name and namespace in other branches
  1. 6.2 modules/smart_ip_views_bridge/smart_ip_views_bridge.module \smart_ip_views_bridge_views_pre_execute()

Implements hook_views_pre_execute(). Override the query SQL in a view

File

modules/smart_ip_views_bridge/smart_ip_views_bridge.module, line 67
Smart IP Views Bridge exposes Smart IP visitor's location details to Views field (coordinates, country, ISO 3166 2-character country code, region, region code (FIPS), city and zip) and filter (country, ISO 3166 2-character country code,…

Code

function smart_ip_views_bridge_views_pre_execute(&$view) {

  // Openlayers proximity filter
  foreach ($view->filter as $proximity_type => $proximity) {
    if (strpos($proximity_type, 'flat') === 0 || strpos($proximity_type, 'circle') === 0) {
      $smart_ip = smart_ip_views_bridge_substitute_token_value($view->filter[$proximity_type]->value['location'], $proximity->options['value']['location']);

      // Replace the tokens with the Smart IP values found in widget forms
      $view->filter[$proximity_type]->view->exposed_widgets = str_replace($proximity->options['value']['location'], $smart_ip, $view->filter[$proximity_type]->view->exposed_widgets);
    }
  }

  // Openlayers filter
  foreach ($view->filter as $openlayers_key => $openlayers) {
    if (strpos($openlayers_key, 'openlayers_wkt') !== FALSE) {
      if (is_array($proximity->value)) {
        $token = $proximity->value['location'];
      }
      else {
        $token = $proximity->value;
      }
      $smart_ip = smart_ip_views_bridge_substitute_token_value($view->filter[$openlayers_key]->value, $token);

      // Replace the tokens with the Smart IP values found in widget forms
      $view->filter[$openlayers_key]->view->exposed_widgets = str_replace($token, $smart_ip, $view->filter[$openlayers_key]->view->exposed_widgets);
      smart_ip_views_bridge_substitute_token_value($view->filter[$openlayers_key]->options['value'], $token);
      smart_ip_views_bridge_substitute_token_value($view->filter[$openlayers_key]->view->exposed_data[$openlayers_key], $token);
      smart_ip_views_bridge_substitute_token_value($view->filter[$openlayers_key]->view->exposed_raw_input[$openlayers_key], $token);
    }
  }

  // Other fields
  foreach ($view->build_info['query_args'] as $arg_index => $query_args) {
    $condition_value = $query_args;
    if (is_array($condition_value)) {
      foreach ($condition_value as $value) {
        $smart_ip_index = explode('][', $value);
        $smart_ip_index_count = count($smart_ip_index);
        if ($smart_ip_index_count == 1) {
          $smart_ip_index = explode('.', $value);
          $smart_ip_index_count = count($smart_ip_index);
        }
        $smart_ip_session = smart_ip_session_get($smart_ip_index[0]);
        if ($smart_ip_index_count == 3 && isset($smart_ip_session[$smart_ip_index[1]][$smart_ip_index[2]])) {
          $values[] = $smart_ip_session[$smart_ip_index[1]][$smart_ip_index[2]];
        }
      }
      if (isset($values)) {
        $view->build_info['query_args'][$arg_index] = $values;
      }
    }
    else {
      smart_ip_views_bridge_substitute_token_value($view->build_info['query_args'][$arg_index], $condition_value);
    }
  }
}