openlayers.render.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * @file
  4. * Processing functions for layers and behaviors
  5. * @ingroup openlayers
  6. */
  7. /**
  8. * Initialize the layer array into an indexed array of layer objects
  9. *
  10. * @param $layers
  11. * Array of layers to process
  12. * @param $map
  13. * Map array
  14. * @return $layer_data
  15. * Array of initialized layer objects
  16. */
  17. function _openlayers_layers_process($layers = array(), &$map = array()) {
  18. $layer_data = array();
  19. // Load Layers and assign weights
  20. foreach ($layers as $key => $layer){
  21. if ($layer_object = openlayers_layer_load($layer)) {
  22. $layers[$key] = $layer_object;
  23. if (!empty($map['layer_weight'][$key])) {
  24. $layers[$key]->weight = $map['layer_weight'][$key];
  25. }
  26. else $layers[$key]->weight = 0;
  27. }
  28. else unset($layers[$key]);
  29. }
  30. // Sort layers
  31. usort($layers, '_openlayers_layers_process_sort');
  32. // Process into array-based layer data for the map
  33. foreach ($layers as $type => $layer_object) {
  34. if (is_object($layer_object)) {
  35. $layer_object->render($map);
  36. $layer_object->data['title'] = $layer_object->title;
  37. $layer_object->data['weight'] = $layer_object->weight;
  38. $layer_data[$layer_object->name] = $layer_object->data;
  39. }
  40. }
  41. return $layer_data;
  42. }
  43. /**
  44. * Callback function for sorting
  45. *
  46. * @param $a
  47. * Layer $a
  48. * @param $b
  49. * Layer $b
  50. * @return $a_greater_b
  51. * Return the weight different - allowing usort to sort
  52. */
  53. function _openlayers_layers_process_sort($a, $b) {
  54. return intval($a->weight - $b->weight);
  55. }
  56. /**
  57. * Execute render() method for all enabled behaviors.
  58. *
  59. * @param $behaviors
  60. * Array of behaviors to process
  61. * @param $map
  62. * Map array
  63. * @return $rendered
  64. * Indexed array of rendered behaviors
  65. */
  66. function _openlayers_behaviors_render($behaviors = array(), &$map = array()) {
  67. $rendered = array();
  68. foreach (openlayers_behaviors() as $key => $plugin) {
  69. if (isset($behaviors[$key]) && $class = ctools_plugin_get_class($plugin, 'behavior')) {
  70. $behavior = new $class($behaviors[$key], $map);
  71. $rendered[$key] = $behavior->render($map);
  72. }
  73. }
  74. return $rendered;
  75. }
  76. /**
  77. * Process Styles
  78. *
  79. * Get full data for any styles. The idea is that we load
  80. * all the needed styles into the ['styles'] key of the
  81. * map object, and keep a reference in ['layer_styles']
  82. * and ['layer_styles_select'] for layer specific styling.
  83. *
  84. * TODO: Overall, this is not a great approach to managing
  85. * styles.
  86. *
  87. * @param $styles
  88. * Array of map styles ( <style_role> : <style_name> | <style_array> )
  89. * @param $layer_styles
  90. * Array of layer styles ( <layer_name> : <style_name> )
  91. * @param $layer_styles_select
  92. * Array of layer styles ( <layer_name> : <style_name> )
  93. * @param $map
  94. * Map array
  95. * @return $processed
  96. * Array of processed styles ( <style_name> => <style_array> )
  97. */
  98. function _openlayers_styles_process($styles = array(),
  99. $layer_styles = array(), $layer_styles_select = array(), &$map = array()) {
  100. // Get styles info array
  101. $styles_info = openlayers_styles();
  102. // Go through styles
  103. $processed = array();
  104. foreach ($styles as $k => $style) {
  105. // Check if array, if array, just pass on
  106. if (is_array($style)) {
  107. $processed[$k] = $style;
  108. }
  109. elseif (!empty($styles_info[$style]) && $info = $styles_info[$style]->data) {
  110. $processed[$k] = $info;
  111. }
  112. }
  113. // Add layer styles
  114. foreach ($layer_styles as $style) {
  115. if (!isset($processed[$style]) &&
  116. !empty($styles_info[$style]) &&
  117. $info = $styles_info[$style]->data) {
  118. $processed[$style] = $info;
  119. }
  120. }
  121. // Add layer styles select
  122. foreach ($layer_styles_select as $style) {
  123. if (!isset($processed[$style]) &&
  124. !empty($styles_info[$style]) &&
  125. $info = $styles_info[$style]->data) {
  126. $processed[$style] = $info;
  127. }
  128. }
  129. // Add layer styles
  130. foreach ($layer_styles as $style) {
  131. if (!isset($processed[$style]) &&
  132. !empty($styles_info[$style]) &&
  133. $info = $styles_info[$style]->data) {
  134. $processed[$style] = $info;
  135. }
  136. }
  137. // Update URLs to support different types of paths
  138. foreach ($processed as $k => $style) {
  139. $processed[$k] = openlayers_render_style($style);
  140. }
  141. // Run through theme function
  142. $processed = theme('openlayers_styles', array(
  143. 'styles' => $processed,
  144. 'map' => $map)
  145. );
  146. // Return processed
  147. return $processed;
  148. }
  149. /**
  150. * Render style array.
  151. *
  152. * At the moment, this only makes the external grpahics
  153. * relative.
  154. */
  155. function openlayers_render_style($style = array()) {
  156. // Relative path conversion
  157. if (!empty($style['externalGraphic'])) {
  158. // Check full URL or absolute path
  159. if (!valid_url($style['externalGraphic'], TRUE)
  160. && strpos($style['externalGraphic'], '/') !== 0) {
  161. // Make full URL from Drupal path
  162. $style['externalGraphic'] = openlayers_style_path($style['externalGraphic']);
  163. }
  164. }
  165. return $style;
  166. }
  167. /**
  168. * Create Map ID
  169. *
  170. * Create a unique ID for any maps that are not assigned an ID
  171. *
  172. * @note
  173. * Technically someone can assign a map ID identical
  174. * to the one that is created
  175. * @return
  176. * New map id
  177. */
  178. function _openlayers_create_map_id() {
  179. return 'openlayers-map-' . substr(md5(uniqid(mt_rand())), 0, 8);
  180. }
  181. /**
  182. * URL Style
  183. *
  184. * Takes in a path and makes full URL for style. Overall, this
  185. * can be handled by url(), but we have to avoid some encoding
  186. * for variable replacement. Note that this is not perfect as
  187. * it will decode values that maybe not specifically part of the
  188. * attribute replacement.
  189. *
  190. * A value that is just a replacement value, ${value} should
  191. * not be run through the file_create_url() function.
  192. *
  193. * @param $path
  194. * Path to process.
  195. * @return
  196. * Processed path.
  197. */
  198. function openlayers_style_path($path) {
  199. if (strpos($path, '${') !== 0) {
  200. $path = file_create_url($path);
  201. $path = str_replace('%24%7B', '${', $path);
  202. $path = str_replace('%7D', '}', $path);
  203. }
  204. return $path;
  205. }