-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_module.module
437 lines (338 loc) · 11.9 KB
/
custom_module.module
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
<?php
/**
* @file
*
*/
/**
* Implements hook_help().
*/
function custom_module_help($path, $arg) {
switch ($path) {
case 'admin/help#custom_module':{
$ret_val = '<h3>' . t('About') . '</h3>';
$ret_val .= '<p>' . t('Custom Module.') . '</p>';
return $ret_val;
break;
}
}
}
/**
* Implements hook_permission().
*/
function custom_module_permission() {
return array(
'Custom Module Drupal 7' => array(
'title' => t('Custom Module'),
'description' => t('Custom Module in Drupal 7.'),
),
);
}
/**
* Implements hook_menu().
*/
function custom_module_menu() {
$items = array();
// Admin configuration group.
$items['admin/config/custom_module'] = array(
'title' => 'Custom Module',
'description' => 'Administer Custom Module in Drupal.',
'access arguments' => array('administer Custom Module'),
);
// Admin configuration - Settings.
$items['admin/config/custom_module/manage'] = array(
'title' => 'Custom Module settings',
'description' => 'Manage Custom Module settings and configurations.',
'access arguments' => array('administer Custom Module'),
'page callback' => 'drupal_get_form',
'page arguments' => array('custom_module_admin_settings_form'),
);
return $items;
}
/**
* Implements hook_form().
*/
function custom_module_admin_settings_form($node, &$form_state) {
$form = array();
$form['overview'] = array(
'#markup' => t('This interface allows administrators to manage general Custom Module Settings'),
'#prefix' => '<p>',
'#suffix' => '</p>',
);
$form['custom_module_gmap'] = array(
'#title' => t('Enable Google Maps'),
'#description' => t('When enabled, Google Maps will be rendered if latitude and longitude are known.'),
'#type' => 'checkbox',
'#default_value' => '#default_value' => variable_get('custom_module_gmap'),
);
$form['default_center'] = array(
'#title' => t('Map Center'),
'#description' => t('Location of the center of the map of Custom Module.'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['default_center']['custom_module_default_center_lat'] = array(
'#title' => t('Latitude'),
'#description' => t('Signed degrees format (DDD.dddd)'),
'#type' => 'textfield',
'#default_value' => variable_get('custom_module_default_center_lat'),
'#required' => TRUE,
);
$form['default_center']['custom_module_default_center_long'] = array(
'#title' => t('Longitude'),
'#description' => t('Signed degrees format (DDD.dddd)'),
'#type' => 'textfield',
'#default_value' => variable_get('custom_module_default_center_long'),
'#required' => TRUE,
);
$options = range(0, 20, 1);
$options[0] = t('0 - Furthest');
$options[20] = t('20 - Closest');
$form['custom_module_default_gmap_zoom'] = array(
'#title' => t('Google Map zoom'),
'#description' => t('Default level of zoom, between 0 and 20.'),
'#type' => 'select',
'#options' => $options,
'#default_value' => variable_get('custom_module_default_gmap_zoom'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
//return system_settings_form($form);
return $form;
}
/**
* Validates Custom Module admin settings.
*/
function custom_module_admin_settings_form_validate($form, &$form_state) {
//dpm($form_state['values']);
// Regular expression for validating signed degrees.
//$signed_degree_regex = '/^[+-]?\d+(\.\d+)?$/';
// Shorthand for long array names.
//$lat = $form_state['values']['custom_module_default_center_lat'];
//$long = $form_state['values']['custom_module_default_center_long'];
// Validate latitude and longitude format.
/* if (!preg_match($signed_degree_regex, $lat)) {
form_set_error('custom_module_default_center_lat', t('Invalid latitude; must be a signed degree (DDD.dddd).'));
}
if (!preg_match($signed_degree_regex, $long)) {
form_set_error('custom_module_default_center_long', t('Invalid longitude; must be a signed degree (DDD.dddd).'));
}*/
// Validate latitude and longitude values.
/* if (!((-180 <= $lat) && ($lat <= 180))) {
form_set_error('custom_module_default_center_lat', t('Latitude must be between -180 and 180'));
}
if (!((-180 <= $long) && ($long <= 180))) {
form_set_error('custom_module_default_center_long', t('Longitude must be between -180 and 180'));
}
*/
// Validate Unit Count.
$value = $form_state['values']['custom_module_unit_count'][LANGUAGE_NONE][0]['value'];
if ($value !== '' && (!is_numeric($value) || intval($value) != $value || $value <= 0)) {
form_set_error('custom_modulem_unit_count', t('The number of units on a custom_module must be a positive whole number.'));
}
// Validate latitude.
$lat = $form_state['values']['custom_module_latitude'][LANGUAGE_NONE][0]['value'];
if (!_custom_module_validate_geo_coord_range($lat) || !_custom_module_validate_signed_degrees($lat)) {
form_set_error('custom_module_latitude', t('Latitude must be valid coordinates in signed degrees.'));
}
// Validate longitude.
$long = $form_state['values']['custom_module_longitude'][LANGUAGE_NONE][0]['value'];
if (!_custom_module_validate_geo_coord_range($long) || !_custom_module_validate_signed_degrees($long)) {
form_set_error('custom_module_longitude', t('Longitude must be valid coordinates in signed degrees.'));
}
}
/**
* Determine if a value is in signed degrees format.
*
* @param string $value
* The value to be tested.
*
* @return boolean
* TRUE if correctly formatted.
*/
function _custom_module_validate_signed_degrees($value) {
$signed_degree_regex = '/^[+-]?\d+(\.\d+)?$/';
return preg_match($signed_degree_regex, $value) ? TRUE : FALSE;
}
/**
* Determine if a geographic coordinate is within the valid range.
*
* @param string $value
* The value to be tested.
*
* @return boolean
* TRUE if between -180 and 180.
*/
function _custom_module_validate_geo_coord_range($value) {
return ((-180 <= $value) && ($value <= 180));
}
/**
* Process a validated Custom Module admin setting submission.
*/
function custom_module_admin_settings_form_submit($form, &$form_state) {
// Rebuild the form.
$form_state['rebuild'] = TRUE;
// Save Custom Module setting variables.
variable_set('custom_module_gmap', $form_state['values']['custom_module_gmap']);
variable_set('custom_module_default_center_lat', $form_state['values']['custom_module_default_center_lat']);
variable_set('custom_module_default_center_long', $form_state['values']['custom_module_default_center_long']);
variable_set('custom_module_default_gmap_zoom', $form_state['values']['custom_module_default_gmap_zoom']);
// Notify user.
drupal_set_message(t('Custom Module settings saved.'));
}
/**
* Implements hook_node_view().
*/
function custom_module_node_view($node, $view_mode, $langcode) {
if ($node->type == 'custom_module' && $view_mode == 'full') {
$node->content['custom_module_gmap'] = array(
'#markup' => theme('custom_module_gmap', array(
'node' => $node,
)),
'#weight' => 100,
);
}
}
/**
* Implements hook_theme().
*/
function custom_module_theme($existing, $type, $theme, $path) {
return array(
'custom_module_gmap' => array(
'variables' => array('node' => NULL),
),
);
}
/**
* Custom Module Google Map theme function.
*/
function theme_custom_module_gmap($variables) {
//dpm ($variables)
// Check to see if the Google Map is enabled.
if (!variable_get('custom_module_gmap')) {
return;
}
$lat = $variables['node']->custom_module_latitude[LANGUAGE_NONE][0]['value'];
$long = $variables['node']->custom_module_longitude[LANGUAGE_NONE][0]['value'];
// Cannot render map without both a latitude and longitude.
if (($lat == '') || ($long == '')) {
return;
}
module_load_include('inc', 'gmap3_tools');
$facility_name = check_plain($variables['node']->title);
$description = check_plain($variables['node']->body[LANGUAGE_NONE][0]['value']);
gmap3_tools_add_map(array(
'mapId' => 'gmap-canvas-' . $variables['node']->nid,
'mapOptions' => array(
'zoom' => (int) variable_get('custom_module_default_gmap_zoom'),
'mapTypeId' => GMAP3_TOOLS_MAP_TYPE_ID_SATELLITE,
),
'markers' => array(
gmap3_tools_create_marker($lat, $long, $facility_name, $description),
),
'gmap3ToolsOptions' => array(
'defaultMarkersPosition' => GMAP3_TOOLS_DEFAULT_MARKERS_POSITION_CENTER,
),
));
$output = '<div id="gmap-canvas-' . $variables['node']->nid . '" style="width:500px;height:400px;"></div>';
return $output;
}
/**
* Implements hook_block_info().
*/
function custom_module_block_info() {
$blocks = array();
$blocks['gmap'] = array(
'info' => t('Custom Module Map'),
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function custom_module_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'gmap':{
$block['subject'] = t('Custom Module Map');
$block['content'] = custom_module_block_contents($delta);
break;
}
}
return $block;
}
/**
* Custom Module Block contents.
*
* @param string $delta
* The block ID.
*
* @return string
* HTML output.
*/
function custom_module_block_contents($delta) {
$output = '';
switch ($delta) {
case 'gmap':{
module_load_include('inc', 'location', 'earth');
// 100 miles.
$distance_meters = 100 * 1609.34;
// Determine the range of lat and long from the default center.
$lat = variable_get('custom_module_default_center_lat');
$long = variable_get('custom_module_default_center_long');
$range_lat = earth_latitude_range($long, $lat, $distance_meters);
$range_long = earth_longitude_range($long, $lat, $distance_meters);
// Build query.
$query = new EntityFieldQuery();
// Only show nodes.
$query->entityCondition('entity_type', 'node');
// Only show Custom Module.
$query->entityCondition('bundle', 'custom_module');
// Only show latitude between range.
$query->fieldCondition('custom_module_latitude', 'value', $range_lat, 'BETWEEN');
// Only show longitude between range.
$query->fieldCondition('custom_module_longitude', 'value', $range_long, 'BETWEEN');
// Execute query.
$result = $query->execute();
// No results.
if (!isset($result['node'])) {
return '';
}
$nodes = node_load_multiple(array_keys($result['node']));
module_load_include('inc', 'gmap3_tools');
$markers = array();
foreach ($nodes as $node) {
$lat = $node->custom_module_latitude[LANGUAGE_NONE][0]['value'];
$long = $node->custom_module_longitude[LANGUAGE_NONE][0]['value'];
// Cannot render map without both.
if (($lat == '') || ($long == '')) {
continue;
}
$facility_name = check_plain($node->title);
$description = check_plain($node->body[LANGUAGE_NONE][0]['value']);
// Add a link to the node.
$description .= ' ' . l(t('More info'), 'node/' . $node->nid);
// Add to markers.
$markers[] = gmap3_tools_create_marker($lat, $long, $facility_name, $description);
}
// Create map with all the markers.
gmap3_tools_add_map(array(
'mapId' => 'gmap-canvas-block',
'mapOptions' => array(
'mapTypeId' => GMAP3_TOOLS_MAP_TYPE_ID_SATELLITE,
),
'markers' => $markers,
'gmap3ToolsOptions' => array(
'defaultMarkersPosition' => GMAP3_TOOLS_DEFAULT_MARKERS_POSITION_CENTER_ZOOM,
),
));
// HTML container for the map.
$output = '<div id="gmap-canvas-block" style="width:500px;height:400px;"></div>';
break;
}
}
return $output;
}