You are here

getdirections.module in Get Directions 7.2

Fetches google map directions.

File

getdirections.module
View source
<?php

/**
 * @file
 * Fetches google map directions.
 */
define('GETDIRECTIONS_PATH', drupal_get_path('module', 'getdirections'));

/**
 * Implements hook_help().
 */
function getdirections_help($path, $arg) {
  switch ($path) {
    case 'admin/help#getdirections':
      $output = '<p>' . t('Fetches google map directions.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_init().
 */
function getdirections_init() {
  if (!module_load_include('inc', 'getdirections', 'getdirections.config')) {
    define('GETDIRECTIONS_WAYPOINTS_MAX', 8);
  }
  module_load_include('inc', 'getdirections', 'getdirections.api');
  if (module_exists('colorbox')) {
    $getdirections_colorbox = variable_get('getdirections_colorbox', array(
      'enable' => 0,
      'width' => 600,
      'height' => 600,
    ));
    $settings = array(
      'getdirections_colorbox' => $getdirections_colorbox,
    );
    drupal_add_js($settings, 'setting');
    drupal_add_js(GETDIRECTIONS_PATH . '/getdirections.js');
  }
}

/**
 * Implements hook_permission().
 */
function getdirections_permission() {
  return array(
    'access gmap getdirections' => array(
      'title' => t('Access Getdirections'),
      'description' => t('View and use the Getdirections module.'),
    ),
    'access user getdirections' => array(
      'title' => t('Access user Getdirections'),
      'description' => t('View users with the Getdirections module.'),
    ),
  );
}

/**
 * Implement hook_menu().
 */
function getdirections_menu() {
  $items = array();
  $items['admin/config/services/getdirections'] = array(
    'title' => 'Get directions',
    'description' => 'Configure Get directions',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'getdirections_settings_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'getdirections.admin.inc',
  );
  $items['getdirections'] = array(
    'title' => 'Get directions',
    'access arguments' => array(
      'access gmap getdirections',
    ),
    'page callback' => 'getdirections_direction',
    'type' => MENU_SUGGESTED_ITEM,
  );

  // location enabled
  $items['getdirections/location/%getdirections_fromto/%getdirections_id'] = array(
    'title' => 'Get directions',
    'access callback' => 'getdirections_access_location',
    'page callback' => 'getdirections_setlocation',
    'page arguments' => array(
      2,
      3,
    ),
    'type' => MENU_CALLBACK,
  );
  $items['getdirections/locations/%getdirections_id/%getdirections_id'] = array(
    'title' => 'Get directions',
    'access callback' => 'getdirections_access_location',
    'page callback' => 'getdirections_setlocations',
    'page arguments' => array(
      2,
      3,
    ),
    'type' => MENU_CALLBACK,
  );
  $items['getdirections/locations_via/%getdirections_via'] = array(
    'title' => 'Get directions',
    'access callback' => 'getdirections_access_location',
    'page callback' => 'getdirections_setlocations_via',
    'page arguments' => array(
      2,
    ),
    'type' => MENU_CALLBACK,
  );
  $items['getdirections/location_user/%getdirections_fromto/%getdirections_uid'] = array(
    'title' => 'Get directions',
    'access callback' => 'getdirections_access_user_location',
    'page callback' => 'getdirections_user_setlocation',
    'page arguments' => array(
      2,
      3,
    ),
    'type' => MENU_CALLBACK,
  );
  $items['getdirections/locations_user/%getdirections_uid/%getdirections_uid'] = array(
    'title' => 'Get directions',
    'access callback' => 'getdirections_access_user_location',
    'page callback' => 'getdirections_user_setlocations',
    'page arguments' => array(
      2,
      3,
    ),
    'type' => MENU_CALLBACK,
  );
  $items['getdirections/location_n2u/%getdirections_id/%getdirections_uid'] = array(
    'title' => 'Get directions',
    'access callback' => 'getdirections_access_user_location',
    'page callback' => 'getdirections_n2u_setlocation',
    'page arguments' => array(
      2,
      3,
    ),
    'type' => MENU_CALLBACK,
  );
  $items['getdirections/location_u2n/%getdirections_uid/%getdirections_id'] = array(
    'title' => 'Get directions',
    'access callback' => 'getdirections_access_user_location',
    'page callback' => 'getdirections_u2n_setlocation',
    'page arguments' => array(
      2,
      3,
    ),
    'type' => MENU_CALLBACK,
  );
  $items['getdirections/locations_user_via/%getdirections_via'] = array(
    'title' => 'Get directions',
    'access callback' => 'getdirections_access_location',
    'page callback' => 'getdirections_user_setlocations_via',
    'page arguments' => array(
      2,
    ),
    'type' => MENU_CALLBACK,
  );
  $items['getdirections/latlon/%getdirections_fromto/%getdirections_latlon/%'] = array(
    'title' => 'Get directions',
    'access arguments' => array(
      'access gmap getdirections',
    ),
    'page callback' => 'getdirections_latlon',
    'page arguments' => array(
      2,
      3,
      4,
    ),
    'type' => MENU_CALLBACK,
  );
  $items['getdirections/latlons/%'] = array(
    'title' => 'Get directions',
    'access arguments' => array(
      'access gmap getdirections',
    ),
    'page callback' => 'getdirections_latlons',
    'page arguments' => array(
      2,
    ),
    'type' => MENU_CALLBACK,
  );
  $items['getdirections/location_id/%getdirections_fromto/%getdirections_id'] = array(
    'title' => 'Get directions',
    'access callback' => 'getdirections_access_location',
    'page callback' => 'getdirections_setlocation_id',
    'page arguments' => array(
      2,
      3,
    ),
    'type' => MENU_CALLBACK,
  );
  if (module_exists('getlocations_fields')) {
    if (getlocations_get_vocabularies()) {

      // taxonomy_term
      $items['getdirections/location_term/%getdirections_fromto/%getdirections_id'] = array(
        'title' => 'Get directions',
        'access callback' => 'getdirections_access_location',
        'page callback' => 'getdirections_term_setlocation',
        'page arguments' => array(
          2,
          3,
        ),
        'type' => MENU_CALLBACK,
      );
    }
    if (getlocations_check_entity_type('comment')) {

      // comment
      $items['getdirections/location_comment/%getdirections_fromto/%getdirections_id'] = array(
        'title' => 'Get directions',
        'access callback' => 'getdirections_access_location',
        'page callback' => 'getdirections_comment_setlocation',
        'page arguments' => array(
          2,
          3,
        ),
        'type' => MENU_CALLBACK,
      );
    }
  }
  $items['getdirections_box'] = array(
    'title' => 'Get directions',
    'access arguments' => array(
      'access gmap getdirections',
    ),
    'page callback' => 'getdirections_direction_box',
    'type' => MENU_CALLBACK,
  );
  if (module_exists('date_popup')) {
    $items['getdirections/getdirections_get_datestamp'] = array(
      'access arguments' => array(
        'access gmap getdirections',
      ),
      'page callback' => 'getdirections_get_datestamp',
      'type' => MENU_CALLBACK,
    );
  }
  if (module_exists('smart_ip')) {
    $items['getdirections/smart_ip'] = array(
      'page callback' => 'getdirections_smart_ip',
      'access arguments' => array(
        'access content',
      ),
      'type' => MENU_CALLBACK,
    );
  }
  if (module_exists('ip_geoloc')) {
    $items['getdirections/ip_geoloc'] = array(
      'page callback' => 'getdirections_ip_geoloc',
      'access arguments' => array(
        'access content',
      ),
      'type' => MENU_CALLBACK,
    );
  }
  return $items;
}

/**
 * menu placeholder functions
 */
function getdirections_fromto_load($arg) {
  $allowed = array(
    'from',
    'to',
  );
  return in_array($arg, $allowed) ? $arg : FALSE;
  return $arg;
}
function getdirections_id_load($arg) {
  return is_numeric($arg) && $arg > 0 ? $arg : FALSE;
}
function getdirections_uid_load($arg) {
  return is_numeric($arg) && $arg > 0 && getdirections_user_check($arg) ? $arg : FALSE;
}
function getdirections_via_load($arg) {
  if (stristr($arg, ',')) {
    $x = explode(',', $arg);
    foreach ($x as $v) {
      if (!is_numeric($v)) {
        return FALSE;
      }
    }
    return $arg;
  }
  return FALSE;
}
function getdirections_latlon_load($arg) {
  if (getdirections_latlon_check($arg)) {
    return $arg;
  }
  return FALSE;
}

/**
 * menu access functions
 */
function getdirections_access_location() {
  if (user_access('access gmap getdirections')) {
    if (getdirections_get_current_supported_module()) {
      return TRUE;
    }
  }
  return FALSE;
}
function getdirections_access_user_location() {
  if (user_access('access user getdirections')) {
    if (getdirections_get_current_supported_module()) {
      return TRUE;
    }
  }
  return FALSE;
}

/**
 * Function to setup the map and form
 *
 * @param string $direction
 *   Optional. The direction the supplied input applies to
 * @param string $location
 *   Optional. The string to search with
 * @param string $country
 *   Optional. The country
 * @param string $latlon
 *   Optional. Comma delimited string containing latitude and longitude
 * @return
 *   Returns the themed map and form
 */
function getdirections_direction($direction = '', $location = '', $country = '', $latlon = '', $id = 0, $type = 'node', $width = '', $height = '') {
  if (!getdirections_check()) {
    return t('You need to !c first.', array(
      '!c' => l('configure Getdirections', 'admin/config/services/getdirections'),
    ));
  }
  $getdirections_defaults = getdirections_defaults();
  $getdirections_misc = getdirections_misc_defaults();
  getdirections_setup_map($getdirections_defaults, $getdirections_misc);
  $getdirections_settings = getdirections_get_settings($getdirections_defaults, $getdirections_misc);
  $getdirections_settings['switchfromto'] = $getdirections_misc['switchfromto'];
  drupal_add_js(array(
    'getdirections' => $getdirections_settings,
  ), 'setting');
  if ($getdirections_misc['keep_title'] && $id && $direction) {
    if ($direction == 'to') {
      $direction_translated = t('to');
    }
    elseif ($direction == 'from') {
      $direction_translated = t('from');
    }
    if ($type == 'node') {
      $node = node_load($id);
      drupal_set_title(t('Get directions @d !t', array(
        '@d' => $direction_translated,
        '!t' => $node->title,
      )));
    }
    elseif ($type == 'user') {
      $account = user_load($id);
      drupal_set_title(t('Get directions @d !t', array(
        '@d' => $direction_translated,
        '!t' => $account->name,
      )));
    }
    elseif ($type == 'term') {
      $term = taxonomy_term_load($id);
      drupal_set_title(t('Get directions @d !t', array(
        '@d' => $direction_translated,
        '!t' => $term->name,
      )));
    }
    elseif ($type == 'location') {
      $t = getdirections_get_lid_title($id);
      if ($t) {
        drupal_set_title(t('Get directions @d !t', array(
          '@d' => $direction_translated,
          '!t' => $t,
        )));
      }
    }
  }
  if (empty($width)) {
    $width = $getdirections_defaults['width'];
  }
  if (empty($height)) {
    $height = $getdirections_defaults['height'];
  }

  // remove brackets if any
  $latlon = preg_replace("/^\\(|\\)\$/", '', $latlon);
  $raw_form = drupal_get_form('getdirections_direction_form', $direction, $location, $country, $latlon);
  $form = render($raw_form);
  return theme('getdirections_show', array(
    'form' => $form,
    'width' => $width,
    'height' => $height,
    'nid' => $id,
    'type' => $type,
  ));
}

/**
 * Function to setup the map
 *
 * @param string $fromlocs
 *   Required. The string to display for From.
 * @param string $fromlatlon
 *   Required. The location point for From. In decimal latitude,longitude
 * @param string $tolocs
 *   Required. The string to display for To.
 * @param string $tolatlon
 *   Required. The location point for To. In decimal latitude,longitude
 * @return
 *   Returns the themed map.
 */
function getdirections_locations($fromlocs, $fromlatlon, $tolocs, $tolatlon, $width = '', $height = '') {
  if (!getdirections_check()) {
    return t('You need to !c first.', array(
      '!c' => l('configure Getdirections', 'admin/config/services/getdirections'),
    ));
  }
  $getdirections_defaults = getdirections_defaults();
  $getdirections_misc = getdirections_misc_defaults();
  getdirections_setup_map($getdirections_defaults, $getdirections_misc);
  $getdirections_settings = getdirections_get_settings($getdirections_defaults, $getdirections_misc);

  // remove brackets if any
  $fromlatlon = preg_replace("/^\\(|\\)\$/", '', $fromlatlon);
  $getdirections_settings['fromlatlon'] = $fromlatlon;
  $tolatlon = preg_replace("/^\\(|\\)\$/", '', $tolatlon);
  $getdirections_settings['tolatlon'] = $tolatlon;
  drupal_add_js(array(
    'getdirections' => $getdirections_settings,
  ), 'setting');
  if (empty($width)) {
    $width = $getdirections_defaults['width'];
  }
  if (empty($height)) {
    $height = $getdirections_defaults['height'];
  }
  return theme('getdirections_show_locations', array(
    'width' => $width,
    'height' => $height,
    'fromlocs' => $fromlocs,
    'tolocs' => $tolocs,
  ));
}

/**
 * Function to setup the map
 *
 * @param array $locs
 *   Required. An array of location descriptions.
 * @param array $latlons
 *   Required. An array of decimal latitude,longitude
 * @return
 *   Returns the themed map.
 */
function getdirections_locations_via($locs, $latlons, $width = '', $height = '') {
  if (!getdirections_check()) {
    return t('You need to !c first.', array(
      '!c' => l('configure Getdirections', 'admin/config/services/getdirections'),
    ));
  }
  $getdirections_defaults = getdirections_defaults();
  $getdirections_misc = getdirections_misc_defaults();
  getdirections_setup_map($getdirections_defaults, $getdirections_misc);
  $getdirections_settings = getdirections_get_settings($getdirections_defaults, $getdirections_misc);
  $getdirections_settings['latlons'] = implode('|', $latlons);
  drupal_add_js(array(
    'getdirections' => $getdirections_settings,
  ), 'setting');
  if (empty($width)) {
    $width = $getdirections_defaults['width'];
  }
  if (empty($height)) {
    $height = $getdirections_defaults['height'];
  }
  return theme('getdirections_show_locations_via', array(
    'width' => $width,
    'height' => $height,
    'locs' => $locs,
  ));
}
function getdirections_get_settings($getdirections_defaults, $getdirections_misc) {
  global $language;
  $lla = explode(',', $getdirections_defaults['latlong']);
  $settings = array(
    'lat' => $lla[0],
    'lng' => $lla[1],
    'zoom' => $getdirections_defaults['zoom'],
    'maptype' => $getdirections_defaults['maptype'],
    'controltype' => $getdirections_defaults['controltype'],
    'mtc' => $getdirections_defaults['mtc'],
    'baselayers' => $getdirections_defaults['baselayers'],
    'scale' => $getdirections_defaults['behavior']['scale'],
    'overview' => $getdirections_defaults['behavior']['overview'],
    'googlebar' => $getdirections_defaults['behavior']['googlebar'],
    'mylocale' => $language->language,
    'waypoints' => intval($getdirections_defaults['waypoints']),
    'waypoint_color' => $getdirections_defaults['waypoint_color'],
    'show_distance' => $getdirections_misc['show_distance'] ? t('Total distance') : '',
    'trafficinfo' => $getdirections_misc['trafficinfo'],
    'show_duration' => $getdirections_misc['show_duration'] ? t('Total duration') : '',
  );
  if (variable_get('getdirections_use_v3', 0)) {
    $settings['scrollwheel'] = $getdirections_defaults['behavior']['scrollwheel'];
    $settings['draggable'] = $getdirections_defaults['behavior']['draggable'];
    $settings['unitsystem'] = $getdirections_defaults['unitsystem'];
    $settings['overview_opened'] = $getdirections_defaults['behavior']['overview_opened'];
    $settings['streetview_show'] = $getdirections_defaults['streetview_show'];
    $settings['pancontrol'] = $getdirections_defaults['pancontrol'];
    $settings['advanced_autocomplete'] = $getdirections_defaults['advanced_autocomplete'];
    $settings['advanced_autocomplete_bias'] = $getdirections_defaults['advanced_autocomplete_bias'];
    $settings['advanced_autocomplete_via'] = $getdirections_defaults['advanced_autocomplete_via'];
    $settings['panoramio_show'] = $getdirections_misc['panoramio_show'];
    $settings['panoramio_state'] = $getdirections_misc['panoramio_state'];
    $settings['bicycleinfo'] = $getdirections_misc['bicycleinfo'];
    $settings['bicycleinfo_state'] = $getdirections_misc['bicycleinfo_state'];
    $settings['transitinfo'] = $getdirections_misc['transitinfo'];
    $settings['transitinfo_state'] = $getdirections_misc['transitinfo_state'];
    $settings['trafficinfo_state'] = $getdirections_misc['trafficinfo_state'];
    $settings['geolocation_enable'] = $getdirections_misc['geolocation_enable'];
    $settings['geolocation_option'] = $getdirections_misc['geolocation_option'];
    $settings['waypoints_optimise'] = intval($getdirections_defaults['waypoints_optimise']);
    $settings['transit_date_format'] = $getdirections_defaults['transit_date_format'];
    $settings['map_backgroundcolor'] = $getdirections_defaults['map_backgroundcolor'];
  }
  $settings['use_https'] = $getdirections_defaults['use_https'];
  return $settings;
}

/**
 * Function to setup the form
 *
 * @param string $fromto
 *   Optional. The direction the supplied input applies to
 * @param string $loc
 *   Optional. The string to search with
 * @param string $country
 *   Optional. The country
 * @param string $latlon
 *   Optional. Comma delimited string containing latitude and longitude
 * @return
 *   Returns the form
 */
function getdirections_direction_form($form, &$form_state, $fromto = '', $loc = '', $country = '', $latlon = '') {
  $getdirections_defaults = getdirections_defaults();
  $islocation = module_exists('location');
  $isgetlocations_fields = module_exists('getlocations_fields');
  if ($getdirections_defaults['use_country_dropdown'] && ($islocation || $isgetlocations_fields) && !$getdirections_defaults['advanced_autocomplete']) {
    $default_country = $islocation ? variable_get('location_default_country', '') : variable_get('site_default_country', '');
    $countries = array(
      '#type' => 'select',
      '#title' => t('Country'),
      '#options' => array_merge(array(
        '' => t('Please select'),
      ), $islocation ? _location_supported_countries() : getlocations_fields_get_countries_list()),
      '#default_value' => $default_country,
    );
  }
  $setdirections = "Drupal.getdirections.mygetDirections(); return false;";

  // weights
  if (getdirections_is_advanced()) {
    $from_country_weight = 1;
    $from_weight = 2;
    $to_country_weight = 3;
    $to_weight = 4;
  }
  else {
    $from_weight = 1;
    $from_country_weight = 2;
    $to_weight = 3;
    $to_country_weight = 4;
  }
  $form = array(
    '#action' => '#',
    '#attributes' => array(
      'class' => 'getdirections_form',
      'onsubmit' => $setdirections,
    ),
  );
  if ($fromto == 'from') {
    $form['from'] = array(
      '#type' => 'hidden',
      '#value' => $latlon,
    );
    if (empty($loc)) {
      if (empty($country)) {
        $mfrom = t('Starting point');
      }
      else {
        $mfrom = drupal_strtoupper($country);
      }
    }
    else {
      $mfrom = $loc;
    }
    $form['mfrom'] = array(
      '#type' => 'item',
      '#title' => t('From'),
      '#markup' => $mfrom,
    );
  }
  else {
    $from_desc = t('Enter the address, postcode and/or city and country from where you want to start your journey.');
    if (getdirections_is_advanced() && $getdirections_defaults['advanced_autocomplete']) {
      $from_desc = t('Start typing the address of your starting point, then select from the dropdown menu.');
    }
    if ($getdirections_defaults['use_country_dropdown'] && ($islocation || $isgetlocations_fields) && !$getdirections_defaults['advanced_autocomplete']) {
      $form['country_from'] = $countries;
      $form['country_from']['#weight'] = $from_country_weight;
      $from_desc = t('Enter the address, postcode and/or city from where you want to start your journey.');
    }
    $form['from'] = array(
      '#type' => 'textfield',
      '#title' => t('Starting from'),
      '#size' => $getdirections_defaults['from_width'],
      '#required' => TRUE,
      '#description' => $from_desc,
      '#weight' => $from_weight,
    );
  }
  if ($fromto == 'to') {
    $form['to'] = array(
      '#type' => 'hidden',
      '#value' => $latlon,
    );
    if (empty($loc)) {
      if (empty($country)) {
        $mto = t('Destination');
      }
      else {
        $mto = drupal_strtoupper($country);
      }
    }
    else {
      $mto = $loc;
    }
    $form['mto'] = array(
      '#type' => 'item',
      '#title' => t('To'),
      '#markup' => $mto,
    );
  }
  else {
    $to_desc = t('Enter the address, postcode and/or city and country of your journey destination.');
    if (getdirections_is_advanced() && $getdirections_defaults['advanced_autocomplete']) {
      $to_desc = t('Start typing the address of your destination, then select from the dropdown menu.');
    }
    if ($getdirections_defaults['use_country_dropdown'] && ($islocation || $isgetlocations_fields) && !$getdirections_defaults['advanced_autocomplete']) {
      $form['country_to'] = $countries;
      $form['country_to']['#weight'] = $to_country_weight;
      $to_desc = t('Enter the address, postcode and/or city of your journey destination.');
    }
    $form['to'] = array(
      '#type' => 'textfield',
      '#title' => t('Destination'),
      '#size' => $getdirections_defaults['to_width'],
      '#required' => TRUE,
      '#description' => $to_desc,
      '#weight' => $to_weight,
    );
  }
  $wt = 6;
  if (variable_get('getdirections_use_v3', 0)) {
    if ($getdirections_defaults['travelmode_show']) {
      $form['travelmode'] = array(
        '#type' => 'select',
        '#title' => t('Travel mode'),
        '#options' => array(
          'driving' => t('Driving'),
          'walking' => t('Walking'),
          'bicycling' => t('Bicycling'),
          'transit' => t('Public transport'),
        ),
        '#default_value' => 'driving',
        '#weight' => $wt,
      );
      $wt = $wt + 2;
      if (module_exists('date_popup')) {
        $form['transit_date_select'] = array(
          '#type' => 'select',
          '#title' => t('Date select'),
          '#options' => array(
            'depart' => t('Departure time'),
            'arrive' => t('Arrival time'),
          ),
          '#default_value' => 'depart',
          '#prefix' => '<div id="getdirections_transit_dates_wrapper">',
          '#weight' => $wt,
        );
        $wt = $wt + 2;
        if ($getdirections_defaults['transit_date_format'] == 'int') {
          $fmt = 'j/n/Y H:i';
        }
        else {
          $fmt = 'n/j/Y H:i';
        }
        $form['transit_dates'] = array(
          '#type' => 'date_popup',
          '#default_value' => '',
          '#date_year_range' => '-0:+1',
          '#date_format' => $fmt,
          '#mindate' => "+0",
          '#suffix' => '</div>',
          '#weight' => $wt,
        );
        $wt = $wt + 2;
      }
    }
    if ($getdirections_defaults['travelextras_show']) {
      $form['travelextras'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Travel options'),
        '#options' => array(
          'avoidhighways' => t('Avoid Highways'),
          'avoidtolls' => t('Avoid Tolls'),
          'altroute' => t('Alternative routes'),
        ),
        '#weight' => $wt,
      );
      $wt = $wt + 2;
    }
  }
  if (getdirections_is_advanced() && $getdirections_defaults['advanced_autocomplete'] && $getdirections_defaults['waypoints'] > 0 && $getdirections_defaults['advanced_autocomplete_via'] && !$getdirections_defaults['advanced_alternate']) {
    for ($ct = 1; $ct <= $getdirections_defaults['waypoints']; $ct++) {
      $form['via_autocomplete_' . $ct] = array(
        '#type' => 'textfield',
        '#title' => t('Via !c', array(
          '!c' => $ct,
        )),
        '#size' => $getdirections_defaults['advanced_autocomplete_via_width'],
        '#weight' => $wt,
      );
      $wt = $wt + 2;
    }
  }
  $getdirections_misc = getdirections_misc_defaults();
  if ($getdirections_misc['trafficinfo']) {
    $form['trafficinfo'] = array(
      '#type' => 'markup',
      '#markup' => '<input type="button" onclick="Drupal.getdirections.toggleTraffic();" title="Limited Availability" id="edit-trafficinfo" name="op" value="Toggle traffic info">',
      '#weight' => $wt,
    );
    $wt = $wt + 2;
  }
  if (variable_get('getdirections_use_v3', 0)) {
    if ($getdirections_misc['bicycleinfo']) {
      $form['bicycleinfo'] = array(
        '#type' => 'markup',
        '#markup' => '<input type="button" onclick="Drupal.getdirections.toggleBicycle();" title="Limited Availability" id="edit-bicycleinfo" name="op" value="Toggle bicycling info">',
        '#weight' => $wt,
      );
      $wt = $wt + 2;
    }
    if ($getdirections_misc['transitinfo']) {
      $form['transitinfo'] = array(
        '#type' => 'markup',
        '#markup' => '<input type="button" onclick="Drupal.getdirections.toggleTransit();" title="Limited Availability" id="edit-transitinfo" name="op" value="Toggle Public transport">',
        '#weight' => $wt,
      );
      $wt = $wt + 2;
    }
    if ($getdirections_misc['panoramio_show']) {
      $form['panoramio'] = array(
        '#type' => 'markup',
        '#markup' => '<input type="button" onclick="Drupal.getdirections.togglePanoramio();" id="edit-panoramio" name="op" value="Toggle Panoramio">',
        '#weight' => $wt,
      );
      $wt = $wt + 2;
    }
  }
  if ($getdirections_misc['switchfromto'] && !$fromto && !getdirections_is_advanced()) {
    $form['switchfromto'] = array(
      '#type' => 'markup',
      '#markup' => '<input type="button" onclick="Drupal.getdirections.toggleFromto();" id="edit-switchfromto" name="op" value="Switch From/To">',
      '#weight' => $wt,
    );
    $wt = $wt + 2;
  }
  if (getdirections_is_advanced()) {
    $form['next'] = array(
      '#type' => 'button',
      '#value' => t('Next'),
      '#attributes' => array(
        'onclick' => 'Drupal.getdirections.nextbtn(); return false;',
      ),
      '#weight' => $wt,
    );
    $wt = $wt + 2;
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Get Directions'),
    '#weight' => $wt,
  );
  return $form;
}

/**
 * Some defaults.
 */
function getdirections_defaults() {
  $getdirections_use_v3 = variable_get('getdirections_use_v3', 0);
  if (module_exists('getlocations_fields') && !$getdirections_use_v3) {
    variable_set('getdirections_use_v3', 1);
    $getdirections_use_v3 = 1;
  }

  // no gmap for v3 googlemap API
  if ($getdirections_use_v3) {
    $defaults = array(
      'api_version' => 3,
      'width' => '300px',
      'height' => '200px',
      'zoom' => 3,
      'controltype' => 'small',
      'pancontrol' => 1,
      'latlong' => '40,0',
      'maptype' => 'Map',
      'mtc' => 'standard',
      'baselayers' => array(
        'Map' => 1,
        'Satellite' => 1,
        'Hybrid' => 1,
        'Physical' => 1,
      ),
      'behavior' => array(
        'scale' => 0,
        'scrollwheel' => 0,
        'draggable' => 1,
        'overview' => 0,
        'overview_opened' => 0,
        'googlebar' => 0,
      ),
      'unitsystem' => 'metric',
      'from_width' => 20,
      'to_width' => 20,
      'travelmode_show' => 0,
      'transit_dates' => 0,
      'transit_date_format' => 'int',
      'travelextras_show' => 0,
      'streetview_show' => 0,
      'use_advanced' => 0,
      'use_country_dropdown' => 1,
      'waypoints' => 0,
      'waypoint_color' => 'white',
      'waypoints_optimise' => 0,
      'advanced_alternate' => 0,
      'advanced_autocomplete' => 0,
      'advanced_autocomplete_bias' => 0,
      'advanced_autocomplete_via' => 0,
      'advanced_autocomplete_via_width' => 20,
      'map_backgroundcolor' => '',
    );
  }
  else {
    if (module_exists('gmap')) {
      $defaults = gmap_defaults();
      $defaults['api_version'] = GMAP_API_VERSION;
      $defaults['from_width'] = 20;
      $defaults['to_width'] = 20;
      $defaults['use_advanced'] = 0;
      $defaults['waypoints'] = 0;
      $defaults['waypoint_color'] = 'white';
      $defaults['waypoints_optimise'] = 0;
      $defaults['advanced_alternate'] = 0;
      $defaults['advanced_autocomplete'] = 0;
      $defaults['advanced_autocomplete_bias'] = 0;
      $defaults['advanced_autocomplete_via'] = 0;
      $defaults['advanced_autocomplete_via_width'] = 20;
    }
    else {
      $defaults = array(
        'api_version' => 2,
        'width' => '300px',
        'height' => '200px',
        'zoom' => 3,
        'controltype' => 'small',
        'latlong' => '40,0',
        'maptype' => 'Map',
        'mtc' => 'standard',
        'baselayers' => array(
          'Map' => 1,
          'Satellite' => 1,
          'Hybrid' => 1,
          'Physical' => 0,
        ),
        'behavior' => array(
          'scale' => 0,
          'overview' => 0,
          'googlebar' => 0,
        ),
        'from_width' => 20,
        'to_width' => 20,
        'use_advanced' => 0,
        'waypoints' => 0,
        'waypoint_color' => 'white',
        'waypoints_optimise' => 0,
        'advanced_alternate' => 0,
        'advanced_autocomplete' => 0,
        'advanced_autocomplete_bias' => 0,
        'advanced_autocomplete_via' => 0,
        'advanced_autocomplete_via_width' => 20,
      );
    }
  }
  $defaults['use_country_dropdown'] = 1;
  global $is_https;
  $defaults['use_https'] = $is_https ? 1 : 0;
  $getdirections_defaults = variable_get('getdirections_default', array());

  // array_merge deletes things in $defaults that are not in $getdirections_defaults ;-/
  // roll my own
  $newdefaults = array();
  foreach ($defaults as $k => $v) {
    if (is_array($v)) {
      foreach ($defaults[$k] as $k1 => $v1) {
        if (isset($getdirections_defaults[$k][$k1])) {
          $newdefaults[$k][$k1] = $getdirections_defaults[$k][$k1];
        }
        else {
          $newdefaults[$k][$k1] = $v1;
        }
      }
    }
    else {
      if (isset($getdirections_defaults[$k])) {
        $newdefaults[$k] = $getdirections_defaults[$k];
      }
      else {
        $newdefaults[$k] = $v;
      }
    }
  }
  return $newdefaults;
}
function getdirections_misc_defaults() {
  $getdirections_misc = variable_get('getdirections_misc', array(
    'keep_title' => 0,
    'show_distance' => 0,
    'trafficinfo' => 0,
    'trafficinfo_state' => 0,
    'bicycleinfo' => 0,
    'bicycleinfo_state' => 0,
    'switchfromto' => 0,
    'show_duration' => 0,
    'panoramio_show' => 0,
    'panoramio_state' => 0,
    'transitinfo' => 0,
    'transitinfo_state' => 0,
    'geolocation_enable' => 0,
    'geolocation_option' => 1,
    'geolocation_mobile_check' => 1,
  ));
  return $getdirections_misc;
}

/**
 * Function to setup the map and form
 *
 * @param string $direction
 *   Required. The direction the supplied input applies to
 * @param int $nid
 *   Required. The nid of the location node
 * @return
 *   Returns the themed map and form
 */
function getdirections_setlocation($direction, $nid, $width = '', $height = '') {
  if (is_numeric($nid) && $nid) {
    $vid = getdirections_get_vid($nid);
    $location = getdirections_load_locations($vid);
    if (count($location)) {
      $country = isset($location[0]['country']) ? $location[0]['country'] : '';
      $latlon = $location[0]['latitude'] . ',' . $location[0]['longitude'];

      // sanity check
      if (!getdirections_latlon_check($latlon)) {
        watchdog('getdirections', 'Lat, Lon failed for node id !nid', array(
          '!nid' => $nid,
        ), WATCHDOG_WARNING);
        $latlon = getdirections_load_latlon_defaults();
      }

      // $locs only used for display now
      $locs = _getdirections_loadaddress($location[0]);
      $content = getdirections_direction($direction, $locs, $country, $latlon, $nid, 'node', $width, $height);
      return $content;
    }
  }
  return getdirections_direction();
}

/**
 * Function to setup the map and form
 *
 * @param string $direction
 *   Required. The direction the supplied input applies to
 * @param int $uid
 *   Required. The uid of the location owner
 * @return
 *   Returns the themed map and form
 */
function getdirections_user_setlocation($direction, $uid, $width = '', $height = '') {
  if (is_numeric($uid) && $uid) {
    $location = getdirections_load_locations($uid, 'uid');
    if (count($location)) {
      $country = isset($location[0]['country']) ? $location[0]['country'] : '';
      $latlon = $location[0]['latitude'] . ',' . $location[0]['longitude'];

      // sanity check
      if (!getdirections_latlon_check($latlon)) {
        watchdog('getdirections', 'Lat, Lon failed for user id !uid', array(
          '!uid' => $uid,
        ), WATCHDOG_WARNING);
        $latlon = getdirections_load_latlon_defaults();
      }

      // $locs only used for display now
      $locs = _getdirections_loadaddress($location[0]);
      $content = getdirections_direction($direction, $locs, $country, $latlon, $uid, 'user', $width, $height);
      return $content;
    }
  }
  return getdirections_direction();
}

/**
 * Function to setup the map
 *
 * @param int $fromnid
 *   Required. The nid of the starting point
 * @param int $tonid
 *   Required. The nid of the destination
 * @return
 *   Returns the themed map
 */
function getdirections_setlocations($fromnid, $tonid, $width = '', $height = '') {
  if (is_numeric($fromnid) && $fromnid && is_numeric($tonid) && $tonid) {

    // From
    $fromvid = getdirections_get_vid($fromnid);
    $fromlocation = getdirections_load_locations($fromvid);
    if (count($fromlocation)) {
      $fromlatlon = $fromlocation[0]['latitude'] . ',' . $fromlocation[0]['longitude'];

      // sanity check
      if (!getdirections_latlon_check($fromlatlon)) {
        watchdog('getdirections', 'Lat, Lon failed for node id !nid', array(
          '!nid' => $fromnid,
        ), WATCHDOG_WARNING);
        $fromlatlon = getdirections_load_latlon_defaults();
      }
      $fromlocs = _getdirections_loadaddress($fromlocation[0]);

      // To
      $tovid = getdirections_get_vid($tonid);
      $tolocation = getdirections_load_locations($tovid);
      if (count($tolocation)) {
        $tolatlon = $tolocation[0]['latitude'] . ',' . $tolocation[0]['longitude'];

        // sanity check
        if (!getdirections_latlon_check($tolatlon)) {
          watchdog('getdirections', 'Lat, Lon failed for node id !nid', array(
            '!nid' => $tonid,
          ), WATCHDOG_WARNING);
          $tolatlon = getdirections_load_latlon_defaults();
        }
        $tolocs = _getdirections_loadaddress($tolocation[0]);
        $content = getdirections_locations($fromlocs, $fromlatlon, $tolocs, $tolatlon, $width, $height);
        return $content;
      }
    }
  }
  return getdirections_direction();
}

/**
 * Function to setup the map
 *
 * @param int $fromuid
 *   Required. The uid of the starting point
 * @param int $touid
 *   Required. The uid of the destination
 * @return
 *   Returns the themed map
 */
function getdirections_user_setlocations($fromuid, $touid, $width = '', $height = '') {
  if (is_numeric($fromuid) && $fromuid && is_numeric($touid) && $touid) {

    // From
    $fromlocation = getdirections_load_locations($fromuid, 'uid');
    if (count($fromlocation)) {
      $fromlatlon = $fromlocation[0]['latitude'] . ',' . $fromlocation[0]['longitude'];

      // sanity check
      if (!getdirections_latlon_check($fromlatlon)) {
        watchdog('getdirections', 'Lat, Lon failed for user id !nid', array(
          '!nid' => $fromuid,
        ), WATCHDOG_WARNING);
        $fromlatlon = getdirections_load_latlon_defaults();
      }
      $fromlocs = _getdirections_loadaddress($fromlocation[0]);

      // To
      $tolocation = getdirections_load_locations($touid, 'uid');
      if (count($tolocation)) {
        $tolatlon = $tolocation[0]['latitude'] . ',' . $tolocation[0]['longitude'];

        // sanity check
        if (!getdirections_latlon_check($tolatlon)) {
          watchdog('getdirections', 'Lat, Lon failed for user id !nid', array(
            '!nid' => $touid,
          ), WATCHDOG_WARNING);
          $tolatlon = getdirections_load_latlon_defaults();
        }
        $tolocs = _getdirections_loadaddress($tolocation[0]);
        $content = getdirections_locations($fromlocs, $fromlatlon, $tolocs, $tolatlon, $width, $height);
        return $content;
      }
    }
  }
  return getdirections_direction();
}

/**
 * Function to setup the map to display waypoints
 *
 * @param string $nids
 *   Required. A comma delimited list of nids
 * @return
 *   Returns the themed map
 */
function getdirections_setlocations_via($nids, $width = '', $height = '') {
  $arrnids = explode(',', $nids);
  foreach ($arrnids as $key => $nid) {
    if ($key > GETDIRECTIONS_WAYPOINTS_MAX && $key < count($arrnids)) {
      continue;
    }
    if (!is_numeric($nid) || !$nid) {
      continue;
    }
    $vid = getdirections_get_vid($nid);
    $location = getdirections_load_locations($vid);
    if (!count($location)) {
      continue;
    }
    $locs = _getdirections_loadaddress($location[0]);
    $latlon = $location[0]['latitude'] . ',' . $location[0]['longitude'];

    // sanity check
    if (!getdirections_latlon_check($latlon)) {
      watchdog('getdirections', 'Lat, Lon failed for node id !nid', array(
        '!nid' => $nid,
      ), WATCHDOG_WARNING);
      continue;
    }
    $latlons[] = $latlon;
  }
  $content = getdirections_locations_via($locs, $latlons, $width, $height);
  return $content;
}
function getdirections_latlons($lls, $width = '', $height = '') {
  $latlons = array();
  $locs = array();
  $arrlls = explode('|', $lls);
  foreach ($arrlls as $key => $ll) {
    if ($key > GETDIRECTIONS_WAYPOINTS_MAX && $key < count($arrlls)) {
      continue;
    }
    if (!getdirections_latlon_check($ll)) {
      watchdog('getdirections', 'Lat, Lon failed for !ll', array(
        '!ll' => $ll,
      ), WATCHDOG_WARNING);
      continue;
    }
    $latlons[] = $ll;
  }
  $content = getdirections_locations_via($locs, $latlons, $width, $height);
  return $content;
}

/**
 * Function to setup the map
 *
 * @param int $nid
 *   Required. The nid of the starting point
 * @param int $uid
 *   Required. The uid of the destination
 * @return
 *   Returns the themed map
 */
function getdirections_n2u_setlocation($nid, $uid, $width = '', $height = '') {
  if (is_numeric($uid) && $uid && is_numeric($nid) && $nid) {

    // From
    $vid = getdirections_get_vid($nid);
    $fromlocation = getdirections_load_locations($vid);
    if (count($fromlocation)) {
      $fromlatlon = $fromlocation[0]['latitude'] . ',' . $fromlocation[0]['longitude'];

      // sanity check
      if (!getdirections_latlon_check($fromlatlon)) {
        watchdog('getdirections', 'Lat, Lon failed for node id !nid', array(
          '!nid' => $nid,
        ), WATCHDOG_WARNING);
        $fromlatlon = getdirections_load_latlon_defaults();
      }
      $fromlocs = _getdirections_loadaddress($fromlocation[0]);

      // To
      $tolocation = getdirections_load_locations($uid, 'uid');
      if (count($tolocation)) {
        $tolatlon = $tolocation[0]['latitude'] . ',' . $tolocation[0]['longitude'];

        // sanity check
        if (!getdirections_latlon_check($tolatlon)) {
          watchdog('getdirections', 'Lat, Lon failed for user id !nid', array(
            '!nid' => $uid,
          ), WATCHDOG_WARNING);
          $tolatlon = getdirections_load_latlon_defaults();
        }
        $tolocs = _getdirections_loadaddress($tolocation[0]);
        $content = getdirections_locations($fromlocs, $fromlatlon, $tolocs, $tolatlon, $width, $height);
        return $content;
      }
    }
  }
  return getdirections_direction();
}

/**
 * Function to setup the map and form
 *
 * @param int $uid
 *   Required. The uid of the starting point
 * @param int $nid
 *   Required. The nid of the destination
 * @return
 *   Returns the themed map
 */
function getdirections_u2n_setlocation($uid, $nid, $width = '', $height = '') {
  if (is_numeric($uid) && $uid && is_numeric($nid) && $nid) {

    // From
    $fromlocation = getdirections_load_locations($uid, 'uid');
    if (count($fromlocation)) {
      $fromlatlon = $fromlocation[0]['latitude'] . ',' . $fromlocation[0]['longitude'];

      // sanity check
      if (!getdirections_latlon_check($fromlatlon)) {
        watchdog('getdirections', 'Lat, Lon failed for user id !nid', array(
          '!nid' => $uid,
        ), WATCHDOG_WARNING);
        $fromlatlon = getdirections_load_latlon_defaults();
      }
      $fromlocs = _getdirections_loadaddress($fromlocation[0]);

      // To
      $vid = getdirections_get_vid($nid);
      $tolocation = getdirections_load_locations($vid);
      if (count($tolocation)) {
        $tolatlon = $tolocation[0]['latitude'] . ',' . $tolocation[0]['longitude'];

        // sanity check
        if (!getdirections_latlon_check($tolatlon)) {
          watchdog('getdirections', 'Lat, Lon failed for node id !nid', array(
            '!nid' => $nid,
          ), WATCHDOG_WARNING);
          $tolatlon = getdirections_load_latlon_defaults();
        }
        $tolocs = _getdirections_loadaddress($tolocation[0]);
        $content = getdirections_locations($fromlocs, $fromlatlon, $tolocs, $tolatlon, $width, $height);
        return $content;
      }
    }
  }
  return getdirections_direction();
}

/**
 * Function to setup the map to display waypoints
 *
 * @param $uids
 *   Required. A comma delimited list of uids
 * @return
 *   Returns the themed map
 */
function getdirections_user_setlocations_via($uids, $width = '', $height = '') {
  $arruids = explode(',', $uids);
  foreach ($arruids as $key => $uid) {
    if ($key > GETDIRECTIONS_WAYPOINTS_MAX && $key < count($arruids)) {
      continue;
    }
    if (!is_numeric($uid) || !$uid) {
      continue;
    }
    $locations = getdirections_load_locations($uid, 'uid');
    if (!count($locations)) {
      continue;
    }
    $locs = _getdirections_loadaddress($locations[0]);
    $latlon = $locations[0]['latitude'] . ',' . $locations[0]['longitude'];

    // sanity check
    if (!getdirections_latlon_check($latlon)) {
      watchdog('getdirections', 'Lat, Lon failed for user id !nid', array(
        '!nid' => $uid,
      ), WATCHDOG_WARNING);
      continue;
    }
    $latlons[] = $latlon;
  }
  $content = getdirections_locations_via($locs, $latlons, $width, $height);
  return $content;
}

/**
 * Function to setup the map and form
 *
 * @param string $direction
 *   Required. The direction the supplied input applies to
 * @param string $latlon
 *   Required. A latitude,longitude pair
 * @param string $locs
 * Optional. a description of the target.
 * @return
 *   Returns the themed map and form
 */
function getdirections_latlon($direction, $latlon, $locs = '', $width = '', $height = '') {
  $country = '';
  $nid = 0;
  $type = 'other';
  if (!getdirections_latlon_check($latlon)) {
    watchdog('getdirections', 'Lat, Lon failed for user id !uid', array(
      '!uid' => $uid,
    ), WATCHDOG_WARNING);
    $latlon = getdirections_load_latlon_defaults();
  }
  $content = getdirections_direction($direction, $locs, $country, $latlon, $nid, $type, $width, $height);
  return $content;
}

/**
 * Function to setup the map and form
 *
 * @param string $direction
 *   Required. The direction the supplied input applies to
 * @param int $lid
 *   Required. The lid of the location
 * @return
 *   Returns the themed map and form
 */
function getdirections_setlocation_id($direction, $lid, $width = '', $height = '') {
  if (is_numeric($lid) && $lid) {
    $location = getdirections_load_location($lid);
    if (count($location)) {
      $country = isset($location['country']) ? $location['country'] : '';
      $latlon = $location['latitude'] . ',' . $location['longitude'];

      // sanity check
      if (!getdirections_latlon_check($latlon)) {
        watchdog('getdirections', 'Lat, Lon failed for location id !lid', array(
          '!lid' => $lid,
        ), WATCHDOG_WARNING);
        $latlon = getdirections_load_latlon_defaults();
      }

      // $locs only used for display now
      $locs = _getdirections_loadaddress($location);
      $content = getdirections_direction($direction, $locs, $country, $latlon, $lid, 'location', $width, $height);
      return $content;
    }
  }
  return getdirections_direction();
}

/**
 * Function to setup the map and form
 *
 * @param string $direction
 *   Required. The direction the supplied input applies to
 * @param int $tid
 *   Required. The tid of the location enabled taxonomy term
 * @return
 *   Returns the themed map and form
 */
function getdirections_term_setlocation($direction, $tid, $width = '', $height = '') {
  if (is_numeric($tid) && $tid) {
    $locations = getdirections_load_locations($tid, 'tid');
    if (count($locations)) {
      $country = isset($locations[0]['country']) ? $locations[0]['country'] : '';
      $latlon = $locations[0]['latitude'] . ',' . $locations[0]['longitude'];

      // sanity check
      if (!getdirections_latlon_check($latlon)) {
        watchdog('getdirections', 'Lat, Lon failed for term id !tid', array(
          '!tid' => $tid,
        ), WATCHDOG_WARNING);
        $latlon = getdirections_load_latlon_defaults();
      }

      // $locs only used for display now

      #$loc = _getdirections_loadaddress($location[0]);
      $term = taxonomy_term_load($tid);
      $locs = $term->name;
      $content = getdirections_direction($direction, $locs, $country, $latlon, $tid, 'term', $width, $height);
      return $content;
    }
  }
  return getdirections_direction();
}

/**
 * Function to setup the map and form
 *
 * @param string $direction
 *   Required. The direction the supplied input applies to
 * @param int $cid
 *   Required. The cid of the location enabled comment
 * @return
 *   Returns the themed map and form
 */
function getdirections_comment_setlocation($direction, $cid, $width = '', $height = '') {
  if (is_numeric($cid) && $cid) {
    $locations = getdirections_load_locations($cid, 'cid');
    if (count($locations)) {
      $country = isset($locations[0]['country']) ? $locations[0]['country'] : '';
      $latlon = $locations[0]['latitude'] . ',' . $locations[0]['longitude'];

      // sanity check
      if (!getdirections_latlon_check($latlon)) {
        watchdog('getdirections', 'Lat, Lon failed for comment id !cid', array(
          '!cid' => $cid,
        ), WATCHDOG_WARNING);
        $latlon = getdirections_load_latlon_defaults();
      }
      $comment = comment_load($cid);

      // $locs only used for display now
      $locs = $comment->subject;
      $content = getdirections_direction($direction, $locs, $country, $latlon, $cid, 'comment', $width, $height);
      return $content;
    }
  }
  return getdirections_direction();
}

/**
 * Function to setup the map scripts
 *
 * @param $defaults
 *   Required. the current settings.
 * Sets up the call to googlemaps, css and the relevant getdirections js in html head
 */
function getdirections_setup_map($defaults, $misc_defaults) {
  global $language;
  $getdirections_paths = getdirections_paths_get();
  if (variable_get('getdirections_use_v3', 0)) {

    // v3 googlemaps API
    $key = variable_get('getdirections_api3_key', '');
    $query = array();
    if (!empty($key)) {
      $query['key'] = $key;
    }
    $query['sensor'] = 'false';
    $query['language'] = $language->language;
    $libraries = array();
    if ($defaults['advanced_autocomplete']) {
      $libraries[] = 'places';
    }
    if ($misc_defaults['panoramio_show']) {
      $libraries[] = 'panoramio';
    }

    // more libraries here
    // load libraries if any
    if (count($libraries)) {
      $query['libraries'] = implode(',', $libraries);
    }
    $scheme = $defaults['use_https'] ? 'https' : 'http';
    $gmapdomain = "maps.google.com/maps/api/js";
    if (!empty($key)) {
      $gmapdomain = "maps.googleapis.com/maps/api/js";
    }
    $gmaplink = url($scheme . '://' . $gmapdomain, array(
      'query' => $query,
    ));
    if ($defaults['use_advanced']) {
      if ($defaults['advanced_alternate']) {
        $js = $getdirections_paths['getdirections_v3b_path'];
      }
      else {
        $js = $getdirections_paths['getdirections_v3a_path'];
      }
    }
    else {
      $js = $getdirections_paths['getdirections_v3_path'];
    }
  }
  else {

    // v2 googlemaps API
    if (module_exists('gmap')) {
      $query = array(
        'file' => 'api',
        'v' => $defaults['api_version'] ? $defaults['api_version'] : GMAP_API_VERSION,
        'key' => gmap_get_key(),
        'hl' => $language->language,
      );
    }
    else {
      $query = array(
        'file' => 'api',
        'v' => $defaults['api_version'] ? $defaults['api_version'] : '2',
        'key' => variable_get('getdirections_api_key', ''),
        'hl' => $language->language,
      );
    }
    $scheme = 'http://maps.google.com/maps';
    if ($defaults['use_https']) {
      $scheme = 'https://maps-api-ssl.google.com/maps';
    }
    $gmaplink = url($scheme, array(
      'query' => $query,
    ));
    if ($defaults['use_advanced']) {
      $js = $getdirections_paths['getdirections_v2a_path'];
    }
    else {
      $js = $getdirections_paths['getdirections_v2_path'];
    }
  }
  drupal_add_js($gmaplink, 'external');
  drupal_add_css(GETDIRECTIONS_PATH . '/getdirections.css');
  drupal_add_js($js);
}

/**
 * Implements hook_views_api().
 */
function getdirections_views_api() {
  return array(
    'api' => 3,
  );
}

/**
 * Function to collect an address
 *
 * @param array $location
 *
 * @return
 *   Returns array.
 */
function _getdirections_loadaddress($location, $addcountry = TRUE) {

  // theme it so others can change it
  return theme('getdirections_loadaddress', array(
    'location' => $location,
    'addcountry' => $addcountry,
  ));
}

/**
 * Function to check the user status and existence
 *
 * @param int $uid
 *
 * @return
 *   Returns status.
 */
function getdirections_user_check($uid) {
  $query = db_select('users', 'u')
    ->fields('u', array(
    'status',
  ))
    ->condition('u.uid', $uid);
  $row = $query
    ->execute()
    ->fetchAssoc();
  return isset($row['status']) && $row['status'] ? TRUE : FALSE;
}

/**
 * Function to get the vid from node table.
 *
 * @param int $nid
 *
 * @return
 *   Returns vid.
 */
function getdirections_get_vid($nid) {
  if (is_numeric($nid) && $nid) {
    $query = db_select('node', 'n')
      ->fields('n', array(
      'vid',
    ))
      ->condition('n.nid', $nid);
    $row = $query
      ->execute()
      ->fetchAssoc();
    return isset($row['vid']) && $row['vid'] ? $row['vid'] : FALSE;
  }
  return FALSE;
}
function getdirections_get_nid($vid) {
  if (is_numeric($vid) && $vid) {
    $query = db_select('node', 'n')
      ->fields('n', array(
      'nid',
    ))
      ->condition('n.vid', $vid);
    $row = $query
      ->execute()
      ->fetchAssoc();
    return isset($row['nid']) && $row['nid'] ? $row['nid'] : FALSE;
  }
  return FALSE;
}
function getdirections_get_nid_from_lid($lid) {
  if ($lid) {
    $query = FALSE;
    if (module_exists('getlocations_fields')) {
      $nid = getlocations_get_nid_from_lid($lid);
      if ($nid) {
        return $nid;
      }
    }
    elseif (module_exists('location')) {
      $query = db_select('location_instance', 'i')
        ->fields('i', array(
        'nid',
      ))
        ->condition('i.lid', $lid);
    }
    if ($query) {
      $row = $query
        ->execute()
        ->fetchAssoc();
      return isset($row['nid']) && $row['nid'] ? $row['nid'] : FALSE;
    }
  }
  return FALSE;
}
function getdirections_get_tid_from_lid($lid) {
  $tid = FALSE;
  if (module_exists('getlocations_fields')) {
    $tid = getlocations_get_tid_from_lid($lid);
  }
  if ($tid) {
    return $tid;
  }
  return FALSE;
}
function getdirections_get_lid_title($lid) {
  if ($lid) {
    $query = FALSE;
    if (module_exists('getlocations_fields')) {
      $query = db_select('getlocations_fields', 'i')
        ->fields('i', array(
        'name',
      ))
        ->condition('i.glid', $lid);
    }
    elseif (module_exists('location')) {
      $query = db_select('location', 'i')
        ->fields('i', array(
        'name',
      ))
        ->condition('i.lid', $lid);
    }
    if ($query) {
      $row = $query
        ->execute()
        ->fetchAssoc();
      return isset($row['name']) && $row['name'] ? $row['name'] : FALSE;
    }
  }
  return FALSE;
}
function getdirections_is_advanced() {
  $defaults = getdirections_defaults();
  if (isset($defaults['use_advanced']) && $defaults['use_advanced']) {
    return TRUE;
  }
  return FALSE;
}
function getdirections_load_latlon_defaults() {
  $getdirections_defaults = getdirections_defaults();
  return $getdirections_defaults['latlong'];
}
function getdirections_check() {
  $check = variable_get('getdirections_default', 'xxx');
  if ($check == 'xxx') {
    return FALSE;
  }
  return TRUE;
}

/**
 * Function to check a lat,lon string
 *
 * @param string $latlon
 *
 * @return
 *   Returns true or false
 */
function getdirections_latlon_check($latlon) {
  if (!empty($latlon) && preg_match("/[0-9.\\-],[0-9.\\-]/", $latlon)) {
    $ll = explode(',', $latlon);
    if (isset($ll[0]) && is_numeric($ll[0]) && $ll[0] != 0 && isset($ll[1]) && is_numeric($ll[1]) && $ll[1] != 0) {
      return $latlon;
    }
  }
  return FALSE;
}

// for colorbox and suchlike
function getdirections_direction_box() {
  $a1 = FALSE;
  $a2 = FALSE;
  $a3 = FALSE;
  $a4 = FALSE;
  if (arg(1)) {
    $a1 = arg(1);
  }
  if (arg(2)) {
    $a2 = arg(2);
  }
  if (arg(3)) {
    $a3 = arg(3);
  }
  if (arg(4)) {
    $a4 = arg(4);
  }
  if ($a1 == 'location' && ($a2 == 'from' || $a2 == 'to') && is_numeric($a3)) {
    $content = getdirections_setlocation($a2, $a3);
  }
  elseif ($a1 == 'locations' && is_numeric($a2) && is_numeric($a3)) {
    $content = getdirections_setlocations($a2, $a3);
  }
  elseif ($a1 == 'locations_via' && $a2) {
    $content = getdirections_setlocations_via($a2);
  }
  elseif ($a1 == 'locations_user_via' && $a2) {
    $content = getdirections_user_setlocations_via($a2);
  }
  elseif ($a1 == 'location_user' && ($a2 == 'from' || $a2 == 'to') && is_numeric($a3)) {
    $content = getdirections_user_setlocation($a2, $a3);
  }
  elseif ($a1 == 'locations_users' && is_numeric($a2) && is_numeric($a3)) {
    $content = getdirections_user_setlocations($a2, $a3);
  }
  elseif ($a1 == 'location_n2u' && is_numeric($a2) && is_numeric($a3)) {
    $content = getdirections_n2u_setlocation($a2, $a3);
  }
  elseif ($a1 == 'location_u2n' && is_numeric($a2) && is_numeric($a3)) {
    $content = getdirections_u2n_setlocation($a2, $a3);
  }
  elseif ($a1 == 'latlon' && ($a2 == 'from' || $a2 == 'to')) {
    $content = getdirections_latlon($a2, $a3, $a4);
  }
  elseif ($a1 == 'latlons' && $a2) {
    $content = getdirections_latlons($a2);
  }
  elseif ($a1 == 'location_id' && ($a2 == 'from' || $a2 == 'to')) {
    $content = getdirections_setlocation_id($a2, $a3);
  }
  elseif ($a1 == 'location_term' && ($a2 == 'from' || $a2 == 'to')) {
    $content = getdirections_term_setlocation($a2, $a3);
  }
  elseif ($a1 == 'location_comment' && ($a2 == 'from' || $a2 == 'to')) {
    $content = getdirections_comment_setlocation($a2, $a3);
  }
  else {
    $content = getdirections_direction();
  }
  $output = theme('getdirections_box', array(
    'content' => $content,
  ));
  print $output;
  exit;
}
function getdirections_load_locations($id, $key = 'vid') {
  $locations = '';
  $module = getdirections_get_current_supported_module();
  if ($module == 'getlocations_fields') {
    $locations = getlocations_fields_load_locations($id, $key);
  }
  elseif ($module == 'location_node' || $module == 'location_cck') {
    $locations = location_load_locations($id, $key);
  }
  elseif ($module == 'geofield') {
    $locations = getdirections_other_load_locations($id, $key, 'geofield');
  }
  elseif ($module == 'geolocation') {
    $locations = getdirections_other_load_locations($id, $key, 'geolocation');
  }
  return $locations;
}
function getdirections_load_location($lid) {
  $location = '';
  if (module_exists('getlocations_fields')) {
    $location = getlocations_fields_load_location($lid);
  }
  elseif (module_exists('location')) {
    $location = location_load_location($lid);
  }
  return $location;
}
function getdirections_other_load_locations($id, $key = 'vid', $module = '') {
  global $language;
  $locations = array();
  if (empty($module)) {
    return $locations;
  }
  if ($key == 'nid') {
    $entity_type = 'node';
    $obj = node_load($id);
    $location_name = $obj->title;
  }
  elseif ($key == 'vid') {
    $entity_type = 'node';
    $obj = node_load(NULL, $id);
    $location_name = $obj->title;
  }
  elseif ($key == 'uid') {
    $entity_type = 'user';
    $obj = user_load($id);
    $location_name = $obj->name;
  }
  elseif ($key == 'tid') {
    $entity_type = 'taxonomy_term';
    $obj = taxonomy_term_load($id);
    $location_name = $obj->name;
  }
  elseif ($key == 'cid') {
    $entity_type = 'comment';
    $obj = comment_load($id);
    $location_name = $obj->subject;
  }

  // get the field_xxx names
  $objfields = array();
  foreach ($obj as $k => $therest) {
    if (preg_match("/^field_/", $k)) {
      $objfields[] = $k;
    }
  }
  if (empty($objfields)) {
    return $locations;
  }

  // find a geofield
  if ($module == 'geofield') {
    $type = 'geofield';
    $geofields = getdirections_other_get_fieldname($type, $module, $entity_type);
    if (!empty($geofields)) {
      $geofield = '';
      foreach ($objfields as $field) {
        if (in_array($field, $geofields)) {
          $geofield = $field;
        }
      }
      if (empty($geofield)) {
        return $locations;
      }

      // got one
      $geo = $obj->{$geofield};
      $geo_keys = array_keys($geo);
      foreach ($geo_keys as $lang) {
        if ($lang == $language->language || isset($obj->language) && $lang == $obj->language || $lang == 'und') {
          $ct = 0;
          foreach ($geo[$lang] as $g) {
            $locations[$ct]['latitude'] = $g['lat'];
            $locations[$ct]['longitude'] = $g['lon'];
            $locations[$ct][$key] = $id;
            if ($key == 'vid') {
              $locations[$ct]['nid'] = getdirections_get_nid($id);
            }
            $locations[$ct]['key'] = $key;
            $ct++;
          }
        }
      }
    }
  }
  elseif ($module == 'geolocation') {
    $type = 'geolocation_latlng';
    $geolocationfields = getdirections_other_get_fieldname($type, $module, $entity_type);
    if (!empty($geolocationfields)) {
      $geolocationfield = '';
      foreach ($objfields as $field) {

        // test for geolocationfields
        if (in_array($field, $geolocationfields)) {
          $geolocationfield = $field;
        }
      }
      if (empty($geolocationfield)) {
        return $locations;
      }

      // got one
      $geo = $obj->{$geolocationfield};
      $geo_keys = array_keys($geo);
      foreach ($geo_keys as $lang) {
        if ($lang == $language->language || isset($obj->language) && $lang == $obj->language || $lang == 'und') {
          $ct = 0;
          foreach ($geo[$lang] as $g) {
            $locations[$ct]['latitude'] = $g['lat'];
            $locations[$ct]['longitude'] = $g['lng'];
            $locations[$ct][$key] = $id;
            if ($key == 'vid') {
              $locations[$ct]['nid'] = getdirections_get_nid($id);
            }
            $locations[$ct]['key'] = $key;
            $locations[$ct]['name'] = $location_name;
            $ct++;
          }
        }
      }
    }
  }

  // sanity check
  if (empty($locations)) {
    return $locations;
  }

  // addressfield
  if (module_exists('addressfield')) {

    // find an addressfield
    $type = 'addressfield';
    $addressfields = getdirections_other_get_fieldname($type, 'addressfield', $entity_type);
    if (!empty($addressfields)) {
      foreach ($objfields as $field) {
        if (in_array($field, $addressfields)) {
          $addressfield = $field;
        }
      }
      if (empty($addressfield)) {
        return $locations;
      }

      // got one
      $addr = $obj->{$addressfield};
      $addr_keys = array_keys($addr);
      foreach ($addr_keys as $lang) {
        if ($lang == $language->language || isset($obj->language) && $lang == $obj->language || $lang == 'und') {
          $ct = 0;
          foreach ($addr[$lang] as $a) {
            if (isset($locations[$ct])) {
              $locations[$ct] += getdirections_addressfield_convert($a);
            }
            $ct++;
          }
        }
      }
    }
  }
  return $locations;
}
function getdirections_other_get_fieldname($type, $module, $entity_type = '') {
  $query = db_select('field_config', 'fc');
  $query
    ->fields('fc', array(
    'field_name',
  ));
  $query
    ->join('field_config_instance', 'i', 'fc.id = i.field_id');
  $query
    ->condition('fc.module', $module);
  $query
    ->condition('fc.type', $type);
  $query
    ->condition('fc.active', 1);
  $query
    ->condition('fc.storage_active', 1);
  if (!empty($entity_type)) {
    $query
      ->condition('i.entity_type', $entity_type);
  }
  $rows = $query
    ->execute();
  $field_names = array();
  foreach ($rows as $row) {
    $field_names[] = $row->field_name;
  }
  return $field_names;
}
function getdirections_paths_get() {
  $getdirections_paths = variable_get('getdirections_paths', array(
    'getdirections_v2_path' => GETDIRECTIONS_PATH . '/getdirections_v2.js',
    'getdirections_v2a_path' => GETDIRECTIONS_PATH . '/getdirections_v2a.js',
    'getdirections_v3_path' => GETDIRECTIONS_PATH . '/getdirections_v3.js',
    'getdirections_v3a_path' => GETDIRECTIONS_PATH . '/getdirections_v3a.js',
    'getdirections_v3b_path' => GETDIRECTIONS_PATH . '/getdirections_v3b.js',
  ));
  return $getdirections_paths;
}
function getdirections_get_datestamp() {
  $d = $_GET['d'];
  $t = $_GET['t'];
  $date = $d . ' ' . $t;
  $time = strtotime($date);
  drupal_json_output(array(
    'content' => $time * 1000,
  ));
}
function getdirections_is_mobile() {
  $getdirections_misc_defaults = getdirections_misc_defaults();
  if (!$getdirections_misc_defaults['geolocation_mobile_check']) {
    return TRUE;
  }
  $getdirections_ua = getdirections_ua_get();
  $str = "/" . $getdirections_ua['ua_string'] . "/i";
  $is_mobile = FALSE;
  $ua = $_SERVER['HTTP_USER_AGENT'];
  if (preg_match($str, $ua)) {
    $is_mobile = TRUE;
  }
  return $is_mobile;
}
function getdirections_ua_get() {
  $default = array(
    'ua_string' => "iphone|ipad|android|blackberry|blazer|bolt|symbian|mobile safari|fennec|gobrowser|iemobile|maemo browser|profile midp|minimo|kindle|opera mini|opera mobi|skyfire|teashark|uzardweb",
  );
  $getdirections_ua = variable_get('getdirections_ua', $default);
  return $getdirections_ua;
}

/**
 * @return
 *   Returns location array
 *
 */
function getdirections_smart_ip() {
  if (module_exists('smart_ip')) {
    global $user;
    $location = FALSE;
    if ($user->uid > 0 && isset($user->data['geoip_location'])) {
      $location = $user->data['geoip_location'];
    }
    elseif (isset($_SESSION['smart_ip']['location'])) {
      $location = $_SESSION['smart_ip']['location'];
    }
    drupal_json_output($location);
  }
  exit;
}

/**
 * @return
 *   Returns location array
 *
 */
function getdirections_ip_geoloc() {
  if (module_exists('ip_geoloc')) {
    $location = FALSE;
    if (isset($_SESSION['ip_geoloc']['location'])) {
      $location = $_SESSION['ip_geoloc']['location'];
    }
    drupal_json_output($location);
  }
  exit;
}
function getdirections_supported_modules() {
  return array(
    'getlocations_fields',
    'geofield',
    'geolocation',
    'location_node',
    'location_cck',
  );
}
function getdirections_get_current_supported_module() {
  $modules = getdirections_supported_modules();
  foreach ($modules as $module) {
    if (module_exists($module)) {
      return $module;
    }
  }
  return FALSE;
}
function getdirections_addressfield_convert($address) {
  $location = theme('getdirections_addressfield_convert', array(
    'address' => $address,
  ));
  return $location;
}

/**
 * Implements hook_theme().
 *
 * This lets us tell Drupal about our theme functions and their variables.
 */
function getdirections_theme() {
  return array(
    'getdirections_show' => array(
      'variables' => array(
        'form' => NULL,
        'width' => '',
        'height' => '',
        'nid' => 0,
        'type' => '',
      ),
    ),
    'getdirections_show_locations' => array(
      'variables' => array(
        'width' => '',
        'height' => '',
        'fromlocs' => '',
        'tolocs' => '',
      ),
    ),
    'getdirections_show_locations_via' => array(
      'variables' => array(
        'width' => '',
        'height' => '',
        'locs' => NULL,
      ),
    ),
    'getdirections_direction_form' => array(
      'render element' => 'form',
    ),
    'getdirections_box' => array(
      'variables' => array(
        'content' => NULL,
      ),
      'template' => 'getdirections_box',
    ),
    'getdirections_loadaddress' => array(
      'variables' => array(
        'location' => array(),
        'addcountry' => FALSE,
      ),
    ),
    'getdirections_addressfield_loadaddress' => array(
      'variables' => array(
        'address' => array(),
        'addcountry' => FALSE,
      ),
    ),
    'getdirections_addressfield_convert' => array(
      'variables' => array(
        'address' => array(),
      ),
    ),
  );
}

/**
 * Theme to use for when one or no locations are supplied.
 *
 */
function theme_getdirections_show($variables) {
  $form = $variables['form'];
  $width = $variables['width'];
  $height = $variables['height'];
  $nid = $variables['nid'];
  $type = $variables['type'];
  $output = '';
  $getdirections_returnlink_default = array(
    'page_enable' => 0,
    'page_link' => t('Return to page'),
    'user_enable' => 0,
    'user_link' => t('Return to page'),
    'term_enable' => 0,
    'term_link' => t('Return to page'),
    'comment_enable' => 0,
    'comment_link' => t('Return to page'),
  );
  $getdirections_returnlink = variable_get('getdirections_returnlink', $getdirections_returnlink_default);
  $returnlink = FALSE;
  if (isset($getdirections_returnlink['page_enable']) && $getdirections_returnlink['page_enable'] && $nid > 0 && $type == 'node') {
    $node = node_load($nid);
    if ($node) {
      $linktext = $getdirections_returnlink['page_link'];
      if (preg_match("/%t/", $linktext)) {
        $linktext = preg_replace("/%t/", $node->title, $linktext);
      }
      $l = l($linktext, 'node/' . $node->nid);
      $returnlink = '<div class="getdirections_returnlink">' . $l . '</div>';
    }
  }
  elseif (isset($getdirections_returnlink['user_enable']) && $getdirections_returnlink['user_enable'] && $nid > 0 && $type == 'user') {

    // $nid is actually uid
    $account = user_load($nid);
    if ($account) {
      $linktext = $getdirections_returnlink['user_link'];
      if (preg_match("/%n/", $linktext)) {
        $linktext = preg_replace("/%n/", $account->name, $linktext);
      }
      $l = l($linktext, 'user/' . $account->uid);
      $returnlink = '<div class="getdirections_returnlink">' . $l . '</div>';
    }
  }
  elseif (isset($getdirections_returnlink['page_enable']) && $getdirections_returnlink['page_enable'] && $nid > 0 && $type == 'location') {

    // $nid is actually lid
    $id = getdirections_get_nid_from_lid($nid);
    if ($id) {
      $node = node_load($id);
      $linktext = $getdirections_returnlink['page_link'];
      if (preg_match("/%t/", $linktext)) {
        $linktext = preg_replace("/%t/", $node->title, $linktext);
      }
      $l = l($linktext, 'node/' . $node->nid);
      $returnlink = '<div class="getdirections_returnlink">' . $l . '</div>';
    }
  }
  elseif (isset($getdirections_returnlink['term_enable']) && $getdirections_returnlink['term_enable'] && $nid > 0 && $type == 'term') {

    // $nid is actually tid
    $term = taxonomy_term_load($nid);
    if ($term) {
      $linktext = $getdirections_returnlink['term_link'];
      if (preg_match("/%n/", $linktext)) {
        $linktext = preg_replace("/%n/", $term->name, $linktext);
      }
      $l = l($linktext, 'taxonomy/term/' . $term->tid);
      $returnlink = '<div class="getdirections_returnlink">' . $l . '</div>';
    }
  }
  elseif (isset($getdirections_returnlink['comment_enable']) && $getdirections_returnlink['comment_enable'] && $nid > 0 && $type == 'comment') {

    // $nid is actually cid
    $comment = comment_load($nid);
    if ($comment) {
      $linktext = $getdirections_returnlink['comment_link'];
      if (preg_match("/%n/", $linktext)) {
        $linktext = preg_replace("/%n/", $comment->subject, $linktext);
      }
      $l = l($linktext, 'comment/' . $comment->cid);
      $returnlink = '<div class="getdirections_returnlink">' . $l . '</div>';
    }
  }
  if ($returnlink) {
    $output .= $returnlink;
  }
  $output .= $form;
  $getdirections_defaults = getdirections_defaults();
  $getdirections_misc = getdirections_misc_defaults();
  if ($getdirections_misc['show_distance']) {
    $output .= '<div id="getdirections_show_distance"></div>';
  }
  if ($getdirections_misc['show_duration']) {
    $output .= '<div id="getdirections_show_duration"></div>';
  }
  $help = '';
  if (getdirections_is_advanced()) {
    if ($getdirections_defaults['waypoints'] > 0 && !$getdirections_defaults['advanced_alternate']) {
      $help = t('Drag <img src="http://labs.google.com/ridefinder/images/mm_20_!c.png"> to activate a waypoint', array(
        '!c' => $getdirections_defaults['waypoint_color'],
      ));
      if ($getdirections_defaults['advanced_autocomplete'] && $getdirections_defaults['advanced_autocomplete_via']) {
        $help .= ' ' . t('or use the Autocomplete boxes');
      }
    }
    elseif ($getdirections_defaults['advanced_alternate']) {
      $help = t('You can drag the route to change it');
    }
  }
  $output .= '<div id="getdirections_help">' . $help . '</div>';
  $header = array();
  $rows[] = array(
    array(
      'data' => '<div id="getdirections_map_canvas" style="width: ' . $width . '; height: ' . $height . '" ></div>',
      'valign' => 'top',
      'align' => 'center',
      'class' => 'getdirections-map',
    ),
    array(
      'data' => (getdirections_is_advanced() && $getdirections_defaults['advanced_alternate'] ? '<button id="getdirections-undo" onclick="Drupal.getdirectionsundo()">' . t('Undo') . '</button>' : '') . '<div id="getdirections_directions"></div>',
      'valign' => 'top',
      'align' => 'left',
      'class' => 'getdirections-list',
    ),
  );
  $output .= '<div class="getdirections">' . theme('table', array(
    'header' => $header,
    'rows' => $rows,
  )) . '</div>';
  return $output;
}

/**
 * Theme to use for when both locations are supplied.
 *
 */
function theme_getdirections_show_locations($variables) {
  $width = $variables['width'];
  $height = $variables['height'];
  $fromlocs = $variables['fromlocs'];
  $tolocs = $variables['tolocs'];
  $output = "";
  $output .= "<div class='getdirections_display'><label>" . t('From') . ":</label> " . $fromlocs . "</div>";
  $output .= "<div class='getdirections_display'><label>" . t('To') . ":</label> " . $tolocs . "</div>";
  $getdirections_misc = getdirections_misc_defaults();
  if ($getdirections_misc['show_distance']) {
    $output .= '<div id="getdirections_show_distance"></div>';
  }
  if ($getdirections_misc['show_duration']) {
    $output .= '<div id="getdirections_show_duration"></div>';
  }
  $header = array();
  $rows[] = array(
    array(
      'data' => '<div id="getdirections_map_canvas" style="width: ' . $width . '; height: ' . $height . '" ></div>',
      'valign' => 'top',
      'align' => 'center',
      'class' => 'getdirections-map',
    ),
    array(
      'data' => '<div id="getdirections_directions"></div>',
      'valign' => 'top',
      'align' => 'left',
      'class' => 'getdirections-list',
    ),
  );
  $output .= '<div class="getdirections">' . theme('table', array(
    'header' => $header,
    'rows' => $rows,
  )) . '</div>';
  return $output;
}

/**
 * Theme to use for when both locations are supplied with waypoints.
 *
 */
function theme_getdirections_show_locations_via($variables) {
  $width = $variables['width'];
  $height = $variables['height'];
  $locs = $variables['locs'];
  $output = "";
  foreach ($locs as $key => $loc) {
    if ($key == 0) {
      $label = t('From');
    }
    elseif ($key == count($locs) - 1) {
      $label = t('To');
    }
    else {
      $label = t('Via');
    }
    $output .= "<div class='getdirections_display'><label>" . $label . ":</label> " . $loc . "</div>";
  }
  $getdirections_misc = getdirections_misc_defaults();
  if ($getdirections_misc['show_distance']) {
    $output .= '<div id="getdirections_show_distance"></div>';
  }
  if ($getdirections_misc['show_duration']) {
    $output .= '<div id="getdirections_show_duration"></div>';
  }
  $header = array();
  $rows[] = array(
    array(
      'data' => '<div id="getdirections_map_canvas" style="width: ' . $width . '; height: ' . $height . '" ></div>',
      'valign' => 'top',
      'align' => 'center',
      'class' => 'getdirections-map',
    ),
    array(
      'data' => '<div id="getdirections_directions"></div>',
      'valign' => 'top',
      'align' => 'left',
      'class' => 'getdirections-list',
    ),
  );
  $output .= '<div class="getdirections">' . theme('table', array(
    'header' => $header,
    'rows' => $rows,
  )) . '</div>';
  return $output;
}
function theme_getdirections_direction_form($variables) {
  $form = $variables['form'];

  // if you want to do fancy things with the form, do it here ;-)
  $getdirections_defaults = getdirections_defaults();
  $getdirections_misc = getdirections_misc_defaults();
  if (isset($form['mto'])) {
    $form['mto']['#prefix'] = '<div class="container-inline getdirections_display">';
    $form['mto']['#suffix'] = '</div>';
  }
  if (isset($form['mfrom'])) {
    $form['mfrom']['#prefix'] = '<div class="container-inline getdirections_display">';
    $form['mfrom']['#suffix'] = '</div>';
  }
  if (isset($form['travelmode'])) {
    $form['travelmode']['#prefix'] = '<div class="container-inline getdirections_display">';
    $form['travelmode']['#suffix'] = '</div>';
  }
  if (isset($form['travelextras'])) {
    $form['travelextras']['#prefix'] = '<div class="container-inline getdirections_display">';
    $form['travelextras']['#suffix'] = '</div>';
  }
  if (!$getdirections_defaults['advanced_autocomplete']) {
    if ($getdirections_misc['geolocation_enable'] && getdirections_is_mobile()) {
      if ($getdirections_misc['geolocation_option'] == 1) {

        // html5 geolocation
        $geolocation_button = '<input type="button" value="' . t('Find Location') . '" title="' . t('Get the latitude and longitude for your current position from the browser') . '" id="getdirections_geolocation_button_from" class="form-submit" />';
        $geolocation_button .= '<span id="getdirections_geolocation_status_from" ></span>';
        $form['from']['#field_suffix'] = '&nbsp;&nbsp;&nbsp;' . $geolocation_button;
      }
      elseif ($getdirections_misc['geolocation_option'] == 2 && module_exists('smart_ip')) {

        // smart ip
        $geolocation_button = '<input type="button" value="' . t('Locate by Smart IP') . '" title="' . t('Get the latitude and longitude for your current position from Smart IP') . '" id="getdirections_geolocation_button_from" class="form-submit" />';
        $geolocation_button .= '<span id="getdirections_geolocation_status_from" ></span>';
        $form['from']['#field_suffix'] = '&nbsp;&nbsp;&nbsp;' . $geolocation_button;
      }
      elseif ($getdirections_misc['geolocation_option'] == 3 && module_exists('ip_geoloc')) {

        // ip_geoloc
        $geolocation_button = '<input type="button" value="' . t('Locate by IP Geolocation') . '" title="' . t('Get the latitude and longitude for your current position from IP Geolocation') . '" id="getdirections_geolocation_button_from" class="form-submit" />';
        $geolocation_button .= '<span id="getdirections_geolocation_status_from" ></span>';
        $form['from']['#field_suffix'] = '&nbsp;&nbsp;&nbsp;' . $geolocation_button;
      }
    }
  }
  if (getdirections_is_advanced()) {
    $desc = t('Fill in the form below.<br />You can also click on the map and move the marker.');
    if (isset($form['country_from'])) {
      $desc = t('Select a country first, then type in a town.<br />You can also click on the map and move the marker.');
      $form['country_from']['#prefix'] = '<div id="getdirections_start"><div class="container-inline getdirections_display">';
      $form['country_from']['#suffix'] = '</div>';
    }
    if (isset($form['from']) && $form['from']['#type'] == 'textfield' && (module_exists('location') || module_exists('getlocations_fields')) && !$getdirections_defaults['advanced_autocomplete'] && isset($form['country_from'])) {
      $form['from']['#suffix'] = '</div>';
    }
    if (isset($form['country_to'])) {
      $form['country_to']['#prefix'] = '<div id="getdirections_end"><div class="container-inline getdirections_display">';
      $form['country_to']['#suffix'] = '</div>';
    }
    if (isset($form['to']) && $form['to']['#type'] == 'textfield' && (module_exists('location') || module_exists('getlocations_fields')) && !$getdirections_defaults['advanced_autocomplete'] && isset($form['country_to'])) {
      $form['to']['#suffix'] = '</div>';
    }
    if ($getdirections_defaults['advanced_autocomplete'] && $getdirections_defaults['waypoints'] > 0 && $getdirections_defaults['advanced_autocomplete_via'] && !$getdirections_defaults['advanced_alternate']) {
      for ($ct = 1; $ct <= $getdirections_defaults['waypoints']; $ct++) {
        if ($ct == 1) {
          $form['via_autocomplete_' . $ct]['#prefix'] = '<div id="autocomplete_via_wrapper"><div class="container-inline getdirections_display">';
          $form['via_autocomplete_' . $ct]['#suffix'] = '</div>';
        }
        elseif ($ct == $getdirections_defaults['waypoints']) {
          $form['via_autocomplete_' . $ct]['#prefix'] = '<div class="container-inline getdirections_display">';
          $form['via_autocomplete_' . $ct]['#suffix'] = '</div></div>';
        }
        else {
          $form['via_autocomplete_' . $ct]['#prefix'] = '<div class="container-inline getdirections_display">';
          $form['via_autocomplete_' . $ct]['#suffix'] = '</div>';
        }
      }
    }
  }
  else {
    if (isset($form['country_from'])) {
      $form['country_from']['#prefix'] = '<div class="container-inline getdirections_display">';
      $form['country_from']['#suffix'] = '</div>';
    }
    if (isset($form['country_to'])) {
      $form['country_to']['#prefix'] = '<div class="container-inline getdirections_display">';
      $form['country_to']['#suffix'] = '</div>';
    }
    $desc = t('Fill in the form below.');
  }
  if (isset($form['trafficinfo'])) {
    $form['trafficinfo']['#prefix'] = '<div id="getdirections_trafficinfo">';
    $form['trafficinfo']['#suffix'] = '</div>';
  }
  if (isset($form['bicycleinfo'])) {
    $form['bicycleinfo']['#prefix'] = '<div id="getdirections_bicycleinfo">';
    $form['bicycleinfo']['#suffix'] = '</div>';
  }
  if (isset($form['transitinfo'])) {
    $form['transitinfo']['#prefix'] = '<div id="getdirections_transitinfo">';
    $form['transitinfo']['#suffix'] = '</div>';
  }
  if (isset($form['panoramio'])) {
    $form['panoramio']['#prefix'] = '<div id="getdirections_panoramio">';
    $form['panoramio']['#suffix'] = '</div>';
  }
  if (isset($form['switchfromto'])) {
    $form['switchfromto']['#prefix'] = '<div id="getdirections_switchfromto">';
    $form['switchfromto']['#suffix'] = '</div>';
  }
  if (isset($form['next'])) {
    $form['next']['#prefix'] = '<div id="getdirections_nextbtn">';
    $form['next']['#suffix'] = '</div>';
  }
  if (isset($form['submit'])) {
    $form['submit']['#prefix'] = '<div id="getdirections_btn">';
    $form['submit']['#suffix'] = '</div>';
  }
  $output = '<p class="description">' . $desc . '</p>';
  $output .= drupal_render_children($form);
  return $output;
}
function template_preprocess_getdirections_box(&$variables) {
  if (module_exists('admin_menu')) {
    admin_menu_suppress();
  }
  drupal_add_js(GETDIRECTIONS_PATH . '/getdirections_box.js');

  // Construct page title
  if (drupal_get_title()) {
    $head_title = array(
      strip_tags(drupal_get_title()),
      variable_get('site_name', 'Drupal'),
    );
  }
  else {
    $head_title = array(
      variable_get('site_name', 'Drupal'),
    );
    if (variable_get('site_slogan', '')) {
      $head_title[] = variable_get('site_slogan', '');
    }
  }
  $variables['head_title'] = implode(' | ', $head_title);
  $variables['base_path'] = base_path();
  $variables['head'] = drupal_get_html_head();
  $variables['language'] = $GLOBALS['language'];
  $variables['language']->dir = $GLOBALS['language']->direction ? 'rtl' : 'ltr';
  $variables['css'] = drupal_add_css();
  $variables['styles'] = drupal_get_css();
  $variables['scripts'] = drupal_get_js();
  $variables['title'] = drupal_get_title();

  // Closure should be filled last.
  $variables['closure'] = theme('closure');

  // Compile a list of classes that are going to be applied to the body element.
  // This allows advanced theming based on context (home page, node of certain type, etc.).
  $body_classes = array();

  // Add a class that tells us whether we're on the front page or not.
  $body_classes[] = $variables['is_front'] ? 'front' : 'not-front';

  // Add a class that tells us whether the page is viewed by an authenticated user or not.
  $body_classes[] = $variables['logged_in'] ? 'logged-in' : 'not-logged-in';
  $body_classes[] = 'no-sidebars';

  // Implode with spaces.
  $variables['body_classes'] = implode(' ', $body_classes);
}
function theme_getdirections_loadaddress($variables) {
  $location = $variables['location'];
  $addcountry = $variables['addcountry'];
  $loc = array();
  if (isset($location['name']) && $location['name']) {
    $loc[] = $location['name'];
  }
  if (isset($location['street']) && $location['street']) {
    $loc[] = $location['street'];
  }
  if (isset($location['additional']) && $location['additional']) {
    $loc[] = $location['additional'];
  }
  if (isset($location['city']) && $location['city']) {
    $loc[] = $location['city'];
  }
  if (isset($location['province_name']) && $location['province_name']) {
    $loc[] = $location['province_name'];
  }
  if (isset($location['postal_code']) && $location['postal_code']) {
    $loc[] = $location['postal_code'];
  }
  if ($addcountry) {
    if (isset($location['country']) && $location['country']) {
      $loc[] = drupal_strtoupper($location['country']);
    }
  }
  $output = implode(', ', $loc);
  return $output;
}
function theme_getdirections_addressfield_loadaddress($variables) {
  $address = $variables['address'];
  $addcountry = $variables['addcountry'];
  $location = getdirections_addressfield_convert($address);
  return theme('getdirections_loadaddress', array(
    'location' => $location,
    'addcountry' => $addcountry,
  ));
}
function theme_getdirections_addressfield_convert($variables) {
  $address = $variables['address'];
  $location = array();
  if (is_array($address) && !empty($address)) {
    $name = array();
    if (isset($address['name_line']) && $address['name_line']) {
      $name[] = $address['name_line'];
    }
    if (isset($address['first_name']) && $address['first_name']) {
      if (isset($address['last_name']) && $address['last_name']) {
        $name[] = $address['first_name'] . ' ' . $address['last_name'];
      }
      else {
        $name[] = $address['first_name'];
      }
    }
    elseif (isset($address['last_name']) && $address['last_name']) {
      $name[] = $address['last_name'];
    }
    if (isset($address['organisation_name']) && $address['organisation_name']) {
      $name[] = $address['organisation_name'];
    }
    $location['name'] = count($name) ? implode(", ", $name) : '';
    $location['street'] = isset($address['thoroughfare']) ? $address['thoroughfare'] : '';
    $additional = array();
    if (isset($address['premise']) && $address['premise']) {
      $additional[] = $address['premise'];
    }
    if (isset($address['sub_premise']) && $address['sub_premise']) {
      $additional[] = $address['sub_premise'];
    }
    $location['additional'] = count($additional) ? implode(", ", $additional) : '';
    $city = array();
    if (isset($address['dependent_locality']) && $address['dependent_locality']) {
      $city[] = $address['dependent_locality'];
    }
    if (isset($address['locality']) && $address['locality']) {
      $city[] = $address['locality'];
    }
    $location['city'] = count($city) ? implode(", ", $city) : '';
    $admin_area = array();
    if (isset($address['sub_administrative_area']) && $address['sub_administrative_area']) {
      $admin_area[] = $address['sub_administrative_area'];
    }
    if (isset($address['administrative_area']) && $address['administrative_area']) {
      $admin_area[] = $address['administrative_area'];
    }
    $location['province_name'] = count($admin_area) ? implode(", ", $admin_area) : '';
    $location['postal_code'] = isset($address['postal_code']) ? drupal_strtoupper($address['postal_code']) : '';
    $location['country_name'] = isset($address['country_name']) ? $address['country_name'] : $address['country'];
    $location['country'] = $address['country'];
  }
  return $location;
}

Functions

Namesort descending Description
getdirections_access_location menu access functions
getdirections_access_user_location
getdirections_addressfield_convert
getdirections_check
getdirections_comment_setlocation Function to setup the map and form
getdirections_defaults Some defaults.
getdirections_direction Function to setup the map and form
getdirections_direction_box
getdirections_direction_form Function to setup the form
getdirections_fromto_load menu placeholder functions
getdirections_get_current_supported_module
getdirections_get_datestamp
getdirections_get_lid_title
getdirections_get_nid
getdirections_get_nid_from_lid
getdirections_get_settings
getdirections_get_tid_from_lid
getdirections_get_vid Function to get the vid from node table.
getdirections_help Implements hook_help().
getdirections_id_load
getdirections_init Implements hook_init().
getdirections_ip_geoloc
getdirections_is_advanced
getdirections_is_mobile
getdirections_latlon Function to setup the map and form
getdirections_latlons
getdirections_latlon_check Function to check a lat,lon string
getdirections_latlon_load
getdirections_load_latlon_defaults
getdirections_load_location
getdirections_load_locations
getdirections_locations Function to setup the map
getdirections_locations_via Function to setup the map
getdirections_menu Implement hook_menu().
getdirections_misc_defaults
getdirections_n2u_setlocation Function to setup the map
getdirections_other_get_fieldname
getdirections_other_load_locations
getdirections_paths_get
getdirections_permission Implements hook_permission().
getdirections_setlocation Function to setup the map and form
getdirections_setlocations Function to setup the map
getdirections_setlocations_via Function to setup the map to display waypoints
getdirections_setlocation_id Function to setup the map and form
getdirections_setup_map Function to setup the map scripts
getdirections_smart_ip
getdirections_supported_modules
getdirections_term_setlocation Function to setup the map and form
getdirections_theme Implements hook_theme().
getdirections_u2n_setlocation Function to setup the map and form
getdirections_ua_get
getdirections_uid_load
getdirections_user_check Function to check the user status and existence
getdirections_user_setlocation Function to setup the map and form
getdirections_user_setlocations Function to setup the map
getdirections_user_setlocations_via Function to setup the map to display waypoints
getdirections_via_load
getdirections_views_api Implements hook_views_api().
template_preprocess_getdirections_box
theme_getdirections_addressfield_convert
theme_getdirections_addressfield_loadaddress
theme_getdirections_direction_form
theme_getdirections_loadaddress
theme_getdirections_show Theme to use for when one or no locations are supplied.
theme_getdirections_show_locations Theme to use for when both locations are supplied.
theme_getdirections_show_locations_via Theme to use for when both locations are supplied with waypoints.
_getdirections_loadaddress Function to collect an address

Constants

Namesort descending Description
GETDIRECTIONS_PATH @file Fetches google map directions.