-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathding_library_opening_hours.module
More file actions
425 lines (367 loc) · 12.7 KB
/
ding_library_opening_hours.module
File metadata and controls
425 lines (367 loc) · 12.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
<?php
/**
* @file
* Code for the Ding library opening hours related blocks.
*/
/**
* Implements hook_menu().
*/
function ding_library_opening_hours_menu() {
$menu = array();
$menu['ding_library_hours_ajax/%/%'] = array(
'title' => 'Ding library opening/today hours',
'description' => 'Ajax representation of widget',
'access arguments' => array('access content'),
'page callback' => 'ding_library_opening_hours_ajax',
'page arguments' => array(1, 2),
);
return $menu;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function ding_library_opening_hours_form_ding_ddbasic_opening_hours_settings_form_alter(&$form, &$form_state, $form_id) {
$form['dloh'] = array(
'#type' => 'fieldset',
'#title' => 'Ding library opening hours settings',
);
$form['dloh']['ding_library_opening_hours_notice_hide_icon'] = array(
'#type' => 'checkbox',
'#title' => t('Show notice as text'),
'#description' => t('Toggle display of notice in "Opened today" libraries block.'),
'#default_value' => variable_get('ding_library_opening_hours_notice_hide_icon', ''),
);
$form['dloh']['ding_library_opening_hours_show_in_tabs'] = array(
'#type' => 'checkbox',
'#title' => t('Show opening hours in tabs?'),
'#default_value' => variable_get('ding_library_opening_hours_show_in_tabs', 0),
);
}
/**
* Ajax handler.
*/
function ding_library_opening_hours_ajax($type, $id) {
$theme = 'ding_library_' . $type . '_view';
$commands = array();
$commands[] = ajax_command_replace('#' . $id . ".ding_library_hours", theme($theme));
return ajax_deliver(array('#type' => 'ajax', '#commands' => $commands));
}
/**
* Implements hook_block_info().
*/
function ding_library_opening_hours_block_info() {
return array(
'ding_library_opening_hours' => array(
'info' => t('Ding: Opening hours for this week'),
'cache' => DRUPAL_NO_CACHE,
),
'ding_library_opened_today' => array(
'info' => t("Ding: Today's opening hours for all libraries"),
'cache' => DRUPAL_NO_CACHE,
),
);
}
/**
* Implements hook_block_view().
*/
function ding_library_opening_hours_block_view($delta = '') {
$block = array();
drupal_add_js(drupal_get_path('module', 'ding_library_opening_hours') . '/js/ding_library_opening_hours.js');
if ($delta == 'ding_library_opening_hours' && $node = menu_get_object()) {
drupal_add_css(drupal_get_path('module', 'ding_library_opening_hours') . '/css/ding_library_opening_hours.css', array('type' => 'file'));
$block['subject'] = t('Opening hours');
$block['content'] = array(
'#theme' => 'ding_library_opening_hours_view',
'#node' => $node,
);
}
if ($delta == 'ding_library_opened_today') {
drupal_add_css(drupal_get_path('module', 'ding_library_opening_hours') . '/css/ding_library_opening_hours.css', array('type' => 'file'));
$block['subject'] = t('Opened today');
$block['content'] = array(
'#theme' => 'ding_library_opened_today_view',
);
if (variable_get('ding_library_opening_hours_show_in_tabs', 0) == 1) {
$block['content'] = array(
'#theme' => 'ding_library_opening_hours_all_tabbed',
);
}
}
return $block;
}
/**
* Implements hook_preprocess_theme().
*/
function ding_library_opening_hours_preprocess_opening_hours_week(&$vars) {
$library = $vars['node'];
$hide_from_overview = FALSE;
if (isset($library->field_ding_library_overview_val[LANGUAGE_NONE][0]['value'])) {
$hide_from_overview = $library->field_ding_library_overview_val[LANGUAGE_NONE][0]['value'];
}
$vars['ding_library_overview'] = $hide_from_overview;
}
/**
* Implements hook_theme().
*/
function ding_library_opening_hours_theme($existing, $type, $theme, $path) {
return array(
'ding_library_opening_hours_view' => array(
'path' => $path . '/templates',
'template' => 'ding_library_opening_hours_view',
'variables' => array('node' => NULL),
),
'ding_library_opened_today_view' => array(
'path' => $path . '/templates',
'template' => 'ding_library_opened_today_view',
'variables' => array('libraries' => NULL),
),
'ding_library_opening_hours_all_tabbed' => array(
'variables' => array(
'today' => NULL,
),
),
);
}
/**
* Preprocess variables for our template.
*/
function template_preprocess_ding_library_opening_hours_view(&$vars) {
$node = $vars['node'];
// Some minor date manipulations to get the correct range for the block.
$full_day = 24 * 60 * 60;
$current_date = time();
$current_day_of_week = date('w');
$start_of_week = $current_date - ($full_day * $current_day_of_week) + 1;
$start_date = date('Y-m-d', $start_of_week);
// Generate dummy holder for the current week.
$days = array();
for ($i = 0; $i < 7; $i++) {
$start_of_week += $full_day;
$day_date = date('Y-m-d', $start_of_week);
$days[$day_date] = array(
'day_name' => date('l', $start_of_week),
);
}
$end_date = date('Y-m-d', $start_of_week);
// Load the opening hours from DB for theming.
$opening_hours = opening_hours_instance_load_multiple($node->nid, $start_date, $end_date);
foreach ($opening_hours as $instance) {
if (isset($days[$instance->date]['day_name'])) {
$days[$instance->date]['end_time'] = $instance->end_time;
$days[$instance->date]['start_time'] = $instance->start_time;
}
}
$vars['days'] = $days;
}
/**
* Preprocess variables for our template.
*/
function template_preprocess_ding_library_opened_today_view(&$vars) {
$libraries = array();
$today = date('Y-m-d');
// Load all libraries form list, because there are less libraries than dates
// and because of how opening_hours_instance_load_multiple works.
$sql = 'SELECT nq.qid, nq.show_in_tab, nq.show_in_links, nq.show_in_ui, nq.i18n ' .
'FROM {nodequeue_queue} nq ' .
'INNER JOIN {nodequeue_types} nt ON nt.qid = nq.qid ' .
"WHERE nt.type = :type ";
$result = db_query($sql, array(':type' => 'ding_library'));
$qids = array();
foreach ($result as $qid) {
$qids[$qid->qid] = $qid;
}
$queue_id = array_keys($qids);
$nodes = nodequeue_load_nodes($queue_id[0], FALSE, 0, 50);
if (!empty($nodes)) {
foreach ($nodes as $library) {
$hide_from_main_widget = FALSE;
if (isset($library->field_ding_library_main_val[LANGUAGE_NONE][0]['value'])) {
$hide_from_main_widget = $library->field_ding_library_main_val[LANGUAGE_NONE][0]['value'];
}
if (!$hide_from_main_widget) {
$libraries[$library->nid]['title'] = l(
$library->title,
'node/' . $library->nid,
array('fragment' => 'toggle-opening-hours', 'external' => TRUE)
);
}
}
// Get all dates for today.
if (!empty($libraries) > 0) {
$opening_hours = opening_hours_instance_load_multiple(array_keys($libraries), $today, $today);
$i = 0;
foreach ($opening_hours as $hour) {
$libraries[$hour->nid]['opening_hours'][$i]['start_time'] = $hour->start_time;
$libraries[$hour->nid]['opening_hours'][$i]['end_time'] = $hour->end_time;
$libraries[$hour->nid]['opening_hours'][$i]['notice'] = $hour->notice;
$i++;
}
// Check if we should display text instead of "info" icon for notice.
$notice_format = variable_get('ding_library_opening_hours_notice_hide_icon', FALSE);
$vars['notice_format'] = $notice_format;
}
}
$vars['libraries'] = $libraries;
}
/**
* Implements hook_views_pre_render().
*/
function ding_library_opening_hours_views_pre_render(&$view) {
if ($view->name == 'ding_library' && $view->current_display == 'ding_library_libraries_list') {
foreach ($view->result as $row => $item) {
$node = node_load($item->nid);
$field = field_get_items('node', $node, 'field_ding_library_overview_val');
$ding_library_overview = $field[0]['value'];
if (!empty($ding_library_overview)) {
unset($view->result[$row]);
}
}
}
}
/**
* Categorized library opening hours.
*/
function theme_ding_library_opening_hours_all_tabbed($variables) {
return $variables['tabbed'];
}
/**
* Preprocessing categorized library opening hours.
*/
function template_preprocess_ding_library_opening_hours_all_tabbed(&$variables) {
drupal_add_library('system', 'ui.tabs');
drupal_add_js('jQuery(document).ready(function(){jQuery("#oh-tabs").tabs();});', 'inline');
$today = strtotime('today');
if (!empty($variables['today'])) {
$today = $variables['today'];
}
$order = [];
$queue = nodequeue_load_queue_by_name('ding_library_listing');
foreach (nodequeue_load_nodes($queue->qid, FALSE, 0, 0) as $node) {
if (empty($node->field_ding_library_main_val[LANGUAGE_NONE][0]['value'])) {
$order[$node->title] = $node->nid;
}
}
// Get and sort all the instances in the given timespan.
$instances = opening_hours_instance_load_multiple(
array_values($order),
date('Y-m-d', $today),
date('Y-m-d', $today)
);
foreach ($order as $library_nid) {
$closed = TRUE;
foreach ($instances as $instance) {
if ($instance->nid == $library_nid) {
$closed = FALSE;
}
}
if ($closed === TRUE) {
$instances[] = (object) [
'instance_id' => -1,
'nid' => $library_nid,
'start_time' => '0',
'end_time' => '0',
'category_tid' => NULL,
'date' => date('Y-m-d', $today),
'closed' => TRUE,
'notice' => t('Closed'),
];
}
}
usort($instances, 'ding_ddbasic_opening_hours_sort');
$order = array_flip($order);
foreach ($instances as $instance) {
$category_weight = ding_ddbasic_opening_hours_get_category_weight($instance->category_tid);
if (!isset($categories[$category_weight])) {
$categories[$category_weight] = ding_ddbasic_opening_hours_get_category_name($instance->category_tid);
// Forming tab heading names.
foreach ($categories as $key => $category) {
// Resetting default tab name.
if ($key == '-1') {
$category = t('Library and citizens');
}
$tabs_heading[$key] = "<a href='#tab-$key'>$category</a>";
}
}
$library = $order[$instance->nid];
if (!isset($structured[$library])) {
$structured[$library] = [
'cols' => [],
'extra' => [],
'name' => l($library, 'node/' . $instance->nid),
];
}
if (!empty($instance->notice)) {
$notice = '<span class="opening-hours-table-notice">' . $instance->notice . '</span>';
}
else {
$notice = '';
}
if (empty($structured[$library]['cols'][$category_weight])) {
$structured[$library]['cols'][$category_weight] = '';
}
if (empty($instance->closed)) {
$structured[$library]['cols'][$category_weight] .= '<div class="oh-info-item">' . $notice . '<span class="oh-period">' . t('@from - @to', [
'@from' => $instance->start_time,
'@to' => $instance->end_time,
]) . '</span></div>';
}
else {
$structured[$library]['cols'][$category_weight] = $notice;
// Passing 'closed' attribute in order to know that library is closed.
$structured[$library]['extra']['closed'] = $category_weight;
}
$structured = array_merge(array_intersect_key(array_flip($order), $structured), $structured);
}
// Start rendering block with tabs.
foreach ($tabs_heading as $k => $tab) {
$div[$k] = [
'#type' => 'container',
'#attributes' => [
'id' => 'tab-' . $k,
],
];
// Rendering opening hours.
foreach ($structured as $lib => $item) {
$inlined = '';
foreach ($item['cols'] as $i => $col) {
// Display opening hours item for tab with related category.
if ($i == $k) {
$inlined .= $col;
}
// Show if library is closed.
else {
if (!empty($item['extra']['closed'])) {
$inlined = $item['cols']['-1'];
}
}
}
// If library doesn't have opening hours - we hide this, otherwise show
// info for this library.
if (!empty($inlined)) {
$div[$k][] = [
$lib = [
'#prefix' => '<div class="oh-item">',
'#suffix' => '</div>',
'#type' => 'markup',
'#markup' => '<h3>' . $item['name'] . '</h3><div class="oh-info">' . $inlined . '</div>',
],
];
}
}
}
// Forming container for rendering.
$content_block = [
'#type' => 'container',
'#attributes' => ['id' => 'oh-tabs'],
'tabs' => [
'#theme' => 'item_list',
'#items' => $tabs_heading,
],
'contents' => [
'#type' => 'markup',
'#markup' => drupal_render($div),
],
];
// Return rendered block into pane.
$variables['tabbed'] = drupal_render($content_block);
}