You are here

wysiwyg_map.module in WYSIWYG Map 7

File

wysiwyg_map.module
View source
<?php

/**
 * @file
 * wysiwyg_map.module
 */

/**
 * Implements hook_wysiwyg_include_directory().
 */
function wysiwyg_map_wysiwyg_include_directory($type) {
  switch ($type) {
    case 'plugins':
      return 'wysiwyg_plugins';
      break;
  }
}

/**
 * Implements hook_menu().
 */
function wysiwyg_map_menu() {
  $items = array();
  $items['wysiwyg-map/token-builder'] = array(
    'access arguments' => array(
      'access content',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'wysiwyg_map_tokenbuilder_form',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/config/wysiwyg-map/settings'] = array(
    'title' => 'Configure WYSIWYG Map',
    'description' => 'Configure settings for the wysiwyg map.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'wysiwyg_map_settings_form',
    ),
    'access arguments' => array(
      'administer wysiwyg_map',
    ),
  );
  return $items;
}
function wysiwyg_map_permission() {
  return array(
    'administer wysiwyg_map' => array(
      'title' => t('Administer the WYSIWYG Map'),
    ),
  );
}

/**
 * Form builder for the google map field token builder.
 */
function wysiwyg_map_tokenbuilder_form($form, &$form_state) {
  drupal_set_title(t('WYSIWYG Map Token Builder'));
  $form = array();
  $form['token_builder']['map'] = array(
    '#markup' => theme('wysiwyg_map_selector'),
    '#prefix' => '<div class="wysiwyg-map-tokenbuilder"><div class="wysiwyg-map-tokenbuilder-map">',
  );
  $form['token_builder']['token'] = array(
    '#type' => 'hidden',
    '#attributes' => array(
      'id' => 'edit-token',
    ),
    '#default_value' => 'lat=51.0,lon=0.12,width=300,height=250,zoom=9,map_type=roadmap',
  );
  $form['token_builder']['center_on'] = array(
    '#type' => 'textfield',
    '#title' => t('Center On'),
    '#description' => t('To center the map on a location, please enter the location in the box above, e.g. "London, UK" or "1 Southwark Street, London, UK" and click \'Center map\''),
    '#size' => 40,
    '#attributes' => array(
      'onkeyup' => 'return wysiwyg_map_buildToken();',
    ),
  );
  $form['token_builder']['do_center'] = array(
    '#type' => 'button',
    '#value' => t('Center map'),
    '#attributes' => array(
      'onclick' => 'return wysiwyg_map_doCenterPopup();',
    ),
    '#suffix' => '</div>',
  );
  $form['token_builder']['width'] = array(
    '#type' => 'textfield',
    '#title' => t('Width'),
    '#description' => 'Maximum: 600, minimum: 100.',
    '#size' => 15,
    '#default_value' => '300',
    '#attributes' => array(
      'onkeyup' => 'return wysiwyg_map_buildToken();',
    ),
    '#prefix' => '<div class="wysiwyg-map-tokenbuilder-fields-1">',
  );
  $form['token_builder']['height'] = array(
    '#type' => 'textfield',
    '#title' => t('Height'),
    '#description' => 'Maximum: 600, minimum: 100.',
    '#size' => 15,
    '#default_value' => '250',
    '#attributes' => array(
      'onkeyup' => 'return wysiwyg_map_buildToken();',
    ),
  );
  $form['token_builder']['css_class'] = array(
    '#type' => 'textfield',
    '#title' => t('CSS Class'),
    '#description' => 'To align the map left, right or center, please use the following classes: pull-left, pull-right, pull-center.',
    '#size' => 15,
    '#default_value' => '',
    '#attributes' => array(
      'onkeyup' => 'return wysiwyg_map_buildToken();',
    ),
  );
  $form['token_builder']['map_caption'] = array(
    '#type' => 'textfield',
    '#title' => t('Map caption'),
    '#description' => 'This is displayed below the map, as a description. If left empty, it will not display.',
    '#size' => 94,
    '#default_value' => '',
    '#attributes' => array(
      'onkeyup' => 'return wysiwyg_map_buildToken();',
    ),
  );
  $form['token_builder']['marker_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Marker title'),
    '#description' => 'This displays in a tooltip text when hovering the marker. If left empty, it will not display.',
    '#size' => 94,
    '#default_value' => '',
    '#attributes' => array(
      'onkeyup' => 'return wysiwyg_map_buildToken();',
    ),
    '#suffix' => '</div>',
  );
  $form['token_builder']['marker_popup_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Popup title'),
    '#description' => 'Text displayed as title when clicking the marker. If both the popup title and popup text are left empty, no popup will display.',
    '#size' => 94,
    '#default_value' => '',
    '#attributes' => array(
      'onkeyup' => 'return wysiwyg_map_buildToken();',
    ),
    '#prefix' => '<div class="wysiwyg-map-tokenbuilder-fields-2">',
  );
  $form['token_builder']['marker_popup_text'] = array(
    '#type' => 'textarea',
    '#title' => t('Popup text'),
    '#description' => 'Text displayed below the title when clicking the marker. If both the popup title and popup text are left empty, no popup will display.',
    '#size' => 15,
    '#default_value' => '',
    '#attributes' => array(
      'onkeyup' => 'return wysiwyg_map_buildToken();',
    ),
  );
  $form['token_builder']['marker_popup_default'] = array(
    '#type' => 'checkbox',
    '#title' => t('Display the popup on page load'),
    '#description' => 'You can choose to display the popup by default or not.',
    '#size' => 15,
    '#attributes' => array(
      'onchange' => 'return wysiwyg_map_buildToken();',
    ),
    '#suffix' => '</div>',
  );
  $form['token_builder']['map_type'] = array(
    '#type' => 'hidden',
    '#attributes' => array(
      'id' => 'edit-map-type',
    ),
    '#default_value' => 'roadmap',
  );
  $form['token_builder']['zoom'] = array(
    '#type' => 'hidden',
    '#default_value' => '9',
    '#attributes' => array(
      'id' => 'edit-zoom',
    ),
    '#suffix' => '</div></div>',
  );
  return $form;
}

