mappy.module 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * @file
  4. * Module file for mappy.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function mappy_menu() {
  10. // Settings page.
  11. $items['admin/config/content/mappy/settings'] = array(
  12. 'title' => 'Mappy settings',
  13. 'page callback' => 'drupal_get_form',
  14. 'page arguments' => array('mappy_settings'),
  15. 'access arguments' => array('administer site configuration'),
  16. 'file' => 'mappy.admin.inc',
  17. 'file path' => drupal_get_path('module', 'mappy'),
  18. );
  19. return $items;
  20. }
  21. /**
  22. * Implements hook_page_build().
  23. */
  24. function mappy_page_build(&$page) {
  25. // Patterns for matching.
  26. $pages = drupal_strtolower(variable_get('mappy_load_pages_path'));
  27. // Convert path alias to lowercase.
  28. $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
  29. // Compare pages to aliased path.
  30. $page_match = drupal_match_path($path, $pages);
  31. // Compare for original paths.
  32. if ($path != $_GET['q']) {
  33. $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
  34. }
  35. // Type of the matching path.
  36. // 0 - All pages except those listed.
  37. // 1 - Only the listed pages.
  38. $match_type = variable_get('mappy_load_pages_match', 0);
  39. // First comparsion for 'All pages except those listed'.
  40. // Second for 'Only the listed pages'.
  41. if ($match_type == "0" && !$page_match || $match_type && $page_match) {
  42. // Add settings to js file.
  43. drupal_add_js(array(
  44. 'mappy_location' => drupal_get_path('module', 'mappy'),
  45. 'mappy_google_width' => variable_get('mappy_google_width', 640),
  46. 'mappy_google_height' => variable_get('mappy_google_height', 480),
  47. 'mappy_yandex_width' => variable_get('mappy_yandex_width', 640),
  48. 'mappy_yandex_height' => variable_get('mappy_yandex_height', 480),
  49. 'mappy_yandex_version' => variable_get('mappy_yandex_version', 2),
  50. ), 'setting');
  51. drupal_add_js(drupal_get_path('module', 'mappy') . '/js/mappy.js');
  52. }
  53. }
  54. /**
  55. * Implements hook_token_info().
  56. *
  57. * Declare out token.
  58. */
  59. function mappy_token_info() {
  60. $info = array();
  61. $info['types']['mappy'] = array(
  62. 'name' => 'Mappy',
  63. 'description' => 'Mappy token',
  64. );
  65. $info['tokens']['mappy']['mappy'] = array(
  66. 'name' => 'Mappy',
  67. 'description' => 'Mappy token',
  68. );
  69. return $info;
  70. }
  71. /**
  72. * Implements hook_tokens().
  73. */
  74. function mappy_tokens($type, $tokens, array $data = array(), array $options = array()) {
  75. $replacements = array();
  76. if ($type == 'mappy') {
  77. foreach ($tokens as $name => $original) {
  78. $replacements[$original] = mappy_token_to_html($original);
  79. }
  80. }
  81. return $replacements;
  82. }
  83. /**
  84. * Implements hook_filter_info().
  85. *
  86. * Declare our filter.
  87. */
  88. function mappy_filter_info() {
  89. $filters['filter_mappy'] = array(
  90. 'title' => t('Mappy filter.'),
  91. 'description' => t('Every [mappy:service address:\'address\'] token will be replaced with map.'),
  92. 'prepare callback' => '_mappy_filter_prepare',
  93. 'process callback' => '_mappy_filter_process',
  94. 'tips callback' => '_mappy_filter_tips',
  95. );
  96. return $filters;
  97. }
  98. /**
  99. * Prepare text to filtering.
  100. *
  101. * On this stage, we replace token [mappy] on html tag.
  102. */
  103. function _mappy_filter_prepare($text, $filter) {
  104. // First, we find all mappy tokens.
  105. $pattern = "/\\[mappy(\\:(.+))?( .+)?\\]/isU";
  106. preg_match_all($pattern, $text, $matches);
  107. // In founded tokens, we get parameters.
  108. foreach ($matches[0] as $ci => $token) {
  109. $tag = mappy_token_to_html($token);
  110. // Replace token for tag.
  111. $text = str_replace($token, $tag, $text);
  112. }
  113. return $text;
  114. }
  115. /**
  116. * Return prepared tag.
  117. */
  118. function _mappy_filter_process($text, $filter) {
  119. return $text;
  120. }
  121. /**
  122. * Information about our tag in filter description.
  123. */
  124. function _mappy_filter_tips($filter, $format, $long = FALSE) {
  125. return t('Every [mappy:service address:\'address\'] will be replaced with map.');
  126. }
  127. /**
  128. * Replace token [mappy:google ... ] to html analogue <mappy:google ...>.
  129. */
  130. function mappy_token_to_html($token) {
  131. $pattern = "/\\[mappy(\\:(.+))?( .+)?\\]/isU";
  132. preg_match_all($pattern, $token, $matches);
  133. $service = $matches[2][0];
  134. // This is the real magic of Hogwarts. I spent totaly about 5-6 hours
  135. // to make this pattern work.
  136. preg_match_all("/(\\s)+(\\w+):(((\\s)*(\\w+))|(\\'(?:\\.|[^\\'\\\\])*\\'))/i", str_replace("&#39;", "'", $token), $parameters);
  137. // And write parameters to an array.
  138. $att = array();
  139. foreach ($parameters['0'] as $key => $name) {
  140. // Additional, we remove quotes.
  141. $att[$parameters['2'][$key]] = str_replace("'", "", $parameters['3'][$key]);
  142. }
  143. // Now we generate HTML Mappy tag.
  144. // Generate attributes.
  145. $attributes = "";
  146. foreach ($att as $key => $value) {
  147. $attributes .= " {$key}=\"{$value}\"";
  148. }
  149. // And add all to tag.
  150. // Without '<mappy:' because drupal remove colons from tag.
  151. $tag = "<{$service}{$attributes}></{$service}>";
  152. return $tag;
  153. }