-
Notifications
You must be signed in to change notification settings - Fork 1
/
todays_hours.module
402 lines (381 loc) · 13.3 KB
/
todays_hours.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
<?php
/**
* @file
* Today's Hours
*
* Creates Today's Hours blocks from Opening Hours
*/
/**
* Implements hook_block_info()
*/
function todays_hours_block_info() {
$blocks = array();
$result = db_query('SELECT nid, info, machine_name, additional_mobile FROM {opening_hours_today_block} ORDER BY info');
foreach ($result as $block) {
$delta = $block->machine_name;
$blocks[$delta]['info'] = $block->info;
$blocks[$delta]['cache'] = DRUPAL_NO_CACHE;
if ($block->additional_mobile) {
$blocks[$delta . '__mobile']['info'] = 'Mobile: ' . $block->info;
$blocks[$delta . '__mobile']['cache'] = DRUPAL_NO_CACHE;
}
}
return $blocks;
}
/**
* Implements hook_view()
*/
function todays_hours_block_view($delta = '') {
$block = array();
// Check if Today's Hours block
$parts = explode('__', $delta);
$delta = $parts[0];
/*need to explode the delta here bc some of them have __mobile attached*/
$result = db_query('SELECT nid, additional_mobile FROM {opening_hours_today_block} WHERE machine_name = :delta', array(':delta' => $delta));
$title_result = db_query('SELECT title, additional_mobile FROM {opening_hours_today_block} WHERE machine_name = :delta', array(':delta' => $delta));
// Check if Today's Hours block
$record = $result->fetchAssoc();
$record_title=$title_result->fetchAssoc();
$nid = $record['nid'];
$title = $record_title['title'];
if (isset($parts[1]) && $parts[1] == 'mobile') {
// Additional mobile Today's Hours block
$block['subject'] = $title;
$block['content'] = array(
'#theme' => 'todays_hours_mobile',
'#display' => 'default',
'#hours' => todays_hours_query($nid),
);
}
else {
$block['subject'] = $title;
$block['content'] = array(
'#theme' => 'todays_hours',
'#display' => 'default',
'#hours' => todays_hours_query($nid),
);
}
return $block;
}
/**
* Today's Hours Query
*
* Provides a keyed array containing formatted values for today's hours blocks
* Queries Open Hours tables based on today's date
*
* @param int $nid
* The node id for the corresponding opening hours node.
*
* @return
* An associative array with the following keys:
* - open: opening time with an array containing 24:00 time and am/pm
* - close: closing time with an array containing 24:00 time and am/pm
*/
function todays_hours_query($nid = 0) {
$hours = array();
$hours['open'] = array(
'hour-min' => '00:00',
'am-pm' => NULL,
);
$hours['close'] = array(
'hour-min' => '00:00',
'am-pm' => NULL,
);
$hours['more-url'] = NULL;
if ($nid) {
$result = db_query("SELECT TIME_FORMAT(start_time, '%l:%i') AS open_hr_min,
TIME_FORMAT(start_time, '%p') AS open_am_pm,
TIME_FORMAT(end_time, '%l:%i') AS close_hr_min,
TIME_FORMAT(end_time, '%p') AS close_am_pm,
more_url
FROM {opening_hours} oh
JOIN {opening_hours_today_block} th ON th.nid=oh.nid
WHERE oh.nid=:nid AND date = CURDATE()", array(':nid' => $nid));
$record = $result->fetchObject();
if ($record) {
$hours['open'] = array(
'hour-min' => $record->open_hr_min,
'am-pm' => strtolower($record->open_am_pm),
);
$hours['close'] = array(
'hour-min' => $record->close_hr_min,
'am-pm' => strtolower($record->close_am_pm),
);
$hours['more-url'] = $record->more_url;
}
}
return $hours;
}
/**
* Implements hook_theme()
*/
function todays_hours_theme($existing, $type, $theme, $path) {
return array(
'todays_hours' => array(
'variables' => array(
'display' => 'default',
'hours' => array(),
'more_url' => '',
),
'file' => 'todays_hours.theme.inc',
),
'todays_hours_mobile' => array(
'variables' => array(
'display' => 'default',
'hours' => array(),
'more_url' => '',
),
'file' => 'todays_hours.theme.inc',
),
);
}
/**
* Implements theme_preprocess_block().
*
* Override or insert variables into the block templates.
*
* @param $variables
* An array of variables to pass to the theme template.
* @param $hook
* The name of the template being rendered ("block" in this case.)
*/
function todays_hours_preprocess_block(&$variables, $hook) {
// Set block template and classes for Today's Hours
$block = $variables['elements']['#block'];
if(($block->module) == 'todays_hours'){
$parts = explode('__', $block->delta);
if (isset($parts[1]) && $parts[1] == 'mobile') {
$variables['classes_array'][] = 'todays-hours-block--mobile';
}
else{
$variables['classes_array'][] = 'todays-hours-block';
}
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*
* Provide checkbox and description for Today's Hours block per node
* with Opening Hours enabled.
*/
function todays_hours_form_node_form_alter(&$form, $form_state) {
$node = $form['#node'];
$variable_name = 'opening_hours_enabled_' . $node->type;
$opening_hours_enabled = variable_get($variable_name, array());
if (!empty($opening_hours_enabled) && user_access('administer todays hours')) {
$form['opening_hours'] = array(
'#title' => t('Opening Hours'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#group' => 'additional_settings',
);
$form['opening_hours']['todays_hours_enabled'] = array(
'#title' => t('Enable Today\'s Hours block for this %type node.', array('%type' => node_type_get_name($node))),
'#type' => 'checkbox',
'#default_value' => isset($node->todays_hours_enabled) ? $node->todays_hours_enabled : FALSE,
);
$form['opening_hours']['todays_hours_info'] = array(
'#title' => t('Block description'),
'#description' => t('A brief description of the block. Used on the Blocks administration page.'),
'#type' => 'textfield',
'#default_value' => isset($node->todays_hours_info) ? $node->todays_hours_info : FALSE,
'#size' => 60,
'#maxlength' => 128,
'#states' => array(
'invisible' => array(
':input[name="todays_hours_enabled"]' => array('checked' => FALSE),
),
),
);
$form['opening_hours']['todays_hours_additional_mobile'] = array(
'#title' => t('Additional mobile block'),
'#type' => 'checkbox',
'#description' => t('Create an additional mobile block for this node.'),
'#states' => array(
'invisible' => array(
':input[name="todays_hours_enabled"]' => array('checked' => FALSE),
),
),
);
$form['opening_hours']['todays_hours_block_title'] = array(
'#title' => t('Block title'),
'#description' => t("The title of the block."),
'#type' => 'textfield',
'#default_value' => isset($node->todays_hours_block_title) ? $node->todays_hours_block_title : t("Today's hours"),
'#size' => 60,
'#maxlength' => 128,
'#states' => array(
'invisible' => array(
':input[name="todays_hours_enabled"]' => array('checked' => FALSE),
),
),
);
$form['opening_hours']['todays_hours_machine_name'] = array(
'#type' => 'machine_name',
'#title' => t('Machine name'),
'#description' => t('A machine readable name to identify the block; helps with sharing across sites'),
'#machine_name' => array(
'exists' => 'todays_hours_check_machine_name_exists',
'source' => array('todays_hours_block_name($node->todays_machine_name)'), // the name of the source field that we will Take the User Friendly name from and convert it to Machine Friedly name ), ); - See more at: http://drupalst.com/blog/how-create-machine-name-field-drupal-7#sthash.8ptRx8py.dpuf
//'replace_pattern' => '[^a-z0-9-]+',
//'replace' => '_',
),
'#required' => FALSE,
'#size' => 60,
'#maxlength' => 60,
'#default_value' => isset($node->todays_hours_machine_name) ? $node->todays_hours_machine_name : FALSE,
'#states' => array(
'invisible' => array(
':input[name="todays_hours_enabled"]' => array('checked' => FALSE),
),
),
);
$form['opening_hours']['todays_hours_more_url'] = array(
'#title' => t('More URL path'),
'#type' => 'textfield',
'#description' => t('The URL path for the \'More\' link (href attribute).'),
'#default_value' => isset($node->todays_hours_more_url) ? $node->todays_hours_more_url : FALSE,
'#size' => 60,
'#maxlength' => 128,
'#states' => array(
'invisible' => array(
':input[name="todays_hours_enabled"]' => array('checked' => FALSE),
),
),
);
$form['opening_hours']['todays_hours_additional_mobile'] = array(
'#title' => t('Additional mobile block'),
'#type' => 'checkbox',
'#description' => t('Create an additional mobile block for this node.'),
'#default_value' => isset($node->todays_hours_additional_mobile) ? $node->todays_hours_additional_mobile : FALSE,
'#states' => array(
'invisible' => array(
':input[name="todays_hours_enabled"]' => array('checked' => FALSE),
),
),
);
}
}
/**
* KB added for machine_name field.
*/
function todays_hours_check_machine_name_exists($name) {
$exists = db_query('SELECT 1 FROM {opening_hours_today_block} WHERE machine_name = :name', array(':name' => $name))->fetchField();
return $exists;
}
/**
* Implements hook_permission().
*/
function todays_hours_permission() {
return array(
'administer todays hours' => array(
'title' => t('Administer today\'s hours configuration'),
'description' => t('Allows the user to administer Today\'s Hours blocks.'),
),
);
}
/**
* Implements hook_node_load().
*/
function todays_hours_node_load($nodes, $types) {
foreach ($nodes as $nid => $node) {
$data = todays_hours_get_todays_hours_enabled($node->nid);
$node->todays_hours_enabled = (bool) $data['nid'];
$node->todays_hours_info = $data['info'];
$node->todays_hours_block_title = $data['title'];
$node->todays_hours_machine_name = $data['machine_name'];
$node->todays_hours_more_url = $data['more_url'];
$node->todays_hours_additional_mobile = $data['additional_mobile'];
}
}
/**
* Implements hook_node_insert().
*/
function todays_hours_node_insert($node) {
if (isset ($node->todays_hours_enabled)) {
todays_hours_set_todays_hours_enabled($node->nid, $node->todays_hours_info, $node->todays_hours_machine_name, $node->todays_hours_block_title, $node->todays_hours_more_url, $node->todays_hours_additional_mobile);
}
}
/**
* Implements hook_node_update().
*/
function todays_hours_node_update($node) {
todays_hours_delete_todays_hours_enabled($node->nid);
if ($node->todays_hours_enabled) {
todays_hours_set_todays_hours_enabled($node->nid, $node->todays_hours_info, $node->todays_hours_machine_name, $node->todays_hours_block_title, $node->todays_hours_more_url, $node->todays_hours_additional_mobile);
}
else {
todays_hours_delete_todays_hours_block($node->todays_hours_machine_name);
}
}
/**
* Implements hook_node_delete().
*/
function todays_hours_node_delete($node) {
todays_hours_delete_todays_hours_block($node->todays_hours_machine_name);
todays_hours_delete_todays_hours_enabled($node->nid);
}
/**
* Determine if a node is set to create a Today's Hours block
*
* @param int $nid
* The node id of the node in question.
*
* @return $result
* An associative array containing:
* - nid: the node id with the enabled Today's Hours block
* - info: the block description
* - machine_name: a machine readable name of the block
* - more_url: the URL path for more information
* - additional_mobile: option for an additional block themed for mobile
*/
function todays_hours_get_todays_hours_enabled($nid) {
if (is_numeric($nid)) {
$result = db_query("SELECT nid, info, machine_name, title, more_url, additional_mobile FROM {opening_hours_today_block} WHERE nid = :nid",
array('nid' => $nid))->fetchAssoc();
return $result;
}
return FALSE;
}
/**
* Add an entry for a node's Today's Hours block.
*
* @param int $nid
* The node id of the node in question.
* - info: the block description
* - more_url: the URL path for more information
* - additional_mobile: option for an additional block themed for mobile
*/
function todays_hours_set_todays_hours_enabled($nid, $info, $machine_name, $title, $more_url, $additional_mobile) {
if (is_numeric($nid) &! todays_hours_get_todays_hours_enabled($nid)) {
db_insert('opening_hours_today_block')
->fields(array('nid' => $nid, 'info' => $info, 'machine_name' => $machine_name, 'title' => $title, 'more_url' => $more_url, 'additional_mobile' => $additional_mobile))
->execute();
}
}
/**
* Remove an entry for a node's Today's Hours block.
*
* @param int $nid
* The node id of the node in question.
*/
function todays_hours_delete_todays_hours_enabled($nid) {
if (is_numeric($nid)) {
db_delete('opening_hours_today_block')
->condition('nid', $nid)
->execute();
}
}
/**
* Remove a Today's Hours block configuration when deleted
*
* @param var $machine_name
* The machine name of the node will ID the related blocks
*/
function todays_hours_delete_todays_hours_block($machine_name) {
db_delete('block')
->condition('module', 'todays_hours')
->condition(db_or()-> condition('delta', $machine_name)-> condition('delta', $machine_name .'__mobile'))
->execute();
}