/**
 * Implements hook_form_alter().
 */
function wysiwyg_map_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['#node_edit_form']) && $form['#node_edit_form'] || $form_id == 'block_add_block_form') {
    drupal_add_css(drupal_get_path('module', 'wysiwyg_map') . '/wysiwyg_plugins/wysiwyg_map_tokenbuilder/wysiwyg_map_tokenbuilder.css');
    if (!empty($api_key = variable_get('wysiwyg_map_api_key'))) {
      drupal_add_js('//maps.googleapis.com/maps/api/js?key=' . $api_key . '', 'external');
    }
    elseif (!empty($client_id = variable_get('wysiwyg_map_client_id'))) {
      drupal_add_js('//maps.googleapis.com/maps/api/js?client=' . $client_id . '', 'external');
    }
    else {
      drupal_add_js('//maps.googleapis.com/maps/api/js', 'external');
    }
    drupal_add_js(drupal_get_path('module', 'wysiwyg_map') . '/js/wysiwyg_map_edit_form.js', 'file');
    drupal_add_library('system', 'ui.dialog');
    drupal_add_library('system', 'ui.draggable');
  }
}

/**
 * Implements hook_theme().
 */
function wysiwyg_map_theme($existing, $type, $theme, $path) {
  if ($type == 'module') {
    return array(
      'wysiwyg_map_selector' => array(
        'render_element' => 'element',
      ),
      'wysiwyg_map_picker' => array(
        'render_element' => 'element',
      ),
      'wysiwyg_map_token' => array(
        'render_element' => 'element',
        'variables' => array(
          'delta' => NULL,
          'width' => NULL,
          'height' => NULL,
          'css_class' => NULL,
          'map_caption' => NULL,
          'marker_title' => NULL,
          'marker_popup_default' => NULL,
          'marker_popup_title' => NULL,
          'marker_popup_text' => NULL,
        ),
      ),
    );
  }
  return array();
}

