-
Notifications
You must be signed in to change notification settings - Fork 3
/
ding_custom_reservation.module
423 lines (387 loc) · 14.7 KB
/
ding_custom_reservation.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
<?php
/**
* @file
* Customizes reservation flow for users.
*/
define('DING_CUSTOM_RESERVATION_PRIMARY_BUTTON', 0);
define('DING_CUSTOM_RESERVATION_SECONDARY_BUTTON', 1);
/**
* Implements hook_menu().
*/
function ding_custom_reservation_menu() {
$items = array();
$items['admin/config/ding/customreservation'] = [
'title' => 'Ding custom reservation Settings',
'description' => 'Manage custom reservations module',
'page callback' => 'drupal_get_form',
'page arguments' => ['ding_custom_reservation_admin_form'],
'access arguments' => ['administer site configuration'],
'file' => 'includes/ding_custom_reservation.admin.inc',
'type' => MENU_NORMAL_ITEM,
];
$items['admin/config/ding/customreservation/settings'] = [
'type' => MENU_DEFAULT_LOCAL_TASK,
'title' => 'Custom reservation settings',
'weight' => 1,
];
$items['admin/config/ding/customreservation/statistics'] = [
'title' => 'Custom reservation statistics',
'description' => 'Statistics for custom reservations module',
'page callback' => 'drupal_get_form',
'page arguments' => ['ding_custom_reservation_statistics_form'],
'access arguments' => ['administer site configuration'],
'file' => 'includes/ding_custom_reservation.statistics.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 2,
];
$items['ting/object/%ting_object/customreserve'] = array(
'page callback' => 'ding_custom_reservation_reserve_ajax',
'page arguments' => array(2,4),
'delivery callback' => 'ajax_deliver',
'access arguments' => array('perform reservation'),
);
return $items;
}
/**
* Implements hook_cron().
*
* Expire outdated statistic entries.
*/
function ding_custom_reservation_cron() {
$days = (int) variable_get('ding_custom_reservation_keep_statistics_days', 0);
if ($days != 0) {
// Get timestamp for 00:00 today minus days.
$day = _ding_custom_reservation_get_day() - $days * 86400;
try {
db_delete('ding_custom_reservation_statistics')
->condition('day', $day, '<')
->execute();
} catch (Exception $e) {
watchdog('ding_custom_reservation', 'Failed to delete old statistics from the database: %message', ['%message'=> $e->getMessage()], WATCHDOG_ERROR);
}
}
}
/*
* Implements hook_menu_alter().
*/
function ding_custom_reservation_menu_alter(&$items){
// We handle the reserve periodics callback with our custom reservation form.
$items['ting/object/%ting_object/reserve/%/%/%']['page callback'] = 'ding_custom_reservation_periodical_reserve_ajax';
}
/**
* Implements hook_module_implements_alter().
*
* This module overrides the reservation button provided in ding_reservation so that hook is removed.
*/
function ding_custom_reservation_module_implements_alter(&$implementations, $hook) {
if ($hook == 'ding_entity_buttons') {
if (isset($implementations['ding_reservation'])) {
unset($implementations['ding_reservation']);
}
}
}
/**
* Implements hook_form_profile2_edit_PROFILE_TYPE_form_alter().
*
* Removes the redundant interest period field.
*/
function ding_custom_reservation_form_profile2_edit_provider_fbs_form_alter(&$form, &$form_state) {
$form['profile_provider_fbs']['field_fbs_interest_period']['#access'] = false;
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Alters the ding_reservation_reserve_form with custom interest periods.
*/
function ding_custom_reservation_form_ding_reservation_reserve_form_alter(&$form, &$form_state, $reservable, $hide_options = FALSE) {
if (isset($form['provider_options'])) {
if (isset($form['provider_options']['fbs_preferred_branch'])) {
unset($form['provider_options']['fbs_preferred_branch']);
}
if (isset($form['provider_options']['interest_period'])) {
$form['provider_options']['interest_period']['#options'] = ding_custom_reservation_interest_periods();
$form['provider_options']['interest_period']['#default_value'] = variable_get('ding_custom_reservations_interest_period');
}
}
$title = variable_get('ding_custom_reservation_title_text', t('Reservation'));
$description = variable_get('ding_custom_reservation_title_description', ['value' => '']);
$form['customtitle'] = array(
'#markup' => '<div class="popupbar-title"><h1>' . $title . '</h1></div>',
'#weight' => '-20',
);
if (!empty($description['value'])) {
$form['customdescription'] = array(
'#prefix' => '<div class="form-item form-type-markup description">',
'#markup' => check_markup($description['value'], 'ding_wysiwyg'),
'#suffix' => '</div>',
'#weight' => '-10',
);
}
$form['#validate'] = ['ding_custom_reservation_reserve_form_validate'];
// Add JS to handle focus off.
$form['#attached']['js'] = [drupal_get_path('module', 'ding_custom_reservation') . '/js/ding_custom_reservation.js'];
}
/**
* Ajax periodical reservation callback.
*
* @param TingEntity $entity
* Ting entity for the periodical.
* @param string $local_id
* Local library id (faust) for the issue (base64 encoded).
* @param string $volume
* Volume number (for year books the year, base64 encoded).
* @param string $issue
* The issue in the volume to reserve (base64_encoded).
*
* @return array
* Render array with Ajax commands.
*/
function ding_custom_reservation_periodical_reserve_ajax(TingEntity $entity, $local_id, $volume, $issue) {
$reservable = new DingAvailabilityPeriodicalReservable(base64_decode($local_id), base64_decode($volume), base64_decode($issue), $entity);
// Use ding reservation logic to create the reservation and ajax commands.
return ding_custom_reservation_reserve_ajax($entity, null, $reservable);
}
/**
* Implements hook_ding_entity_buttons().
*/
function ding_custom_reservation_ding_entity_buttons($type, $entity, $view_mode, $widget = 'default') {
$button = '';
if ($type == 'ding_entity' && $entity->is('library_material')) {
switch ($widget) {
case 'ajax':
// The AJAX-widget will use AJAX-request for checking if entity is
// reservable and also when performing the reservation.
$button = [];
$button[] = [
'#theme' => 'link',
'#text' => t('Reserve'),
'#path' => 'ting/object/' . $entity->id . '/customreserve',
'#options' => [
'attributes' => [
'class' => [
'action-button',
'reserve-button',
'use-ajax',
'js-check-reservability',
],
'data-local-id' => $entity->localId,
],
'html' => FALSE,
],
'#attached' => [
'js' => [
[
'type' => 'file',
'data' => drupal_get_path('module', 'ding_reservation') . '/js/ding_reservation_reservability.js',
],
],
'library' => [
['system', 'drupal.ajax'],
],
],
];
if (variable_get('ding_custom_reservation_secondary_reservation_button', FALSE)) {
$button[] = [
'#theme' => 'link',
'#text' => t(variable_get('ding_custom_reservation_secondary_reservation_button_text', '')),
'#path' => 'ting/object/' . $entity->id . '/customreserve/fast',
'#options' => [
'attributes' => [
'class' => [
'action-button',
'reserve-button',
'use-ajax',
'js-check-reservability',
],
'data-local-id' => $entity->localId,
],
'html' => FALSE,
],
'#attached' => [
'js' => [
[
'type' => 'file',
'data' => drupal_get_path('module', 'ding_reservation') . '/js/ding_reservation_reservability.js',
],
],
'library' => [
['system', 'drupal.ajax'],
],
],
];
}
break;
// TODO. Unsure if this is ever reached. Would invoke the standard reservation flow.
default:
// The last parameter to the form below (TRUE) hides the provider
// options in the form (interest period and branch).
$reservable = ding_provider_invoke('reservation', 'is_reservable', [$entity->localId]);
if (!empty($reservable[$entity->localId])) {
$button = array(
ding_provider_get_form('ding_reservation_reserve_form', new DingReservationReservableEntity($entity), TRUE),
);
}
break;
}
}
return $button;
}
/**
* Ajax entry callback.
*
* Try to reserve the material, if the user is not logged in trigger a ajax
* login.
*
* This is a copy of the corresponding method in ding_reservation. With few lines off customizing.
*
* @param TingEntity $entity
* Ting entity object.
* @param DingReservationReservable $reservable
* Object with information about the entity to reserve. Used to make
* reservation of periodical, where volume and issue is part of the
* reservation.
*
* @return array
* Render array with Ajax commands.
*/
function ding_custom_reservation_reserve_ajax($entity, $fast = null, $reservable = NULL) {
$commands = array();
// Check if the logged-in user is a library user.
global $user;
if (!user_is_logged_in()) {
// Trigger log-in (the reservation link will be triggered on success).
$commands[] = ajax_command_ding_user_authenticate('');
}
elseif (!ding_user_is_provider_user($user)) {
// Error not library user.
$commands[] = ajax_command_ding_popup('ding_reservation', t('Error'), '<p>' . t('Only library user can make reservations.') . '</p>');
}
elseif (!(is_object($entity) && $entity instanceof TingEntity)) {
// Error not ting entity.
$commands[] = ajax_command_ding_popup('ding_reservation', t('Error'), '<p>' . t('Unable to load information about the material.') . '</p>');
}
else {
// Try to make reservation.
// Start off customized section.
try {
if (is_null($reservable)) {
// If no object passed assume "normal" reservation (not periodical).
$reservable = new DingReservationReservableEntity($entity);
}
if ($fast != null) {
$interest_period = variable_get('ding_custom_reservation_secondary_reservation_button_interest_period', 60);
$form_state = [];
$form_state['values']['provider_options']['interest_period'] = $interest_period;
$form_state['values']['reservation_type'] = DING_CUSTOM_RESERVATION_SECONDARY_BUTTON;
drupal_form_submit('ding_reservation_reserve_form', $form_state, $reservable);
$status_messages = theme('status_messages');
$commands[] = ajax_command_ding_popup('ding_reservation', t('Reservation'), $status_messages, array('refresh' => TRUE));
} else {
$form = ding_provider_get_form('ding_reservation_reserve_form', $reservable, FALSE);
$commands[] = ajax_command_ding_popup('ding_reservation', t('Reservation'), render($form));
}
// End off customized section.
}
catch (DingProviderAuthException $exception) {
// The form may have thrown an Auth exception, so display login. (the
// reservation link will be triggered on success).
$commands[] = ajax_command_ding_user_authenticate('');
}
catch (Exception $exception) {
// The form may have thrown an auth exception as the login may have
// timed-out (the reservation link will be triggered on success).
$commands[] = ajax_command_ding_popup('ding_reservation', t('Error'), '<p>' . t('Unknown error in reservation, please contact the library.') . '</p>');
// Log exception.
watchdog_exception('ding_reservation', $exception);
}
}
// Return the ajax commands as an render array.
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Reserve form validation.
*/
function ding_custom_reservation_reserve_form_validate($form, &$form_state) {
global $user;
if (!(user_is_logged_in() && ding_user_is_provider_user($user))) {
throw new DingProviderAuthException();
}
ding_custom_reservation_log_statistics($form_state);
}
/**
* Log reservation for statistics.
*/
function ding_custom_reservation_log_statistics($form_state) {
$material_type = '';
$classification = '';
$interest_period = 0;
$reservation_type = DING_CUSTOM_RESERVATION_PRIMARY_BUTTON;
if (isset($form_state['values'])) {
if (isset($form_state['values']['reservable']) && is_a($form_state['values']['reservable'], 'DingReservationReservableEntity')) {
$object = $form_state['values']['reservable'];
$material_type = $object->getEntity()->getType();
$classification = $object->getEntity()->getClassification();
}
if (isset($form_state['values']['provider_options']) && isset($form_state['values']['provider_options']['interest_period']) ) {
$interest_period = $form_state['values']['provider_options']['interest_period'];
}
}
if (empty($material_type) || $interest_period == 0) {
// Something is wrong. Don't log statistics.
return;
}
if (isset($form_state['input']) && isset($form_state['input']['reservation_type'])) {
$reservation_type = DING_CUSTOM_RESERVATION_SECONDARY_BUTTON;
}
$date = _ding_custom_reservation_get_day();
ding_custom_reservation_statistics_update_db($reservation_type, $interest_period, $material_type, $date, $classification );
}
/**
* Update statistics table.
*/
function ding_custom_reservation_statistics_update_db($reservation_type, $interest_period, $material_type, $date, $classification) {
try {
db_insert('ding_custom_reservation_statistics')
->fields(array(
'reservationtype' => $reservation_type,
'interestperiod' => $interest_period,
'materialtype' => $material_type,
'classification' => $classification,
'day' => $date,
))
->execute();
} catch (Exception $e) {
watchdog('ding_custom_reservation', 'Failed to insert statistics data to database: %message', ['%message'=> $e->getMessage()], WATCHDOG_ERROR);
}
}
/**
* Default values for interest period.
*/
function ding_custom_reservation_default_interest_periods() {
$periods = [
1 => 30,
2 => 60,
3 => 90,
6 => 180,
12 => 360,
];
$options = [];
foreach ($periods as $months => $days) {
$options[$days] = format_plural($months, '1 month', '@count months');
}
return $options;
}
/**
* Get interest periods.
*/
function ding_custom_reservation_interest_periods() {
return variable_get('ding_custom_reservation_interest_periods', ding_custom_reservation_default_interest_periods());
}
/**
* Utility time function.
*
* Effectively returns time() rounded down to the nearest day.
*/
function _ding_custom_reservation_get_day() {
return mktime(0, 0, 0);
}