1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @defgroup openlayers OpenLayers provides an API and |
---|
5 | * Modules to interface with OpenLayers |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * @file |
---|
10 | * Main OpenLayers API File |
---|
11 | * |
---|
12 | * This file holds the main Drupal hook functions, |
---|
13 | * and the openlayers API functions for the openlayers module. |
---|
14 | * |
---|
15 | * @ingroup openlayers |
---|
16 | */ |
---|
17 | |
---|
18 | /** |
---|
19 | * Implementation of hook_help(). |
---|
20 | */ |
---|
21 | function openlayers_help($path, $arg) { |
---|
22 | switch ($path) { |
---|
23 | case 'admin/help#openlayers': |
---|
24 | $output = '<p>'. t('The OpenLayers module is the base module for the |
---|
25 | OpenLayers suite of modules, and provides the main API.') .'</p>'; |
---|
26 | } |
---|
27 | return ''; |
---|
28 | } |
---|
29 | |
---|
30 | /** |
---|
31 | * Implementation of hook_theme(). |
---|
32 | */ |
---|
33 | function openlayers_theme($existing, $type, $theme, $path) { |
---|
34 | return array( |
---|
35 | 'openlayers_map' => array( |
---|
36 | 'arguments' => array( |
---|
37 | 'map' => array(), |
---|
38 | 'preset_name' => '', |
---|
39 | ), |
---|
40 | 'file' => 'includes/openlayers.theme.inc', |
---|
41 | ), |
---|
42 | 'openlayers_styles' => array( |
---|
43 | 'arguments' => array( |
---|
44 | 'styles' => array(), |
---|
45 | 'map' => array(), |
---|
46 | ), |
---|
47 | 'file' => 'includes/openlayers.theme.inc', |
---|
48 | ), |
---|
49 | ); |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * Include necessary CSS and JS for rendering maps |
---|
54 | * |
---|
55 | * @ingroup openlayers_api |
---|
56 | */ |
---|
57 | function openlayers_include() { |
---|
58 | // Use a static variable to prevent running URL check code repeatedly. |
---|
59 | static $once; |
---|
60 | if (!isset($once)) { |
---|
61 | $once = TRUE; |
---|
62 | |
---|
63 | $path = check_plain(variable_get('openlayers_source', |
---|
64 | 'http://openlayers.org/api/2.9/OpenLayers.js')); |
---|
65 | |
---|
66 | // Correctly handle URLs beginning with a double backslash, see RFC 1808 Section 4 |
---|
67 | if (substr($path, 0, 2) == '//') { |
---|
68 | $http_protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http'; |
---|
69 | $path = $http_protocol .':'. $path; |
---|
70 | } |
---|
71 | |
---|
72 | // Check for full URL and include it manually if external. |
---|
73 | if (valid_url($path, TRUE)) { |
---|
74 | drupal_set_html_head('<script src="' . check_url($path) . |
---|
75 | '" type="text/javascript"></script>'); |
---|
76 | } |
---|
77 | else { |
---|
78 | drupal_add_js($path); |
---|
79 | } |
---|
80 | drupal_add_css(drupal_get_path('module', 'openlayers') . |
---|
81 | '/openlayers.css', 'module'); |
---|
82 | drupal_add_js(drupal_get_path('module', 'openlayers') . |
---|
83 | '/js/openlayers.js', 'module'); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * Prepare a map for rendering. |
---|
89 | * |
---|
90 | * Takes a map array and builds up the data given the |
---|
91 | * reference to objects like styles, layers, and behaviors. |
---|
92 | * |
---|
93 | * @ingroup openlayers_api |
---|
94 | * |
---|
95 | * @param $map |
---|
96 | * Array of map settings |
---|
97 | * @return |
---|
98 | * Filled in map array. |
---|
99 | */ |
---|
100 | function openlayers_build_map($map = array()) { |
---|
101 | // Get the necessary parts |
---|
102 | openlayers_include(); |
---|
103 | module_load_include('inc', 'openlayers', 'includes/openlayers.render'); |
---|
104 | |
---|
105 | // If no map is specified, use the default preset. |
---|
106 | if (empty($map)) { |
---|
107 | if ($preset = openlayers_preset_load(variable_get('openlayers_default_preset', 'default'))) { |
---|
108 | $map = $preset->data; |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | // Create ID for map as this will help with alters. |
---|
113 | $map['id'] = !isset($map['id']) ? |
---|
114 | _openlayers_create_map_id() : $map['id']; |
---|
115 | |
---|
116 | // Hook to alter map before main processing. Styles, behaviors, |
---|
117 | // layers may all be added here. |
---|
118 | // hook_openlayers_map_preprocess_alter($map) |
---|
119 | drupal_alter('openlayers_map_preprocess', $map); |
---|
120 | |
---|
121 | // styles and layer styles are not required parameters |
---|
122 | $map['styles'] = isset($map['styles']) ? |
---|
123 | $map['styles'] : array(); |
---|
124 | |
---|
125 | $layer_styles = array(); |
---|
126 | if ( isset($map['layer_styles']) ) { |
---|
127 | foreach ($map['layer_styles'] as $layer => $style) { |
---|
128 | // UPGRADE NOTE: |
---|
129 | // Presets up to 6.x-2.x-alpha10 always had a single style |
---|
130 | // per layer specified. Newer ones have them splitted by role |
---|
131 | if ( is_array($style) ) { |
---|
132 | $layer_styles[$layer] = $style; |
---|
133 | } |
---|
134 | else { |
---|
135 | $layer_styles[$layer] = array( |
---|
136 | 'default' => $style, |
---|
137 | 'select' => $style, |
---|
138 | 'temporary' => $style |
---|
139 | ); |
---|
140 | } |
---|
141 | } |
---|
142 | } |
---|
143 | $map['layer_styles'] = $layer_styles; |
---|
144 | |
---|
145 | $map['layers'] = _openlayers_layers_process($map['layers'], $map); |
---|
146 | $map['behaviors'] = _openlayers_behaviors_render($map['behaviors'], $map); |
---|
147 | $map['styles'] = _openlayers_styles_process( |
---|
148 | $map['styles'], |
---|
149 | $map['layer_styles'], |
---|
150 | $map); |
---|
151 | |
---|
152 | // Hook to alter map one last time. Final modification to existing |
---|
153 | // styles, behaviors, layers can happen here, but adding new styles, |
---|
154 | // behaviors will not get rendered. |
---|
155 | // hook_openlayers_map_alter($map) |
---|
156 | drupal_alter('openlayers_map', $map); |
---|
157 | |
---|
158 | // Check map for errors |
---|
159 | $map['errors'] = openlayers_error_check_map($map); |
---|
160 | return $map; |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * Render map array |
---|
165 | * |
---|
166 | * Given a map array, render into HTML to display |
---|
167 | * a map. |
---|
168 | * |
---|
169 | * @ingroup openlayers_api |
---|
170 | * |
---|
171 | * @param $map |
---|
172 | * Associative array of map paramters. |
---|
173 | * @param $preset_name |
---|
174 | * Name of the preset used for use in the theme function. |
---|
175 | * @return |
---|
176 | * Map HTML. |
---|
177 | */ |
---|
178 | function openlayers_render_map($map = array(), $preset_name = '') { |
---|
179 | // Run map through build process |
---|
180 | $map = openlayers_build_map($map); |
---|
181 | |
---|
182 | // Given hide_empty_map flag, check if the map has any features |
---|
183 | // defined. If not, assume it is an empty map and shouldn't be displayed. |
---|
184 | if (isset($map['hide_empty_map']) && $map['hide_empty_map'] == TRUE) { |
---|
185 | $empty = TRUE; |
---|
186 | $layers = $map['layers']; |
---|
187 | foreach ($layers as $layer) { |
---|
188 | if (isset($layer['features']) && count($layer['features'])) { |
---|
189 | $empty = FALSE; |
---|
190 | } |
---|
191 | } |
---|
192 | if ($empty) { |
---|
193 | return ''; |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | // Return themed map if no errors found |
---|
198 | if (empty($map['errors'])) { |
---|
199 | $js = array('openlayers' => array('maps' => array($map['id'] => $map))); |
---|
200 | // Try to use more efficient json_encode function, if available |
---|
201 | if (function_exists('json_encode')) { |
---|
202 | drupal_add_js('Drupal.settings.openlayers.maps["' . $map['id'] . |
---|
203 | '"] = ' . json_encode($map) . ';', 'inline', 'header', TRUE); |
---|
204 | } |
---|
205 | else { |
---|
206 | drupal_add_js($js, 'setting'); |
---|
207 | } |
---|
208 | // Push map through theme function and return |
---|
209 | $output = theme('openlayers_map', $map, $preset_name); |
---|
210 | } |
---|
211 | return $output; |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | * Get layer object |
---|
216 | * |
---|
217 | * @ingroup openlayers_api |
---|
218 | * @param $reset |
---|
219 | * Boolean whether to reset cache or not |
---|
220 | * @return array |
---|
221 | * array of layer info |
---|
222 | */ |
---|
223 | function openlayers_get_layer_object($layer, $map = array()) { |
---|
224 | // Static cache because this function will possibly be called in big loops |
---|
225 | static $layer_types; |
---|
226 | if (!isset($layer_types)) { |
---|
227 | $layer_types = openlayers_layer_types(); |
---|
228 | } |
---|
229 | |
---|
230 | $layer->title = t($layer->title); |
---|
231 | $layer->description = t($layer->description); |
---|
232 | |
---|
233 | // Attempt to get ctool class |
---|
234 | // class is renamed klass because of PHP keywords |
---|
235 | if (isset($layer_types[$layer->data['layer_type']]) && |
---|
236 | $klass = ctools_plugin_get_class( |
---|
237 | $layer_types[$layer->data['layer_type']], |
---|
238 | 'layer_type') |
---|
239 | ) { |
---|
240 | $layer_object = new $klass($layer, $map); |
---|
241 | return $layer_object; |
---|
242 | } |
---|
243 | else { |
---|
244 | watchdog('openlayers', 'Layer !layer_name is unavailable because its |
---|
245 | layer type or the module that provides its layer type is missing', |
---|
246 | array('!layer_name' => $layer->title), |
---|
247 | WATCHDOG_ERROR); |
---|
248 | return FALSE; |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | /** |
---|
253 | * Menu loader for layers. %openlayers_layer_export |
---|
254 | * @ingroup openlayers_api |
---|
255 | * |
---|
256 | * @param $name |
---|
257 | * Layer name |
---|
258 | * @return object |
---|
259 | * Layer export |
---|
260 | */ |
---|
261 | function openlayers_layer_export_load($name, $reset = TRUE) { |
---|
262 | ctools_include('export'); |
---|
263 | if ($reset) ctools_export_load_object_reset('openlayers_layers'); |
---|
264 | $layers = ctools_export_load_object('openlayers_layers', 'all', array($name)); |
---|
265 | return !empty($layers[$name]) ? $layers[$name] : FALSE; |
---|
266 | } |
---|
267 | |
---|
268 | /** |
---|
269 | * Get all openlayers layers. |
---|
270 | * @ingroup openlayers_api |
---|
271 | * |
---|
272 | * @param $reset |
---|
273 | * Boolean whether to reset cache or not |
---|
274 | * @return array |
---|
275 | * layer exports |
---|
276 | */ |
---|
277 | function openlayers_layers_export_load($reset = TRUE) { |
---|
278 | ctools_include('export'); |
---|
279 | if ($reset) ctools_export_load_object_reset('openlayers_layers'); |
---|
280 | $layers = ctools_export_load_object('openlayers_layers', 'all', array()); |
---|
281 | return $layers; |
---|
282 | } |
---|
283 | |
---|
284 | /** |
---|
285 | * Menu loader for layers. (%openlayers_layer) |
---|
286 | * @ingroup openlayers_api |
---|
287 | * |
---|
288 | * @param $name |
---|
289 | * Layer name |
---|
290 | * @return array |
---|
291 | * Layer export |
---|
292 | */ |
---|
293 | function openlayers_layer_load($name, $reset = FALSE) { |
---|
294 | ctools_include('export'); |
---|
295 | if ($reset) ctools_export_load_object_reset('openlayers_layers'); |
---|
296 | $layer = ctools_export_load_object('openlayers_layers', 'names', array($name)); |
---|
297 | if ($layer) { |
---|
298 | $layer_object = openlayers_get_layer_object($layer[$name]); |
---|
299 | if (openlayers_layer_sanity_check($layer_object)) { |
---|
300 | return $layer_object; |
---|
301 | } |
---|
302 | } |
---|
303 | else { |
---|
304 | watchdog('openlayers', 'Layer %layer not found.', |
---|
305 | array('%layer' => $name), WATCHDOG_ERROR); |
---|
306 | return FALSE; |
---|
307 | } |
---|
308 | } |
---|
309 | |
---|
310 | /** |
---|
311 | * Get all openlayers layers as objects. |
---|
312 | * @ingroup openlayers_api |
---|
313 | * |
---|
314 | * @param $reset |
---|
315 | * Boolean whether to reset cache or not |
---|
316 | * @return array |
---|
317 | * array of layer info |
---|
318 | */ |
---|
319 | function openlayers_layers_load($reset = FALSE) { |
---|
320 | ctools_include('export'); |
---|
321 | $layer_objects = array(); |
---|
322 | if ($reset) ctools_export_load_object_reset('openlayers_layers'); |
---|
323 | $layers = ctools_export_load_object('openlayers_layers', 'all', array()); |
---|
324 | foreach ($layers as $layer) { |
---|
325 | $layer_objects[$layer->name] = openlayers_get_layer_object($layer); |
---|
326 | } |
---|
327 | $layer_objects = array_filter($layer_objects, 'openlayers_layer_sanity_check'); |
---|
328 | return $layer_objects; |
---|
329 | } |
---|
330 | |
---|
331 | /** |
---|
332 | * Check layer to determine whether it has all the |
---|
333 | * necessary attributes to be rendered. This is necessary |
---|
334 | * because of API changes, and is a consolidation from other |
---|
335 | * layer-error-checking in this module |
---|
336 | * |
---|
337 | * @param $layer |
---|
338 | * Layer object |
---|
339 | * @param $projection |
---|
340 | * Projection number (EPSG) to check compatibility with |
---|
341 | * @param $strict |
---|
342 | * reject invalid layers |
---|
343 | * @return boolean |
---|
344 | * layer validity if strict is set, otherwise always true |
---|
345 | */ |
---|
346 | function openlayers_layer_sanity_check($layer, $projection = FALSE, $strict = FALSE) { |
---|
347 | // Handle layers after they've been rendered for a map |
---|
348 | $layer = (is_array($layer)) ? (object) $layer : $layer; |
---|
349 | |
---|
350 | if (!isset($layer->data['projection']) || !is_array($layer->data['projection'])) { |
---|
351 | watchdog('openlayers', 'Layer %name does not have a projection set.', |
---|
352 | array('%name' => $layer->name)); |
---|
353 | drupal_set_message( |
---|
354 | t('OpenLayers layers failed the sanity check. See the |
---|
355 | <a href="@drupallog">Drupal log</a> for details', |
---|
356 | array('@drupallog' => url('admin/reports/dblog'))) |
---|
357 | ); |
---|
358 | return !$strict; |
---|
359 | } |
---|
360 | |
---|
361 | if (!isset($layer->data['layer_type'])) { |
---|
362 | watchdog('openlayers', 'Layer %name does not have its layer_type set.', |
---|
363 | array('%name' => $layer->name)); |
---|
364 | drupal_set_message( |
---|
365 | t('OpenLayers layers failed the sanity check. See the |
---|
366 | <a href="@drupallog">Drupal log</a> for details', |
---|
367 | array('@drupallog' => url('admin/reports/dblog'))) |
---|
368 | ); |
---|
369 | return !$strict; |
---|
370 | } |
---|
371 | |
---|
372 | if ($projection && empty($layer->data['vector']) && |
---|
373 | (!in_array($projection, $layer->data['projection']))) { |
---|
374 | watchdog('openlayers', |
---|
375 | 'The layer %layer_name cannot be reprojected to the map projection: EPSG: %map_proj', |
---|
376 | array( |
---|
377 | '%layer_name' => $layer->name, |
---|
378 | '%map_proj' => $map['projection'], |
---|
379 | ) |
---|
380 | ); |
---|
381 | return !$strict; |
---|
382 | } |
---|
383 | |
---|
384 | return TRUE; |
---|
385 | } |
---|
386 | |
---|
387 | /** |
---|
388 | * Save layer. |
---|
389 | * @ingroup openlayers_api |
---|
390 | * |
---|
391 | * @param $layer |
---|
392 | * Layer object |
---|
393 | * @return |
---|
394 | * bool for success in saving |
---|
395 | */ |
---|
396 | function openlayers_layer_save($layer) { |
---|
397 | if (!empty($layer->name)) { |
---|
398 | $exists = db_result(db_query("SELECT name FROM {openlayers_layers} WHERE name = '%s'", $layer->name)); |
---|
399 | return $exists ? |
---|
400 | drupal_write_record('openlayers_layers', $layer, 'name') : |
---|
401 | drupal_write_record('openlayers_layers', $layer); |
---|
402 | } |
---|
403 | return FALSE; |
---|
404 | } |
---|
405 | |
---|
406 | /** |
---|
407 | * Delete a layer object from the database. |
---|
408 | * |
---|
409 | * @ingroup openlayers_api |
---|
410 | * |
---|
411 | * @param $preset |
---|
412 | * String identifier of a layer or layer object with name. |
---|
413 | * @return |
---|
414 | * The results of DB delete. |
---|
415 | */ |
---|
416 | function openlayers_layer_delete($layer) { |
---|
417 | return openlayers_object_delete($layer, 'layer'); |
---|
418 | } |
---|
419 | |
---|
420 | /** |
---|
421 | * Get all layer types. |
---|
422 | * |
---|
423 | * @ingroup openlayers_api |
---|
424 | * |
---|
425 | * @param $reset |
---|
426 | * Boolean whether to reset cache or not. |
---|
427 | * @return |
---|
428 | * Array of layer type info. |
---|
429 | */ |
---|
430 | function openlayers_layer_types($reset = FALSE) { |
---|
431 | ctools_include('plugins'); |
---|
432 | return ctools_get_plugins('openlayers', 'layer_types'); |
---|
433 | } |
---|
434 | |
---|
435 | /** |
---|
436 | * Menu loader for layer types. |
---|
437 | * |
---|
438 | * @ingroup openlayers_api |
---|
439 | * |
---|
440 | * @param $name |
---|
441 | * String identifier of layer type. |
---|
442 | * @param $reset |
---|
443 | * Boolean whether to reset cache or not. |
---|
444 | * @return |
---|
445 | * An instantiated layer type object or FALSE if not found. |
---|
446 | */ |
---|
447 | function openlayers_layer_type_load($name, $reset = FALSE) { |
---|
448 | ctools_include('plugins'); |
---|
449 | |
---|
450 | if ($layer_type_class = ctools_plugin_load_class( |
---|
451 | 'openlayers', 'layer_types', $name, 'layer_type')) { |
---|
452 | $layer_type = new $layer_type_class(); |
---|
453 | return $layer_type; |
---|
454 | } |
---|
455 | return FALSE; |
---|
456 | } |
---|
457 | |
---|
458 | /** |
---|
459 | * Get all behaviors. |
---|
460 | * |
---|
461 | * @ingroup openlayers_api |
---|
462 | * |
---|
463 | * @param $reset |
---|
464 | * Boolean whether to reset cache or not. |
---|
465 | * @return |
---|
466 | * Array of behavior info. |
---|
467 | */ |
---|
468 | function openlayers_behaviors($reset = FALSE) { |
---|
469 | ctools_include('plugins'); |
---|
470 | return ctools_get_plugins('openlayers', 'behaviors'); |
---|
471 | } |
---|
472 | |
---|
473 | /** |
---|
474 | * Get all style plugins. |
---|
475 | * |
---|
476 | * @ingroup openlayers_api |
---|
477 | * |
---|
478 | * @param $reset |
---|
479 | * Boolean whether to reset cache or not. |
---|
480 | * @return |
---|
481 | * Array of style handler info. |
---|
482 | */ |
---|
483 | function openlayers_style_plugins($reset = FALSE) { |
---|
484 | ctools_include('plugins'); |
---|
485 | return ctools_get_plugins('openlayers', 'style_plugin'); |
---|
486 | } |
---|
487 | |
---|
488 | /** |
---|
489 | * Get all openlayers styles. |
---|
490 | * |
---|
491 | * @ingroup openlayers_api |
---|
492 | * |
---|
493 | * @param $reset |
---|
494 | * Boolean whether to reset cache or not. |
---|
495 | * @return |
---|
496 | * Array of all available styles. |
---|
497 | */ |
---|
498 | function openlayers_styles($reset = FALSE) { |
---|
499 | ctools_include('export'); |
---|
500 | if ($reset) { |
---|
501 | ctools_export_load_object_reset('openlayers_styles'); |
---|
502 | } |
---|
503 | |
---|
504 | $styles = ctools_export_load_object('openlayers_styles', 'all', array()); |
---|
505 | return $styles; |
---|
506 | } |
---|
507 | |
---|
508 | /** |
---|
509 | * Load a style object by name. |
---|
510 | * |
---|
511 | * This function can also be used as a |
---|
512 | * menu loader for a style. |
---|
513 | * |
---|
514 | * @ingroup openlayers_api |
---|
515 | * |
---|
516 | * @param $name |
---|
517 | * The string identifier of the style. |
---|
518 | * @param $reset |
---|
519 | * Boolean whether to reset the cache or not. |
---|
520 | * @return |
---|
521 | * A style object or FALSE if not found. |
---|
522 | */ |
---|
523 | function openlayers_style_load($name, $reset = FALSE) { |
---|
524 | $styles = openlayers_styles($reset); |
---|
525 | return !empty($styles[$name]) ? $styles[$name] : FALSE; |
---|
526 | } |
---|
527 | |
---|
528 | /** |
---|
529 | * Save style. |
---|
530 | * |
---|
531 | * @ingroup openlayers_api |
---|
532 | * |
---|
533 | * @param $style |
---|
534 | * The style object to save. |
---|
535 | * @return |
---|
536 | * The results of DB write or FALSE if no name. |
---|
537 | */ |
---|
538 | function openlayers_style_save($style) { |
---|
539 | if (!empty($style->name)) { |
---|
540 | $exists = db_result(db_query("SELECT name FROM {openlayers_styles} |
---|
541 | WHERE name = '%s'", $style->name)); |
---|
542 | return $exists ? |
---|
543 | drupal_write_record('openlayers_styles', $style, 'name') : |
---|
544 | drupal_write_record('openlayers_styles', $style); |
---|
545 | } |
---|
546 | return FALSE; |
---|
547 | } |
---|
548 | |
---|
549 | /** |
---|
550 | * Delete a style object from the database. |
---|
551 | * |
---|
552 | * @ingroup openlayers_api |
---|
553 | * |
---|
554 | * @param $preset |
---|
555 | * String identifier of a style or style object with name. |
---|
556 | * @return |
---|
557 | * The results of DB delete. |
---|
558 | */ |
---|
559 | function openlayers_style_delete($style) { |
---|
560 | return openlayers_object_delete($style, 'style'); |
---|
561 | } |
---|
562 | |
---|
563 | /** |
---|
564 | * Get Presets from DB or code, via cache. |
---|
565 | * |
---|
566 | * @ingroup openlayers_api |
---|
567 | * |
---|
568 | * @param $reset |
---|
569 | * Boolean whether to reset or not. |
---|
570 | * @return |
---|
571 | * Return array of presets. |
---|
572 | */ |
---|
573 | function openlayers_presets($reset = FALSE) { |
---|
574 | ctools_include('export'); |
---|
575 | if ($reset) { |
---|
576 | ctools_export_load_object_reset('openlayers_map_presets'); |
---|
577 | } |
---|
578 | |
---|
579 | $presets = ctools_export_load_object( |
---|
580 | 'openlayers_map_presets', 'all', array()); |
---|
581 | return $presets; |
---|
582 | } |
---|
583 | |
---|
584 | /** |
---|
585 | * Given a preset name, get full preset object. |
---|
586 | * |
---|
587 | * This function can also be used as a |
---|
588 | * menu loader for a style. |
---|
589 | * |
---|
590 | * @ingroup openlayers_api |
---|
591 | * |
---|
592 | * @param $name |
---|
593 | * String identifier of the preset. |
---|
594 | * @param $reset |
---|
595 | * Boolean whether to reset cache. |
---|
596 | * @return |
---|
597 | * Preset object or FALSE if not found. |
---|
598 | */ |
---|
599 | function openlayers_preset_load($name = '', $reset = FALSE) { |
---|
600 | ctools_include('export'); |
---|
601 | if ($reset) { |
---|
602 | ctools_export_load_object_reset('openlayers_map_presets'); |
---|
603 | } |
---|
604 | |
---|
605 | $presets = ctools_export_load_object( |
---|
606 | 'openlayers_map_presets', 'names', array($name)); |
---|
607 | if (empty($presets[$name])) { |
---|
608 | return FALSE; |
---|
609 | } |
---|
610 | else { |
---|
611 | $preset = $presets[$name]; |
---|
612 | $preset->data['preset_name'] = $name; |
---|
613 | return $preset; |
---|
614 | } |
---|
615 | } |
---|
616 | |
---|
617 | /** |
---|
618 | * Save a preset object to the database. |
---|
619 | * |
---|
620 | * @ingroup openlayers_api |
---|
621 | * |
---|
622 | * @param $preset |
---|
623 | * Preset object. |
---|
624 | * @return |
---|
625 | * The results of DB write or FALSE if no name. |
---|
626 | */ |
---|
627 | function openlayers_preset_save($preset) { |
---|
628 | if (!empty($preset->name)) { |
---|
629 | $exists = db_result(db_query("SELECT name FROM {openlayers_map_presets} |
---|
630 | WHERE name = '%s'", $preset->name)); |
---|
631 | return $exists ? |
---|
632 | drupal_write_record('openlayers_map_presets', $preset, 'name') : |
---|
633 | drupal_write_record('openlayers_map_presets', $preset); |
---|
634 | } |
---|
635 | return FALSE; |
---|
636 | } |
---|
637 | |
---|
638 | /** |
---|
639 | * Delete a preset object from the database. |
---|
640 | * |
---|
641 | * @ingroup openlayers_api |
---|
642 | * |
---|
643 | * @param $preset |
---|
644 | * String identifier of a preset or preset object with name. |
---|
645 | * @return |
---|
646 | * The results of DB delete. |
---|
647 | */ |
---|
648 | function openlayers_preset_delete($preset) { |
---|
649 | return openlayers_object_delete($preset, 'preset'); |
---|
650 | } |
---|
651 | |
---|
652 | /** |
---|
653 | * Get preset options in an array suitable for a FormAPI element. |
---|
654 | * |
---|
655 | * @ingroup openlayers_api |
---|
656 | * |
---|
657 | * @param $reset |
---|
658 | * Boolean whether to reset or not. |
---|
659 | * @return |
---|
660 | * Return array of formatted data. |
---|
661 | */ |
---|
662 | function openlayers_preset_options($reset = FALSE) { |
---|
663 | $presets = openlayers_presets($reset); |
---|
664 | $options = array(); |
---|
665 | foreach ($presets as $preset) { |
---|
666 | $options[$preset->name] = $preset->title; |
---|
667 | } |
---|
668 | return $options; |
---|
669 | } |
---|
670 | |
---|
671 | /** |
---|
672 | * Delete an object from the database. |
---|
673 | * |
---|
674 | * @ingroup openlayers_api |
---|
675 | * |
---|
676 | * @param $ol_object |
---|
677 | * String identifier of an object or the object with name. |
---|
678 | * @param $type |
---|
679 | * Type of object to delete. The options are the following: |
---|
680 | * - 'layer' |
---|
681 | * - 'style' |
---|
682 | * = 'preset' |
---|
683 | * @return |
---|
684 | * The results of the DB delete. |
---|
685 | */ |
---|
686 | function openlayers_object_delete($ol_object, $type) { |
---|
687 | // Check for object or name |
---|
688 | if (is_object($ol_object) && isset($ol_object->name)) { |
---|
689 | $ol_object = $ol_object->name; |
---|
690 | } |
---|
691 | |
---|
692 | // Determine query to use |
---|
693 | switch ($type) { |
---|
694 | case 'style': |
---|
695 | $query = "DELETE FROM {openlayers_styles} WHERE name = '%s'"; |
---|
696 | break; |
---|
697 | |
---|
698 | case 'layer': |
---|
699 | $query = "DELETE FROM {openlayers_layers} WHERE name = '%s'"; |
---|
700 | break; |
---|
701 | |
---|
702 | case 'preset': |
---|
703 | $query = "DELETE FROM {openlayers_map_presets} WHERE name = '%s'"; |
---|
704 | break; |
---|
705 | |
---|
706 | default: |
---|
707 | return FALSE; |
---|
708 | } |
---|
709 | return db_query($query, $ol_object); |
---|
710 | } |
---|
711 | |
---|
712 | /** |
---|
713 | * Checks map array for incompatibilities or errors. |
---|
714 | * |
---|
715 | * @ingroup openlayers_api |
---|
716 | * |
---|
717 | * @param $map |
---|
718 | * Map array |
---|
719 | * @param $log_errors |
---|
720 | * Boolean whether to log errors. |
---|
721 | * @return |
---|
722 | * FALSE if passed. Array of descriptive errors if fail. |
---|
723 | */ |
---|
724 | function openlayers_error_check_map($map, $log_errors = TRUE) { |
---|
725 | $errors = array(); |
---|
726 | |
---|
727 | // Check for layers |
---|
728 | if (!is_array($map['layers'])) { |
---|
729 | $errors[] = t('Map contains no renderable layers.'); |
---|
730 | } |
---|
731 | else { |
---|
732 | // Check layer projections |
---|
733 | foreach ($map['layers'] as $layer) { |
---|
734 | openlayers_layer_sanity_check( |
---|
735 | array('data' => $layer), |
---|
736 | $map['projection'], |
---|
737 | TRUE); |
---|
738 | } |
---|
739 | } |
---|
740 | |
---|
741 | // Check if any errors found to log |
---|
742 | if (count($errors) > 0 && $log_errors) { |
---|
743 | // Log the Error(s) |
---|
744 | watchdog('openlayers', implode(', ', $errors), array(), WATCHDOG_ERROR); |
---|
745 | } |
---|
746 | |
---|
747 | // Check if errors and return |
---|
748 | return (count($errors) > 0) ? $errors : FALSE; |
---|
749 | } |
---|
750 | |
---|
751 | /** |
---|
752 | * Get extent given projection |
---|
753 | * |
---|
754 | * Returns standard world-max-extents for common projections. |
---|
755 | * Layers implementing other projections and subsets of the |
---|
756 | * world should define their maxExtent in the layer array. |
---|
757 | * |
---|
758 | * @ingroup openlayers_api |
---|
759 | * |
---|
760 | * @param $projection |
---|
761 | * String of the projection value. Currently |
---|
762 | * supports 900913, 4326. |
---|
763 | * @return |
---|
764 | * Array of maxExtent in OpenLayers toArray() format. |
---|
765 | */ |
---|
766 | function openlayers_get_extent($projection) { |
---|
767 | switch ($projection) { |
---|
768 | case '900913': |
---|
769 | return array(-20037508, -20037508, 20037508, 20037508); |
---|
770 | case '4326': |
---|
771 | return array(-180, -90, 180, 90); |
---|
772 | } |
---|
773 | } |
---|
774 | |
---|
775 | /** |
---|
776 | * Get resolution given projection |
---|
777 | * |
---|
778 | * Returns a default set of resolutions for standard |
---|
779 | * TMS, WMS, etc servers serving up common projections. |
---|
780 | * Layers supporting less common projections and resolutions |
---|
781 | * can easily define custom resolutions arrays. |
---|
782 | * |
---|
783 | * @ingroup openlayers_api |
---|
784 | * |
---|
785 | * @param $projection |
---|
786 | * String specifying which projection this should take, like 900913. |
---|
787 | * @param $zoom_start |
---|
788 | * Integer of first zoom level, default 0. |
---|
789 | * @param $zoom_end |
---|
790 | * Integer of last zoom level, default FALSE. |
---|
791 | * @return |
---|
792 | * Array of resolutions. |
---|
793 | */ |
---|
794 | function openlayers_get_resolutions($projection, $zoom_start = 0, $zoom_end = FALSE) { |
---|
795 | switch ($projection) { |
---|
796 | case '900913': |
---|
797 | // 16 zoom levels, taken from |
---|
798 | // openlayers.org TMS map |
---|
799 | $res = array( |
---|
800 | 156543.0339, |
---|
801 | 78271.51695, |
---|
802 | 39135.758475, |
---|
803 | 19567.8792375, |
---|
804 | 9783.93961875, |
---|
805 | 4891.969809375, |
---|
806 | 2445.9849046875, |
---|
807 | 1222.99245234375, |
---|
808 | 611.496226171875, |
---|
809 | 305.7481130859375, |
---|
810 | 152.87405654296876, |
---|
811 | 76.43702827148438, |
---|
812 | 38.21851413574219, |
---|
813 | 19.109257067871095, |
---|
814 | 9.554628533935547, |
---|
815 | 4.777314266967774, |
---|
816 | 2.388657133483887, |
---|
817 | 1.1943285667419434, |
---|
818 | 0.5971642833709717); |
---|
819 | break; |
---|
820 | case '4326': |
---|
821 | // 16 zoom levels, taken from |
---|
822 | // openlayers.org default WMS map |
---|
823 | $res = array( |
---|
824 | 0.703125, |
---|
825 | 0.3515625, |
---|
826 | 0.17578125, |
---|
827 | 0.087890625, |
---|
828 | 0.0439453125, |
---|
829 | 0.02197265625, |
---|
830 | 0.010986328125, |
---|
831 | 0.0054931640625, |
---|
832 | 0.00274658203125, |
---|
833 | 0.001373291015625, |
---|
834 | 0.0006866455078125, |
---|
835 | 0.00034332275390625, |
---|
836 | 0.000171661376953125, |
---|
837 | 0.0000858306884765625, |
---|
838 | 0.00004291534423828125, |
---|
839 | 0.000021457672119140625); |
---|
840 | break; |
---|
841 | default: |
---|
842 | $res = array(); |
---|
843 | break; |
---|
844 | } |
---|
845 | $length = ($zoom_end == FALSE) ? NULL : $zoom_end - $zoom_start; |
---|
846 | // By default this will not actually clip the array |
---|
847 | $resolutions = array_slice($res, $zoom_start, $length); |
---|
848 | return $resolutions; |
---|
849 | } |
---|
850 | |
---|
851 | /** |
---|
852 | * We define base classes in the core module. |
---|
853 | * All other parent classes can be autoloaded through ctools. |
---|
854 | */ |
---|
855 | class openlayers_behavior { |
---|
856 | var $options, $map; |
---|
857 | |
---|
858 | function __construct($options = array(), $map = array()) { |
---|
859 | $this->options = $options + $this->options_init(); |
---|
860 | $this->map = $map; |
---|
861 | } |
---|
862 | |
---|
863 | /* |
---|
864 | * @return array of JavaScript functions required to be defined |
---|
865 | * in order for this function to work |
---|
866 | */ |
---|
867 | function js_dependency() { |
---|
868 | return array(); |
---|
869 | } |
---|
870 | |
---|
871 | function options_init() { |
---|
872 | return array(); |
---|
873 | } |
---|
874 | |
---|
875 | /* |
---|
876 | * @param $defaults default values for the form |
---|
877 | * @return a FormAPI form |
---|
878 | */ |
---|
879 | function options_form($defaults = array()) { |
---|
880 | return array(); |
---|
881 | } |
---|
882 | |
---|
883 | function render(&$map) {} |
---|
884 | } |
---|
885 | |
---|
886 | /** |
---|
887 | * We define base classes in the core module. |
---|
888 | * All other parent classes can be autoloaded through ctools. |
---|
889 | */ |
---|
890 | class openlayers_layer_type { |
---|
891 | var $options, $map; |
---|
892 | |
---|
893 | function __construct($layer = array(), $map = array()) { |
---|
894 | foreach (array('name', 'title', 'description', 'data', 'export_type') as $k) { |
---|
895 | if (isset($layer->{$k})) { |
---|
896 | $this->{$k} = $layer->{$k}; |
---|
897 | } |
---|
898 | } |
---|
899 | if (isset($this->data) && is_array($this->data)) { |
---|
900 | $this->data += $this->options_init(); |
---|
901 | } |
---|
902 | $this->map = $map; |
---|
903 | } |
---|
904 | |
---|
905 | function options_init() { |
---|
906 | return array(); |
---|
907 | } |
---|
908 | |
---|
909 | function options_form() { |
---|
910 | return array(); |
---|
911 | } |
---|
912 | |
---|
913 | /** |
---|
914 | * @return |
---|
915 | * A version of this layer_type which can be saved, |
---|
916 | * when attempting to save a modified layer |
---|
917 | */ |
---|
918 | function to_record() { |
---|
919 | return array( |
---|
920 | 'name' => $this->name, |
---|
921 | 'description' => $this->description, |
---|
922 | 'title' => $this->title, |
---|
923 | 'data' => $this->data |
---|
924 | ); |
---|
925 | } |
---|
926 | |
---|
927 | /** |
---|
928 | * @return |
---|
929 | * Success value on saving this layer |
---|
930 | */ |
---|
931 | function save() { |
---|
932 | if (!empty($this->name)) { |
---|
933 | $exists = db_result(db_query("SELECT name FROM {openlayers_layers} WHERE name = '%s'", $this->name)); |
---|
934 | // If this layer exists, specify that 'name' is |
---|
935 | // the primary key for the layer which will be updated |
---|
936 | return $exists ? |
---|
937 | drupal_write_record('openlayers_layers', $this->to_record(), 'name') : |
---|
938 | drupal_write_record('openlayers_layers', $this->to_record()); |
---|
939 | } |
---|
940 | } |
---|
941 | |
---|
942 | function render(&$map) {} |
---|
943 | } |
---|
944 | |
---|
945 | /** |
---|
946 | * Base class for style plugins |
---|
947 | * |
---|
948 | * We define base classes in the core module. |
---|
949 | * All other parent classes can be autoloaded through ctools. |
---|
950 | */ |
---|
951 | class openlayers_style_plugin { |
---|
952 | |
---|
953 | /** |
---|
954 | * Return true if this plugin can handle |
---|
955 | * the given property |
---|
956 | */ |
---|
957 | function can_handle_property($propname) { |
---|
958 | return array_key_exists($propname, $this->get_context_properties()); |
---|
959 | } |
---|
960 | |
---|
961 | /** |
---|
962 | * Get an array of style property callbacks |
---|
963 | * |
---|
964 | * @return |
---|
965 | * Array of <property_name> => <callback_name> |
---|
966 | */ |
---|
967 | function get_context_properties() { |
---|
968 | return array(); |
---|
969 | } |
---|
970 | |
---|
971 | /** |
---|
972 | * Initial default options. |
---|
973 | * |
---|
974 | * @return |
---|
975 | * Array of default options. |
---|
976 | */ |
---|
977 | function options_init() { |
---|
978 | return array(); |
---|
979 | } |
---|
980 | |
---|
981 | /** |
---|
982 | * Options form. |
---|
983 | * |
---|
984 | * @param $defaults |
---|
985 | * Array of default values for the form. |
---|
986 | * @return |
---|
987 | * Array of Drupal form elements. |
---|
988 | */ |
---|
989 | function options_form($defaults = array()) { |
---|
990 | return array(); |
---|
991 | } |
---|
992 | |
---|
993 | /** |
---|
994 | * Render the style. |
---|
995 | */ |
---|
996 | function render() { |
---|
997 | // Render style. |
---|
998 | } |
---|
999 | } |
---|
1000 | |
---|
1001 | /** |
---|
1002 | * Implementation of hook_ctools_plugin_directory |
---|
1003 | */ |
---|
1004 | function openlayers_ctools_plugin_directory($module, $plugin) { |
---|
1005 | // The format of plugin includes should be the |
---|
1006 | // following: |
---|
1007 | // modulename_plugin_name.inc |
---|
1008 | // |
---|
1009 | // For example: |
---|
1010 | // openlayers_style_plugin_name.inc |
---|
1011 | |
---|
1012 | // If this module needed to supply style plugins. |
---|
1013 | /* |
---|
1014 | if ($module == 'openlayers' && $plugin == 'style_plugin') { |
---|
1015 | return 'plugins/style_plugin'; |
---|
1016 | } |
---|
1017 | */ |
---|
1018 | |
---|
1019 | // This should change to the following when converted: |
---|
1020 | /* |
---|
1021 | if ($module == 'openlayers') { |
---|
1022 | return 'plugins/' . $plugin; |
---|
1023 | } |
---|
1024 | */ |
---|
1025 | } |
---|
1026 | |
---|
1027 | /** |
---|
1028 | * Implementation of hook_ctools_plugin |
---|
1029 | */ |
---|
1030 | function openlayers_ctools_plugin_behaviors() { |
---|
1031 | return array( |
---|
1032 | 'use hooks' => TRUE, |
---|
1033 | ); |
---|
1034 | } |
---|
1035 | |
---|
1036 | /** |
---|
1037 | * Implementation of hook_ctools_plugin |
---|
1038 | */ |
---|
1039 | function openlayers_ctools_plugin_layer_types() { |
---|
1040 | return array( |
---|
1041 | 'use hooks' => TRUE, |
---|
1042 | ); |
---|
1043 | } |
---|
1044 | |
---|
1045 | /** |
---|
1046 | * Implementation of hook_ctools_plugin_api(). |
---|
1047 | */ |
---|
1048 | function openlayers_ctools_plugin_api($module, $api) { |
---|
1049 | if ($module == "openlayers") { |
---|
1050 | switch ($api) { |
---|
1051 | case 'openlayers_presets': |
---|
1052 | return array('version' => 1); |
---|
1053 | |
---|
1054 | case 'openlayers_layers': |
---|
1055 | return array('version' => 1); |
---|
1056 | |
---|
1057 | case 'openlayers_styles': |
---|
1058 | return array('version' => 1); |
---|
1059 | |
---|
1060 | } |
---|
1061 | } |
---|
1062 | } |
---|
1063 | |
---|
1064 | /** |
---|
1065 | * Implementation of hook_openlayers_layers(). |
---|
1066 | */ |
---|
1067 | function openlayers_openlayers_layers() { |
---|
1068 | module_load_include('inc', 'openlayers', 'includes/openlayers.layers'); |
---|
1069 | return _openlayers_openlayers_layers(); |
---|
1070 | } |
---|
1071 | |
---|
1072 | /** |
---|
1073 | * Implementation of hook_openlayers_behaviors(). |
---|
1074 | */ |
---|
1075 | function openlayers_openlayers_behaviors() { |
---|
1076 | module_load_include('inc', 'openlayers', 'includes/openlayers.behaviors'); |
---|
1077 | return _openlayers_openlayers_behaviors(); |
---|
1078 | } |
---|
1079 | |
---|
1080 | /** |
---|
1081 | * Implementation of hook_openlayers_styles(). |
---|
1082 | */ |
---|
1083 | function openlayers_openlayers_styles() { |
---|
1084 | module_load_include('inc', 'openlayers', 'includes/openlayers.styles'); |
---|
1085 | return _openlayers_openlayers_styles(); |
---|
1086 | } |
---|
1087 | |
---|
1088 | /** |
---|
1089 | * Implementation of hook_openlayers_presets(). |
---|
1090 | */ |
---|
1091 | function openlayers_openlayers_presets() { |
---|
1092 | module_load_include('inc', 'openlayers', 'includes/openlayers.presets'); |
---|
1093 | return _openlayers_openlayers_presets(); |
---|
1094 | } |
---|
1095 | |
---|
1096 | /** |
---|
1097 | * Implementation of hook_openlayers_layer_types(). |
---|
1098 | */ |
---|
1099 | function openlayers_openlayers_layer_types() { |
---|
1100 | module_load_include('inc', 'openlayers', 'includes/openlayers.layer_types'); |
---|
1101 | return _openlayers_openlayers_layer_types(); |
---|
1102 | } |
---|