forked from ding2/ding_reservation
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathding_reservation.module
More file actions
703 lines (642 loc) · 22.2 KB
/
ding_reservation.module
File metadata and controls
703 lines (642 loc) · 22.2 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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
<?php
/**
* @file
* Handles display and creation/deletion of reservations for users.
*/
// Default interest period is ~six months.
define('DING_RESERVATION_DEFAULT_INTEREST_PERIOD', 183 * 86400);
/**
* Implements hook_ctools_plugin_directory().
*
* It simply tells panels where to find the .inc files that define various
* args, contexts, content_types. In this case the subdirectories of
* ctools_plugin_example/panels are used.
*/
function ding_reservation_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' && !empty($plugin)) {
return "plugins/$plugin";
}
}
/**
* Implements hook_menu().
*/
function ding_reservation_menu() {
// @todo ding_reservation really doesn't know that this path is correct. How
// to handle it?
$items['user/%user/status/reservations/update/%'] = array(
'title' => 'Update reservations',
'page callback' => 'drupal_get_form',
'page arguments' => array('ding_reservation_update_reservations_form', 1, 5),
'access callback' => 'ding_reservation_access',
'access arguments' => array(1),
);
return $items;
}
/**
* Access callback. Ensure that current user is the same.
*/
function ding_reservation_access($account) {
global $user;
return $user->uid == $account->uid;
}
/**
* Implements hook_ding_entity_menu().
*/
function ding_reservation_ding_entity_menu(&$items, $type, $path, $index) {
if ($type == 'ding_entity') {
$items[$path . '/reserve'] = array(
'title' => 'Reserve',
'page callback' => 'ding_provider_get_form',
'page arguments' => array('ding_reservation_reserve_form', $index),
'access callback' => TRUE,
);
}
}
/**
* Implements hook_ding_entity_buttons().
*/
function ding_reservation_ding_entity_buttons($type, $entity) {
if ($type == 'ding_entity' && $entity->is('reservable')) {
return array(ding_provider_get_form('ding_reservation_reserve_form', new DingReservationReservableEntity($entity), TRUE));
}
}
/**
* Implements hook_ding_provider_user().
*/
function ding_reservation_ding_provider_user() {
return array(
'reservation' => array(
'required' => TRUE,
'install time setup' => TRUE,
),
);
}
/**
* Implements hook_forms().
*
* If the forms listing ready and not ready for pickup reservations is listed
* on the same page, they need to have different form ids in order for Drupal
* to be able to tell which was submitted. As we're using one builder
* function, use this to let have their own form id.
*/
function ding_reservation_forms($form_id, $args) {
$forms['ding_reservation_reservations_ready_form'] = array(
'callback' => 'ding_reservation_reservations_form',
);
$forms['ding_reservation_reservations_notready_form'] = array(
'callback' => 'ding_reservation_reservations_form',
);
return $forms;
}
/**
* Reserve form callback.
*
* Note that this form relies on form caching, which is triggered by
* ajaxifying the submit button.
*/
function ding_reservation_reserve_form($form, &$form_state, $reservable, $hide_options = FALSE) {
global $user;
if (!($reservable instanceof DingReservationReservable)) {
// Assume that we were given a reservable entity.
$reservable = new DingReservationReservableEntity($reservable);
}
$entity = $reservable->getEntity();
$uri = ding_entity_uri('ding_entity', $entity);
// We post to our own path as we might want to rebuild the form.
// @todo move *_get_destination to ding_base?
if ($_GET['q'] != $uri['path'] . '/reserve') {
$form['#action'] = url($uri['path'] . '/reserve', array('query' => ding_provider_get_destination()));
}
$form['reservable'] = array(
'#type' => 'value',
'#value' => $reservable,
);
$form['provider_options'] = array(
'#type' => 'value',
'#value' => array(),
);
$hide_options = !isset($form_state['options_hidden']) ? $hide_options : FALSE;
$form_state['options_hidden'] = $hide_options;
if (!$hide_options) {
if (ding_provider_implements('reservation', 'options') && $provider_form = ding_provider_invoke('reservation', 'options', 'create', $user, $reservable)) {
$form['provider_options'] = $provider_form + array(
'#tree' => TRUE,
);
}
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Reserve'),
'#ajax' => array(
'callback' => 'ding_reservation_reserve_form_callback',
'wrapper' => 'ding-reservation-reserve-form',
),
);
return $form;
}
/**
* Form validation.
*/
function ding_reservation_reserve_form_validate($form, &$form_state) {
global $user;
if (ding_provider_implements('reservation', 'options_validate')) {
$res = ding_provider_invoke('reservation', 'options_validate', 'create', $user, $form_state['values']['reservable'], $form_state['values']['provider_options']);
/**
* We cannot set the value of the individual provider form elements, as
* they might not have been show, and thus not exist. However, setting the
* value of the parent element to an associative array gives the same end
* result.
*/
$provider_options = array();
foreach ($res as $key => $value) {
if (is_array($value) && !empty($value['#error'])) {
if (!$form_state['options_hidden']) {
// Only show an error if the user had a choice.
form_error($form['provider_options'], $res['#error']);
}
else {
// Else simply rebuild the form.
$form_state['rebuild'] = TRUE;
}
}
else {
$provider_options[$key] = $value;
}
}
if (!empty($provider_options)) {
form_set_value($form['provider_options'], $provider_options, $form_state);
}
}
}
/**
* Form submission handler.
*/
function ding_reservation_reserve_form_submit($form, &$form_state) {
global $user;
if (ding_provider_implements('reservation', 'options_submit')) {
ding_provider_invoke('reservation', 'options_submit', 'create', $user, $form_state['values']['reservable'], $form_state['values']['provider_options']);
}
if ($form_state['values']['reservable']) {
$reservable = $form_state['values']['reservable'];
try {
$reservation_result = ding_provider_invoke('reservation', 'create', $user, $reservable->getProviderId(), $form_state['values']['provider_options']);
$form_state['reserved'] = TRUE;
// @todo use profile
if (!empty($reservation_result['branch']) && ding_provider_implements('reservation', 'branch_name')) {
$branch_name = ding_provider_invoke('reservation', 'branch_name', $reservation_result['branch']);
drupal_set_message(t('"@title" reserved and will be available for pickup at @branch.', array('@title' => $reservable->getTitle(), '@branch' => $branch_name)));
}
else {
drupal_set_message(t('"@title" reserved.', array('@title' => $reservable->getTitle())));
}
if (is_array($reservation_result) and !empty($reservation_result['queue_number'])) {
drupal_set_message(t('You are number @number in queue.', array('@number' => $reservation_result['queue_number'])));
}
}
catch (DingProviderUserException $e) {
drupal_set_message($e->getMessageT(array('@title' => $reservable->getTitle())), 'error');
}
catch (DingProviderAuthException $e) {
// Just rethrow.
throw $e;
}
catch (Exception $e) {
drupal_set_message(t('An error occurred while reserving item. Please try again later.'), 'error');
watchdog('ding_reservation', 'Error while reserving, message: @message', array('@message', $e->getMessage()), WATCHDOG_ERROR);
}
}
}
/**
* Ajax callback.
*/
function ding_reservation_reserve_form_callback($form, &$form_state) {
$response = array(
'#type' => 'ajax',
'#commands' => array(),
);
$html = theme('status_messages');
if ($form_state['rebuild'] || form_get_errors()) {
// Redisplay form.
$html .= drupal_render($form);
}
$response['#commands'][] = ajax_command_ding_popup('ding_reservation', t('Reserve'), $html);
return $response;
}
/**
* Show a reservation list form.
*/
function ding_reservation_reservations_form($form, &$form_state, $items = array(), $type = 'not_ready_for_pickup') {
$options = array();
$destination = drupal_get_destination();
switch ($type) {
case 'ready_for_pickup':
$header = array(
'title' => t('Title'),
'created' => t('Created date'),
'pickup_date' => t('Pickup date'),
'pickup_branch' => t('Pickup branch'),
'operations' => '',
);
uasort($items, 'ding_reservation_sort_queue_by_pickup_date');
break;
case 'not_ready_for_pickup':
$header = array(
'title' => t('Title'),
'created' => t('Created date'),
'expiry' => t('Expiry date'),
'pickup_branch' => t('Pickup branch'),
'queue_number' => t('Queue number'),
'operations' => '',
);
uasort($items, 'ding_reservation_sort_queue_by_queue_number');
break;
}
foreach ($items as $id => $item) {
$entity = $item->entity;
$pickup_branch = ding_provider_invoke('reservation', 'branch_name', $item->pickup_branch_id);
switch ($type) {
case 'ready_for_pickup':
$options[$item->id] = array(
'title' => array(
'data' => array($entity ? ting_object_view($entity, 'user_list') : array('#markup' => $item->display_name)),
'class' => 'title',
),
'created' => array(
'data' => $item->created ? format_date(strtotime(check_plain($item->created)), 'date_only') : '',
'class' => 'created-date',
),
'pickup_date' => array(
'data' => $item->pickup_date ? format_date(strtotime(check_plain($item->pickup_date)), 'date_only' ) : '',
'class' => 'pickup-date',
),
'pickup_branch' => array(
'data' => $pickup_branch ? check_plain($pickup_branch) : '',
'class' => 'pickup-branch',
),
'operations' => array(
'data' => array(
'#prefix' => '<div class="delete-reservation">',
'#suffix' => '</div>',
'#type' => 'submit',
'#submit' => array('ding_reservation_reservations_delete_submit'),
'#reservation_id' => $item->id,
'#name' => 'delete-' . preg_replace('/\W/', '-', $item->id), // Need this for formAPI can tell buttons apart
'#value' => t('Delete'),
'#options' => array('query' => $destination),
),
'class' => 'operations',
),
);
if (isset($item->order_arrived) && !$item->order_arrived) {
$options[$item->id]['title']['data'][] = array(
'#type' => 'markup',
'#prefix' => '<p class="order-arrived">',
'#markup' => t('The material is in transit and is still not available for loan on the library'),
'#suffix' => '</p>',
);
} else {
if (isset($item->pickup_order_id)) {
$options[$item->id]['title']['data'][] = array(
'#type' => 'markup',
'#prefix' => '<p class="pickup-order-id">',
'#markup' => t('(Pickup id: @pickup_id)', array('@pickup_id' => check_plain($item->pickup_order_id))),
'#suffix' => '</p>',
);
}
if (!isset($item->pickup_order_id) && isset($item->order_id)) {
$options[$item->id]['title']['data'][] = array(
'#type' => 'markup',
'#prefix' => '<p class="order-id">',
'#markup' => t('(Order no. @order_id)', array('@order_id' => check_plain($item->order_id))),
'#suffix' => '</p>',
);
}
}
$form['reservations'] = array(
'#type' => 'tableselect_form',
'#header' => $header,
'#options' => $options,
'#empty' => t('No reservations ready for pickup'),
);
break;
case 'not_ready_for_pickup':
$options[$item->id] = array(
'title' => array(
'data' => array($entity ? ting_object_view($entity, 'user_list') : array('#markup' => $item->display_name)),
'class' => 'title',
),
'created' => array(
'data' => $item->created ? format_date(strtotime(check_plain($item->created)), 'date_only') : '',
'class' => 'created-date',
),
'expiry' => array(
'data' => $item->created ? format_date(strtotime(check_plain($item->expiry)), 'date_only') : '',
'class' => 'expire-date',
),
'pickup_branch' => array(
'data' => $pickup_branch ? check_plain($pickup_branch) : '',
'class' => 'pickup-branch',
),
'queue_number' => array(
'data' => $item->queue_number ? check_plain($item->queue_number) : '',
'class' => 'queue-number',
),
'operations' => array(
'data' => array(
'#prefix' => '<div class="delete-reservation">',
'#suffix' => '</div>',
'#type' => 'submit',
'#submit' => array('ding_reservation_reservations_delete_submit'),
'#reservation_id' => $item->id,
'#name' => 'delete-' . preg_replace('/\W/', '-', $item->id), // Need this for formAPI can tell buttons apart
'#value' => t('Delete'),
'#options' => array('query' => $destination),
),
'class' => 'operations',
),
);
if (isset($item->order_id)) {
$options[$item->id]['title']['data'][] = array(
'#type' => 'markup',
'#prefix' => '<p class="order-id">',
'#markup' => t('(Order no. @order_id)', array('@order_id' => check_plain($item->order_id))),
'#suffix' => '</p>',
);
}
$form['reservations'] = array(
'#type' => 'tableselect_form',
'#header' => $header,
'#options' => $options,
'#empty' => t('No Reservations'),
);
break;
}
}
$form['submit'] = array(
'#prefix' => '<div class="delete-reservations">',
'#suffix' => '</div>',
'#submit' => array('ding_reservation_reservations_delete_submit'),
'#type' => 'submit',
'#value' => t('Delete reservations'),
);
if ($type == 'not_ready_for_pickup') {
$form['update'] = array(
'#prefix' => '<div class="update-reservations">',
'#suffix' => '</div>',
'#type' => 'submit',
/* '#submit' => array('ding_reservation_update_reservation_form'), */
'#submit' => array('ding_reservation_reservations_form_submit'),
'#value' => t('Update reservations'),
'#ajax' => array(
'callback' => 'ding_reservation_reservations_form_callback',
'wrapper' => 'ding-reservation-reservations-form',
),
);
}
return $form;
}
function ding_reservation_reservations_delete_submit($form, &$form_state) {
global $user;
if (!empty($form_state['triggering_element']['#reservation_id'])) {
$reservations = array($form_state['triggering_element']['#reservation_id']);
}
else {
$reservations = array_filter($form_state['values']['reservations']);
}
foreach ($reservations as $entity_id) {
ding_provider_invoke('reservation', 'delete', $user, $entity_id);
}
}
/**
* Submit handler for the form.
*/
function ding_reservation_reservations_form_submit($form, &$form_state) {
global $user;
$ids = join(',', array_map('rawurlencode', array_filter($form_state['values']['reservations'],'is_string')));
$form_state['encoded_reservations'] = $ids;
$form_state['redirect'] = array('user/' . $user->uid . '/status/reservations/update/' . $ids, array('query' => drupal_get_destination()));
}
/**
* Ajax callback.
*/
function ding_reservation_reservations_form_callback($form, &$form_state) {
global $user;
$response = array(
'#type' => 'ajax',
'#commands' => array(),
);
$html = theme('status_messages');
$html .= drupal_render(drupal_get_form('ding_reservation_update_reservations_form', $user, $form_state['encoded_reservations']));
if ($html) {
$response['#commands'][] = ajax_command_ding_popup('ding_reservation', t('Update reservations'), $html, array('refresh' => TRUE));
}
return $response;
}
/**
* Update reservations form.
*/
function ding_reservation_update_reservations_form($form, $form_state, $account, $reservation_ids) {
global $user;
$ids = array_map('rawurldecode', explode(',', $reservation_ids));
$form_state['cache'] = TRUE;
$form['reservations'] = array(
'#type' => 'value',
'#value' => $ids,
);
if (ding_provider_implements('reservation', 'options') && $provider_form = ding_provider_invoke('reservation', 'options', 'update', $user, NULL)) {
$form['provider_options'] = $provider_form + array(
'#tree' => TRUE,
);
}
$form['submit'] = array(
'#type' => 'submit',
'#submit' => array('ding_reservation_update_reservations_form_submit'),
'#ajax' => array(
'callback' => 'ding_reservation_update_reservations_form_callback',
'wrapper' => 'ding-reservation-update-reservation-form',
),
'#value' => t('Update reservations'),
);
return $form;
}
/**
* Validation handler.
*/
function ding_reservation_update_reservations_form_validate($form, &$form_state) {
global $user;
if (ding_provider_implements('reservation', 'options_validate')) {
$res = ding_provider_invoke('reservation', 'options_validate', 'create', $user, $form_state['values']['reservations'], $form_state['values']['provider_options']);
/**
* We cannot set the value of the individual provider form elements, as
* they might not have been show, and thus not exist. However, setting the
* value of the parent element to an associative array gives the same end
* result.
*/
$provider_options = array();
foreach ($res as $key => $value) {
if (is_array($value) && !empty($value['#error'])) {
if (!$form_state['options_hidden']) {
// Only show an error if the user had a choice.
form_error($form['provider_options'], $res['#error']);
}
else {
// Else simply rebuild the form.
$form_state['rebuild'] = TRUE;
}
}
else {
$provider_options[$key] = $value;
}
}
if (!empty($provider_options)) {
form_set_value($form['provider_options'], $provider_options, $form_state);
}
}
}
/**
* Submit handler.
*
* Updates selected reservations.
*/
function ding_reservation_update_reservations_form_submit($form, &$form_state) {
global $user;
ding_provider_invoke('reservation', 'update', $user, $form_state['values']['reservations'], $form_state['values']['provider_options']);
}
/**
* Ajax callback function.
*/
function ding_reservation_update_reservations_form_callback($form, &$form_state) {
$response = array(
'#type' => 'ajax',
'#commands' => array(),
);
$html = theme('status_messages') . t('Your reservations has been updated.');
$response['#commands'][] = ajax_command_ding_popup('ding_reservation', t('Update reservations'), $html, array('refresh' => TRUE));
return $response;
}
/**
* Default options handling.
*
* Reservation providers may use these to use standard implementations of
* widgets like pickup branch and get much logic for free.
*/
/**
* Create a pickup branch selector.
*
* Returns form element(s) for selecting a pickup branch.
*/
function ding_reservation_default_options_branch($type, $name, $default, $options) {
$create = ($type == 'create');
$allowed_branches = ($create ? array() : array('' => t('No change'))) + $options;
$default_value = $create ? $default : '';
$form[$name] = array(
'#type' => 'select',
'#title' => t('Select branch'),
'#options' => $allowed_branches,
'#default_value' => $default_value,
);
if ($create) {
$form[$name . 'description'] = array(
'#markup' => '<p>' . t('In order to make quick reservations, you must select a default pickup branch.') . '</p>',
);
}
return $form;
}
/**
* Create an interest period selector.
*
* Returns form element(s) for selecting an interest period.
*/
function ding_reservation_interest_period_selector($type, $name, $default, $options) {
$create = ($type == 'create');
$allowed_periods = ($create ? array() : array('' => t('No change'))) + $options;
// $default_value = $create ? $default : '';
$form[$name] = array(
'#type' => 'select',
'#title' => t('Select interest period'),
'#options' => $allowed_periods,
'#default_value' => $default,
);
if ($create) {
$form[$name . 'description'] = array(
'#markup' => '<p>' . t('Select an interest period.') . '</p>',
);
}
return $form;
}
/**
* Validate pickup branch selector.
*
* Returns what ding_reservation expects.
*/
function ding_reservation_default_options_branch_validate($type, $name, $default, $values) {
if (empty($values[$name])) {
if ($type == 'create' && empty($default)) {
$result['openruth_preferred_branch'] = array(
'#error' => 'You must select a branch',
);
}
else {
$result[$name] = $default;
}
return $result;
} else {
return $values;
}
}
/**
* Submit pickup branch selector.
*
* Returns new properties to save, if any.
*/
function ding_reservation_default_options_branch_submit($type, $name, $default, $values) {
$result = array();
if ($type == 'create' && !empty($values[$name]) && $values[$name] != $default) {
$result[$name] = $values['name'];
}
return $result;
}
/**
* Callback function to sort array by pickup date
*/
function ding_reservation_sort_queue_by_pickup_date($a, $b) {
if ($a->pickup_date == $b->pickup_date) {
return 0;
}
return ($a->pickup_date < $b->pickup_date) ? -1 : 1;
}
/**
* Callback function for sorting loans by queue_number
*/
function ding_reservation_sort_queue_by_queue_number($a, $b) {
if ($a->queue_number == $b->queue_number) {
return 0;
}
return ($a->queue_number < $b->queue_number) ? -1 : 1;
}
/**
* Interface for reservable items.
*/
interface DingReservationReservable {
public function getProviderId();
// @todo, this should be optional.
public function getEntity();
public function getTitle();
}
/**
* A reservable entity.
*/
class DingReservationReservableEntity implements DingReservationReservable {
public function __construct($entity) {
$this->entity = $entity;
}
public function getProviderId() {
return $this->entity->provider_id;
}
public function getEntity() {
return $this->entity;
}
public function getTitle() {
return $this->entity->title;
}
}