You are here

smart_ip_views_bridge.module in Smart IP 7.2

Non-displayable characters.

File

modules/smart_ip_views_bridge/smart_ip_views_bridge.module
View source
<?php

// $Id$



/******************************************************************************
 * Drupal Hooks                                                               *
 ******************************************************************************/

/**
 * Implements hook_help().
 */
function smart_ip_views_bridge_help($path, $arg) {
  switch ($path) {
    case 'admin/help#smart_ip_views_bridge':
      return '<p>' . t("Smart IP Views Bridge exposes Smart IP visitor's location details to \n      Views field (coordinates, country, ISO 3166 2-character country code, \n      region, region code (FIPS), city and zip) and filter (country, ISO 3166 \n      2-character country code, region, region code (FIPS), city and zip).") . '</p><p>' . '<p>' . t('Guide/example of using Smart IP views fields and filters with Location module:') . '</p><p>' . '<ol><li>' . t('Download/install Location module.') . '</li>' . '<li>' . t("Add location CCK field to <strong><em>Page</em></strong> content type at\n      http://www.example.com/?q=admin/structure/types/manage/page/fields. Populate \n      the field label with <strong><em>Location</em></strong> and field name with <strong><em>location</em></strong>. Select <strong><em>Location</em></strong> \n      from Type of data to store.") . '</li>' . '<li>' . t("At <strong><em>Location</em></strong> field settings > Locative Information > Collection Settings \n      select <strong><em>Allow</em></strong> for the <strong><em>City</em></strong> and <strong><em>Country</em></strong> items.") . '</li>' . '<li>' . t("Create a <strong><em>Page</em></strong> content. Populate the <strong><em>Title</em></strong>, <strong><em>City</em></strong> and select a country \n      (Important: be sure that the city and country matches the geolocation that \n      Smart IP has detected based on your IP. To check, enable the device_geolocation \n      block - Please refer to the README.txt of device_geolocation module for the instructions).") . '</li>' . '<li>' . t("Create your views at http://www.example.com/?q=admin/structure/views") . '</li>' . '<li>' . t("Inside your Edit view, add a Filter criteria then select <strong><em>Location: City</em></strong> from the \n      list (click <strong><em>Add and configure filter criteria</em></strong> button).") . '</li>' . '<li>' . t("In <strong><em>Configure filter criterion: Location: City</em></strong>, populate the <strong><em>Value:</em></strong> textfield \n      with <strong><em>smart_ip][location][city</em></strong> Smart IP Views token and leave the <strong><em>Is equal to</em></strong> \n      selected in <strong><em>Operator</em></strong> dropdown menu (click <strong><em>Apply</em></strong> button).") . '</li>' . '<li>' . t("At your Edit view, add a Filter criteria then select <strong><em>Location: Country</em></strong> from the \n      list (click <strong><em>Add and configure filter criteria</em></strong> button).") . '</li>' . '<li>' . t("In <strong><em>Configure filter criterion: Location: Country</em></strong>, scroll down at the \n      bottom of the <strong><em>Country</em></strong> list box and select <strong><em>Smart IP: visitor's country code</em></strong>. \n      And select <strong><em>Is one of</em></strong> from <strong><em>Operator</em></strong> radio selection (click <strong><em>Apply</em></strong> button).") . '<blockquote><p>' . t("This will filter contents with the country defined in Location CCK fields (Country \n      and City) in ralation to your visitor's country and city detected by Smart IP.") . '</p></blockquote></li>' . '<li>' . t("Add a field then select <strong><em>Smart IP: Country</em></strong> from the list (click \n      <strong><em>Add and configure fields</em></strong> button).") . '</li>' . '<li>' . t("In <strong><em>Configure field: Smart IP: Country</em></strong>, change the <strong><em>Label</em></strong> to <strong><em>Your Country</em></strong>\n      then select <strong><em>Country name</em></strong> from the <strong><em>Display style:</em></strong> dropdown menu (click \n      <strong><em>Update</em></strong> button). This will display the country of your visitor along with other CCK \n      fields you've included in your views.") . '</li></ol>';
      break;
  }
}

/**
 * Implements hook_form_alter().
 */
