-
Notifications
You must be signed in to change notification settings - Fork 354
/
OverdriveController.php
579 lines (540 loc) · 20.3 KB
/
OverdriveController.php
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
<?php
/**
* Overdrive Controller
*
* PHP version 8
*
* @category VuFind
* @package Controller
* @author Brent Palmer <brent-palmer@ipcl.org>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org Main Site
*/
namespace VuFind\Controller;
use Laminas\Log\LoggerAwareInterface;
use Laminas\ServiceManager\ServiceLocatorInterface;
use VuFind\DigitalContent\OverdriveConnector;
use function is_array;
/**
* Overdrive Controller supports actions for Overdrive Integration
*
* @category VuFind
* @package Controller
* @author Brent Palmer <brent-palmer@ipcl.org>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:plugins:controllers Wiki
*/
class OverdriveController extends AbstractBase implements LoggerAwareInterface
{
use \VuFind\Log\LoggerAwareTrait {
logError as error;
}
/**
* Overdrive Connector
*
* @var OverdriveConnector $connector Overdrive Connector
*/
protected $connector;
/**
* Constructor
*
* @param ServiceLocatorInterface $sm Service locator
*/
public function __construct(ServiceLocatorInterface $sm)
{
$this->setLogger($sm->get(\VuFind\Log\Logger::class));
$this->connector
= $sm->get(\VuFind\DigitalContent\OverdriveConnector::class);
parent::__construct($sm);
}
/**
* My Content Action
* Prepares the view for the Overdrive MyContent template.
*
* @return array|bool|\Laminas\View\Model\ViewModel
*/
public function mycontentAction()
{
$this->debug('ODC mycontent action');
// Force login
if (!is_array($patron = $this->catalogLogin())) {
return $patron;
}
$holds = [];
$checkouts = [];
$checkoutsUnavailable = false;
$holdsUnavailable = false;
// Check on this patrons's access to Overdrive
$odAccessResult = $this->connector->getAccess();
if (!($odAccessResult->status ?? false)) {
$this->debug('result:' . print_r($odAccessResult, true));
$this->flashMessenger()->addErrorMessage(
$this->translate(
$odAccessResult->code ?? 'An error has occurred',
['%%message%%' => $odAccessResult->msg ?? '']
)
);
$checkoutsUnavailable = true;
$holdsUnavailable = true;
} else {
// Get the current Overdrive checkouts
// for this user and add to our array of IDS
$checkoutResults = $this->connector->getCheckouts(true);
if (!($checkoutResults->status ?? false)) {
$this->flashMessenger()->addMessage(
$checkoutResults->code ?? 'An error has occurred',
'error'
);
$checkoutsUnavailable = true;
} else {
foreach ($checkoutResults->data as $checkout) {
$mycheckout = compact('checkout');
if ($checkout->metadata->mediaType == 'Magazine') {
$mycheckout['checkout']->isMagazine = true;
$this->debug("loading magazine metadata for {$checkout->reserveId}");
$idToLoad = strtolower($checkout->metadata->parentMagazineReferenceId);
$this->debug("loading magazine parent with id: $idToLoad instead");
} else {
$mycheckout['checkout']->isMagazine = false;
$idToLoad = strtolower($checkout->reserveId);
}
try {
$this->debug("loading checkout using: $idToLoad");
$mycheckout['record'] = $this->getService(\VuFind\Record\Loader::class)
->load($idToLoad);
$checkouts[] = $mycheckout;
} catch (\VuFind\Exception\RecordMissing $e) {
$this->debug("missing record in index: $idToLoad");
// checkout is missing from Solr
$this->flashMessenger()->addMessage(
'One or more checkouts could not be displayed properly: ' .
$e->getMessage(),
'error'
);
// get metadata from overdrive.
$meta = $this->connector->getMetadata([strtolower($checkout->reserveId)]);
$mycheckout['metadata'] = $meta[strtolower($checkout->reserveId)];
$checkouts[] = $mycheckout;
}
}
}
// Get the current Overdrive holds for this user and add to
// our array of IDS
$holdsResults = $this->connector->getHolds(true);
if (
!($holdsResults->status ?? false)
&& ($checkoutResults->status ?? false) // avoid double errors
) {
$this->flashMessenger()->addMessage(
$holdsResults->code ?? 'An error has occurred',
'error'
);
$holdsUnavailable = true;
} else {
foreach ($holdsResults->data as $hold) {
$myhold['hold'] = $hold;
try {
$myhold['record']
= $this->getService(\VuFind\Record\Loader::class)
->load(strtolower($hold->reserveId));
$holds[] = $myhold;
} catch (\VuFind\Exception\RecordMissing $e) {
// hold is missing from Solr
$this->flashMessenger()->addMessage(
'One or more holds could not be displayed properly: ' .
$e->getMessage(),
'error'
);
// get metadata from overdrive.
$meta = $this->connector->getMetadata([$hold->reserveId]);
$myhold['metadata'] = $meta[$hold->reserveId];
$holds[] = $myhold;
}
}
}
}
$view = $this->createViewModel(
compact(
'checkoutsUnavailable',
'holdsUnavailable',
'checkouts',
'holds'
)
);
$view->setTemplate('myresearch/odmycontent');
return $view;
}
/**
* Get Status Action
* Supports the ajax getStatus calls
*
* @return array|bool|\Laminas\View\Model\ViewModel
*/
public function getStatusAction()
{
$this->debug('ODC getStatus action');
$ids = $this->params()->fromPost(
'id',
$this->params()->fromQuery('id', [])
);
$result = $this->connector->getAvailabilityBulk($ids);
$view = $this->createViewModel(compact('ids', 'result'));
$view->setTemplate('RecordDriver/SolrOverdrive/status-full');
$this->layout()->setTemplate('layout/lightbox');
return $view;
}
/**
* Hold Action
*
* Hold Action handles all of the actions involving
* Overdrive content including checkout, hold, cancel hold etc.
*
* @return array|bool|\Laminas\View\Model\ViewModel
* @todo Deal with situation that an unlogged in user requests
* an action but the action is no longer valid since they
* already have the content on hold/checked out or do not have access
*/
public function holdAction()
{
$this->debug('ODC Hold action');
if (!is_array($patron = $this->catalogLogin())) {
return $patron;
}
$od_id = $this->params()->fromQuery('od_id');
$rec_id = $this->params()->fromQuery('rec_id');
$action = $this->params()->fromQuery('action');
$edition = $this->params()->fromPost(
'edition',
$this->params()->fromQuery('edition', false)
);
$holdEmail = '';
// Action comes in through the form
if (null !== $this->params()->fromPost('doAction')) {
$action = $this->params()->fromPost('doAction');
}
// Place hold action comes in through the form
if (null !== $this->params()->fromPost('getTitleFormat')) {
$format = $this->params()->fromPost('getTitleFormat');
}
$format = $this->params()->fromQuery('getTitleFormat');
$this->debug("ODRC od_id=$od_id rec_id=$rec_id action=$action");
// Load the Record Driver. Should be a SolrOverdrive driver.
$driver = $this->getService(\VuFind\Record\Loader::class)->load(
$rec_id
);
$formats = $driver->getDigitalFormats();
$title = $driver->getTitle();
$cover = $driver->getThumbnail('small');
$listAuthors = $driver->getPrimaryAuthors();
$result = null;
$actionTitleCode = '';
if (!$action) {
// Double check the availability in case it has changed since the page
// was loaded.
$avail = $driver->getOverdriveAvailability();
$action = ($avail->copiesAvailable > 0) ? 'checkoutConfirm' : 'holdConfirm';
}
$actions = [
'checkoutConfirm' => ['titleCode' => 'od_checkout', 'resMeth' => 'getConfirmCheckoutRes'],
'holdConfirm' => ['titleCode' => 'od_hold', 'resMeth' => 'getHoldConfirmRes'],
'cancelHoldConfirm' => ['titleCode' => 'od_cancel_hold', 'resMeth' => null],
'suspHoldConfirm' => ['titleCode' => 'od_susp_hold', 'resMeth' => null],
'editHoldConfirm' => ['titleCode' => 'od_susp_hold_edit', 'resMeth' => null],
'editHoldEmailConfirm' => ['titleCode' => 'od_edit_hold_email', 'resMeth' => 'getEditHoldEmailConfRes'],
'returnTitleConfirm' => ['titleCode' => 'od_early_return', 'resMeth' => 'getReturnTitleConfirmResult'],
'doCheckout' => ['titleCode' => 'od_checkout', 'resMeth' => 'getCheckoutRes'],
'placeHold' => ['titleCode' => 'od_hold', 'resMeth' => 'getPlaceHoldRes'],
'editHoldEmail' => ['titleCode' => 'od_hold', 'resMeth' => 'getEditHoldEmailRes'],
'suspendHold' => ['titleCode' => 'od_susp_hold', 'resMeth' => 'getSuspendHoldRes'],
'editSuspendedHold' => ['titleCode' => 'od_susp_hold_edit', 'resMeth' => 'getEditSuspendedRes'],
'cancelHold' => ['titleCode' => 'od_cancel_hold', 'resMeth' => 'getCancelHoldRes'],
'returnTitle' => ['titleCode' => 'od_early_return', 'resMeth' => 'getReturnTitleRes'],
'getTitle' => ['titleCode' => 'od_get_title', 'resMeth' => 'getDownloadTitleRes'],
];
if (isset($actions[$action])) {
$actionTitleCode = $actions[$action]['titleCode'];
$result = $actions[$action]['resMeth'] ? $this->{$actions[$action]['resMeth']}() : false;
} else {
$this->logWarning("overdrive action not defined: $action");
}
$view = $this->createViewModel(
compact(
'od_id',
'rec_id',
'action',
'result',
'formats',
'cover',
'title',
'actionTitleCode',
'listAuthors',
'edition'
)
);
$view->setTemplate('RecordDriver/SolrOverdrive/hold');
return $view;
}
/**
* Hold Confirm Result
*
* Get result of the action
*
* @return obj Result Object
*/
public function getHoldConfirmRes()
{
$od_id = $this->params()->fromQuery('od_id');
$result = $this->connector->getResultObject();
// Check to make sure they don't already have this checked out.
// Shouldn't need to refresh.
if ($checkout = $this->connector->getCheckout($od_id, true)) {
$result->status = false;
$result->data = (object)[];
$result->data->checkout = $checkout;
$result->code = 'OD_CODE_ALREADY_CHECKED_OUT';
$this->debug("title already checked out: $od_id");
} elseif ($hold = $this->connector->getHold($od_id, true)) {
$result->status = false;
$result->data = (object)[];
$result->data->hold = $hold;
$result->code = 'OD_CODE_ALREADY_ON_HOLD';
$this->debug("title already on hold: $od_id");
} else {
$result->status = true;
}
return $result;
}
/**
* Confirm Checkout Result
*
* Get result of the action
*
* @return obj Result Object
*/
public function getConfirmCheckoutRes()
{
$od_id = $this->params()->fromQuery('od_id');
$rec_id = $this->params()->fromQuery('rec_id');
// Load the Record Driver. Should be a SolrOverdrive driver.
$driver = $this->getService(\VuFind\Record\Loader::class)->load(
$rec_id
);
$formats = $driver->getDigitalFormats();
// Looks like this is a magazine...
if (current($formats)->id == 'magazine-overdrive') {
$isMagazine = true;
$result = $this->connector->getMagazineIssues($od_id, true);
if ($result->status) {
$issues = $result->data->products;
$result->data->isMagazine = true;
} else {
$this->debug("couldn't get issues for checkout");
$result->status = false;
$result->code = 'OD_CODE_NOMAGISSUES';
$result->msg = 'No magazine issue available.';
}
} else {
$result = $this->connector->getResultObject();
$result->data = (object)['isMagazine' => false];
// Check to make sure they don't already have this checked out
// shouldn't need to refresh.
if ($checkout = $this->connector->getCheckout($od_id, false)) {
$result->status = false;
$result->data->checkout = $checkout;
$result->code = 'OD_CODE_ALREADY_CHECKED_OUT';
} elseif ($hold = $this->connector->getHold($od_id, false)) {
if ($hold->holdReadyForCheckout) {
$result->status = true;
} else {
$result->status = false;
$result->data->hold = $hold;
$result->code = 'OD_CODE_ALREADY_ON_HOLD';
}
} else {
$result->status = true;
}
}
return $result;
}
/**
* Checkout Result
*
* Get result of the action
*
* @return obj Result Object
*/
public function getCheckoutRes()
{
$od_id = $this->params()->fromQuery('od_id');
$edition = $this->params()->fromPost(
'edition',
$this->params()->fromQuery('edition', false)
);
if ($edition) {
$od_id = $edition;
}
$result = $this->connector->doOverdriveCheckout($od_id);
return $result;
}
/**
* Place Hold Result
*
* Get result of the action
*
* @return obj Result Object
*/
public function getPlaceHoldRes()
{
$od_id = $this->params()->fromQuery('od_id');
$email = $this->params()->fromPost('email');
$result = $this->connector->placeOverDriveHold($od_id, $email);
if ($result->status) {
$result->code = 'od_hold_place_success';
$result->codeParams = ['%%holdListPosition%%' => $result->data->holdListPosition];
} else {
$result->code = 'od_hold_place_failure';
}
return $result;
}
/**
* Edit Hold Email Confirm Result
*
* Get result of the action
*
* @return obj Result Object
*/
public function getEditHoldEmailConfRes()
{
$od_id = $this->params()->fromQuery('od_id');
$hold = $this->connector->getHold($od_id, true);
$result = $this->connector->getResultObject(true);
$result->data = (object)[];
$result->data->hold = $hold;
return $result;
}
/**
* Edit Hold Email Result
*
* Get result of the action
*
* @return obj Result Object
*/
public function getEditHoldEmailRes()
{
$od_id = $this->params()->fromQuery('od_id');
$email = $this->params()->fromPost('email');
$result = $this->connector->updateOverDriveHold($od_id, $email);
$result->code = $result->status ? 'od_hold_update_success' : 'od_hold_update_failure';
return $result;
}
/**
* Return Title Confirmation Result
*
* Get result of the action
*
* @return obj Result Object
*/
public function getReturnTitleConfirmResult()
{
$result = $this->connector->getResultObject();
$rec_id = $this->params()->fromQuery('rec_id');
// Load the SolrOverdrive driver.
$driver = $this->getService(\VuFind\Record\Loader::class)->load(
$rec_id
);
$formats = $driver->getDigitalFormats();
$result->data = (current($formats)->id == 'magazine-overdrive') ?: false;
return $result;
}
/**
* Suspend Hold Result
*
* Get result of the action
*
* @return obj Result Object
*/
public function getSuspendHoldRes()
{
$od_id = $this->params()->fromQuery('od_id');
$suspendValue = $this->params()->fromPost('suspendValue');
$hold = $this->connector->getHold($od_id, false);
$holdEmail = $hold->emailAddress;
$suspensionType = $suspendValue == -1 ? 'indefinite' : 'limited';
$result = $this->connector->suspendHold($od_id, $holdEmail, $suspensionType, $suspendValue);
if ($result->status) {
if ($suspensionType == 'indefinite') {
$result->code = 'od_hold_susp_indef';
} else {
$result->code = 'od_hold_redelivery';
$result->codeParams = ['%%days%%' => $result->data->holdSuspension->numberOfDays];
}
} else {
$result->code = 'od_hold_update_failure';
}
return $result;
}
/**
* Edit Suspended Hold Result
*
* Get result of the action
*
* @return obj Result Object
*/
public function getEditSuspendedRes()
{
$od_id = $this->params()->fromQuery('od_id');
$suspendValue = $this->params()->fromPost('suspendValue');
if ($suspendValue == 0) {
$result = $this->connector->deleteHoldSuspension($od_id);
} else {
$hold = $this->connector->getHold($od_id, false);
$holdEmail = $hold->emailAddress;
$suspensionType = $suspendValue == -1 ? 'indefinite' : 'limited';
$result = $this->connector->editSuspendedHold($od_id, $holdEmail, $suspensionType, $suspendValue);
}
$result->code = $result->status ? 'od_hold_update_success' : 'od_hold_update_failure';
return $result;
}
/**
* Cancel Hold Result
*
* Get result of the action
*
* @return obj Result Object
*/
public function getCancelHoldRes()
{
$od_id = $this->params()->fromQuery('od_id');
$result = $this->connector->cancelHold($od_id);
$result->code = $result->status ? 'od_hold_cancel_success' : 'od_hold_cancel_failure';
return $result;
}
/**
* Return Title Result
*
* Get result of the action
*
* @return obj Result Object
*/
public function getReturnTitleRes()
{
$od_id = $this->params()->fromQuery('od_id');
$result = $this->connector->returnResource($od_id);
$result->code = $result->status ? 'od_return_success' : 'od_return_failure';
return $result;
}
/**
* Download Title Result
*
* Get result of the action
*
* @return obj Result Object
*/
public function getDownloadTitleRes()
{
$od_id = $this->params()->fromQuery('od_id');
$result = $this->connector->getDownloadRedirect($od_id);
$result->code = $result->status ? '' : 'od_gettitle_failure';
return $result;
}
}