-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathec_mapeditor.module
More file actions
executable file
·674 lines (616 loc) · 18.7 KB
/
ec_mapeditor.module
File metadata and controls
executable file
·674 lines (616 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
<?php
/**
* @file
* Provides functionality for the map entity.
*/
// Includes code with function names that do not comply with standards.
module_load_include('inc', 'ec_mapeditor', 'ec_mapeditor.misc');
module_load_include('inc', 'ec_mapeditor', 'ec_mapeditor.forms');
/**
* Implements hook_entity_info().
*/
function ec_mapeditor_entity_info() {
$return = array(
'map' => array(
'label' => t('Map'),
'entity class' => 'EcMapeditorMap',
'controller class' => 'EcMapeditorController',
'base table' => 'map',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'mid',
),
'admin ui' => array(
'path' => 'admin/structure/maps',
'controller class' => 'EcMapeditorMapUIController',
'menu wildcard' => '%map',
'file' => 'ec_mapeditor.admin.inc',
),
'bundles' => array(
'map' => array(
'label' => t('Map'),
'admin' => array(
'path' => 'admin/structure/maps',
'access arguments' => array('administer maps'),
),
),
),
'load hook' => 'ec_mapeditor_load',
'view modes' => array(
'full' => array(
'label' => t('Default'),
'custom settings' => FALSE,
),
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'module' => 'ec_mapeditor',
'access callback' => 'ec_mapeditor_access',
),
);
return $return;
}
/**
* Implements hook_menu().
*/
function ec_mapeditor_menu() {
$items = array();
$items['map/add'] = array(
'title' => 'Add map',
'page callback' => 'ec_mapeditor_add_map',
'access callback' => 'entity_access',
'access arguments' => array('create', 'map', 'map'),
'file' => 'ec_mapeditor.admin.inc',
'type' => MENU_LOCAL_ACTION,
'tab_parent' => 'map',
'tab_root' => 'map',
);
$map_uri = 'map/%ec_mapeditor';
$map_uri_argument_position = 1;
$items[$map_uri] = array(
'title callback' => 'entity_label',
'title arguments' => array('map', $map_uri_argument_position),
'page callback' => '_ec_mapeditor_view',
'page arguments' => array($map_uri_argument_position),
'access callback' => 'entity_access',
'access arguments' => array('view', 'map', $map_uri_argument_position),
'file' => 'ec_mapeditor.pages.inc',
);
$items[$map_uri . '/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[$map_uri . '/delete'] = array(
'title' => 'Delete map',
'title callback' => 'ec_mapeditor_label',
'title arguments' => array($map_uri_argument_position),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ec_mapeditor_delete_form',
$map_uri_argument_position,
),
'access callback' => 'entity_access',
'access arguments' => array('edit', 'map', $map_uri_argument_position),
'file' => 'ec_mapeditor.admin.inc',
);
$items[$map_uri . '/edit'] = array(
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array('map_form', $map_uri_argument_position),
'access callback' => 'entity_access',
'access arguments' => array('edit', 'map', $map_uri_argument_position),
'file' => 'ec_mapeditor.admin.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
$items['ec_mapeditor/webtools'] = array(
'title' => 'Webtools example',
'page callback' => '_ec_mapeditor_webtools',
'access arguments' => array('access content'),
'type' => MENU_SUGGESTED_ITEM,
'file' => 'ec_mapeditor.pages.inc',
);
return $items;
}
/**
* Implements hook_menu_alter().
*/
function ec_mapeditor_menu_alter(&$items) {
// Map Layer Types should not be created nor managed by the user.
if (isset($items['admin/structure/map-layer-types'])) {
$items['admin/structure/map-layer-types']['access callback'] = FALSE;
}
}
/**
* Implements hook_permission().
*/
function ec_mapeditor_permission() {
$permissions = array(
'create ec_mapeditor entities' => array(
'title' => t('Create maps'),
'description' => t('Allows users to create maps.'),
'restrict access' => TRUE,
),
'view ec_mapeditor entities' => array(
'title' => t('View maps'),
'description' => t('Allows users to view maps.'),
'restrict access' => TRUE,
),
'edit any ec_mapeditor entities' => array(
'title' => t('Edit any maps'),
'description' => t('Allows users to edit any maps.'),
'restrict access' => TRUE,
),
'edit own ec_mapeditor entities' => array(
'title' => t('Edit own maps'),
'description' => t('Allows users to edit own maps.'),
'restrict access' => TRUE,
),
);
return $permissions;
}
/**
* Implements hook_entity_property_info_alter().
*/
function ec_mapeditor_entity_property_info_alter(&$info) {
$properties = &$info['map']['properties'];
$properties['created'] = array(
'label' => t("Date created"),
'type' => 'date',
'description' => t("The date the map was posted."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer nodes',
'schema field' => 'created',
);
$properties['changed'] = array(
'label' => t("Date changed"),
'type' => 'date',
'schema field' => 'changed',
'description' => t("The date the map was most recently updated."),
);
$properties['uid'] = array(
'label' => t("Author"),
'type' => 'user',
'description' => t("The author of the map."),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer ec_mapeditor entities',
'required' => TRUE,
'schema field' => 'uid',
);
}
/**
* Access callback for map.
*
* @param string $op
* Operation being performed.
* @param object $map
* Map entity object.
* @param string $account
* The account to check.
*
* @return bool
* Boolean TRUE if user has requested permission.
*/
function ec_mapeditor_access($op, $map, $account = NULL, $entity_type = NULL) {
global $user;
if (!isset($account)) {
$account = $user;
}
switch ($op) {
case 'create':
return user_access('administer ec_mapeditor entities', $account)
|| user_access('create ec_mapeditor entities', $account);
case 'view':
return user_access('administer ec_mapeditor entities', $account)
|| user_access('view ec_mapeditor entities', $account);
case 'edit':
return user_access('administer ec_mapeditor entities')
|| user_access('edit any ec_mapeditor entities')
|| (user_access('edit own ec_mapeditor entities') && ($map->uid == $account->uid));
}
}
/**
* Loads a map.
*
* @param int $mid
* The map entity ID.
* @param bool $reset
* Reset the internal cache for the requested entity type.
*
* @return array
* The firs map entity object stored in the Array returned by entity_load().
*/
function ec_mapeditor_load($mid, $reset = FALSE) {
$maps = ec_mapeditor_load_multiple(array($mid), array(), $reset);
return reset($maps);
}
/**
* Loads multiple maps based on certain conditions.
*
* @param array $mids
* Array with map entity IDs.
* @param array $conditions
* Associative Array of conditions on the base table.
* @param bool $reset
* Reset the internal cache for the requested entity type.
*
* @return array
* Array of entity objects indexed by their ids (see entity_load()).
*/
function ec_mapeditor_load_multiple($mids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('map', $mids, $conditions, $reset);
}
/**
* Deletes single map.
*
* @param object $map
* The map entity object to delete.
*/
function _ec_mapeditor_delete_map($map) {
entity_delete('map', entity_id('map', $map));
}
/**
* Deletes multiple maps.
*
* @param object $map_ids
* IDs of the map entities to delete.
*/
function ec_mapeditor_delete_map_multiple($map_ids) {
entity_delete_multiple('map', $map_ids);
}
/**
* Implements hook_map_form_elements().
*
* Defines custom map form elements. These elements are added to the map and
* map layers forms via hook_layer_info().
*/
function ec_mapeditor_map_form_elements() {
// Defines zoom options.
$zoom_options = array(
0 => t('0 - Low/Far'),
18 => t('18 - High/Close'),
);
for ($i = 1; $i < 18; $i++) {
$zoom_options[$i] = $i;
}
ksort($zoom_options);
// Defines form elements.
$form_elements = array(
// Defines form elements to control zooming.
'zoom' => array(
'#type' => 'fieldset',
'#title' => t('Zooming'),
'#weight' => 12,
'initialZoom' => array(
'#title' => t('Initial zoom level'),
'#description' => t('The starting zoom level when this map is rendered. Restricted by min and max zoom settings.'),
'#type' => 'select',
'#options' => $zoom_options,
),
'minZoom' => array(
'#title' => t('Minimum zoom level'),
'#description' => t('The minimum zoom level allowed. (How far away can you view from?)'),
'#type' => 'select',
'#options' => $zoom_options,
),
'maxZoom' => array(
'#title' => t('Maximum zoom level'),
'#description' => t('The maximum zoom level allowed. (How close in can you get?).'),
'#type' => 'select',
'#options' => $zoom_options,
),
),
// Defines form elements to choose background map images (tiles).
'tiles' => array(
'#type' => 'fieldset',
'#title' => t('Map background'),
'#weight' => 16,
'tiles' => array(
'#type' => 'select',
'#title' => t('Tiles'),
'#description' => t('Tiles to use a map background'),
'#options' => _ec_mapeditor_tiles(),
),
),
// Defines form elements to choose background map images (tiles).
'fields_in_popup' => array(
'#type' => 'fieldset',
'#title' => t('Single Node Popup'),
'#weight' => 30,
'fields_in_popup' => array(
'#type' => 'textfield',
'#size' => 80,
'#placeholder' => 'title, field_description, field_coordinates',
'#title' => t('Fields in Popup'),
'#description' => t('Fields to show within the Popup window on single node page. <br />
Use CSV format. Example: title, field_description, field_address, ... '),
),
),
// Defines form elements to set map height.
'height' => array(
'#type' => 'container',
'#weight' => 6,
'height' => array(
'#title' => t('Map height'),
'#type' => 'textfield',
'#field_suffix' => t('px'),
'#size' => 4,
'#required' => FALSE,
'#element_validate' => array('ec_mapeditor_validate_height'),
'#suffix' => "<div id='min-height-label' class='description'></div>",
'#attached' => array(
'js' => array(
array(
'type' => 'file',
'data' => drupal_get_path('module', 'ec_mapeditor') . '/js/forms.js',
),
array(
'data' => array(
'map_height_form' => array(
'min_height' => 300,
'label' => t('To ensure the map menu displays correctly, it is
recommended to choose a height higher than 300px.'),
'warning' => t('<strong>Attention. The entered height is lower than
recommended</strong>.'),
),
),
'type' => 'setting',
),
),
),
),
),
// Defines form elements to control map center.
'center' => array(
'#type' => 'fieldset',
'#title' => t('Map center'),
'#description' => t('Center of the map. E.g. latitude 50.84 and 4.36 longitude for Brussels'),
'#weight' => 8,
'lat' => array(
'#title' => t('Latitude'),
'#type' => 'textfield',
'#size' => 10,
'#required' => FALSE,
),
'lon' => array(
'#title' => t('Longitude'),
'#type' => 'textfield',
'#size' => 10,
'#required' => FALSE,
),
'fitbounds' => array(
'#type' => 'checkbox',
'#title' => t('Fit map to visible markers'),
'#description' => t('This sets the center of the map automatically based on the visible markers. It ignores the map center coordinates set above.'),
),
),
// Defines form element to control disclaimer.
'disclaimer' => array(
'#type' => 'fieldset',
'#title' => t('Disclaimer'),
'#weight' => 17,
'disclaimerControl' => array(
'#type' => 'checkbox',
'#title' => t('HIde disclaimer'),
),
),
);
return $form_elements;
}
/**
* Provides available tiles.
*
* @return array
* A list of tile names that can be used in a form select element.
*/
function _ec_mapeditor_tiles() {
return array(
'osmec' => 'Open Street Map customised for European Commission',
'graybg' => 'Gray background with country outlines',
'coast' => 'Gray background with continent outlines',
'gray' => 'Gray shaded relief of earth',
'hypso' => 'Climate shaded relief of earth',
'natural' => 'Landcover shaded relief of earth',
'bmarble' => 'Satellite images of earth',
'copernicus003' => 'Copernicus Core003 mosaic',
'countrynames_europe' => 'Country names Europe',
'citynames_europe' => 'City names Europe',
'roadswater_europe' => 'Roads and waterways Europe',
'countryboundaries_world' => 'Country boundaries world',
);
}
/**
* Validates the height form element.
*/
function ec_mapeditor_validate_height(&$form, &$form_state) {
if (isset($form_state['values']['height'])) {
if (!is_numeric($form_state['values']['height']) || $form_state['values']['height'] < 0) {
form_error($form['height'], t('Map height needs to be a positive number'));
}
}
}
/**
* Validate zoom values.
*/
function _ec_mapeditor_validate_zoom($form, &$form_state) {
$element = "";
$max_zoom = isset($form_state['values']['max_zoom'][LANGUAGE_NONE]) ? $form_state['values']['max_zoom'][LANGUAGE_NONE][0]['value'] : NULL;
$min_zoom = isset($form_state['values']['min_zoom'][LANGUAGE_NONE]) ? $form_state['values']['min_zoom'][LANGUAGE_NONE][0]['value'] : NULL;
if (isset($max_zoom) && $max_zoom != "-1" && isset($min_zoom)) {
if ($max_zoom > $min_zoom) {
form_error($element, t('The minimum zoom setting must be less than or equal to the maximum zoom setting'));
}
}
}
/**
* Implementation of hook_ctools_plugin_directory() to let the system know
* we implement plugins.
*/
function ec_mapeditor_ctools_plugin_directory($module, $plugin) {
return 'plugins/' . $plugin;
}
/**
* Implements hook_ctools_plugin_type
*/
function ec_mapeditor_ctools_plugin_type() {
return array(
'geocoder_handler' => array(
'cache' => TRUE,
),
);
}
/**
* Saves map entity.
*
* Collects map values for storing as JSON in settings.
*
* @param object $map
* Map object before saving.
*/
function _ec_mapeditor_save_map($map) {
// Fetches field values from form state.
$settings = array(
'tiles' => array(
'tiles' => $map->tiles,
),
'height' => array(
'height' => $map->height,
),
'center' => array(
'lat' => $map->lat,
'lon' => $map->lon,
'fitbounds' => $map->fitbounds,
),
'zoom' => array(
'initialZoom' => $map->initialZoom,
'minZoom' => $map->minZoom,
'maxZoom' => $map->maxZoom,
),
'show_disclaimer' => $map->show_disclaimer,
'show_author' => $map->show_author,
'show_settings' => $map->show_settings,
);
$map->settings = drupal_json_encode($settings);
$map->changed = REQUEST_TIME;
entity_save('map', $map);
}
/**
* Class providing JavaScript settings for a map.
*/
class EcMapeditorMapSettings {
/**
* Provides the settings array and default values.
*/
public function __construct() {
$this->settings = array(
'center' => array(
'lat' => 55.106,
'lon' => 28.154,
'fitbounds' => TRUE,
'fitbounds_padding' => variable_get('ec_mapeditor_fitbounds_padding', 30),
),
'tiles' => array(
'tiles' => 'osmec',
),
'dragging' => TRUE,
'touchZoom' => TRUE,
'scrollWheelZoom' => TRUE,
'zoom' => array(
'initialZoom' => 4,
'minZoom' => 2,
'maxZoom' => 10,
),
'height' => array(
'height' => 430,
),
'icon' => array(
'icon' => 'blue',
),
'show_disclaimer' => FALSE,
'show_author' => FALSE,
'show_settings' => FALSE,
'fields_in_popup' => NULL,
);
}
/**
* Changes the default settings.
*/
public function customize($settings) {
foreach ($settings as $key => $setting) {
$this->settings[$key] = $setting;
}
}
/**
* Removes a setting.
*/
public function remove($settings) {
foreach ($settings as $key => $setting) {
unset($this->settings[$key]);
}
}
}
/**
* Implements hook_views_api().
*/
function ec_mapeditor_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'ec_mapeditor'),
);
}
/**
* Implements hook_admin_paths().
*/
function ec_mapeditor_admin_paths() {
if (variable_get('node_admin_theme')) {
$paths = array(
'map/*/edit' => TRUE,
'map/add' => TRUE,
'map/*/delete' => TRUE,
);
return $paths;
}
}
/**
* Implements hook_feature_set_api().
*/
function ec_mapeditor_feature_set_api() {
return array('version' => '1.0');
}
/**
* Provides custom form elements.
*
* Cycles through all the form elements defined by all modules and fetches the
* elements for one particular module. Assigns also the default form element
* value by fetching values from the settings.
*
* @param array $elements
* List of form elements keys that must be fetched from the list of all form
* elements.
* @param array $settings
* List of form elements values that were stored when the map layer was
* saved and that shoudl be used as the default value.
*
* @return array $form
* List of custom for elements that can be added to the map layer form.
*/
function _ec_mapeditor_custom_map_form_elements($elements, $settings) {
$form = array();
// Fetches all custom form elements defined by all modules.
$form_elements = module_invoke_all('map_form_elements');
// Fetches desired containers and fieldsets.
foreach ($elements as $element) {
$form[$element] = $form_elements[$element];
// Cycles through all the sub elements of containers.
foreach (element_children($form_elements[$element]) as $key) {
// Adds sub elements (fields, select lists etc.) to form.
$form[$element][$key] = $form_elements[$element][$key];
// Fetches default value for sub elements from settings.
if (isset($settings[$element][$key])) {
$form[$element][$key]['#default_value'] = $settings[$element][$key];
}
}
}
return $form;
}