-
Notifications
You must be signed in to change notification settings - Fork 1
/
ding_wagnerguide.module
293 lines (261 loc) · 10.6 KB
/
ding_wagnerguide.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
<?php
/**
* Implementation of hook_init().
*/
function ding_wagnerguide_init() {
drupal_add_js(drupal_get_path('module', 'ding_wagnerguide') . '/ding_wagnerguide.js', 'module', 'footer', TRUE);
drupal_add_css(drupal_get_path('module', 'ding_wagnerguide') .'/ding_wagnerguide.css');
drupal_add_js(array(
'ding_wagnerguide' => array(
'modulePath' => drupal_get_path('module', 'ding_wagnerguide'),
),
), 'setting');
}
/**
* Implementation of hook_perm().
*
* @return array
*/
function ding_wagnerguide_perm() {
return array('administer ding_wagnerguide');
}
/**
* @return array
*/
function ding_wagnerguide_admin() {
$link_base = '{
"Vej":"http://web.wagnerguide.com/vejleLibrary.aspx",
"Bor":"http://web.wagnerguide.com/BorkopLibrary.aspx",
"Egt":"http://web.wagnerguide.com/EgtvedLibrary.aspx",
"Giv":"http://web.wagnerguide.com/GiveLibrary.aspx",
"Jel":"http://web.wagnerguide.com/JellingLibrary.aspx"
}';
$transformation_array = '{
"department_id":{},
"location_id":{
"bus":{},
"dagpleje":{},
"depot":{
"popup,Some text or HTML to popup"
},
"kontor":{},
"magasin":{
"popup,Some text or HTML to popup"
},
"magasin1":{},
"magasin2":{},
"magasin3":{}
},
"sublocation_id":{}
}';
$form = array();
$form['ding_wagnerguide_linkbase'] = array(
'#type' => 'textarea',
'#title' => t('Base URL for branches'),
'#default_value' => variable_get('ding_wagnerguide_linkbase', $link_base),
'#rows' => 7,
'#description' => t("
Contains an array in <a target=\"_blank\" href=\"http://en.wikipedia.org/wiki/JSON\">JSON</a> format.<br /><br />
Make one entry pr. branch, and provide the WagnerGUIDE base URL for each.<br /><br />
The key (first part) must be the Alma representation 'branch_id', typically three letters.
"),
'#required' => TRUE,
);
// Make a transformation array text area per branch.
$branches = json_decode(variable_get('ding_wagnerguide_linkbase', $link_base), TRUE);
foreach ($branches as $branch => $link) {
$field = 'ding_wagnerguide_transformation_array_' . $branch;
$form[$field] = array(
'#type' => 'textarea',
'#title' => t('Translation array for branch "' . $branch . '"'),
'#default_value' => variable_get($field, $transformation_array),
'#rows' => 15,
'#description' => t("
This array in <a target=\"_blank\" href=\"http://en.wikipedia.org/wiki/JSON\">JSON</a> format may contain entries for tranformation of all, some or none of the Alma representaions 'department_id, 'location_id' and 'sublocation_id'.<br /><br />
No link, use:<br />
<pre>\"location_id\":{\n\t\"depot\":{}\n}</pre><br />
Rewriting label, use:<br />
<pre>\"location_id\":{\n\t\"depot\":{\n\"new_label\":\"magasin\"\n}\n}</pre><br />
For popup, use:<br />
<pre>\"location_id\":{\n\t\"depot\":{\n\"popup\":\"Popup text\"\n}\n}</pre>
Ignore rule for certain material types, use:<br />
<pre>\"location_id\":{\n\t\"depot\":{\n\"ignore_mtype\":\"cd,book\",\n\"popup\":\"Popup text\"\n}\n}</pre>
"),
'#required' => TRUE,
);
}
$form['ding_wagnerguide_debug'] = array(
'#type' => 'checkbox',
'#title' => t('Debug mode'),
'#default_value' => variable_get('ding_wagnerguide_debug', FALSE),
'#description' => t("
If set to true, will print HTML comment after WagnerGUIDE link (or where link should have been printed) with the content of 'holding' from Alma.<br /><br />
Beware that it is printed by javascript, so it cannot be seen in the page source. Use the likes of Firebug or \"View selection source\" et al.<br /><br />
Can be used to figure out what to put in the above fields.
"),
'#required' => TRUE,
);
return system_settings_form($form);
}
/**
* @param type $form
* @param type $form_state
*/
function ding_wagnerguide_admin_validate($form, &$form_state) {
$link_base = $form_state['values']['ding_wagnerguide_linkbase'];
if (!is_array(json_decode($link_base, TRUE))) {
form_set_error('ding_wagnerguide_linkbase', t('Malformed JSON: Does not transform to array.'));
}
else {
$branches = json_decode($link_base);
foreach ($branches as $branch => $link) {
$field = 'ding_wagnerguide_transformation_array_' . $branch;
$transformation_array = $form_state['values'][$field];
if (!is_array(json_decode($transformation_array, TRUE))) {
form_set_error($field, t('Malformed JSON: Does not transform to array.'));
}
}
}
}
/**
* Implementation of hook_menu().
*
* @return array
*/
function ding_wagnerguide_menu() {
$items = array();
$items['admin/settings/ding/wagnerguide'] = array(
'title' => t('WagnerGUIDE module settings'),
'description' => t(''),
'page callback' => 'drupal_get_form',
'page arguments' => array('ding_wagnerguide_admin'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
$items['ding/wagnerguide/item/%'] = array(
'title' => 'Ding WagnerGUIDE links',
'description' => 'Returns a list of links to WagnerGUIDE',
'page callback' => 'ding_wagnerguide_get_wagner_links',
'page arguments' => array(3),
'access arguments' => array('access content'),
);
return $items;
}
/**
* Implementation of hook_trampoline().
*
* @todo This function is not being used.
* @see https://github.com/inleadmedia/ding_wagnerguide/blob/master/ding_wagnerguide.js#L26
*
* @return array
*/
function ding_wagnerguide_trampoline() {
$items = array();
$items['ding/wagnerguide/item/%'] = array();
return $items;
}
/**
* Called via AJAX/JavaScript.
* Returns (prints) JSON containing an array of holdings.
*
* @param int $ting_item
* @return null
*/
function ding_wagnerguide_get_wagner_links($ting_item) {
$details = alma_client_invoke('catalogue_record_detail', $ting_item);
$result = array();
if ($details && isset($details['records'])) {
foreach ($details['records'] as $alma_id => $record) {
$result[$alma_id] = array();
$mtype = $record['media_class'];
foreach ($record['holdings'] as $alma_holding) {
// "alma_availability_details()" only return holdings which are not loaned out. So do we:
if ($alma_holding['total_count'] > $alma_holding['checked_out_count']) {
$result[$alma_id][] = ding_wagnerguide_get_holding($alma_holding, $mtype);
}
}
}
}
drupal_set_header('Content-Type: application/json; charset=utf-8');
echo json_encode($result);
return;
}
/**
* Sub function of ding_wagnerguide_get_wagner_links.
* Return one holding i.e. one WagnerGUIDE link or popup data.
*
* @param array $alma_holding, holding data on all physical instances of the item/record
* @param string $mtype, material type of the item/record
* @return mixed null, array or string
*/
function ding_wagnerguide_get_holding($alma_holding, $mtype) {
// Alma parts => Wagner parts.
$alma_holding_parts = array(
'department_id' => 'Department',
'location_id' => 'Location',
'sublocation_id' => 'Sublocation',
'shelf_mark' => 'Shelf'
);
$trans = array(
'~ae' => 'æ',
'~oe' => 'ø',
'~aa' => 'å',
);
$query_string_parts_base = array('Lang=da', 'menu=false');
$link_base = json_decode(variable_get('ding_wagnerguide_linkbase', ''), TRUE);
$query_string_parts = array();
$holding = array('href' => '');
// Firstly check if we know the branch.
if (isset($alma_holding['branch_id']) && isset($link_base[$alma_holding['branch_id']])) {
// Get the tranformations for that branch.
$transformation_array = json_decode(variable_get('ding_wagnerguide_transformation_array_' . $alma_holding['branch_id'], ''), TRUE);
foreach ($alma_holding_parts as $alma_holding_part => $wagner_link_part) {
if (!empty($alma_holding[$alma_holding_part])) {
if ($alma_holding_part == 'shelf_mark') {
/* Shelf_mark is a special case: Use only first letter.
* Two primary cases exist: Fiction literature and subject literature
* Fiction is identified by the string starting with letters, possibly with a leading ">"-sign (this symbol must be filtered out).
* Subject literature has shelf_mark starting with a DK5-no, possibly followed by a label. We need only the number.
* The regex below matches one and only one of the above 2 cases
*/
if (preg_match("/[a-zA-ZæøåÆØÅ]|^[0-9]+\.?[0-9]+/u", $alma_holding['shelf_mark'], $match)) {
$query_string_parts[] = $wagner_link_part . '=' . drupal_strtoupper($match[0]);
}
}
else {
// All others are translated using the transformation array.
$wagner_link_part_array = $transformation_array[$alma_holding_part][$alma_holding[$alma_holding_part]];
// only transform if transformation array contains something. If material type should be ignored, just continue as if transformation array was empty
if (isset($wagner_link_part_array) && (strpos($wagner_link_part_array['ignore_mtype'], $mtype) === false)) {
// If a new_label value exists in the transformation array, transform.
$wagner_link_part_value = $wagner_link_part_array['new_label'];
if (!$wagner_link_part_array) {
// If a value is set to false in $transformation_array, no link will be given.
$query_string_parts = array();
break;
}
elseif (isset($wagner_link_part_array['popup'])) {
// If $transformation_array entry contains a popup value, a popup link will be given. The popup value is the text in the popup.
$query_string_parts = array();
$holding['popup'] = $wagner_link_part_array['popup'];
break;
}
}
else {
// Or just pass the value if no entry in the transformation array, but replace æøå.
$wagner_link_part_value = strtr($alma_holding[$alma_holding_part], $trans);
}
$query_string_parts[] = $wagner_link_part . '=' . $wagner_link_part_value; //EA change to correct value from link part array
}
}
}
}
if ($query_string_parts) {
$query_string = join('&', array_merge($query_string_parts_base, $query_string_parts));
$holding['href'] = $link_base[$alma_holding['branch_id']] . '?' . $query_string;
}
if (variable_get('ding_wagnerguide_debug', FALSE)) {
$holding['debug'] = print_r($alma_holding, TRUE);
}
return $holding;
}