/**
 * Theme function to return map selector div on token builder overlay.
 */
function theme_wysiwyg_map_selector($variables) {
  return '<div id="wysiwyg_map_selector"></div>';
}

/**
 * Theme function to return map div for tokenised maps.
 */
function theme_wysiwyg_map_token($variables) {
  if ($variables['map_caption'] != '') {
    return '
      <div class="map-outer-caption ' . $variables['css_class'] . '" style="width: ' . $variables['width'] . 'px; height: ' . $variables['height'] . 'px; padding: 5px 5px 35px; border: 1px solid #ccc; background-color: #eee; margin-bottom: 10px;">
        <div id="wysiwyg_map_' . $variables['delta'] . '" class="' . $variables['css_class'] . ' wysiwyg-map" style="width: ' . $variables['width'] . 'px; height: ' . $variables['height'] . 'px;"></div>
        <div class="map-inner-caption" style="padding: 5px; clear: both; text-align: center;">' . $variables['map_caption'] . '</div>
      </div>
    ';
  }
  else {
    if ($variables['css_class'] != '') {
      return '
      <div id="wysiwyg_map_' . $variables['delta'] . '" class="' . $variables['css_class'] . ' wysiwyg-map" style="width: ' . $variables['width'] . 'px; height: ' . $variables['height'] . 'px;"></div>
    ';
    }
    else {
      return '
      <div id="wysiwyg_map_' . $variables['delta'] . '" class="wysiwyg-map" style="width: ' . $variables['width'] . 'px; height: ' . $variables['height'] . 'px;"></div>
    ';
    }
  }
}

/**
 * Implements hook_filter_info().
 */
function wysiwyg_map_filter_info() {
  $filters['wysiwyg_map_token'] = array(
    'title' => t('WYSIWYG Map'),
    'description' => t('Use tokens to insert Google Map directly into content.'),
    'process callback' => 'wysiwyg_map_filter_process',
    'cache' => FALSE,
    'weight' => 50,
  );
  return $filters;
}

/**
 * Process callback for hook_filter_info().
 */
function wysiwyg_map_filter_process($text, $filter, $format) {

  // we don't want to load the map apis for admin pages so return
  // the text as-is.
  if (path_is_admin(current_path())) {
    return $text;
  }
  $out = $text;
  $matches = array();
  preg_match_all('/lat\\=(.*?)\\;/', $out, $matches);
  $i = 0;
  $guid = uniqid();
  $maps = array();
  $add_maps_js = FALSE;
  while (isset($matches[1][$i])) {
    $match = str_replace(';', '', $matches[0][$i]);
    $settings = explode('~', $match);
    $map_settings = array();
    $map_settings['container_id'] = $guid;
    foreach ($settings as $setting) {
      $offset = strpos($setting, '=');
      if ($offset !== FALSE) {
        $key = trim(substr($setting, 0, $offset));
        $val = trim(substr($setting, $offset + 1));
        $map_settings[$key] = $val;
      }
    }
    $maps[$i] = $map_settings;
    $replace = $matches[0][$i];
    $settings = array(
      'delta' => $guid,
      'width' => $map_settings['map_width'],
      'height' => $map_settings['map_height'],
      'css_class' => isset($map_settings['css_class']) ? $map_settings['css_class'] : '',
      'map_caption' => isset($map_settings['map_caption']) ? $map_settings['map_caption'] : '',
      'marker_title' => isset($map_settings['marker_title']) ? $map_settings['marker_title'] : '',
      'marker_popup_default' => $map_settings['marker_popup_default'],
      'marker_popup_title' => isset($map_settings['marker_popup_title']) ? $map_settings['marker_popup_title'] : '',
      'marker_popup_text' => isset($map_settings['marker_popup_text']) ? $map_settings['marker_popup_text'] : '',
    );
    $replacement = theme('wysiwyg_map_token', $settings);
    $out = preg_replace('/' . $matches[0][$i] . '/', $replacement, $out, 1);
    $guid = uniqid();
    $i++;
    $add_maps_js = TRUE;
  }
  $map_collection = array(
    'wysiwyg_map_maps' => $maps,
  );
  if ($add_maps_js) {
    if (!empty($api_key = variable_get('wysiwyg_map_api_key'))) {
      drupal_add_js('//maps.googleapis.com/maps/api/js?key=' . $api_key . '', 'external');
    }
    elseif (!empty($client_id = variable_get('wysiwyg_map_client_id'))) {
      drupal_add_js('//maps.googleapis.com/maps/api/js?client=' . $client_id . '', 'external');
    }
    else {
      drupal_add_js('//maps.googleapis.com/maps/api/js', 'external');
    }
    drupal_add_js(drupal_get_path('module', 'wysiwyg_map') . '/js/wysiwyg_map_token_maps.js');
    drupal_add_js($map_collection, 'setting');
  }
  return $out;
}

