-
Notifications
You must be signed in to change notification settings - Fork 1
/
ding_wayfinder.module
399 lines (343 loc) · 12.4 KB
/
ding_wayfinder.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
<?php
include_once('ding_wayfinder.features.inc');
/**
* Implements hook_menu().
*/
function ding_wayfinder_menu() {
$items = array();
$items['biblioteker/%/wayfinder'] = array(
'title' => 'Digital Wayfinder frontend',
'page callback' => 'ding_wayfinder_frontend',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['biblioteker/%/wayfinder/post_settings'] = array(
'title' => 'Digital Wayfinder frontend',
'page callback' => 'ding_wayfinder_ajax_save_clients',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['admin/settings/wayfinder/%node'] = array(
'title' => 'Wayfinder Library',
'description' => 'Setup and administer Digital Wayfinder for all libraries',
'page callback' => 'ding_wayfinder_clients',
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/settings/wayfinder/%/clients'] = array(
'title' => 'Digital Wayfinder Clients',
'access arguments' => array('access administration pages'),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/settings/wayfinder/%/clients/%/remove'] = array(
'title' => 'Digital Wayfinder Clients',
'page callback' => 'ding_wayfinder_clients_remove',
'page arguments' => array(3,5),
'access arguments' => array('access administration pages'),
'type' => MENU_CALLBACK,
);
$items['admin/settings/wayfinder/%/settings'] = array(
'description' => 'settings for wayfinder',
'access arguments' => array('access administration pages'),
'page callback' => 'drupal_get_form',
'page arguments' => array('ding_wayfinder_settings_form',1),
'title' => 'Digital Wayfinder Settings',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Settings form for Wayfinder
*/
function ding_wayfinder_settings_form($form_state) {
$library = arg(3);
$form = array();
$form['wf'] = array(
'#type' => 'fieldset',
'#title' => t('Wayfinder settings'),
'#description' => t('These variables must be set inorder for the library wayfinder to work'),
'#tree' => FALSE,
);
$form['wf']['ding_wayfinder_server'] = array(
'#type' => 'textfield',
'#title' => t('Websocket URL'),
'#description' => t('Where is the websocket and app located? eg. ws://localhost:8080/app'),
'#required' => TRUE,
'#default_value' => variable_get('ding_wayfinder_server', ''),
);
$form['wf']['ding_wayfinder_idletime_'.$library] = array(
'#type' => 'textfield',
'#title' => t('Client Idle time (seconds)'),
'#description' => t('How long can the client remain without getting touched?'),
'#required' => TRUE,
'#default_value' => variable_get('ding_wayfinder_idletime_'.$library, '30'),
);
return system_settings_form($form);
}
/**
* Set library via POST
*/
function ding_wayfinder_ajax_save_clients() {
if($_POST['deviceid'] == ''){
return;
}
$name = filter_xss($_POST['clientname']);
$deviceid = filter_xss($_POST['deviceid']);
$clients = unserialize(variable_get('ding_wayfinder_clients_' . arg(1), array()));
// add client if it dosen't exist and set last update time;
$clients[$deviceid] = array('name' => $name, 'time' => format_date(time()));
variable_set('ding_wayfinder_clients_' . arg(1), serialize($clients));
print t('"!name" is now registered with server', array('!name' => $name));
}
/**
* Pagecallback to list all connected clients
*/
function ding_wayfinder_clients() {
$clients = unserialize(variable_get('ding_wayfinder_clients_' . arg(3), array()));
$i=0;
foreach ($clients as $key => $client) {
$rows[] = array(array('data' => $client['name'], 'class' => $key), 'Wait...', '---', $client['time'], l(t('X'), 'admin/settings/wayfinder/' . arg(3) . '/clients/' . $key . '/remove', array('query' => array('destination' => 'admin/settings/wayfinder/' . arg(3) . '/clients'))));
$clientsJS[] = $key;
$i++;
}
$settings = array(
'wayfinder_path' => drupal_get_path('module','ding_wayfinder'),
'server' => variable_get('ding_wayfinder_server', ''),
);
$data = array('ding_wayfinder' => array('settings' => $settings,'clients' => $clientsJS));
drupal_add_js($data,'setting');
drupal_add_js(drupal_get_path('module', 'ding_wayfinder') . '/js/client/webpush.js');
drupal_add_js(drupal_get_path('module', 'ding_wayfinder') . '/js/admin.js');
// build client list
$output = '<p class="connection_status">' . t('Status: <span> Trying to connect...</span>') . '</p>';
$output .= theme('table', array(t('Clients'), t('Status'), t('Reloaded last'), t('Last answer'), t('Remove')), $rows);
$output .= theme('button', array('#id' => 'reload', '#value' => t('Update clients'), '#type' => 'submit', '#button_type' => 'submit'));
$output .= theme('button', array('#id' => 'reload_list', '#value' => t('Reload client list'), '#type' => 'submit', '#button_type' => 'submit'));
return $output;
}
function ding_wayfinder_clients_remove($library,$client) {
$clients = unserialize(variable_get('ding_wayfinder_clients_' . $library, array()));
unset($clients[$client]);
variable_set('ding_wayfinder_clients_' . $library, serialize($clients));
drupal_goto();
}
/**
* Implements hook_theme().
*/
function ding_wayfinder_theme() {
return array(
'ding_wayfinder_frontend_build' => array(
'template' => 'ding-wayfinder-frontend',
'arguments' => array(
'styles' => null,
'scripts' => null,
),
),
);
}
/**
* Set cache file
*/
function ding_wayfinder_cache() {
header('Content-Type: text/cache-manifest;');
echo "
CACHE MANIFEST
index.html
css/layout.css
css/ios.css
#" . time() . "
";
}
/**
* Build frontend for wayfinder
*/
function ding_wayfinder_frontend() {
$modulekey = 'ding_wayfinder';
$files = array(
'ios.css',
'layout.css',
'map.css',
'messages.css'
);
foreach ($files as $file) {
drupal_add_css(drupal_get_path('module', $modulekey).'/css/client/' . $file);
}
$css = drupal_add_css();
foreach ($css['all']['module'] as $key => $line) {
if (!strpos($key, $modulekey)) {
unset($css['all']['module'][$key]);
}
}
$data = ding_wayfinder_get_keywords(arg(1));
// join floors up on keywords
// for each keyword add to data array. (Sorted floor weight, then keyword weight.) if floor is diffrent from previous floor, make new floor in data array.
$settings = array(
'wayfinder_path' => drupal_get_path('module', 'ding_wayfinder'),
'launched' => format_date(time()),
'device_identifier' => md5(time()),
'server' => variable_get('ding_wayfinder_server', ''),
);
$data = array('ding_wayfinder' => array('data' => $data,'settings' => $settings));
drupal_add_js($data,'setting');
$files = array(
'init.js',
'jquery.js',
'll.js',
'setup.js',
'webpush.js',
'websocket.js'
);
foreach ($files as $file) {
drupal_add_js(drupal_get_path('module', $modulekey).'/js/client/' . $file);
}
$js = drupal_add_js();
foreach ($js['module'] as $key => $line) {
if (!strpos($key, $modulekey)) {
unset( $js['module'][$key]);
}
}
$styles = drupal_get_css($css);
$scripts = drupal_get_js('header', $js);
echo theme('ding_wayfinder_frontend_build', $styles, $scripts);
}
function ding_wayfinder_get_keywords($lib) {
$data = array();
// loop all floors
$result = db_query('SELECT n.nid FROM {node} AS n LEFT JOIN content_type_wf_floor AS floor ON n.nid = floor.nid WHERE floor.field_library_nid = '.$lib. ' ORDER BY n.sticky DESC');
while ($obj = db_fetch_object($result)) {
$node = node_load($obj->nid);
// loop all keywords for given floor
$keyword_result = db_query('SELECT n.nid FROM {node} AS n LEFT JOIN content_type_wf_keyword AS kw ON n.nid = kw.nid WHERE kw.field_floor_nid = '. $obj->nid . ' ORDER BY n.sticky DESC');
while ($obj = db_fetch_object($keyword_result)) {
$keyword_node = node_load($obj->nid);
$keyword['name'] = $keyword_node->title;
$keyword['filename'] = $keyword_node->field_keywordoverlay[0]['filepath'];
if ($keyword_node->taxonomy) {
// get all noded in taxonomy and choose the first one. if first one equals this one, just set keywordid like normal, else set keywordid same as first
$result_taxonomy = taxonomy_select_nodes( array_keys($keyword_node->taxonomy));
// get first node
$obj = db_fetch_object($result_taxonomy);
if ($obj->nid != $keyword_node->nid) {
$keyword['id'] = $obj->nid;
}
else {
$keyword['id'] = $keyword_node->nid;
}
}
else {
$keyword['id'] = $keyword_node->nid;
}
$keywords[] = $keyword;
}
$floor['name'] = $node->title;
$floor['filename'] = $node->field_floorplan[0]['filepath'];
$floor['keywords'] = $keywords;
unset($keywords);
$data[] = $floor;
}
return $data;
}
/**
* Implements hook_views_pre_render().
*/
function ding_wayfinder_views_pre_render(&$view) {
if ($view->name != 'wayfinder') {
return;
}
if ($view->args[0]) {
$node = node_load($view->args[0]);
$library = $node->title;
}
if ($view->args[1]) {
$node = node_load($view->args[1]);
$floor = $node->title;
}
switch ($view->current_display) {
case 'page_1':
if($view->total_rows == 0) {
drupal_set_message(t('Theres no keywords on this floor, therefore you are prompted to add one'));
drupal_goto('node/add/wf-keyword/' . $view->args[1], array('destination' => 'admin/settings/wayfinder/' . $view->args[0]. '/' . $view->args[1]. '/keywords'));
}
$title = $library . ' : ' . $floor . ' : ' . t('Keywords');
break;
case 'page_2':
if($view->total_rows == 0) {
drupal_set_message(t('Theres no floors connected with the library, therefore you are prompted to add one'));
drupal_goto('node/add/wf-floor/' . $view->args[0],array('destination' => 'admin/settings/wayfinder/' . $view->args[0]. '/floors'));
}
$title = $library . ' : ' . t('Floors');
break;
}
drupal_set_title($title);
}
/**
* Implements hook_form_alter().
*/
function ding_wayfinder_form_alter(&$form,$form_state,$form_id) {
if(arg(5) == 'keywords') {
$form['#submit'][] = 'ding_wayfinder_weight_view_weight_form_wayfinder_page_1_drag_submit';
$form['back_link'] = array(
'#type' => 'submit',
'#value' => t('Back to Floors'),
);
$form['new_link'] = array(
'#type' => 'submit',
'#value' => t('Add another keyword'),
);
$form['manage_link'] = array(
'#type' => 'submit',
'#value' => t('Manage keyword collections'),
);
}
if(arg(4) == 'floors') {
$form['#submit'][] = 'ding_wayfinder_weight_view_weight_form_wayfinder_page_2_drag_submit';
$form['back_link'] = array(
'#type' => 'submit',
'#value' => t('Back to Client list'),
);
$form['new_link'] = array(
'#type' => 'submit',
'#value' => t('Add another floor'),
);
}
if($form_id == 'wf_floor_node_form' && arg(3)) {
$form['field_library']['#default_value'][0]['nid'] = arg(3);
}
if($form_id == 'wf_keyword_node_form' && arg(3)) {
$form['field_floor']['#default_value'][0]['nid'] = arg(3);
}
}
function ding_wayfinder_weight_view_weight_form_wayfinder_page_1_drag_submit($form,$form_state) {
switch($form_state['clicked_button']['#id']){
case 'edit-new-link':
drupal_goto(url('node/add/wf-keyword/' . arg(4)),array('destination' => 'admin/settings/wayfinder/' . arg(3) . '/' . arg(4). '/keywords'));
exit();
break;
case 'edit-back-link':
drupal_goto(url('admin/settings/wayfinder/' . arg(3). '/floors'));
exit();
break;
case 'edit-manage-link':
$vocabs = taxonomy_get_vocabularies();
foreach ($vocabs as $vocab_object) {
if ($vocab_object->module == 'ding_wayfinder') {
$vid = $vocab_object->vid;
}
}
drupal_goto(url('admin/content/taxonomy/'.$vid));
exit();
break;
}
}
function ding_wayfinder_weight_view_weight_form_wayfinder_page_2_drag_submit($form,$form_state) {
switch($form_state['clicked_button']['#id']){
case 'edit-new-link':
drupal_goto(url('/node/add/wf-floor/'.arg(3)),array('destination' => 'admin/settings/wayfinder/' . arg(3). '/floors'));
exit();
break;
case 'edit-back-link':
drupal_goto(url('/admin/settings/wayfinder/' . arg(3)));
exit();
break;
}
}