openlayers_filters.module 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * @file
  4. * This file holds the main Drupal hook functions
  5. * and private functions for the openlayers_filters module.
  6. *
  7. * @ingroup openlayers
  8. */
  9. /**
  10. * Implements hook_help().
  11. */
  12. function openlayers_filters_help($path, $arg) {
  13. $output = '';
  14. switch ($path) {
  15. case 'admin/help#openlayers_filters':
  16. $output = '<p>'. t('The OpenLayers Filters module provides input filters
  17. to allow for inline maps.') .'</p>';
  18. return $output;
  19. }
  20. }
  21. /**
  22. * Implements hook_filter_info().
  23. */
  24. function openlayers_filters_filter_info() {
  25. $filters['openlayer'] = array(
  26. 'title' => t('OpenLayers Filters'),
  27. 'description' => t('Substitutes a macro text like !macro_example into an
  28. appropriate rendered OpenLayers map.',
  29. array(
  30. '!macro_example' => '[openlayers preset_name]',
  31. )),
  32. 'process callback' => '_openlayer_filter_process',
  33. 'tips callback' => '_openlayer_filter_tips',
  34. );
  35. return $filters;
  36. }
  37. /**
  38. * Maps filter process callback
  39. *
  40. * Scan text and replace [openlayer preset_name] with OpenLayer objects
  41. *
  42. */
  43. function _openlayer_filter_process($text, $filter) {
  44. $matches = array();
  45. preg_match_all('/\[(openlayers[^\]]*)\]/', $text, $matches);
  46. // Check for found
  47. if (is_array($matches[1]) && count($matches[1]) > 0) {
  48. foreach ($matches[1] as $i => $match) {
  49. $exploded = explode(' ', $match);
  50. if (count($exploded) > 1 && $preset = check_plain($exploded[1])) {
  51. $map = openlayers_preset_load($preset);
  52. }
  53. else {
  54. $map = openlayers_preset_load(
  55. variable_get('openlayers_default_preset', 'default'));
  56. }
  57. if (!empty($map->data) && is_array($map->data)) {
  58. $rendered = openlayers_render_map($map->data);
  59. // Replace text with rendered map preset
  60. $text = str_replace($matches[0][$i], $rendered, $text);
  61. }
  62. }
  63. }
  64. return $text;
  65. }
  66. /**
  67. * Filter tips callback
  68. */
  69. function _openlayer_filter_tips($filter, $format, $long = FALSE) {
  70. if ($long) {
  71. return t('Substitutes a macro text like !macro_example into a the
  72. appropriate rendered OpenLayers map. This will render a map
  73. preset into the body of content. If the preset name is not given,
  74. as in !macro_example_default, the default map preset will be shown.',
  75. array(
  76. '!macro_example' => '[openlayers preset_name]',
  77. '!macro_example_default' => '[openlayers]'
  78. )
  79. );
  80. }
  81. else {
  82. return t('Substitutes a macro text like !macro_example into a the
  83. appropriate rendered OpenLayers map.',
  84. array(
  85. '!macro_example' => '[openlayers preset_name]',
  86. )
  87. );
  88. }
  89. }