This repository has been archived by the owner on May 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleanbility.module
101 lines (85 loc) · 2.73 KB
/
leanbility.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
<?php
// $Id$
// Index variables
define('LEANBILITY_SUB', 'subscribed');
define('LEANBILITY_SUB_TYPE', 'type');
/**
* Implementation of hook_perm().
*/
function leanbility_perm() {
return array(
'configure leanbility',
);
}
/**
* Implementation of hook_menu().
*/
function leanbility_menu() {
$items = array();
$items['admin/settings/leanbility'] = array(
'title' => 'Leanbility',
'description' => t('Configuration of leanbility service.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('leanbility_admin_form'),
'access arguments' => array('configure leanbility'),
'file' => 'leanbility.admin.inc',
);
return $items;
}
/**
* Implementation of hook_flush_caches().
*/
function leanbility_flush_caches() {
return array('cache_leanbility');
}
/**
* Request the leanbility service for the status for the user.
*
* @param string $borr_card
* @return string
*/
function leanbility_status($borr_card) {
module_load_include('inc', 'leanbility', 'includes/service');
$service = new LeanbilityServerProvider($borr_card);
return $service->status();
}
/**
* Changes the leanbility status of the user with $borr_card no
*
* @param int $borr_card
* @param string $type
*/
function leanbility_update($borr_card, $type) {
module_load_include('inc', 'leanbility', 'includes/service');
$service = new LeanbilityServerProvider($borr_card);
$service->update($type);
}
function leanbility_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'alma_user_user_library_settings_form') {
global $user;
$creds = ding_library_user_get_credentials($user);
$defaults = variable_get('leanbility', array());
$defaults['service_types'] = array_filter($defaults['service_types']);
$defaults['choices'] = array();
foreach ($defaults['service_types'] as $key => $value) { // Maybe use array_map
$defaults['choices'][$key] = t(ucfirst($key));
}
$form['leanbility'] = array(
'#type' => 'radios',
'#title' => t('Leanbility choices'),
'#options' => $defaults['choices'],
'#description' => t('How would you like to receive information about the leanbility of your reservations.'),
'#attributes' => array('class' => 'leanbility'),
'#default_value' => leanbility_status($creds['user_id']),
);
$form['#submit'][] = 'leanbility_alma_settings_form_validate';
}
}
function leanbility_alma_settings_form_validate($form, &$form_state) {
$creds = ding_library_user_get_credentials($form_state['values']['account']);
try {
leanbility_update($creds['user_id'], $form_state['values']['leanbility']);
} catch (Exception $e) {
form_set_error('leanbility', t('Leanbility error (' . $e->getCode() . '): ' . $e->getMessage()));
}
}