You are here

mappy.module in Mappy 7

Same filename and directory in other branches
  1. 8 mappy.module

Module file for mappy.

File

mappy.module
View source
<?php

/**
 * @file
 * Module file for mappy.
 */

/**
 * Implements hook_menu().
 */
function mappy_menu() {

  // Settings page.
  $items['admin/config/content/mappy/settings'] = array(
    'title' => 'Mappy settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mappy_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'mappy.admin.inc',
    'file path' => drupal_get_path('module', 'mappy'),
  );
  return $items;
}

/**
 * Implements hook_page_build().
 */
function mappy_page_build(&$page) {

  // Patterns for matching.
  $pages = drupal_strtolower(variable_get('mappy_load_pages_path'));

  // Convert path alias to lowercase.
  $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));

  // Compare pages to aliased path.
  $page_match = drupal_match_path($path, $pages);

  // Compare for original paths.
  if ($path != $_GET['q']) {
    $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
  }

  // Type of the matching path.
  // 0 - All pages except those listed.
  // 1 - Only the listed pages.
  $match_type = variable_get('mappy_load_pages_match', 0);

  // First comparsion for 'All pages except those listed'.
  // Second for 'Only the listed pages'.
  if ($match_type == "0" && !$page_match || $match_type && $page_match) {

    // Add settings to js file.
    drupal_add_js(array(
      'mappy_location' => drupal_get_path('module', 'mappy'),
      'mappy_google_width' => variable_get('mappy_google_width', 640),
      'mappy_google_height' => variable_get('mappy_google_height', 480),
      'mappy_yandex_width' => variable_get('mappy_yandex_width', 640),
      'mappy_yandex_height' => variable_get('mappy_yandex_height', 480),
      'mappy_yandex_version' => variable_get('mappy_yandex_version', 2),
    ), 'setting');
    drupal_add_js(drupal_get_path('module', 'mappy') . '/js/mappy.js');
  }
}

/**
 * Implements hook_token_info().
 *
 * Declare out token.
 */
function mappy_token_info() {
  $info = array();
  $info['types']['mappy'] = array(
    'name' => 'Mappy',
    'description' => 'Mappy token',
  );
  $info['tokens']['mappy']['mappy'] = array(
    'name' => 'Mappy',
    'description' => 'Mappy token',
  );
  return $info;
}

/**
 * Implements hook_tokens().
 */
function mappy_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  if ($type == 'mappy') {
    foreach ($tokens as $name => $original) {
      $replacements[$original] = mappy_token_to_html($original);
    }
  }
  return $replacements;
}

/**
 * Implements hook_filter_info().
 *
 * Declare our filter.
 */
function mappy_filter_info() {
  $filters['filter_mappy'] = array(
    'title' => t('Mappy filter.'),
    'description' => t('Every [mappy:service address:\'address\'] token will be replaced with map.'),
    'prepare callback' => '_mappy_filter_prepare',
    'process callback' => '_mappy_filter_process',
    'tips callback' => '_mappy_filter_tips',
  );
  return $filters;
}

/**
 * Prepare text to filtering.
 *
 * On this stage, we replace token [mappy] on html tag.
 */
function _mappy_filter_prepare($text, $filter) {

  // First, we find all mappy tokens.
  $pattern = "/\\[mappy(\\:(.+))?( .+)?\\]/isU";
  preg_match_all($pattern, $text, $matches);

  // In founded tokens, we get parameters.
  foreach ($matches[0] as $ci => $token) {
    $tag = mappy_token_to_html($token);

    // Replace token for tag.
    $text = str_replace($token, $tag, $text);
  }
  return $text;
}

/**
 * Return prepared tag.
 */
function _mappy_filter_process($text, $filter) {
  return $text;
}

/**
 * Information about our tag in filter description.
 */
function _mappy_filter_tips($filter, $format, $long = FALSE) {
  return t('Every [mappy:service address:\'address\'] will be replaced with map.');
}

/**
 * Replace token [mappy:google ... ] to html analogue <mappy:google ...>.
 */
function mappy_token_to_html($token) {
  $pattern = "/\\[mappy(\\:(.+))?( .+)?\\]/isU";
  preg_match_all($pattern, $token, $matches);
  $service = $matches[2][0];

  // This is the real magic of Hogwarts. I spent totaly about 5-6 hours
  // to make this pattern work.
  preg_match_all("/(\\s)+(\\w+):(((\\s)*(\\w+))|(\\'(?:\\.|[^\\'\\\\])*\\'))/i", str_replace("&#39;", "'", $token), $parameters);

  // And write parameters to an array.
  $att = array();
  foreach ($parameters['0'] as $key => $name) {

    // Additional, we remove quotes.
    $att[$parameters['2'][$key]] = str_replace("'", "", $parameters['3'][$key]);
  }

  // Now we generate HTML Mappy tag.
  // Generate attributes.
  $attributes = "";
  foreach ($att as $key => $value) {
    $attributes .= " {$key}=\"{$value}\"";
  }

  // And add all to tag.
  // Without '<mappy:' because drupal remove colons from tag.
  $tag = "<{$service}{$attributes}></{$service}>";
  return $tag;
}

Functions

Namesort descending Description
mappy_filter_info Implements hook_filter_info().
mappy_menu Implements hook_menu().
mappy_page_build Implements hook_page_build().
mappy_tokens Implements hook_tokens().
mappy_token_info Implements hook_token_info().
mappy_token_to_html Replace token [mappy:google ... ] to html analogue <mappy:google ...>.
_mappy_filter_prepare Prepare text to filtering.
_mappy_filter_process Return prepared tag.
_mappy_filter_tips Information about our tag in filter description.