/**
 * Form builder.
 */
function wysiwyg_map_settings_form($form, &$form_state) {
  $description_text = "\n    <p>Google maps no longer work properly without an API key. To get an API key you must create a Google Maps billing account and follow the instructions to create API credentials.</p>\n    <p>Please see <a href=\"https://developers.google.com/maps/documentation/javascript/get-api-key\" target=\"_blank\">Google\\'s documentation pages</a> for more information on API keys.</p>\n  ";
  $form['wysiwyg_map_api_keys'] = array(
    '#type' => 'fieldset',
    '#title' => t('Google Maps Javascript API keys'),
    '#description' => $description_text,
    '#weight' => 5,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['wysiwyg_map_api_keys']['wysiwyg_map_api_key'] = array(
    '#type' => 'textfield',
    '#title' => t('API key'),
    '#description' => 'Used by either Standard or Premium API.',
    '#default_value' => variable_get('wysiwyg_map_api_key', ''),
  );
  $form['wysiwyg_map_api_keys']['wysiwyg_map_client_id'] = array(
    '#type' => 'textfield',
    '#title' => t('Client ID'),
    '#description' => 'Used by Premium API. <b>Please note:</b> for Premium API only, use either the API key field or the Client ID field, not both.',
    '#default_value' => variable_get('wysiwyg_map_client_id', ''),
  );
  return system_settings_form($form);
}

// form validation
function wysiwyg_map_settings_form_validate($form, $form_state) {
  $wysiwyg_map_api_key = $form_state['values']['wysiwyg_map_api_key'];
  $wysiwyg_map_client_id = $form_state['values']['wysiwyg_map_client_id'];
  if (!empty($wysiwyg_map_api_key) && !empty($wysiwyg_map_client_id)) {
    form_set_error('wysiwyg_map_api_keys', t('You must enter either an API key or Client ID, you cannot use both.'));
    form_set_value($form['wysiwyg_map_api_keys']['wysiwyg_map_api_key'], '', $form_state);
    form_set_value($form['wysiwyg_map_api_keys']['wysiwyg_map_client_id'], '', $form_state);
  }
}

Functions

Namesort descending Description
theme_wysiwyg_map_selector Theme function to return map selector div on token builder overlay.
theme_wysiwyg_map_token Theme function to return map div for tokenised maps.
wysiwyg_map_filter_info Implements hook_filter_info().
wysiwyg_map_filter_process Process callback for hook_filter_info().
wysiwyg_map_form_alter Implements hook_form_alter().
wysiwyg_map_menu Implements hook_menu().
wysiwyg_map_permission
wysiwyg_map_settings_form Form builder.
wysiwyg_map_settings_form_validate
wysiwyg_map_theme Implements hook_theme().
wysiwyg_map_tokenbuilder_form Form builder for the google map field token builder.
wysiwyg_map_wysiwyg_include_directory Implements hook_wysiwyg_include_directory().