-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathding_provider.module
More file actions
751 lines (667 loc) · 22.4 KB
/
ding_provider.module
File metadata and controls
751 lines (667 loc) · 22.4 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
<?php
/**
* @file
* Implements a common interface to backend providers.
*/
/**
* Implements hook_menu().
*/
function ding_provider_menu() {
$items = array();
$items['ding_provider_error'] = array(
'title' => 'Internal error',
'page callback' => 'ding_provider_error_page',
'access callback' => TRUE,
);
$items['admin/config/ding/provider'] = array(
'title' => 'Ding provider',
'description' => 'Configure the provider used to communicate with the library system.',
'page callback' => 'ding_provider_admin_overview',
'access arguments' => array('administer ding provider'),
'type' => MENU_NORMAL_ITEM,
'file' => 'ding_provider.admin.inc',
);
$items['admin/config/ding/provider/overview'] = array(
'title' => 'Overview',
'description' => 'Providers overview',
'type' => MENU_DEFAULT_LOCAL_TASK,
'file' => 'system.admin.inc',
);
$providers = _ding_provider_get_provider();
foreach ($providers as $name => $provider) {
if ($provider['global settings']) {
$items['admin/config/ding/provider/' . $provider['module']] = array(
'title' => $provider['title'],
'page callback' => 'drupal_get_form',
'page arguments' => array($provider['global settings']),
'access arguments' => array('administer ding provider'),
'type' => MENU_LOCAL_TASK,
);
if ($provider['global settings file']) {
$items['admin/config/ding/provider/' . $provider['module']]['file'] = $provider['global settings file'];
}
}
}
return $items;
}
/**
* Implements hook_menu_alter().
*/
function ding_provider_menu_alter(&$items) {
// Override system/ajax with a version that caches
// DingProviderAuthException.
$callback = $items['system/ajax']['page callback'];
if ($callback == 'ajax_form_callback') {
$items['system/ajax']['page callback'] = 'ding_provider_ajax_form_callback';
$items['system/ajax']['file path'] = drupal_get_path('module', 'ding_provider');
$items['system/ajax']['file'] = 'ding_provider.module';
// Delete var, but only if it is set.
if (variable_get('ding_provider_ajax_disabled', NULL)) {
variable_del('ding_provider_ajax_disabled');
}
}
else {
// Set a variable for hook_requirements.
variable_set('ding_provider_ajax_disabled', $callback);
}
}
/**
* Implements hook_permission().
*/
function ding_provider_permission() {
return array(
'administer ding provider' => array(
'title' => t('Administer Ding provider'),
'description' => t('Allow role to configure provider settings.'),
),
);
}
/**
* Implements hook_ding_install_tasks().
*/
function ding_provider_ding_install_tasks() {
$users = _ding_provider_get_provider_users();
$providers = _ding_provider_get_provider();
$return = array();
foreach ($users as $provider => $info) {
// Add global provider settings if required.
if ($info['ding_profile'] && count($info['required']) && isset($providers[$provider]) && $form_id = $providers[$provider]['global settings']) {
$return[$form_id] = array(
// TODO: This doesn't work for potx, we need the generated strings in
// plain form somewhere.
'display_name' => st($providers[$provider]['title']),
'type' => 'form',
);
if ($providers[$provider]['global settings file']) {
$return[$form_id]['file'] = $providers[$provider]['global settings file'];
}
}
// Add settings for individual providers to installer if someone asked for
// it and there's modules that requires this provider.
if ($info['ding_profile'] && count($info['required']) && $form_id = ding_provider_implements($provider, 'settings')) {
$return[$form_id] = array(
// TODO: This doesn't work for potx, we need the generated stings in
// plain form somewhere.
'display_name' => st(drupal_ucfirst($provider) . ' provider'),
'type' => 'form',
);
if ($providers[$provider]['file']) {
$return[$form_id]['file'] = $providers[$provider]['file'];
}
}
}
return $return;
}
/**
* Error page callback.
*/
function ding_provider_error_page() {
return array(
'#markup' => t("Internal error. Please contact the site administrator if the problem persists."),
);
}
/**
* Returns whether a provider implements a given function.
*/
function ding_provider_implements($type, $hook) {
try {
return (bool) _ding_provider_function($type, $hook, TRUE);
}
catch (Exception $e) {
return FALSE;
}
}
/**
* Invoke a provider function.
*
* Simplest of the provider invoking functions. Returns the result of calling
* the provider function, or throws DingProviderAuthException if not
* authenticated.
*
* @see ding_provider_invoke_page()
* @see ding_provider_get_form()
* @see ding_provider_ajax_form_callback()
*/
function ding_provider_invoke($type, $hook) {
$args = func_get_args();
array_shift($args);
array_shift($args);
$function = _ding_provider_function($type, $hook);
if (is_callable($function)) {
return call_user_func_array($function, $args);
}
else {
watchdog('ding_provider', 'Provider invoke request to unknown fucntion (%function)', array('%function' => $function), WATCHDOG_ERROR);
}
}
/**
* Get a form from a provider.
*/
function ding_provider_form($type, $form_id) {
$args = func_get_args();
array_shift($args);
array_shift($args);
$function = _ding_provider_function($type, $form_id);
if ($function) {
return drupal_get_form($function, $args);
}
}
/**
* Invoke a provider function, handling authentication.
*
* Will redirect to the authentication page if DingProviderAuthException is
* thrown, and redirect back on successful authentication.
*
* @see ding_provider_invoke()
* @see ding_provider_get_form()
* @see ding_provider_ajax_form_callback()
*/
function ding_provider_invoke_page($type, $hook) {
$args = func_get_args();
array_shift($args);
array_shift($args);
try {
$function = _ding_provider_function($type, $hook);
if ($function) {
return call_user_func_array($function, $args);
}
}
catch (Exception $e) {
if ($e instanceof DingProviderUserException) {
// Rethrow user exceptions.
throw $e;
}
elseif ($e instanceof DingProviderAuthException) {
// Redirect to auth page.
if (module_exists('ding_user') && ($authpage = ding_user_auth_page())) {
// @todo redirect to auth page
drupal_goto($authpage, array('query' => ding_provider_get_destination()));
}
}
if (arg(0) != 'ding_provider_error') {
watchdog('ding_provider', 'Uncaught exception in ding_provider_invoke_page: @message', array('@message' => $e->getMessage()), WATCHDOG_ERROR);
// Redirect to error page.
drupal_goto('ding_provider_error');
}
else {
return NULL;
}
}
}
/**
* Wrapper around drupal_get_form() that handles authentication.
*
* Will redirect to the authentication page if DingProviderAuthException is
* thrown, and restart form processing on successful authentication.
*
* @see ding_provider_invoke()
* @see ding_provider_invoke_page()
* @see ding_provider_ajax_form_callback()
*/
function ding_provider_get_form($form_id) {
$form_state = array();
$args = func_get_args();
// Remove $form_id from the arguments.
array_shift($args);
$form_state['build_info']['args'] = $args;
return ding_provider_build_form($form_id, $form_state);
}
/**
* Wrapper around drupal_build_form() that handles authentication.
*
* Will redirect to the authentication page if DingProviderAuthException is
* thrown, and restart form processing on successful authentication.
*
* @see ding_provider_get_form()
*/
function ding_provider_build_form($form_id, &$form_state) {
$messages = NULL;
try {
if (isset($_REQUEST['dp_form_id']) && !empty($_REQUEST['dp_form_id'])) {
// Load saved submission.
if ($cached = cache_get('ding_provider_' . $form_id, 'cache_form')) {
$form_state['input'] = $cached->data;
$messages = $form_state['input']['#ding_provider_messages'];
unset($form_state['input']['#ding_provider_messages']);
cache_clear_all('ding_provider_' . $form_id, 'cache_form');
}
}
$old_redirect = isset($form_state['no_redirect']) ? $form_state['no_redirect'] : FALSE;
$form_state['no_redirect'] = TRUE;
$form = drupal_build_form($form_id, $form_state);
// Set any messages we saved.
_ding_provider_message_reset($messages);
$form_state['no_redirect'] = $old_redirect;
if ($form_state['executed'] == TRUE) {
// Redirect if the form was submitted.
drupal_redirect_form($form_state);
}
return $form;
}
catch (Exception $e) {
if ($e instanceof DingProviderUserException) {
// Rethrow user exceptions.
throw $e;
}
elseif ($e instanceof DingProviderAuthException) {
if (module_exists('ding_user') && ($authpage = ding_user_auth_page())) {
// Something needs auth, save state and redirect to authentication page.
$expire = 3600;
$input = $form_state['method'] == 'get' ? $_GET : $_POST;
$input['#ding_provider_messages'] = drupal_get_messages();
cache_set('ding_provider_' . $form_id, $input, 'cache_form', REQUEST_TIME + $expire);
$options = array('query' => ding_provider_get_destination(array('dp_form_id' => $form_id)));
drupal_goto($authpage, $options);
}
}
watchdog('ding_provider', 'Uncaught exception in ding_provider_build_form: @message', array('@message' => $e->getMessage()), WATCHDOG_ERROR);
// Redirect to error page.
drupal_goto('ding_provider_error');
}
}
/**
* Replacement for ajax_form_callback() that handles authentication.
*
* Sends a log in AJAX command (implemented in ding_user), which will resubmit
* the AJAX request on successful submission.
*
* @see ding_provider_invoke()
* @see ding_provider_invoke_page()
* @see ding_provider_get_form()
*/
function ding_provider_ajax_form_callback() {
global $user;
$messages = NULL;
try {
if (isset($_REQUEST['dp_form_id']) && !empty($_REQUEST['dp_form_id'])) {
$form_id = (string) $_REQUEST['dp_form_id'];
// Load saved submission.
if ($cached = cache_get('ding_provider_' . $form_id, 'cache_form')) {
$form_state['input'] = $cached->data;
$messages = $form_state['input']['#ding_provider_messages'];
$orig_user = $form_state['input']['#ding_provider_user'];
unset($form_state['input']['#ding_provider_messages']);
unset($form_state['input']['#ding_provider_user']);
cache_clear_all('ding_provider_' . $form_id, 'cache_form');
}
}
// If the user changed (likely because they just logged in),
// things get complicated as ajax_get_form wont find the cached
// form. So we reset the user momentarily.
if (isset($orig_user) && $user->uid != $orig_user->uid) {
$new_user = $user;
$user = $orig_user;
}
list($form, $form_state, $form_id) = ajax_get_form();
if (isset($new_user)) {
$user = $new_user;
}
drupal_process_form($form['#form_id'], $form, $form_state);
// Set any messages we saved.
_ding_provider_message_reset($messages);
if (!empty($form_state['triggering_element'])) {
$callback = $form_state['triggering_element']['#ajax']['callback'];
}
if (!empty($callback) && function_exists($callback)) {
$x = $callback($form, $form_state);
return $x;
}
}
catch (Exception $e) {
if ($e instanceof DingProviderUserException) {
// Rethrow user exceptions.
throw $e;
}
elseif ($e instanceof DingProviderAuthException || $e instanceof EntityMalformedException) {
if (module_exists('ding_user')) {
// Something needs auth, save state and redirect to authentication page.
$expire = 3600;
$input = $form_state['method'] == 'get' ? $_GET : $_POST;
$input['#ding_provider_messages'] = drupal_get_messages();
$input['#ding_provider_user'] = $user;
cache_set('ding_provider_' . $form_id, $input, 'cache_form', REQUEST_TIME + $expire);
$options = array('query' => array('dp_form_id' => $form_id) + ding_provider_get_destination());
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_ding_user_authenticate(array('dp_form_id' => $form_id)),
),
);
}
}
else {
// Send the exception into watchdog to help debug.
watchdog_exception('ding_provider', $e);
}
}
// Log message to watchdog to help debug the problem.
watchdog('ding_provider', 'Internal error exception unhandled in ajax request.', array(), WATCHDOG_ERROR);
// Else show internal error.
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_alert(t("Internal error. Please contact the site administrator if the problem persists.")),
),
);
}
/**
* Re-sets messages, avoiding duplicates.
*/
function _ding_provider_message_reset($messages) {
if (is_array($messages)) {
foreach ($messages as $type => $message_list) {
foreach ($message_list as $message) {
drupal_set_message($message, $type, FALSE);
}
}
}
}
/**
* Alternative to drupal_get_destination() which will encode an existing
* destination into the new.
*/
function ding_provider_get_destination($query_args = array()) {
// If destination is set, create a new one embedding the old, and
// unset destination, so drupal_goto wont go to the old
// destination.
$path = $_GET['q'];
$query = drupal_http_build_query($query_args + drupal_get_query_parameters());
unset($_REQUEST['destination']);
if ($query != '') {
$path .= '?' . $query;
}
$destination = array(
'destination' => $path,
);
return $destination;
}
/**
* Get the provider type.
*
* @param string $type
* The type of module e.g reservation, availibility etc.
*/
function ding_provider_get_provider_module_name($type) {
$providers = _ding_provider_get_provider();
return $providers[$type]['module'];
}
/**
* Returns the given provider.
*/
function _ding_provider_get_provider($type = NULL) {
$providers = array();
foreach (module_implements('ding_provider') as $module) {
$module_provides = module_invoke($module, 'ding_provider') + array(
'title' => $module . '.module',
'settings' => NULL,
'file' => NULL,
'provides' => array(),
);
foreach ($module_provides['provides'] as $name => $module_provider) {
$module_provider['module'] = $module;
$module_provider['title'] = $module_provides['title'];
$module_provider['global settings'] = $module_provides['settings'];
$module_provider['global settings file'] = $module_provides['file'];
$providers[$name] = $module_provider;
}
}
if (!$type) {
return $providers;
}
if (isset($providers[$type])) {
return $providers[$type];
}
return NULL;
}
/**
* Returns user information on the given provider.
*/
function _ding_provider_get_provider_users($type = NULL) {
static $provider_users;
if (!isset($provider_users)) {
$provider_users = array();
foreach (module_implements('ding_provider_user') as $module) {
$module_uses = module_invoke($module, 'ding_provider_user');
foreach ($module_uses as $name => $options) {
if (!isset($provider_users[$name])) {
$provider_users[$name] = array(
'ding_profile' => FALSE,
'required' => array(),
'users' => array(),
);
}
if (!empty($options['install time setup'])) {
$provider_users[$name]['ding_profile'] = TRUE;
}
if ($options['required']) {
$provider_users[$name]['required'][] = $module;
}
else {
$provider_users[$name]['users'][] = $module;
}
}
}
}
if (!$type) {
return $provider_users;
}
if (isset($provider_users[$type])) {
return $provider_users[$type];
}
return NULL;
}
/**
* Returns the provider function for a hook, loading files if necessary. Logs
* an error if the provider or hook isn't implemented, unless $quiet has been
* specified.
*
* @param $type Provider name.
* @param $hook Hook name.
* @param $quiet Whether to suppress errors.
* @return
* String or NULL.
*/
function _ding_provider_function($type, $hook, $quiet = FALSE) {
$provider = _ding_provider_get_provider($type);
if ($provider) {
$function = $provider['module'] . '_' . (isset($provider['prefix']) ? $provider['prefix'] . '_' : '') . $hook;
if (isset($provider['file'])) {
require_once DRUPAL_ROOT . '/' . $provider['file'];
}
if (function_exists($function)) {
return $function;
}
elseif (!$quiet) {
// Trigger an error. This might be a module attempting to use a wrong
// hook, or an improperly implemented plugin. In either case, it's
// programmer error.
throw new DingProviderDoesntImplement(t('Ding @type provider (@module module) does not implement @function', array('@type' => $type, '@function' => $hook, '@module' => $provider['module'])), E_USER_ERROR);
}
}
elseif (!$quiet) {
throw new DingProviderNoProvider(t("Provider module not configured for provider type @type.", array('@type' => $type)));
}
return NULL;
}
/**
* Wrapper function used to create an pseudo ting entity, which mainly is used
* when the provider returns reservation, loans or debts that do not have a ting
* entity in the date well, but will stile wishes to use it in lists together
* with real ting entities.
*
* @param string $ding_entity_id
* Ding entity ID for the data well object.
*
* @return TingEntity
* Ting entity object based on the entity ID given.
*/
function ding_provider_get_pseudo_entity($ding_entity_id) {
$local_id = drupal_substr($ding_entity_id, strpos($ding_entity_id, ':') + 1);
// Build entity based on available information.
$ting_entity = new TingEntity();
$ting_entity->tid = $local_id;
$ting_entity->localId = $local_id;
$ting_entity->ding_entity_id = $ding_entity_id;
$ting_entity->provider_id = $local_id;
$ting_entity->ding_entity_type = 'ding_entity';
$ting_entity->reply = new TingClientObject();
$ting_entity->reply->id = $ding_entity_id;
$ting_entity->reply->localId = $local_id;
$ting_entity->reply->relations = array();
$ting_entity->reply->ownerId = drupal_substr($ding_entity_id, 0, strpos($ding_entity_id, '-'));
// See if the provider have any information about the entity.
if (ding_provider_implements('loan', 'populate_pseudo_entity')) {
$ting_entity = ding_provider_invoke('loan', 'populate_pseudo_entity', $ting_entity);
}
// Mark this object as pseudo, so formatters can take action.
$ting_entity->is_available = FALSE;
return $ting_entity;
}
/**
* Helper function to create data well 3 PID's.
*
* This function is placed in the provider and not "ting" module as the
* providers uses it and we don't want to have a binding from providers to ting.
*
* @param string $id
* The materials local id number (faust).
* @param string $agency
* Agency ID to which the entity belongs.
*
* @return string
* The PID for the faust in the data well.
*/
function ding_provider_build_entity_id($id, $agency = -1) {
$ting_entity_id = FALSE;
// Get local library faust ID range.
$local_range = '9';
// Get agency ID.
$local_agency = variable_get('ting_agency', '');
switch ($agency) {
case $local_agency:
case -1:
if (preg_match('/^' . $local_range . '/', $id)) {
// If agency is not given fallback to local agency.
if (empty($agency) || $agency == -1) {
$agency = $local_agency;
}
// Local posts.
$ting_entity_id = $agency . '-katalog:' . $id;
}
else {
// Global posts.
$ting_entity_id = '870970-basis:' . $id;
}
break;
case '150021':
$ting_entity_id = '150021-bibliotek:' . $id;
break;
case '150027':
$ting_entity_id = '150021-fjern:' . $id;
break;
case '150030':
$ting_entity_id = '870970-spilmedier:' . $id;
break;
case '870973':
$ting_entity_id = '870973-anmeld:' . $id;
break;
case '150039':
$ting_entity_id = '150015-forlag:' . $id;
break;
case '870976':
$ting_entity_id = '870976-anmeld:' . $id;
break;
case '150048':
$ting_entity_id = '870970-basis:' . $id;
break;
case '150028':
$ting_entity_id = '870970-basis:' . $id;
break;
case '150015':
$ting_entity_id = '870970-basis:' . $id;
break;
case '150033':
$ting_entity_id = '150033-dandyr:' . $id;
break;
case '150040':
$ting_entity_id = '150040-verdyr:' . $id;
break;
case '150018':
$ting_entity_id = '150018-danhist:' . $id;
break;
case '150032':
$ting_entity_id = '150018-samfund:' . $id;
break;
case '150034':
$ting_entity_id = '150018-religion:' . $id;
break;
case '150054':
$ting_entity_id = '150018-biologi:' . $id;
break;
case '150055':
$ting_entity_id = '150018-fysikkemi:' . $id;
break;
case '150056':
$ting_entity_id = '150056-geografi:' . $id;
break;
case '150008':
$ting_entity_id = '150008-academic:' . $id;
break;
case '150043':
$ting_entity_id = '150043-atlas:' . $id;
break;
case '150023':
$ting_entity_id = '150023-sicref:' . $id;
break;
case '150025':
$ting_entity_id = '150008-public:' . $id;
break;
case '150052':
$ting_entity_id = '870970-basis:' . $id;
break;
case '159002':
$ting_entity_id = '159002-lokalbibl:' . $id;
break;
case '150012':
$ting_entity_id = '150012-leksikon:' . $id;
break;
default:
// The agency ID was not translatable, do to one-to-may match from old pid
// to new pid. Or the agency ID is not known. So simply log it under a
// filterable type.
watchdog('ding_provider_pid', 'PID creation failed with agency (%agency) and ID (%id)', array('%agency' => $agency, '%id' => $id), WATCHDOG_ERROR);
break;
}
return $ting_entity_id;
}
/**
* Implements hook_entity_info_alter().
*
* Disable entity cache for profile2 entities, so provider profiles are not
* cached. The profile should not be cached as they contains sensitive
* information about the library users.
*/
function ding_provider_entity_info_alter(&$entity_info) {
$entity_info['profile2']['entity cache'] = FALSE;
$entity_info['profile2_type']['entity cache'] = FALSE;
}