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 1
/
publizon.module
354 lines (317 loc) · 9.72 KB
/
publizon.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
<?php
/**
* @file
* Main module implementation which defines functions used in the administration
* interface and a few helper functions.
*/
/**
* Define constance used in the client to indicate, which type of data we are
* looking for.
*
* @see PublizonClient->getgetLastLoansForAll()
*/
define('PUBLIZON_EBOOK', 1);
define('PUBLIZON_NETSOUND', 2);
/**
* Define constance used to detect the users current device platform.
*/
define('PUBLIZON_PLATFORM_GENERIC', 1);
define('PUBLIZON_PLATFORM_IOS', 2);
/**
* Implements hook_perm().
*/
function publizon_perm() {
return array(
'administre publizon',
);
}
/**
* Implements hook_menu().
*/
function publizon_menu() {
$items = array();
$items['admin/settings/publizon'] = array(
'title' => 'Publizon',
'description' => 'Configuration of communication with the publizon web-service.',
'page callback' => 'drupal_get_form',
'page arguments' => array('publizon_admin_form'),
'access arguments' => array('administre publizon'),
'file' => 'include/publizon.admin.inc',
);
$items['admin/settings/publizon/library/ahah'] = array(
'title' => 'Publizon ahah callback',
'page callback' => 'publizon_admin_form_ahah',
'access arguments' => array('administre publizon'),
'type' => MENU_CALLBACK,
'file' => 'include/publizon.admin.inc',
);
return $items;
}
/**
* Implements hook_theme().
*/
function publizon_theme() {
return array(
// Theme function for custom form element defined in publizon_elements().
'publizon_library_field' => array(
'arguments' => array('element' => NULL),
'template' => 'templates/publizon-library-field',
),
// Add action buttons to loans, products and ting objects.
'publizon_actions' => array(
'arguments' => array('product' => NULL, 'type' => 'default'),
'template' => 'templates/publizon-actions',
),
);
}
/**
* Implements hook_elements().
*
* Defines custom form element which is used by the administration form to
* handle library information.
*
* @see publizon_admin_form()
*/
function publizon_elements() {
return array(
'publizon_library_field' => array(
'#input' => TRUE,
'#process' => array('publizon_library_field_process'),
'#element_validate' => array('publizon_library_field_validate'),
),
);
}
/**
* Implements hook_value().
*
* @see publizon_elements()
*/
function publizon_library_field_value($element, $edit = FALSE) {
if (func_num_args() == 1) {
return $element['#default_value'];
}
return $edit;
}
/**
* Implementation of the library field process function.
*
* @see publizon_elements()
*/
function publizon_library_field_process($element, $edit, &$form_state, $complete_form) {
return $element;
}
/**
* Implementation of the library field validation function.
*
* It ensures that all input fields contains data before submitting the element.
*
* @see publizon_elements()
*/
function publizon_library_field_validate($element, &$form_state) {
// First is to get the remove one more to work.
if (!empty($element['#value']['retailer_id'])) {
if (!is_numeric($element['#value']['retailer_id']) || (int) $element['#value']['retailer_id'] < 0) {
form_error($element, t('!field have to be an interger.', array('!field' => t('Retailer ID'))));
form_set_value($element, 0, $form_state);
}
if (empty($element['#value']['retailer_key_code'])) {
form_error($element, t('!field are required.', array('!field' => t('Library key code'))));
form_set_value($element, 0, $form_state);
}
if (empty($element['#value']['library_name'])) {
form_error($element, t('!field are required.', array('!field' => t('Library name'))));
form_set_value($element, 0, $form_state);
}
}
return $element;
}
/**
* Implements hook_block().
*
* Creates a block with an list over the x last loans from the site.
*/
function publizon_block($op = 'list', $delta = 0) {
$block = array();
switch ($op) {
case 'list':
$block['lastloans'] = array(
'info' => 'Last loans (Publizon)',
'cache' => BLOCK_NO_CACHE,
);
break;
case 'view':
$content = '';
// Get site type.
$type = variable_get('publizon_site_type', PUBLIZON_EBOOK);
// Check for cached version of last loans.
$cid = 'publizon_last_loan_' . $type;
if ($cache = cache_get($cid)) {
$content = $cache->data;
}
else {
try {
// Try to get connection with publizon and fetch list of last loans.
$client = PublizonClient::getClient();
$products = $client->getLastLoansForAll($type);
// Theme the last loans as a product list.
$content = theme('publizon_products_list', $products);
// Store the content in cache (10 min).
cache_set($cid, $content, 'cache', time() + 600);
}
catch (Exception $e) {
// Something was not right, so display the error in the block.
$content = $e->getMessage();
}
}
// Add content to the block.
$block = array(
'subject' => t('Last loans'),
'content' => $content,
);
break;
}
return $block;
}
/**
* Default implementation fo hook_preprocess_publizon_actions().
*
* It generates the action links, button or icons on the different publizon
* product lists.
*/
function template_preprocess_publizon_actions(&$vars) {
$actions = array();
switch ($vars['type']) {
// User loan list.
case 'loan':
// Should be handel by the sites as eReolen no longer supports download
// (only streaming).
break;
// Cart list on the user page (should not have a break statement).
case 'cart':
$actions[] = array(
'#link' => recall_list_delete_link($vars['product']->isbn),
'#class' => 'remove',
'#weight' => 10,
);
case 'default':
// Default button.
if (isset($vars['product']->teaser_link)) {
$actions[] = array(
'#link' => l(t('Sample'), $vars['product']->teaser_link, array(
'html' => TRUE,
'attributes' => array(
'target' => '_blank',
'action' => 'sample',
),
)),
'#class' => 'sample',
'#weight' => -10,
);
}
else {
// The book do not have a sample link, hence can't be downloaded.
$actions[] = array(
'#link' => '<span>' . t('Unavailable') . '</span>',
'#class' => 'unavailable',
'#weight' => 1,
);
}
break;
}
// Sort the actions based on weight.
usort($actions, 'element_sort');
$vars['actions'] = $actions;
}
/**
* Helper function that extracts the name of the author(s) on a ting object.
*
* @param object $ting_object
* Ting object.
* @param bool $link
* If the parameter is true the authors returned is search links else plain
* text.
*
* @return string
* The author(s) found and the empty string if non is found.
*/
function publizon_get_authors($ting_object, $link = TRUE) {
$authors = '';
if (isset($ting_object->record["dc:creator"]["dkdcplus:aut"][0])) {
if ($link) {
$authors = l($ting_object->record["dc:creator"]["dkdcplus:aut"][0], 'ting/search/"' . $ting_object->record["dc:creator"]["dkdcplus:aut"][0] . '"');
}
else {
$authors = $ting_object->record["dc:creator"]["dkdcplus:aut"][0];
}
}
else {
// Find all authors involved.
$contributors = array();
if (isset($ting_object->record['dc:contributor']['dkdcplus:edt'])) {
$contributors = array_merge($contributors, $ting_object->record['dc:contributor']['dkdcplus:edt']);
}
if (isset($ting_object->record['dc:contributor']['dkdcplus:aut'])) {
$contributors = array_merge($contributors, $ting_object->record['dc:contributor']['dkdcplus:aut']);
}
// Loop over the authors and create the string.
sort($contributors);
foreach ($contributors as $contributor) {
if ($link) {
$authors .= l($contributor, 'ting/search/' . $contributor) . ', ';
}
else {
$authors .= $authors . ', ';
}
}
$authors = drupal_substr($authors, 0, -2);
}
return $authors;
}
/**
* Search the T!NG data well for information based on ISBN.
*
* If more than one match is found only the first match is returned.
*
* @param string $isbn
* ISBN number for the product that is searched.
*
* @return TingObject
* Datawell object for the product found, if non is found FALSE will be
* returned.
*/
function publizon_ting_find_object($isbn) {
// Ensure that the ting client is available.
module_load_include('client.inc', 'ting');
// Build ting request.
$request = ting_get_request_factory()->getSearchRequest();
$request = ting_add_agency($request);
$request = ting_add_profile($request);
// Try to fetch ting object based on ISBN number. Sadly not all object in
// ebooks or net sound have ISBN, but uses oss:PROVIDER-ID, which we can not
// search directly on (so no dc.identifier=*). Also therefor it have been
// renamed, because it dose not only find object by ISBN.
$request->setQuery($isbn);
$request->setNumResults(1);
// Execute the fetch.
$response = ting_execute($request);
$object = FALSE;
if (isset($response->collections[0]->objects[0])) {
$object = $response->collections[0]->objects[0];
}
return $object;
}
/**
* Detect the current users platform and return a constance for that platform.
*
* @return int
* Returns the constance PUBLIZON_PLATFORM_IOS or PUBLIZON_PLATFORM_GENERIC.
*/
function publizon_get_client_platform() {
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/(iPod)|(iPad)|(iPhone)/i', $user_agent)) {
return PUBLIZON_PLATFORM_IOS;
}
else {
return PUBLIZON_PLATFORM_GENERIC;
}
}