PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/profiles/openoutreach/modules/contrib/openlayers/includes/openlayers.render.inc

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