-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibhours.module
More file actions
487 lines (424 loc) · 13.9 KB
/
libhours.module
File metadata and controls
487 lines (424 loc) · 13.9 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
<?php
/**
* @file
* Module functions to display library hours.
*
* @author Sean Watkins <slwatkins@uh.edu>
* @copyright 2011 University of Houston Libraries (http://info.lib.uh.edu)
*/
/**
* Implements hook_help().
*/
function libhours_help($path, $arg) {
$output = '';
switch ($path) {
case 'admin/help#libhours':
$output = '<p>' . t('Displays the hours for the libraries.') . '</p>';
break;
}
return $output;
}
/**
* Implements hook_perm().
*/
function libhours_perm() {
return array('administer library hours');
}
/**
* Implements hook_theme().
*/
function libhours_theme() {
return array(
'time' => array(
'arguments' => array('element' => NULL),
),
'hours_display' => array(
'arguments' => array('lid' => 0, 'pid' => 0),
'template' => 'libhours-display',
),
);
}
/**
* Implements hook_menu().
*/
function libhours_menu() {
$items = array();
/* PAGE PATHS */
/* This menu will be deprecated */
$items['libraryhours'] = array(
'title' => 'Library Hours',
'page callback' => 'libhours_redirect',
'access arguments' => array('access content'),
'file' => 'libhours.pages.inc',
);
$items['hours'] = array(
'title' => 'Library Hours',
'page callback' => 'libhours_display',
'access arguments' => array('access content'),
'file' => 'libhours.pages.inc',
);
/* API ENDPOINT */
$items['hours/api'] = array(
'title' => 'Process the API call',
'page callback' => 'libhours_api',
'access arguments' => array('access content'),
'file' => 'libhours.api.inc',
);
/* This menu will be deprecated */
$items['libraryhours/api'] = array(
'title' => 'Process the API call',
'page callback' => 'libhours_api',
'access arguments' => array('access content'),
'file' => 'libhours.api.inc',
);
/* ADMIN PATHS */
$items['admin/content/libhours'] = array(
'title' => 'Libraries Hours',
'description' => 'Administer libraries hours',
'page callback' => 'libhours_admin',
'access arguments' => array('administer library hours'),
'file' => 'libhours.admin.inc',
);
$items['admin/content/libhours/location/add'] = array(
'title' => 'Add Location',
'page callback' => 'libhours_admin_location_edit',
'access arguments' => array('administer library hours'),
'file' => 'libhours.admin.inc',
);
$items['admin/content/libhours/location/edit'] = array(
'title' => 'Edit location',
'page callback' => 'libhours_admin_location_edit',
'access arguments' => array('administer library hours'),
'file' => 'libhours.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/content/libhours/location/delete'] = array(
'title' => 'Delete location',
'page callback' => 'drupal_get_form',
'page arguments' => array('libhours_admin_location_delete_confirm'),
'access arguments' => array('administer library hours'),
'file' => 'libhours.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/content/libhours/location/hours'] = array(
'title' => 'Location Information',
'page callback' => 'libhours_admin_location_hours',
'access arguments' => array('administer library hours'),
'file' => 'libhours.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/content/libhours/period/add'] = array(
'title' => 'Add period',
'page callback' => 'libhours_admin_period_edit',
'access arguments' => array('administer library hours'),
'file' => 'libhours.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/content/libhours/period/edit'] = array(
'title' => 'Edit period',
'page callback' => 'libhours_admin_period_edit',
'access arguments' => array('administer library hours'),
'file' => 'libhours.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/content/libhours/period/delete'] = array(
'title' => 'Delete Period',
'page callback' => 'drupal_get_form',
'page arguments' => array('libhours_admin_period_delete_confirm'),
'access arguments' => array('administer library hours'),
'file' => 'libhours.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/content/libhours/period/hours'] = array(
'title' => 'Period Hours',
'page callback' => 'libhours_admin_period_hours',
'access arguments' => array('administer library hours'),
'file' => 'libhours.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/content/libhours/hours/edit'] = array(
'title' => 'Edit Hours',
'page callback' => 'libhours_admin_hours_edit',
'access arguments' => array('administer library hours'),
'file' => 'libhours.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/content/libhours/hours/delete'] = array(
'title' => 'Delete Hours',
'page callback' => 'drupal_get_form',
'page arguments' => array('libhours_admin_hours_delete_confirm'),
'access arguments' => array('administer library hours'),
'file' => 'libhours.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/content/libhours/exceptions/add'] = array(
'title' => 'Add Exception',
'page callback' => 'libhours_admin_exceptions_edit',
'access arguments' => array('administer library hours'),
'file' => 'libhours.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/content/libhours/exceptions/edit'] = array(
'title' => 'Edit Exception',
'page callback' => 'libhours_admin_exceptions_edit',
'access arguments' => array('administer library hours'),
'file' => 'libhours.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/content/libhours/exceptions/delete'] = array(
'title' => 'Delete Exception',
'page callback' => 'drupal_get_form',
'page arguments' => array('libhours_admin_exceptions_delete_confirm'),
'access arguments' => array('administer library hours'),
'file' => 'libhours.admin.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_elements().
*/
function libhours_elements() {
return array(
'time' => array(
'#input' => TRUE,
'#process' => array('libhours_expand_time'),
'#element_validate' => array('libhours_time_validate'),
),
);
}
/**
* process the time element
*/
function libhours_expand_time($element) {
$hours = range(1, 12);
$minutes = range(0, 60, 5);
/* Need to add in 59 just to specify a closed time of 11:59 pm */
$minutes[59] = 59;
if (empty($element['#value'])) {
$element['#value'] = array('hour' => '-', 'minute' => '-', 'm' => 'am');
}
$element['#tree'] = TRUE;
$parents = $element['#parents'];
$parents[] = 'hour';
$element['hour'] = array(
'#type' => 'select',
'#default_value' => $element['#value']['hour'],
'#options' => $hours,
'#suffix' => ':',
);
$parents = $element['#parents'];
$parents[] = 'minute';
$element['minute'] = array(
'#type' => 'select',
'#default_value' => $element['#value']['minute'],
'#options' => $minutes,
);
$parents = $element['#parents'];
$parents[] = 'm';
$element['m'] = array(
'#type' => 'select',
'#default_value' => $element['#value']['m'],
'#options' => array('am' => 'am', 'pm' => 'pm'),
);
return $element;
}
/**
* validates time element
*/
function libhours_time_validate($element) {
if (trim($element['#value']['hour']) == '-' || trim($element['#value']['minute']) == '-') {
form_error($element, t('Please select a correct hour and minute.'));
}
}
/**
* Implements theme_hook()
*/
function theme_time($element) {
return theme('form_element', $element, '<div class="container-inline">' . $element['#children'] . '</div>');
}
/**
* Preprocess for the hours_display theme
*
* @see libhours-display.tpl.php
*/
function template_preprocess_hours_display(&$variables) {
if (!$variables['lid']) {
$variables['lid'] = 1;
}
$lid = $variables['lid'];
$today = time();
if (!$variables['pid']) {
$variables['pid'] = db_result(db_query_range("SELECT pid FROM {libhours_periods} WHERE %d >= from_date AND %d <= to_date AND lid=%d ORDER BY from_date DESC", $today, $today, $lid, 0, 1));
if (empty($variables['pid'])) {
$variables['pid'] = db_result(db_query_range("SELECT pid FROM {libhours_periods} WHERE from_date >= %d AND lid=%d ORDER BY from_date ASC", $today, $lid, 0, 1));
}
}
$pid = $variables['pid'];
/* Used when displaying the periods for the location */
$location = db_fetch_object(db_query("SELECT * FROM {libhours_locations} WHERE lid=%d", $lid));
$variables['description'] = check_plain($location->description);
/* Load up the available locations */
$variables['locations'] = array();
$lrs = db_query("SELECT * FROM {libhours_locations} WHERE parent=0 ORDER BY name ASC");
while ($row = db_fetch_object($lrs)) {
$children = array();
$crs = db_query("SELECT * FROM {libhours_locations} WHERE parent=%d ORDER BY name ASC", $row->lid);
while ($child = db_fetch_object($crs)) {
$children[] = array('lid' => $child->lid, 'name' => check_plain($child->name));
}
$variables['locations'][] = array(
'lid' => $row->lid,
'name' => check_plain($row->name),
'children' => $children,
);
}
/* Load the next 4 periods */
$variables['periods'] = array();
$rs = db_query_range("SELECT *, s.name FROM {libhours_periods} p
INNER JOIN {libhours_semesters} s ON p.sid = s.sid
WHERE lid = %d AND %d <= to_date ORDER BY from_date ASC", $lid, $today, $today, 0, 4);
while ($period = db_fetch_object($rs)) {
$hours = array();
$display_hours = array();
$rh = db_query("SELECT * FROM {libhours_hours} WHERE pid=%d ORDER BY dow DESC", $period->pid);
while ($hour = db_fetch_object($rh)) {
for ($i = 0; $i < strlen($hour->dow); $i++) {
if ($hour->dow[$i] == '1') {
$hours[$i] = array('open' => $hour->open, 'close' => $hour->close);
}
}
}
for ($i = 0; $i < count($hours); $i++) {
$previdx = ($i == 0) ? (count($hours) - 1) : ($i - 1);
$nextidx = (($i + 1) == count($hours)) ? 0 : ($i + 1);
$display_hours[$i] = _libhours_displayhours($hours[$i]['open'], $hours[$i]['close'], $hours[$previdx], $hours[$nextidx]);
}
/* Load the exceptions within the period */
$vexceptions = array();
$ers = db_query("SELECT * FROM {libhours_exceptions} WHERE lid=%d AND from_date >= %d AND to_date <= %d AND %d <= to_date ORDER BY from_date ASC", $lid, $period->from_date, $period->to_date, $today);
$exceptions = array();
/* Gather together all the exceptions with the same label */
while ($exception = db_fetch_object($ers)) {
if (date("n/d/Y", $exception->from_date) == date("n/d/Y", $exception->to_date)) {
$date = date("l: M j", $exception->from_date);
}
else {
$date = date("M j", $exception->from_date) . ' - ' . date("M j", $exception->to_date);
}
$exceptions[strtoupper($exception->label)][] = $date . ', ' . _libhours_displayhours($exception->open, $exception->close);
}
/* Load all the sorted exceptions for displaying */
foreach ($exceptions as $label => $times) {
$vexceptions[] = array('name' => check_plain($label), 'times' => $times);
}
$variables['periods'][] = array(
'pid' => $period->pid,
'name' => check_plain($period->name),
'from_date' => $period->from_date,
'to_date' => $period->to_date,
'hours' => $display_hours,
'exceptions' => $vexceptions,
);
}
}
/**
* returns day of week
*
* @param int $index
* The week day numeric representation 0 for Sunday through 6 for Saturday
* @param bool $long
* TRUE to return full text, FALSE to return three letter representation of
* a day. (default FALSE)
*/
function _libhours_dow($index, $long = TRUE) {
$_DOW[0] = t('Sunday');
$_DOW[1] = t('Monday');
$_DOW[2] = t('Tuesday');
$_DOW[3] = t('Wednesday');
$_DOW[4] = t('Thursday');
$_DOW[5] = t('Friday');
$_DOW[6] = t('Saturday');
return (($long) ? $_DOW[$index] : substr($_DOW[$index], 0, 3));
}
/**
* Parse time given in integer to a string format g:ia
*
* @param int $time
* Time given as integer 0 - 2400
*/
function _libhours_parsetime($time) {
if ($time < 100) {
$hour = 0;
}
else {
$hour = substr($time, 0, -2);
}
$minute = substr($time, -2);
return date('g:ia', mktime($hour, $minute, 0));
}
/**
* Splits time into an array. used for form select boxes
*
* @param int $time
* Time given in the format of g:ia
*/
function _libhours_splittime($time) {
$meridiem = substr($time, -2);
$time = substr($time, 0, -2);
$hm = explode(':', $time);
return array($hm[0], $hm[1], $meridiem);
}
/**
* Converts day of week binary to day of week string
*
* @param string $dow
* Day of week binary
*/
function _libhours_dowstring($dow) {
$str = '';
for ($i = 0; $i < strlen($dow); $i++) {
if ($dow[$i] == '1') {
$str .= _libhours_dow($i, FALSE) . ' - ';
}
}
$str = substr($str, 0, -3);
return $str;
}
/**
* Displays the open/close hours
*
* @param int $open
* The time, as int, the location opens
* @param int $close
* The time, as int, the location closes
* @param array $prev
* Array of the hours from the previous day
* @param array $next
* Array of the hours for the next day
*/
function _libhours_displayhours($open, $close, $prev = NULL, $next = NULL) {
$openstr = _libhours_parsetime($open);
$closestr = _libhours_parsetime($close);
if ($open == 0 && $close == 2359) {
$r = t('Open 24 hours');
}
elseif ($open == 0 && $close == 0) {
$r = t('Closed');
}
else {
$r = t('@opentime - @closetime', array(
'@opentime' => $openstr,
'@closetime' => $closestr,
));
if ($prev && $next) {
if ($next['open'] == 0 && $next['close'] == 2359) {
$r = t('@opentime - Open', array('@opentime' => $openstr));
}
elseif ($prev['open'] == 0 && $prev['close'] == 2359) {
$r = t('Open - @closetime', array('@closetime' => $closestr));
}
}
}
return $r;
}