forked from artesis/alma
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalma.module
More file actions
815 lines (716 loc) · 27.8 KB
/
alma.module
File metadata and controls
815 lines (716 loc) · 27.8 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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
<?php
/**
* @file
*
*/
define('ALMA_AUTH_BLOCKED', '4e5531951f55ab8f6895684999c69c2');
/**
* Get list of pickup branches.
*/
function alma_reservation_pickup_branches($account = null) {
// Throw exception if we're not logged in. ?? WHY this method needs no credentials
// ding_user_get_creds($account);
// return alma_client_invoke('get_reservation_branches');
// cache alma pickup branches - they are not likely to change
static $alma_pickup_branches;
if (!isset($alma_pickup_branches)) {
$cache = cache_get('alma_pickup_branches');
if ($cache) {
$alma_pickup_branches = $cache->data;
}
else {
$alma_pickup_branches = alma_client_invoke('get_reservation_branches');
cache_set('alma_pickup_branches', $alma_pickup_branches, 'cache');
}
}
return $alma_pickup_branches;
}
/**
* Get a list of allowed interest periods.
*/
function alma_get_interest_periods() {
$field_info = field_info_field('field_alma_interest_period');
$interest_periods = isset($field_info['settings']['allowed_values']) ? $field_info['settings']['allowed_values'] : FALSE;
return $interest_periods;
}
/**
* Implements hook_requirements().
*/
function alma_requirements($phase) {
$requirements = array();
// Ensure translations don't break at install time.
$t = get_t();
if (!function_exists('simplexml_load_string')) {
$requirements['simplexml'] = array(
'title' => 'SimpleXML',
'description' => $t('The Alma module requires SimpleXML to function. Please install and/or enable SimpleXML in your PHP configuration.'),
'severity' => REQUIREMENT_ERROR,
);
}
return $requirements;
}
/**
* Implements hook_theme().
*/
function alma_theme() {
return array(
// Theming of availability table.
'alma_table' => array(
'variables' => array('data' => array(array())),
'template' => 'alma_table',
),
);
}
/**
* Implements hook_ding_provider().
*/
function alma_ding_provider() {
$path = drupal_get_path('module', 'alma');
return array(
'title' => 'Alma provider',
'settings' => 'alma_settings_form',
'provides' => array(
'availability' => array(
'prefix' => 'availability',
'file' => $path . '/includes/alma.availability.inc',
),
'debt' => array(
'prefix' => 'debt',
'file' => $path . '/includes/alma.debt.inc',
),
'loan' => array(
'prefix' => 'loan',
'file' => $path . '/includes/alma.loan.inc',
),
'reservation' => array(
'prefix' => 'reservation',
'file' => $path . '/includes/alma.reservation.inc',
),
'user' => array(
'prefix' => 'user',
'file' => $path . '/includes/alma.user.inc',
),
'wayf' => array(
'prefix' => 'wayf',
'file' => $path . '/includes/alma.wayf.inc',
),
),
);
}
/**
* Form callback for provider module settings.
*
* This is a regular form callback.
*/
function alma_settings_form() {
$form['alma'] = array(
'#type' => 'fieldset',
'#title' => t('Alma service settings'),
'#tree' => FALSE,
);
$form['alma']['alma_base_url'] = array(
'#type' => 'textfield',
'#title' => t('Alma base URL'),
'#description' => t('Base URL for Alma service.'),
'#required' => TRUE,
'#default_value' => variable_get('alma_base_url', ''),
);
$form['alma']['alma_enable_reservation_deletion'] = array(
'#type' => 'checkbox',
'#title' => t('Enable reservation deletion'),
'#default_value' => variable_get('alma_enable_reservation_deletion', FALSE),
'#description' => t('Allow users to delete their reservations as well as ready for pickup ones.'),
);
$form['alma']['alma_enable_logging'] = array(
'#type' => 'checkbox',
'#title' => t('Enable logging'),
'#default_value' => variable_get('alma_enable_logging', FALSE),
'#description' => t('Logs requests to the Alma webservice. Sensitive information such as CPR number and PIN code is stripped from the requests.'),
);
$form['alma']['alma_enable_debug'] = array(
'#type' => 'checkbox',
'#title' => t('Enable debug'),
'#default_value' => variable_get('alma_enable_debug', FALSE),
'#description' => t('Logs item requests time usage.'),
);
return system_settings_form($form);
}
/**
* Implements hook_form_FORM_ID_alter().
* pjo notes: user_profile_form
* Add in pincode validation.
*/
function alma_form_user_profile_form_alter(&$form, &$form_state) {
// Ensure that we're dealing with a provider user.
if (!ding_user_is_provider_user($form_state['user'])) {
return;
}
global $user;
$profile2 = profile2_load_by_user($user, 'provider_alma');
// attach profile2 form to user_profile_form.
// profile2 form could be shown by editing people->permissions->profile2, but profile2 adds a tab and we don't want that.
if (ding_user_is_provider_user($user) && $profile2) {
$form_state['profiles'] = array('provider_alma' => $profile2);
profile2_attach_form($form, $form_state);
}
// change size and length of pincode
$form['account']['pincode']['#size'] = 32;
$form['account']['pincode']['#maxlength'] = 32;
// Hide mobile ID and absent period ID fields
// since they are not relevant for end users.
$form['profile_provider_alma']['field_absent_id']['#access'] = FALSE;
$form['profile_provider_alma']['field_alma_phone_id']['#access'] = FALSE;
// @todo; WHAT are the rules for alma pincode
// HOW do we validate
// custom validator ?? maybe pincode validator ??
$form['#validate'][] = 'alma_profile_form_validate';
}
/**
* Custom profile form validator.
*
* Validate reservatino pause dates.
*
* @see alma_form_user_profile_form_alter()
*/
function alma_profile_form_validate($form, $form_state) {
$input = $form_state['input'];
$start_date = ($input['profile_provider_alma']['field_alma_reservation_pause'][LANGUAGE_NONE][0]['value']['date']) ?: '';
$end_date = ($input['profile_provider_alma']['field_alma_reservation_pause'][LANGUAGE_NONE][0]['value2']['date']) ?: '';
$dt_start = DateTime::createFromFormat('d/m/Y', $start_date);
$dt_end = DateTime::createFromFormat('d/m/Y', $end_date);
if (is_object($dt_start) && is_object($dt_end)) {
$reservation_pause = alma_check_reservation_pause($dt_start->getTimeStamp(), $dt_end->getTimeStamp());
if (!$reservation_pause) {
form_set_error('field_alma_reservation_pause', t('Reservation pause neither can start in the past, start being after end, nor the dates be the same.'));
}
}
}
/**
* Return a fully instantiated AlmaClient instance.
*/
function alma_client() {
// This is basically a singleton. We also wait until right before
// instantiating to include our required classes. That seems like a
// decent performance tradeoff instead of loading everything on every
// page load.
static $client;
if (!isset($client)) {
$path = drupal_get_path('module', 'alma');
try {
$client = new AlmaClient(variable_get('alma_base_url', ''));
}
catch (Exception $e) {
watchdog('alma', 'Constructor error: “@message”', array('@message' => $e->getMessage(), WATCHDOG_ERROR));
return NULL;
}
}
return $client;
}
/**
* Calls the Alma backend, possibly caching the result.
*
* @param $method
* The desired method.
* @param ...
* Arguments to the method.
*
* @return mixed
* NULL on error, or the result of the method call.
*/
function alma_client_invoke($method) {
$args = func_get_args();
array_shift($args); // Lose the method.
$client = alma_client();
try {
$result = call_user_func_array(array($client, $method), $args);
}
catch (Exception $e) {
watchdog('alma', '@method error: “@message”', array('@method' => $method, '@message' => $e->getMessage()), WATCHDOG_ERROR);
throw $e;
}
return $result;
}
/**
* Get the complete organisation info from Alma.
*
* Includes branches, departments, locations, sublocations and collections. The
* information is cache until the cache is cleared or the reset parameter is
* TRUE.
*
* @param boolean $reset
* Optional. If TRUE reset the cached data and reload.
* @return array
* Array of the different organisational unit, each an array of their
* member units.
*/
function alma_get_organisation($reset = FALSE) {
// Return data from cache if available.
static $organisation;
if (!$organisation || $reset) {
if (($cache = cache_get('alma_organisation')) && !empty($cache->data) && !$reset) {
$organisation = $cache->data;
}
else {
$organisation = array(
'branch' => alma_client_invoke('get_branches'),
'department' => alma_client_invoke('get_departments'),
'location' => alma_client_invoke('get_locations'),
'sublocation' => alma_client_invoke('get_sublocations'),
'collection' => alma_client_invoke('get_collections'),
'reservation' => alma_reservation_pickup_branches(), //alma_client_invoke('get_reservation_branches'),
);
// Cache output for 24 hours if we got all the data correctly.
if ($organisation['branch'] && $organisation['department'] &&
$organisation['location'] && $organisation['sublocation'] &&
$organisation['collection'] && $organisation['reservation']) {
cache_set('alma_organisation', $organisation, 'cache');
}
}
}
return $organisation;
}
/**
* Implements hook_user_view
*/
function alma_user_view($account, $view_mode, $langcode) {
try {
$creds = ding_user_get_creds($account);
}
catch (DingProviderAuthException $e) {
return NULL;
}
if (($userInfo = _alma_user_info($creds)) && isset($userInfo)) {
foreach ($userInfo as $key => $prop) {
$data[$key] = isset($prop) ? $prop : '';
}
$account->content['name'] = array(
'#type' => 'item',
'#title' => t('Name'),
'#markup' => $data['firstname'],
'#weight' => -1,
);
if (!empty($data['address']) || !empty($data['postal']) || !empty($data['city'])) {
$address_parts = array();
$address_parts[] = join('<br /> ', array_filter(array($data['address'], $data['postal'], $data['city'])));
// @todo: really, there should be a template for this.
$account->content['address'] = array(
'#type' => 'item',
'#title' => t('Address'),
'#markup' => '<p>' . $data['address'] . '<br />' . $data['postal'] . ' ' . $data['city'] . '</p>',
'#weight' => 0,
);
}
}
// @todo: add provider specific fields
alma_set_profile2_fields($account);
}
/**
* Callback function to get OpenRuth user information
*/
function _alma_user_info($creds = NULL) {
// Get patron informations from ALMA.
$status = alma_get_patron($creds);
// Build basic information array, with patron information.
$info = array(
'firstname' => $status->name,
'lastname' => '',
'email' => $status->email,
'address' => $status->address,
'postal' => $status->postal,
'city' => $status->city,
'mobile' => $status->mobiles,
'agency' => $status->branchName,
);
return $info;
}
function alma_set_profile2_fields($account) {
if (!module_exists('profile2')) {
return;
}
$profile2 = profile2_load_by_user($account, 'provider_alma');
if (empty($profile2)) {
return;
}
// wrapper for profile2-fields
$account->content['profile2'] = array(
'#type' => 'item',
'#prefix' => '<div class="profile2_provider_fields">',
'#suffix' => '</div>',
);
// set preferred branch
if ($pref_branch = alma_get_preferred_branch($profile2)) {
$account->content['profile2']['preferred_branch'] = array(
'#type' => 'item',
'#title' => t('Pickup branch'),
'#markup' => '<span>' . $pref_branch . '</span>',
);
}
// set interest period
if ($interest_period = alma_get_interest_period($profile2)) {
$account->content['profile2']['interest_period'] = array(
'#type' => 'item',
'#title' => t('Interest period'),
'#markup' => '<span>' . t($interest_period['value']) . '</span>',
);
}
// set mobile phone
if ($mob = alma_get_mobile_phone($profile2)) {
$account->content['profile2']['mobile'] = array(
'#type' => 'item',
'#title' => t('Phone'),
'#markup' => '<span>' . $mob . '</span>',
);
}
// set reservation pause
if ($reservation_pause = alma_get_reservation_pause($profile2)) {
//wrapper for reservation pause
$start = str_replace('T00:00:00', '', $reservation_pause['start']);
$start = format_date(strtotime($start), 'custom', 'd/m/Y');
$stop = str_replace('T00:00:00', '', $reservation_pause['stop']);
$stop = format_date(strtotime($stop), 'custom', 'd/m/Y');
$account->content['profile2']['reservation_pause'] = array(
'#type' => 'item',
'#title' => '<h3>' . t('Reservation pause') . '</h3>',
'#prefix' => '<div class="profile2_provider_reservation_pause">',
'#suffix' => '</div>',
);
$account->content['profile2']['reservation_pause']['start'] = array(
'#type' => 'item',
'#title' => t('Start'),
'#markup' => '<span>' . $start . '</span>',
);
$account->content['profile2']['reservation_pause']['stop'] = array(
'#type' => 'item',
'#title' => t('Stop'),
'#markup' => '<span>' . $stop . '</span>',
);
}
}
function alma_get_mobile_phone($profile2) {
$langs = field_language('profile2', $profile2);
if (empty($profile2->field_alma_mobile_phone[$langs['field_alma_mobile_phone']])) {
return false;
}
$mob =
isset($profile2->field_alma_mobile_phone[$langs['field_alma_mobile_phone']][0]['value']) ?
$profile2->field_alma_mobile_phone[$langs['field_alma_mobile_phone']][0]['value'] : FALSE;
return $mob;
}
function alma_get_reservation_pause($profile2) {
// get field languages
$langs = field_language('profile2', $profile2);
if (empty($profile2->field_alma_reservation_pause[$langs['field_alma_reservation_pause']])) {
return false;
}
$res_pause['start'] = $profile2->field_alma_reservation_pause[$langs['field_alma_reservation_pause']][0]['value'];
$res_pause['stop'] = $profile2->field_alma_reservation_pause[$langs['field_alma_reservation_pause']][0]['value2'];
if (strlen($res_pause['start']) < 2 || strlen($res_pause['stop']) < 2) {
return false;
}
return $res_pause;
}
function alma_get_interest_period($profile2) {
// get field languages
$langs = field_language('profile2', $profile2);
// get field-info to retrieve values for select list
$field_info = field_info_field('field_alma_interest_period');
$interest_period = FALSE;
$int_values = isset($field_info['settings']['allowed_values']) ? $field_info['settings']['allowed_values'] : FALSE;
if ($int_values) {
$int_period = isset($profile2->field_alma_interest_period[$langs['field_alma_interest_period']][0]['value']) ? $profile2->field_alma_interest_period[$langs['field_alma_interest_period']][0]['value'] : '_none';
$interest_period = isset($int_values[$int_period]) ? array('key' => $int_period, 'value' => $int_values[$int_period]) : FALSE;
}
return $interest_period;
}
function alma_get_preferred_branch($profile2) {
// get field languages
$langs = field_language('profile2', $profile2);
//get preferred branch
$pref_branch =
isset($profile2->field_alma_preferred_branch[$langs['field_alma_preferred_branch']][0]['value']) ?
$profile2->field_alma_preferred_branch[$langs['field_alma_preferred_branch']][0]['value'] : '';
$branches = alma_reservation_pickup_branches();
$pref = isset($branches[$pref_branch]) ? $branches[$pref_branch] : FALSE;
return $pref;
}
/**
* Implements hook_profile2_presave().
*
* Sends changes to Alma
*/
function alma_profile2_presave($profile2) {
if (!$profile2->type == 'provider_alma') {
return;
}
// Do not presave when initializing
// @see alma.user/alma_user_profile_init
if (isset($profile2->alma_init)) {
return;
}
// Provider fields: field_alma_preferred_branch, field_alma_interest_period, field_alma_reservation_pause; field_alma_mobile_phone
$langs = field_language('profile2', $profile2);
// Reservation pause
$start = empty($profile2->field_alma_reservation_pause[$langs['field_alma_reservation_pause']][0]['value'])
? '' : $profile2->field_alma_reservation_pause[$langs['field_alma_reservation_pause']][0]['value'];
$start = str_replace('T00:00:00', '', $start);
$stop = empty($profile2->field_alma_reservation_pause[$langs['field_alma_reservation_pause']][0]['value2'])
? '' : $profile2->field_alma_reservation_pause[$langs['field_alma_reservation_pause']][0]['value2'];
$stop = str_replace('T00:00:00', '', $stop);
$org_start = empty($profile2->original->field_alma_reservation_pause[$langs['field_alma_reservation_pause']][0]['value'])
? '' : $profile2->original->field_alma_reservation_pause[$langs['field_alma_reservation_pause']][0]['value'];
$org_start = str_replace('T00:00:00', '', $org_start);
$org_stop = empty($profile2->original->field_alma_reservation_pause[$langs['field_alma_reservation_pause']][0]['value2'])
? '' : $profile2->original->field_alma_reservation_pause[$langs['field_alma_reservation_pause']][0]['value2'];
$org_stop = str_replace('T00:00:00', '', $org_stop);
if ($start != $org_start || $stop != $org_stop) {
$changes['reservation_pause_start'] = $start;
$changes['reservation_pause_stop'] = $stop;
$changes['absent_id'] = empty($profile2->field_absent_id[$langs['field_absent_id']][0]['value']) ?
'' : $profile2->field_absent_id[$langs['field_absent_id']][0]['value'];
}
// alma_preferred_branch (patronBranch)
if (!empty($profile2->original->field_alma_preferred_branch)) {
$org_branch = $profile2->original->field_alma_preferred_branch[$langs['field_alma_preferred_branch']][0]['value'];
$new_branch = isset($profile2->field_alma_preferred_branch[$langs['field_alma_preferred_branch']][0]['value']) ? $profile2->field_alma_preferred_branch[$langs['field_alma_preferred_branch']][0]['value'] : '_none';
if ($org_branch != $new_branch) {
$changes['preferred_branch'] = $new_branch;
}
}
// Mobile phone
// If present - update.
if (!empty($profile2->original->field_alma_mobile_phone)) {
$org_phone = $profile2->original->field_alma_mobile_phone[$langs['field_alma_mobile_phone']][0]['value'];
$new_phone = empty($profile2->field_alma_mobile_phone[$langs['field_alma_mobile_phone']][0]['value'])
? 'DELETE' : $profile2->field_alma_mobile_phone[$langs['field_alma_mobile_phone']][0]['value'];
if ($org_phone != $new_phone) {
$changes['mobile'] = $new_phone;
$phone_id = empty($profile2->field_alma_phone_id[$langs['field_alma_phone_id']][0]['value']) ? NULL : $profile2->field_alma_phone_id[$langs['field_alma_phone_id']][0]['value'];
$changes['phone_id'] = $phone_id;
}
}
// If not - add new one.
elseif (!empty($profile2->field_alma_mobile_phone[$langs['field_alma_mobile_phone']][0]['value'])
&& empty($profile2->original->field_alma_mobile_phone[$langs['field_alma_mobile_phone']][0]['value'])) {
$changes['mobile'] = $profile2->field_alma_mobile_phone[$langs['field_alma_mobile_phone']][0]['value'];
}
// Interest period ? apparently not an alma parameter.. @todo; check if Interest period should be saved on provider
// Update provider
if (isset($changes)) {
alma_update_provider($changes, $profile2);
}
}
function alma_update_provider($changes, $profile2) {
$error_message = t('Error communicating with library system. ');
try {
$creds = ding_user_get_creds($profile2);
}
catch (Exception $e) {
// rethrow ??
throw $e;
}
// Phone; add, change, delete.
if (isset($changes['phone_id'])) {
if ($changes['mobile'] == 'DELETE') {
// Delete mobile
try {
$res = alma_client_invoke('remove_phone_number', $creds['name'], $creds['pass'], $changes['phone_id']);
}
catch (Exception $e) {
watchdog('alma', $e->getMessage(), array(), WATCHDOG_ERROR);
}
if (empty($res)) {
$error_message .= t('phone not deleted');
drupal_set_message($error_message, 'warning');
}
}
else {
// Update phone.
try {
$res = alma_client_invoke('change_phone_number', $creds['name'], $creds['pass'], $changes['phone_id'], $changes['mobile']);
}
catch (Exception $e) {
watchdog('alma', $e->getMessage(), array(), WATCHDOG_ERROR);
}
if (empty($res)) {
$error_message .= t('phone not updated');
drupal_set_message($error_message, 'warning');
}
}
}
elseif (isset($changes['mobile'])) {
// Add mobile
try {
$res = alma_client_invoke('add_phone_number', $creds['name'], $creds['pass'], $changes['mobile']);
}
catch (Exception $e) {
watchdog('alma', $e->getMessage(), array(), WATCHDOG_ERROR);
}
if (empty($res)) {
$error_message .= t('phone not added');
drupal_set_message($error_message, 'warning');
}
}
// Update reservation pause (absentPeriod)
if (!empty($changes['absent_id']) && !empty($changes['reservation_pause_start']) && !empty($changes['reservation_pause_stop'])) {
try {
$res = alma_client_invoke('change_absent_period', $creds['name'], $creds['pass'], $changes['absent_id'], $changes['reservation_pause_start'], $changes['reservation_pause_stop']);
}
catch (Exception $e) {
watchdog('alma', $e->getMessage(), array(), WATCHDOG_ERROR);
}
if (empty($res)) {
$error_message .= t('reservation pause not updated');
drupal_set_message($error_message, 'error');
}
}
// Add reservation pause
elseif (!empty($changes['reservation_pause_start']) && !empty($changes['reservation_pause_stop'])) {
try {
$res = alma_client_invoke('add_absent_period', $creds['name'], $creds['pass'], $changes['reservation_pause_start'], $changes['reservation_pause_stop']);
}
catch (Exception $e) {
watchdog('alma', $e->getMessage(), array(), WATCHDOG_ERROR);
}
if (empty($res)) {
$error_message .= t('reservation pause not added');
drupal_set_message($error_message, 'warning');
}
}
// Delete reservation pause
elseif (!empty($changes['absent_id'])) {
try {
$res = alma_client_invoke('remove_absent_period', $creds['name'], $creds['pass'], $changes['absent_id']);
}
catch (Exception $e) {
watchdog('alma', $e->getMessage(), array(), WATCHDOG_ERROR);
}
// this one actually deletes the reservation pause, but returns an error ??
// for now disable messages...@todo; fix it.
/* if(!$res) {
$error_message .= t('reservation pause not deleted');
drupal_set_message($error_message,'warning');
} */
}
// Update preferred branch (patronBranch)
if (!empty($changes['preferred_branch'])) {
try {
$res = alma_client_invoke('change_patron_preferences', $creds['name'], $creds['pass'], $changes['preferred_branch']);
}
catch (Exception $e) {
watchdog('alma', $e->getMessage(), array(), WATCHDOG_ERROR);
}
if (empty($res)) {
$error_message .= t('pickup branch not saved');
drupal_set_message($error_message, 'warning');
}
}
// Refresh patron and update profile field IDs from response.
$info = alma_client_invoke('get_patron_info', $creds['name'], $creds['pass'], TRUE);
$langs = field_language('profile2', $profile2);
$profile2->field_alma_phone_id[$langs['field_alma_phone_id']][0]['value'] = empty($info['phones'][0]['id']) ? NULL : $info['phones'][0]['id'];
$profile2->field_absent_id[$langs['field_absent_id']][0]['value'] = empty($info['absent_periods'][0]['id']) ? NULL : $info['absent_periods'][0]['id'];
}
/**
* Get patron data from Alma.
*
* @param array $creds
* Array with 'name' and 'pass' keys, used to authenticate user with Alma.
* @param boolean $reset
* Reset static caching for this function.
* @return stdClass
* Patron object.
*/
function alma_get_patron($creds = NULL, $reset = FALSE, $as_array = FALSE) {
if (is_null($creds)) {
// Get creds, which may throw an exception that login is required.
global $user;
$creds = ding_user_get_creds($user);
}
static $patron;
if (!$patron || $reset) {
$info = alma_client_invoke('get_patron_info', $creds['name'], $creds['pass'], TRUE);
$organisation = alma_get_organisation();
$branch = empty($info['preferences']['patron_branch']) ? '' : $info['preferences']['patron_branch'];
$branch_name = '';
if (!empty($branch) && !empty($organisation['branch'][$info['preferences']['patron_branch']])) {
$branch_name = $organisation['branch'][$info['preferences']['patron_branch']];
}
$patron = array(
'name' => $info['user_name'],
'email' => isset($info['mails'][0]) ? $info['mails'][0]['mail'] : '',
'address' => isset($info['addresses'][0]) ? $info['addresses'][0]['street'] : '',
'postal' => isset($info['addresses'][0]) ? $info['addresses'][0]['postal_code'] : '',
'city' => isset($info['addresses'][0]) ? $info['addresses'][0]['city'] : '',
'mobiles' => isset($info['phones']) ? $info['phones'] : '',
'branch' => $branch,
'branchName' => $branch_name,
'absentPeriods' => isset($info['absent_periods']) ? $info['absent_periods'] : '',
);
}
if ($as_array) {
return $patron;
}
return (object) $patron;
}
/**
* Replace entity for the reservation in the case object in not in Ting anymore.
*
* @param $item
* Provider reservation object.
* @return
* Ting entity object, filled with data from provider service.
*/
function alma_replace_entity($item) {
$faust_number = explode(':', $item->ding_entity_id);
$faust_number = $faust_number[1];
$cid = md5('alma-' . $item->ding_entity_id);
$cache = cache_get($cid);
$alma_object = NULL;
if ($cache) {
$alma_object = $cache->data;
}
else {
$alma_object = alma_client_invoke('catalogue_record_detail', $faust_number);
cache_set($cid, $alma_object);
}
if ($alma_object['request_status'] == 'ok' && isset($alma_object['records'][$faust_number])) {
$source = $alma_object['records'][$faust_number];
$pseudo_ting_object = new TingEntity();
$pseudo_ting_object->localId = $faust_number;
$pseudo_ting_object->ding_entity_id = $item->ding_entity_id;
$pseudo_ting_object->reply = new TingClientObject();
$pseudo_ting_object->reply->record['dc:title']['dkdcplus:full'] = isset($source['titles'][0]) ? array($source['titles'][0]) : array();
$pseudo_ting_object->reply->record['dc:type']['dkdcplus:BibDK-Type'] = isset($source['media_class']) ? array($source['media_class']) : array();
$pseudo_ting_object->reply->record['dc:creator']['dkdcplus:aut'] = isset($source['notes']) ? $source['notes'] : array();
$pseudo_ting_object->reply->record['dc:creator']['oss:sort'] = isset($source['authors']) ? $source['authors'] : array();
$pseudo_ting_object->reply->record['dc:date'][''] = isset($source['publication_year']) ? array($source['publication_year']) : array();
$pseudo_ting_object->ting_title = array(LANGUAGE_NONE => array(array('id' => $item->ding_entity_id)));
$pseudo_ting_object->ting_type = array(LANGUAGE_NONE => array(array('id' => $item->ding_entity_id)));
$pseudo_ting_object->ting_author = array(LANGUAGE_NONE => array(array('id' => $item->ding_entity_id)));
$pseudo_ting_object->ting_cover = array(LANGUAGE_NONE => array(array('local_id' => $faust_number)));
$pseudo_ting_object->ding_entity_type = 'ding_entity';
$pseudo_ting_object->provider_id = $faust_number;
return $pseudo_ting_object;
}
return FALSE;
}
/**
* Validate the dates.
*
* Start and stop dates cannot be lower that current date,
* can't be the same and start cannot exceed end date.
*
* @param int $start
* Start timestamp.
* @param int $stop
* End timestamp.
* @return boolean
* TRUE/FALSE if dates correspond to conditoons described.
*/
function alma_check_reservation_pause($start, $stop) {
$now = time();
if ($start < $now || $stop < $now || $start == $stop || $start > $stop) {
return FALSE;
}
return TRUE;
}