| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?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("'", "'", $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;
- }
|