function smart_ip_views_bridge_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'views_ui_config_item_form' && $form_state['section'] == 'filters') {
    if (array_key_exists('=', $form['options']['operator']['#options']) || array_key_exists('is', $form['options']['operator']['#options']) || array_key_exists('in', $form['options']['operator']['#options']) || array_key_exists('or', $form['options']['operator']['#options'])) {
      if ($form['options']['value']['#type'] == 'textfield') {
        $form['options']['value']['#description'] = '<p><strong>' . t('Smart IP tokens:') . '</strong><br />' . 'smart_ip][location][country<br />' . 'smart_ip][location][country_code<br />' . 'smart_ip][location][region<br />' . 'smart_ip][location][region_code<br />' . 'smart_ip][location][city<br />' . 'smart_ip][location][zip';
      }
      if ($form['options']['value']['#type'] == 'select' || $form['options']['value']['#type'] == 'checkboxes') {
        $smart_ip_options = smart_ip_views_bridge_form_select_field_tokens();
        $form['options']['value']['#options'] = array_merge($form['options']['value']['#options'], $smart_ip_options);
      }
    }
  }
}

/**
 * Implements hook_build_proximity_index().
 */
function smart_ip_views_bridge_build_proximity_index($node) {
  $index = array();
  foreach (openlayers_proximity_get_wkt_fields($node) as $name => $field) {
    $item = $node->{$name}['und'][0];
    if (!module_invoke($field['module'], 'field_is_empty', $item, $field)) {
      foreach (module_implements('parse_wkt') as $module) {
        if (!empty($item['geom'])) {
          $parsed = module_invoke($module, 'parse_wkt', $item['geom']);
          if (!empty($parsed)) {
            $index += $parsed;
          }
          else {
            $index[] = array(
              $item['lat'],
              $item['lon'],
            );
          }
        }
        else {
          $index[] = array(
            $item['lat'],
            $item['lon'],
          );
        }
      }
      drupal_alter('parse_wkt', $index, $field);
    }
  }
  return $index;
}

/**
 * Implements hook_views_api().
 * Views Integration Hook
 */
function smart_ip_views_bridge_views_api() {
  return array(
    'api' => 3.0,
    'path' => drupal_get_path('module', 'smart_ip_views_bridge') . '/views',
  );
}

/**
 * Form field description showing Smart IP tokens
 */
function smart_ip_views_bridge_form_fields_description() {
  return '<p><strong>' . t('Smart IP tokens:') . '</strong><br />' . 'smart_ip][location][country<br />' . 'smart_ip][location][country_code<br />' . 'smart_ip][location][region<br />' . 'smart_ip][location][region_code<br />' . 'smart_ip][location][city<br />' . 'smart_ip][location][zip</p><p>' . t('Multiple Smart IP tokens should be separated with comma then space ", ". Example:') . '<br />
    smart_ip][location][country, smart_ip][location][region, smart_ip][location][city</p>';
}

/**
 * Form select field Smart IP tokens
 */
function smart_ip_views_bridge_form_select_field_tokens() {
  return array(
    'smart_ip.location.country' => t("Smart IP: visitor's country"),
    'smart_ip.location.country_code' => t("Smart IP: visitor's country code"),
    'smart_ip.location.region' => t("Smart IP: visitor's region"),
    'smart_ip.location.region_code' => t("Smart IP: visitor's region code"),
    'smart_ip.location.city' => t("Smart IP: visitor's city"),
    'smart_ip.location.zip' => t("Smart IP: visitor's zip"),
  );
}

/**
 * Substitute token to Smart IP value
 */
function smart_ip_views_bridge_substitute_token_value(&$view_filter_item, $token) {
  $output = array();
  $mutlti_smart_ip_tokens = explode(', ', $token);
  foreach ($mutlti_smart_ip_tokens as $token) {
    $smart_ip_index = explode('][', $token);
    $smart_ip_index_count = count($smart_ip_index);
    if ($smart_ip_index_count == 1) {
      $smart_ip_index = explode('.', $token);
      $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]])) {
      $output[] = $smart_ip_session[$smart_ip_index[1]][$smart_ip_index[2]];
    }
  }
  if (!empty($output)) {
    $view_filter_item = implode(', ', $output);
  }
  return $view_filter_item;
}

Functions

Namesort descending Description
smart_ip_views_bridge_build_proximity_index Implements hook_build_proximity_index().
smart_ip_views_bridge_form_alter Implements hook_form_alter().
smart_ip_views_bridge_form_fields_description Form field description showing Smart IP tokens
smart_ip_views_bridge_form_select_field_tokens Form select field Smart IP tokens
smart_ip_views_bridge_help Implements hook_help().
smart_ip_views_bridge_substitute_token_value Substitute token to Smart IP value
smart_ip_views_bridge_views_api Implements hook_views_api(). Views Integration Hook