-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathec_mapeditor.controller.inc
More file actions
211 lines (182 loc) · 5.73 KB
/
ec_mapeditor.controller.inc
File metadata and controls
211 lines (182 loc) · 5.73 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
<?php
/**
* @file
* Provides functionality for the map entity.
*/
/**
* Provides map entity properties and content.
*/
class EcMapeditorController extends EntityAPIController {
/**
* Creates map entity and adds common properties such as author.
*/
public function create(array $values = array()) {
global $user;
$values += array(
'title' => '',
'description' => '',
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
'uid' => $user->uid,
);
return parent::create($values);
}
/**
* Builds the content for displaying the map.
*/
public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {
$wrapper = entity_metadata_wrapper('map', $entity);
// Sets defaults for theming properties like fields.
$field_defaults = array(
'#theme' => 'field',
'#access' => TRUE,
'#label_display' => 'hidden',
'#view_mode' => 'default',
'#language' => LANGUAGE_NONE,
'#field_type' => 'text',
'#entity_type' => 'map_layer',
'#bundle' => $entity->type,
);
// Themes description like default field.
$content['description'] = array(
'#weight' => 0,
'#field_name' => 'field_fake_description',
'#title' => t('Description'),
'#items' => array(array('value' => $entity->description)),
'#formatter' => 'text_default',
0 => array(
'#markup' => check_plain($entity->description),
),
) + $field_defaults;
$settings = new EcMapeditorMapSettings();
$settings->customize(drupal_json_decode($wrapper->settings->value()));
// @todo prevent this.
$settings = $settings->settings;
if ($settings['show_author']) {
$content['author'] = array('#markup' => t('Created by: !author', array('!author' => $wrapper->uid->name->value(array('sanitize' => TRUE)))));
}
// Creates the render array for the map.
$map = new EcMapeditorMapBuild($settings);
// Adds custom JavaScripts defined by the map layers.
// @todo figure out how to pass only the active map layer's js.
$custom_js = array();
$map_layers = module_invoke_all('layer_info');
foreach ($map_layers as $map_layer) {
$custom_js[] = $map_layer['custom_js'];
}
// Adds the map layer JavaScript as first (needs to be listed before any
// other custom scripts).
// @todo find decent solution.
if (module_exists('ec_mapeditor_layer')) {
$map_layer_js = base_path() . drupal_get_path('module', 'ec_mapeditor_layer') . "/js/map_layer.js?v=" . rand(0, 33333);
$custom_js[] = $map_layer_js;
}
$ec_mapeditor_js = base_path() . drupal_get_path('module', 'ec_mapeditor') . "/js/map.js?v=" . rand(0, 33333);
$custom_js[] = $ec_mapeditor_js;
$map->setMoreCustomJs($custom_js);
$content['map'] = $map->build;
return parent::buildContent($entity, $view_mode, $langcode, $content);
}
}
/**
* Extends entity for map.
*/
class EcMapeditorMap extends Entity {
/**
* Provides default label.
*/
protected function defaultLabel() {
return $this->title;
}
/**
* Provides uri to view the stand alone map.
*/
protected function defaultUri() {
return array('path' => 'map/' . $this->identifier());
}
}
/**
* UI controller for map layer type.
*/
class EcMapeditorMapUIController extends EntityDefaultUIController {
// @codingStandardsIgnoreStart
/**
* Overrides hook_menu() defaults.
*/
public function hook_menu() {
$items = parent::hook_menu();
$items[$this->path]['description'] = 'Manage maps.';
return $items;
}
// @codingStandardsIgnoreEnd
}
/**
* Provides map render array.
*
* This class constructs the build Drupal render array with the necessary
* JavaScript and markup for the EC map service.
*/
class EcMapeditorMapBuild {
// Holds filename of custom JavaScript to be passed to load.js.
private $customJs;
/**
* Defines defaults for the map construction (both geofield and NUTS).
*/
public function __construct($settings, $map = NULL) {
$this->build = array();
// Prepares general map definitions and settings for JavaScript usage.
// @todo fix different usages of height.
$this->build['#attached']['js'][] = array(
'data' => array(
'settings' => $settings,
'mapeditor_map' => $map,
),
'type' => 'setting',
);
}
/**
* Sets filename of custom JavaScript to be passed to load.js.
*
* @todo. synch with setMoreCustomJs. Check if still in use.
*/
public function setCustomJs($custom_js) {
$this->customJs = $custom_js;
// Defines which custom JavaScript file to provide to load.js.
$js_path = base_path() . drupal_get_path('module', 'ec_mapeditor') . "/js/$this->customJs?v=" . rand(0, 33333);
$options = drupal_json_encode(array(
'service' => 'map',
'custom' => $js_path,
));
$this->build['load_js_vars'] = array(
'#type' => 'markup',
'#markup' => "<script type='application/json'>${options}</script>",
);
}
/**
* Sets filename of custom JavaScript to be passed to load.js.
*
* @todo. synch with setCustomJS.
*/
public function setMoreCustomJs($custom_js) {
// Defines which custom JavaScript file to provide to load.js.
$options = drupal_json_encode(array(
'service' => 'map',
'custom' => $custom_js,
));
$this->build['load_js_vars'] = array(
'#type' => 'markup',
'#markup' => "<script type='application/json'>${options}</script>",
);
}
/**
* Adds map features as JavaScript attachment to the build array.
*/
public function setFeatures($features) {
$this->build['#attached']['js'][] = array(
'data' => array(
'features' => $features,
),
'type' => 'setting',
);
}
}