1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * This file holds the functions for the installing |
---|
6 | * and enabling of the openlayers module. |
---|
7 | * |
---|
8 | * @ingroup openlayers |
---|
9 | */ |
---|
10 | |
---|
11 | /** |
---|
12 | * Implementation of hook_install(). |
---|
13 | */ |
---|
14 | function openlayers_install() { |
---|
15 | // Create tables |
---|
16 | drupal_install_schema('openlayers'); |
---|
17 | } |
---|
18 | |
---|
19 | /** |
---|
20 | * Implementation of hook_uninstall(). |
---|
21 | */ |
---|
22 | function openlayers_uninstall() { |
---|
23 | // Remove tables |
---|
24 | drupal_uninstall_schema('openlayers'); |
---|
25 | |
---|
26 | // Get module variables |
---|
27 | $results = db_query("SELECT v.name FROM {variable} AS v WHERE v.name LIKE '%s%%'", 'openlayers_'); |
---|
28 | // Remove variables |
---|
29 | while ($row = db_fetch_array($results)) { |
---|
30 | variable_del($row['name']); |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | /** |
---|
35 | * Implementation of hook_schema(). |
---|
36 | */ |
---|
37 | function openlayers_schema() { |
---|
38 | $schema = array(); |
---|
39 | |
---|
40 | // Maps table (ctools extras) |
---|
41 | $schema['openlayers_map_presets'] = array( |
---|
42 | 'description' => 'Storage for User defined OpenLayers map presets.', |
---|
43 | 'export' => array( |
---|
44 | 'key' => 'name', |
---|
45 | 'identifier' => 'openlayers_presets', |
---|
46 | 'default hook' => 'openlayers_presets', |
---|
47 | 'api' => array( |
---|
48 | 'owner' => 'openlayers', |
---|
49 | 'api' => 'openlayers_presets', |
---|
50 | 'minimum_version' => 1, |
---|
51 | 'current_version' => 1, |
---|
52 | ), |
---|
53 | ), |
---|
54 | 'fields' => array( |
---|
55 | 'name' => array( |
---|
56 | 'description' => t('The primary identifier for the preset.'), |
---|
57 | 'type' => 'varchar', |
---|
58 | 'length' => 255, |
---|
59 | 'not null' => TRUE, |
---|
60 | ), |
---|
61 | 'title' => array( |
---|
62 | 'description' => t('The title of the preset.'), |
---|
63 | 'type' => 'varchar', |
---|
64 | 'length' => 255, |
---|
65 | 'not null' => TRUE, |
---|
66 | ), |
---|
67 | 'description' => array( |
---|
68 | 'description' => t('The description of the preset.'), |
---|
69 | 'type' => 'text', |
---|
70 | 'not null' => TRUE, |
---|
71 | ), |
---|
72 | 'data' => array( |
---|
73 | 'description' => t('The serialized map.'), |
---|
74 | 'type' => 'text', |
---|
75 | 'not null' => TRUE, |
---|
76 | 'serialize' => TRUE, |
---|
77 | ), |
---|
78 | ), |
---|
79 | 'primary key' => array('name'), |
---|
80 | ); |
---|
81 | |
---|
82 | // Layer table (ctools extras) |
---|
83 | $schema['openlayers_layers'] = array( |
---|
84 | 'description' => 'Storage for user defined OpenLayers layers.', |
---|
85 | 'export' => array( |
---|
86 | 'key' => 'name', |
---|
87 | 'identifier' => 'openlayers_layers', |
---|
88 | 'default hook' => 'openlayers_layers', |
---|
89 | 'api' => array( |
---|
90 | 'owner' => 'openlayers', |
---|
91 | 'api' => 'openlayers_layers', |
---|
92 | 'minimum_version' => 1, |
---|
93 | 'current_version' => 1, |
---|
94 | ), |
---|
95 | ), |
---|
96 | 'fields' => array( |
---|
97 | 'name' => array( |
---|
98 | 'type' => 'varchar', |
---|
99 | 'length' => '255', |
---|
100 | 'not null' => TRUE, |
---|
101 | 'default' => '', |
---|
102 | 'description' => 'Layer name.', |
---|
103 | ), |
---|
104 | 'title' => array( |
---|
105 | 'type' => 'varchar', |
---|
106 | 'length' => '255', |
---|
107 | 'not null' => TRUE, |
---|
108 | 'default' => '', |
---|
109 | 'description' => 'Layer title.', |
---|
110 | ), |
---|
111 | 'description' => array( |
---|
112 | 'type' => 'text', |
---|
113 | 'not null' => TRUE, |
---|
114 | 'description' => 'Layer description.', |
---|
115 | ), |
---|
116 | 'data' => array( |
---|
117 | 'type' => 'text', |
---|
118 | 'not null' => FALSE, |
---|
119 | 'description' => 'Layer data serialized.', |
---|
120 | 'serialize' => TRUE, |
---|
121 | ), |
---|
122 | ), |
---|
123 | 'primary key' => array('name'), |
---|
124 | 'indexes' => array( |
---|
125 | 'name' => array('name'), |
---|
126 | ), |
---|
127 | ); |
---|
128 | |
---|
129 | // Styles table (ctools extras) |
---|
130 | $schema['openlayers_styles'] = array( |
---|
131 | 'description' => 'Storage for user defined OpenLayers styles.', |
---|
132 | 'export' => array( |
---|
133 | 'key' => 'name', |
---|
134 | 'identifier' => 'openlayers_styles', |
---|
135 | 'default hook' => 'openlayers_styles', |
---|
136 | 'api' => array( |
---|
137 | 'owner' => 'openlayers', |
---|
138 | 'api' => 'openlayers_styles', |
---|
139 | 'minimum_version' => 1, |
---|
140 | 'current_version' => 1, |
---|
141 | ), |
---|
142 | ), |
---|
143 | 'fields' => array( |
---|
144 | 'name' => array( |
---|
145 | 'type' => 'varchar', |
---|
146 | 'length' => '255', |
---|
147 | 'not null' => TRUE, |
---|
148 | 'default' => '', |
---|
149 | 'description' => 'Style name.', |
---|
150 | ), |
---|
151 | 'title' => array( |
---|
152 | 'type' => 'varchar', |
---|
153 | 'length' => '255', |
---|
154 | 'not null' => TRUE, |
---|
155 | 'default' => '', |
---|
156 | 'description' => 'Style title.', |
---|
157 | ), |
---|
158 | 'description' => array( |
---|
159 | 'type' => 'text', |
---|
160 | 'not null' => TRUE, |
---|
161 | 'description' => 'Style description.', |
---|
162 | ), |
---|
163 | 'data' => array( |
---|
164 | 'type' => 'text', |
---|
165 | 'not null' => FALSE, |
---|
166 | 'description' => 'Style data serialized.', |
---|
167 | 'serialize' => TRUE, |
---|
168 | ), |
---|
169 | ), |
---|
170 | 'primary key' => array('name'), |
---|
171 | 'indexes' => array( |
---|
172 | 'name' => array('name'), |
---|
173 | ), |
---|
174 | ); |
---|
175 | |
---|
176 | return $schema; |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * Implementation of hook_update_N(). |
---|
181 | */ |
---|
182 | function openlayers_update_6200(&$sandbox) { |
---|
183 | $ret = array(); |
---|
184 | |
---|
185 | // Because we already have two different versions of this |
---|
186 | // module that are not meant to be compatible, we have to |
---|
187 | // check what version we are coming from and do the correct |
---|
188 | // update. Unfortunately Drupal will not tell us what schema |
---|
189 | // we are coming from, so we have set a variable in the 1.x |
---|
190 | // modules. |
---|
191 | $openlayers_version = variable_get('openlayers_schema_version', NULL); |
---|
192 | if (!empty($openlayers_version) && $openlayers_version >= 6100 && $openlayers_version < 6200) { |
---|
193 | // 1.x to 2.x upgrade. |
---|
194 | $ret = _openlayers_upgrade_1xto2x(); |
---|
195 | } |
---|
196 | |
---|
197 | return $ret; |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * Implementation of hook_update_N(). |
---|
202 | */ |
---|
203 | function openlayers_update_6201(&$sandbox) { |
---|
204 | $message = 'Did not alter OpenLayers source.'; |
---|
205 | |
---|
206 | // Due to some changes in the OpenLayers JS in |
---|
207 | // version 2.10, errors crop up, so we will |
---|
208 | // manually set to 2.9. Assume change if |
---|
209 | // not set or set to http://openlayers.org/api/OpenLayers.js |
---|
210 | $current = variable_get('openlayers_source', FALSE); |
---|
211 | if (!$current || $current == 'http://openlayers.org/api/OpenLayers.js') { |
---|
212 | variable_set('openlayers_source', 'http://openlayers.org/api/2.9/OpenLayers.js'); |
---|
213 | $message = 'Set OpenLayers source to hosted 2.9 version.'; |
---|
214 | } |
---|
215 | |
---|
216 | // Add simple note |
---|
217 | $ret[] = array( |
---|
218 | 'success' => TRUE, |
---|
219 | 'query' => $message, |
---|
220 | ); |
---|
221 | |
---|
222 | return $ret; |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | * Function to handle upgrading from 1.x to 2.x |
---|
227 | * |
---|
228 | * @return |
---|
229 | * Valid return array for Drupal update functions |
---|
230 | */ |
---|
231 | function _openlayers_upgrade_1xto2x() { |
---|
232 | $ret = array(); |
---|
233 | |
---|
234 | // Add simple note |
---|
235 | $ret[] = array( |
---|
236 | 'success' => TRUE, |
---|
237 | 'query' => 'Upgrading OpenLayers from 1.x to 2.x', |
---|
238 | ); |
---|
239 | |
---|
240 | // Update preset table |
---|
241 | db_drop_field($ret, 'openlayers_map_presets', 'preset_id'); |
---|
242 | db_change_field($ret, 'openlayers_map_presets', 'preset_name', 'name', |
---|
243 | array('type' => 'varchar', 'length' => 255, 'not null' => TRUE), |
---|
244 | array('primary key' => array('name')) |
---|
245 | ); |
---|
246 | db_change_field($ret, 'openlayers_map_presets', 'preset_title', 'title', |
---|
247 | array('type' => 'varchar', 'length' => 255, 'not null' => TRUE), |
---|
248 | array() |
---|
249 | ); |
---|
250 | db_change_field($ret, 'openlayers_map_presets', 'preset_description', 'description', |
---|
251 | array('type' => 'text', 'not null' => TRUE), |
---|
252 | array() |
---|
253 | ); |
---|
254 | db_change_field($ret, 'openlayers_map_presets', 'preset_data', 'data', |
---|
255 | array('type' => 'text', 'not null' => TRUE), |
---|
256 | array() |
---|
257 | ); |
---|
258 | // Add new tables |
---|
259 | $schema = drupal_get_schema_unprocessed('openlayers'); |
---|
260 | foreach ($schema as $name => $table) { |
---|
261 | if (!db_table_exists($name)) { |
---|
262 | db_create_table($ret, $name, $table); |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | // The only thing we can really do in the upgrade process |
---|
267 | // is to attempt to automate the conversion of the |
---|
268 | // changes in the map preset/array to the new version, |
---|
269 | // and we can only do this for presets in the database, |
---|
270 | // so get all the presets currently in the database. |
---|
271 | $results = db_query("SELECT * FROM {openlayers_map_presets}"); |
---|
272 | while ($row = db_fetch_array($results)) { |
---|
273 | $new_map = unserialize($row['data']); |
---|
274 | |
---|
275 | // Process map parts. |
---|
276 | _openlayers_upgrade_1xto2x_convert_general($new_map, $ret); |
---|
277 | _openlayers_upgrade_1xto2x_convert_layers($new_map, $ret); |
---|
278 | _openlayers_upgrade_1xto2x_convert_behaviors($new_map, $ret); |
---|
279 | _openlayers_upgrade_1xto2x_convert_styles($new_map, $ret); |
---|
280 | |
---|
281 | // Save new Map back into the database. |
---|
282 | $result = db_query("UPDATE {openlayers_map_presets} SET data='%s' WHERE name='%s'", serialize($new_map), $row['name']); |
---|
283 | |
---|
284 | // Debug map array |
---|
285 | // $ret[] = array('success' => TRUE, 'query' => '<pre>' . var_export($new_map, TRUE) . '</pre>'); |
---|
286 | |
---|
287 | // Add some output to user. |
---|
288 | $ret[] = array( |
---|
289 | 'success' => $result, |
---|
290 | 'query' => 'Updated preset: ' . $row['name'] . '. Please ensure to read handbook pages on Drupal.org for manual changes when upgrading.', |
---|
291 | 'rows' => 1, |
---|
292 | ); |
---|
293 | } |
---|
294 | |
---|
295 | return $ret; |
---|
296 | } |
---|
297 | |
---|
298 | /** |
---|
299 | * Converting general map propoerties for the |
---|
300 | * upgrade from 1.x to 2.x |
---|
301 | * |
---|
302 | * @param $map |
---|
303 | * Reference to map array |
---|
304 | * @param $ret |
---|
305 | * Reference to return array |
---|
306 | */ |
---|
307 | function _openlayers_upgrade_1xto2x_convert_general(&$map = array(), &$ret = array()) { |
---|
308 | // Change center |
---|
309 | if (isset($map['center']) && is_array($map['center'])) { |
---|
310 | $map['center'] = array( |
---|
311 | 'initial' => array( |
---|
312 | 'centerpoint' => $map['center']['lat'] . ',' . $map['center']['lon'], |
---|
313 | 'zoom' => $map['center']['zoom'], |
---|
314 | ), |
---|
315 | ); |
---|
316 | } |
---|
317 | |
---|
318 | // Options |
---|
319 | if (isset($map['options']) && is_array($map['options'])) { |
---|
320 | // maxExtent |
---|
321 | // Is this a requirement for 2.x? |
---|
322 | if (isset($map['options']['maxExtent']) && is_array($map['options']['maxExtent'])) { |
---|
323 | $map['options']['maxExtent'] = array( |
---|
324 | $map['options']['maxExtent']['left'], |
---|
325 | $map['options']['maxExtent']['bottom'], |
---|
326 | $map['options']['maxExtent']['right'], |
---|
327 | $map['options']['maxExtent']['top'], |
---|
328 | ); |
---|
329 | } |
---|
330 | } |
---|
331 | } |
---|
332 | |
---|
333 | /** |
---|
334 | * Converting layer arrays for the |
---|
335 | * upgrade from 1.x to 2.x |
---|
336 | * |
---|
337 | * @param $map |
---|
338 | * Reference to map array |
---|
339 | * @param $ret |
---|
340 | * Reference to return array |
---|
341 | */ |
---|
342 | function _openlayers_upgrade_1xto2x_convert_layers(&$map = array(), &$ret = array()) { |
---|
343 | // Array to associate named layers to 2.x |
---|
344 | $layer_convert = array( |
---|
345 | 'openlayers_default_wms', |
---|
346 | 'openlayers_layers_nasa_global_mosaic' => '', |
---|
347 | 'openlayers_layers_nasa_daily_planet' => '', |
---|
348 | 'openlayers_layers_nasa_global_mosaic' => '', |
---|
349 | 'openlayers_layers_nasa_daily_planet' => '', |
---|
350 | 'openlayers_layers_nasa_blue_marble' => '', |
---|
351 | 'openlayers_layers_open_aerial' => '', |
---|
352 | 'openlayers_layers_google_street' => 'google_normal', |
---|
353 | 'openlayers_layers_google_satellite' => 'google_satellite', |
---|
354 | 'openlayers_layers_google_hybrid' => 'google_hybrid', |
---|
355 | 'openlayers_layers_google_physical' => 'google_physical', |
---|
356 | 'openlayers_layers_yahoo_street' => 'yahoo_street', |
---|
357 | 'openlayers_layers_yahoo_satellite' => 'yahoo_satellite', |
---|
358 | 'openlayers_layers_yahoo_hybrid' => 'yahoo_hybrid', |
---|
359 | 'openlayers_layers_virtual_earth_street' => 'virtualearth_street', |
---|
360 | 'openlayers_layers_virtual_earth_satellite' => 'virtualearth_satellite', |
---|
361 | 'openlayers_layers_virtual_earth_hybrid' => 'virtualearth_hybrid', |
---|
362 | 'openlayers_layers_osm_mapnik' => 'osm_mapnik', |
---|
363 | 'openlayers_layers_osm_tah' => 'osm_tah', |
---|
364 | 'openlayers_layers_osm_cycle' => 'osm_cycle', |
---|
365 | 'openlayers_layers_osm_4326_hybrid' => 'osm_4326_hybrid', |
---|
366 | 'openlayers_layers_cloudmade' => '', |
---|
367 | 'openlayers_default_wms' => 'wms_default', |
---|
368 | ); |
---|
369 | |
---|
370 | // Array to associate layer types to 2.x |
---|
371 | $layer_type_convert = array( |
---|
372 | 'WMS' => 'openlayers_layer_type_wms', |
---|
373 | 'Vector' => '', // ?? |
---|
374 | 'KML' => 'openlayers_layer_type_kml', |
---|
375 | 'XYZ' => 'openlayers_layer_type_xyz', |
---|
376 | 'Google' => 'openlayers_layer_type_google', |
---|
377 | 'VirtualEarth' => 'openlayers_layer_type_virtualearth', |
---|
378 | 'Yahoo' => 'openlayers_layer_type_yahoo', |
---|
379 | 'Cloudmade' => 'openlayers_layer_type_cloudmade', |
---|
380 | ); |
---|
381 | |
---|
382 | // Check that there are layers |
---|
383 | if (isset($map['layers']) && is_array($map['layers'])) { |
---|
384 | // Go through layers |
---|
385 | foreach ($map['layers'] as $id => $layer) { |
---|
386 | // Convert named layers |
---|
387 | if (is_string($layer)) { |
---|
388 | if (!empty($layer_convert[$layer])) { |
---|
389 | unset($map['layers'][$id]); |
---|
390 | $map['layers'][$layer_convert[$layer]] = $layer_convert[$layer]; |
---|
391 | } |
---|
392 | // What to do with named layers that dont translate ?? |
---|
393 | |
---|
394 | } |
---|
395 | elseif (is_array($layer)) { |
---|
396 | // Save new layer in DB, then map to preset |
---|
397 | |
---|
398 | |
---|
399 | } |
---|
400 | } |
---|
401 | } |
---|
402 | |
---|
403 | // Check default layer |
---|
404 | if (!empty($map['default_layer']) && is_string($map['default_layer']) && !empty($layer_convert[$map['default_layer']])) { |
---|
405 | $map['default_layer'] = $layer_convert[$map['default_layer']]; |
---|
406 | // Ensure that layer is in layer array |
---|
407 | if (empty($map['layers'][$map['default_layer']])) { |
---|
408 | $map['layers'][$map['default_layer']] = $map['default_layer']; |
---|
409 | } |
---|
410 | } |
---|
411 | } |
---|
412 | |
---|
413 | /** |
---|
414 | * Converting behaviors for the |
---|
415 | * upgrade from 1.x to 2.x |
---|
416 | * |
---|
417 | * @param $map |
---|
418 | * Reference to map array |
---|
419 | * @param $ret |
---|
420 | * Reference to return array |
---|
421 | */ |
---|
422 | function _openlayers_upgrade_1xto2x_convert_behaviors(&$map = array(), &$ret = array()) { |
---|
423 | // Control behavior conversion array |
---|
424 | $control_convert = array( |
---|
425 | 'LayerSwitcher' => 'openlayers_behavior_layerswitcher', |
---|
426 | 'Navigation' => 'openlayers_behavior_navigation', |
---|
427 | 'PanZoomBar' => 'openlayers_behavior_panzoombar', |
---|
428 | 'MousePosition' => 'openlayers_behavior_mouseposition', |
---|
429 | 'Attribution' => 'openlayers_behavior_attribution', |
---|
430 | 'KeyboardDefaults' => 'openlayers_behavior_keyboarddefaults', |
---|
431 | 'Permalink' => 'openlayers_behavior_permalink', |
---|
432 | 'ScaleLine' => 'openlayers_behavior_scaleline', |
---|
433 | 'ZoomBox' => 'openlayers_behavior_zoombox', |
---|
434 | 'ZoomToMaxExtent' => 'openlayers_behavior_zoomtomaxextent', |
---|
435 | ); |
---|
436 | |
---|
437 | // Convert controls |
---|
438 | if (isset($map['controls']) && is_array($map['controls'])) { |
---|
439 | foreach ($map['controls'] as $control => $enabled) { |
---|
440 | if ($enabled && !empty($control_convert[$control])) { |
---|
441 | $map['behaviors'][$control_convert[$control]] = array(); |
---|
442 | } |
---|
443 | |
---|
444 | // Get rid of controls array |
---|
445 | unset($map['controls']); |
---|
446 | } |
---|
447 | } |
---|
448 | |
---|
449 | // Go through behaviors |
---|
450 | if (isset($map['behaviors']) && is_array($map['behaviors'])) { |
---|
451 | foreach ($map['behaviors'] as $id => $behavior) { |
---|
452 | // Full screen |
---|
453 | if ($behavior['type'] == 'openlayers_behaviors_fullscreen') { |
---|
454 | $map['behaviors']['openlayers_behavior_fullscreen'] = array(); |
---|
455 | unset($map['behaviors'][$id]); |
---|
456 | } |
---|
457 | |
---|
458 | // Zoom to Layer |
---|
459 | if ($behavior['type'] == 'openlayers_behaviors_zoom_to_layer' && !empty($behavior['layer'])) { |
---|
460 | $map['behaviors']['openlayers_behaviors_zoomtolayer'] = array( |
---|
461 | 'zoomtolayer' => $behavior['layer'], |
---|
462 | ); |
---|
463 | unset($map['beahviors'][$id]); |
---|
464 | } |
---|
465 | |
---|
466 | // Cluster |
---|
467 | // Zoom to Feature |
---|
468 | // Tooltip |
---|
469 | // Popup |
---|
470 | // Draw Features |
---|
471 | // Declutter |
---|
472 | |
---|
473 | } |
---|
474 | } |
---|
475 | } |
---|
476 | |
---|
477 | /** |
---|
478 | * Converting styles for the |
---|
479 | * upgrade from 1.x to 2.x |
---|
480 | * |
---|
481 | * @param $map |
---|
482 | * Reference to map array |
---|
483 | * @param $ret |
---|
484 | * Reference to return array |
---|
485 | */ |
---|
486 | function _openlayers_upgrade_1xto2x_convert_styles(&$map = array(), &$ret = array()) { |
---|
487 | // Go through styles |
---|
488 | if (isset($map['styles']) && is_array($map['style'])) { |
---|
489 | foreach ($map['styles'] as $id => $style) { |
---|
490 | // Named styles are the same |
---|
491 | |
---|
492 | // Convert style arrays and store in DB |
---|
493 | if (is_array($style)) { |
---|
494 | // Create new object |
---|
495 | $new_style = new stdClass(); |
---|
496 | $new_style->api_version = 1; |
---|
497 | $new_style->name = $id; |
---|
498 | $new_style->title = ucwords(str_replace('_', ' ', $id)); |
---|
499 | $new_style->description = ucwords(str_replace('_', ' ', $id)); |
---|
500 | $new_style->data = $style; |
---|
501 | // Save new style object |
---|
502 | $success = drupal_write_record('openlayers_styles', $style); |
---|
503 | $ret[] = array( |
---|
504 | 'success' => ($success) ? TRUE : FALSE, |
---|
505 | 'query' => 'Attempt to save style: ' . $id, |
---|
506 | ); |
---|
507 | // Now put back in map array |
---|
508 | if ($success) { |
---|
509 | $map['style'][$id] = $id; |
---|
510 | } |
---|
511 | } |
---|
512 | } |
---|
513 | } |
---|
514 | } |
---|