You are here

route_planner.module in Route Planner 7

Same filename and directory in other branches
  1. 8 route_planner.module
  2. 6 route_planner.module

The Route Planner module create blocks to show a route from any address to a fixed point.

File

route_planner.module
View source
<?php

/**
 * @file
 * The Route Planner module create blocks to show a route from any address
 * to a fixed point.
 */

/**
 * Implements hook_menu().
 */
function route_planner_menu() {
  $items = array();

  // Module settings.
  $items['admin/config/content/routeplanner'] = array(
    'title' => 'Route planner settings',
    'description' => 'Route planner settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'route_planner_settings_form',
    ),
    'access arguments' => array(
      'access administration pages',
    ),
    'file' => 'route_planner.admin.inc',
    'file path' => drupal_get_path('module', 'route_planner'),
  );
  return $items;
}

/**
 * Implements hook_block_info()
 */
function route_planner_block_info() {
  $blocks['route_target'] = array(
    'info' => t('Route Planner Address Field'),
    'cache' => DRUPAL_CACHE_PER_ROLE,
  );
  $blocks['map'] = array(
    'info' => t('Route Planner Map Display'),
    'cache' => DRUPAL_CACHE_PER_ROLE,
  );
  return $blocks;
}

/**
 * Implements hook_block_configure().
 */
function route_planner_block_configure($delta = '') {
  module_load_include('inc', 'route_planner', 'route_planner.admin');
  switch ($delta) {
    case 'route_target':
    case 'map':
      return route_planner_settings_form();
      break;
  }
}

/**
 * Implements hook_block_save().
 */
function route_planner_block_save($delta = '', $edit = array()) {
  switch ($delta) {
    case 'route_target':
    case 'map':
      variable_set('route_planner_address', $edit['route_planner_address']);
      variable_set('route_planner_api_key', $edit['route_planner_api_key']);
      variable_set('route_planner_api_language', $edit['route_planner_api_language']);
      variable_set('route_planner_map_height', $edit['route_planner_map_height']);
      variable_set('route_planner_map_width', $edit['route_planner_map_width']);
      variable_set('route_planner_map_zoom', intval($edit['route_planner_map_zoom']));
      variable_set('route_planner_map_zoomcontrol', intval($edit['route_planner_map_zoomcontrol']));
      variable_set('route_planner_map_scrollwheel', intval($edit['route_planner_map_scrollwheel']));
      variable_set('route_planner_map_maptypecontrol', intval($edit['route_planner_map_maptypecontrol']));
      variable_set('route_planner_map_scalecontrol', intval($edit['route_planner_map_scalecontrol']));
      variable_set('route_planner_map_draggable', intval($edit['route_planner_map_draggable']));
      variable_set('route_planner_map_doubbleclick', intval($edit['route_planner_map_doubbleclick']));
      variable_set('route_planner_map_streetviewcontrol', intval($edit['route_planner_map_streetviewcontrol']));
      variable_set('route_planner_map_overviewmapcontrol', intval($edit['route_planner_map_overviewmapcontrol']));
      variable_set('route_planner_unitsystem', intval($edit['route_planner_unitsystem']));
      variable_set('route_planner_map_defaultui', intval($edit['route_planner_map_defaultui']));
      variable_set('route_planner_map_style', $edit['route_planner_map_style']);
      variable_set('route_planner_map_travelmode', intval($edit['route_planner_map_travelmode']));
      break;
  }
}

/**
 * Implements hook_block_view().
 */
function route_planner_block_view($delta) {
  switch ($delta) {
    case 'route_target':
      $block['content'] = route_planner_get_address_form();
      break;
    case 'map':
      $block['content'] = route_planner_map_display();
      break;
  }
  return $block;
}

/**
 * The Address-Form.
 *
 * A form to input a address and get.
 * The driving time and distance will automatically set after clicken the button.
 *
 * @return
 *   The address form.
 */
function route_planner_address_form($form, &$form_state) {
  $form['start'] = array(
    '#type' => 'textfield',
    '#title' => t('Address'),
    '#default_value' => '',
    '#size' => 20,
  );
  if (variable_get('route_planner_address_end', FALSE)) {
    $form['end'] = array(
      '#type' => 'textfield',
      '#title' => t('Target address'),
      '#default_value' => variable_get('route_planner_address', 'Hamburg, Germany'),
      '#size' => 20,
    );
  }
  $form['distance'] = array(
    '#type' => 'textfield',
    '#title' => t('Distance'),
    '#default_value' => '0.00',
    '#size' => 20,
    '#disabled' => TRUE,
  );
  $form['time'] = array(
    '#type' => 'textfield',
    '#title' => t('Driving time'),
    '#default_value' => '0.00',
    '#size' => 20,
    '#disabled' => TRUE,
  );
  if (variable_get('route_planner_map_travelmode', FALSE)) {
    $form['travel_mode'] = array(
      '#type' => 'radios',
      '#title' => t('Travel mode'),
      '#options' => array(
        '0' => t('Driving'),
        '1' => t('Bicycling'),
        '2' => t('Transit'),
        '3' => t('Walking'),
      ),
      '#default_value' => '0',
    );
  }
  $form['button'] = array(
    '#type' => 'button',
    '#value' => t('Calculate route'),
    '#attributes' => array(
      'onClick' => 'return Drupal.myRoutePlanner.calcRoute();',
    ),
  );
  return $form;
}

/**
 * Get Address-Form Block
 *
 * Helper function to add several stuff to the Block.
 *
 * @return
 *   The address form.
 */
function route_planner_get_address_form() {

  // Detect protocol to use when calling maps api.
  global $is_https;
  $protocol = $is_https ? 'https://' : 'http://';

  // Add google maps api. Pass parameters "key" and "language" localization.
  // Parameter "sensor" is deprecated and no more used.
  $key = variable_get('route_planner_api_key', '');
  $language = variable_get('route_planner_api_language', '');
  drupal_add_js($protocol . 'maps.googleapis.com/maps/api/js?key=' . $key . '&language=' . $language, 'external');

  // Add some custom javascript to display the map.
  drupal_add_js(drupal_get_path('module', 'route_planner') . '/route_planner.js');

  // Set variables from Route Planner settings page.
  drupal_add_js(array(
    'routePlanner' => array(
      'zoomlevel' => variable_get('route_planner_map_zoom', 10),
      'zoomcontrol' => variable_get('route_planner_map_zoomcontrol', TRUE),
      'scrollwheel' => variable_get('route_planner_map_scrollwheel', TRUE),
      'mapTypeControl' => variable_get('route_planner_map_maptypecontrol', TRUE),
      'scaleControl' => variable_get('route_planner_map_scalecontrol', TRUE),
      'draggable' => variable_get('route_planner_map_draggable', TRUE),
      'doubbleclick' => variable_get('route_planner_map_doubbleclick', TRUE),
      'streetviewcontrol' => variable_get('route_planner_map_streetviewcontrol', TRUE),
      'overviewmapcontrol' => variable_get('route_planner_map_overviewmapcontrol', TRUE),
      'unitSystem' => variable_get('route_planner_unitsystem', TRUE),
      'end' => variable_get('route_planner_address', 'Hamburg, Germany'),
      'defaultui' => variable_get('route_planner_map_defaultui', TRUE),
      'style' => variable_get('route_planner_map_style', NULL),
      'travelMode' => variable_get('route_planner_map_travelmode', FALSE),
    ),
  ), 'setting');

  // Get the address form.
  $output = drupal_get_form('route_planner_address_form');
  return $output;
}

/**
 * Show a Google Map with a POI or a route.
 *
 * @return
 *   HTML output for the map.
 */
function route_planner_map_display() {

  // Detect protocol to use when calling maps api.
  global $is_https;
  $protocol = $is_https ? 'https://' : 'http://';

  // Add google maps api. Pass parameters "key" and "language" localization.
  // Parameter "sensor" is deprecated and no more used.
  $key = variable_get('route_planner_api_key', '');
  $language = variable_get('route_planner_api_language', '');
  drupal_add_js($protocol . 'maps.googleapis.com/maps/api/js?key=' . $key . '&language=' . $language, 'external');

  // Add some custom javascript to display the map.
  drupal_add_js(drupal_get_path('module', 'route_planner') . '/route_planner.js');
  $address = token_replace(variable_get('route_planner_address', 'Hamburg, Germany'));

  // This HTML is used in the Addressfield Token.
  // We have to replace the stuff with a comma to let Google work with.
  $address = str_replace('<span class="postal-code">', ", ", $address);
  $address = str_replace('<span class="country">', ', ', $address);
  $address = strip_tags($address);

  // Set variables from Route Planner settings page.
  drupal_add_js(array(
    'routePlanner' => array(
      'zoomlevel' => variable_get('route_planner_map_zoom', 10),
      'zoomcontrol' => variable_get('route_planner_map_zoomcontrol', TRUE),
      'scrollwheel' => variable_get('route_planner_map_scrollwheel', TRUE),
      'mapTypeControl' => variable_get('route_planner_map_maptypecontrol', TRUE),
      'scaleControl' => variable_get('route_planner_map_scalecontrol', TRUE),
      'draggable' => variable_get('route_planner_map_draggable', TRUE),
      'doubbleclick' => variable_get('route_planner_map_doubbleclick', TRUE),
      'streetviewcontrol' => variable_get('route_planner_map_streetviewcontrol', TRUE),
      'overviewmapcontrol' => variable_get('route_planner_map_overviewmapcontrol', TRUE),
      'unitSystem' => variable_get('route_planner_unitsystem', TRUE),
      'defaultui' => variable_get('route_planner_map_defaultui', TRUE),
      'end' => variable_get('route_planner_address', 'Hamburg, Germany'),
      'style' => variable_get('route_planner_map_style', NULL),
      'travelMode' => variable_get('route_planner_map_travelmode', FALSE),
    ),
  ), 'setting');

  // Output the map.
  $output = '<div id="map_canvas" style="height:' . check_plain(variable_get('route_planner_map_height', '300px')) . '; width:' . check_plain(variable_get('route_planner_map_width', '100%')) . ';"></div>';
  return $output;
}

Functions