Newer
Older
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
<?php
/**
* PHP version 7
*
* Copyright (C) 2022 Leipzig University Library
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* @category VuFind
* @package Controller
* @author Dorian Merz <merz@ub.uni-leipzig.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU GPLv2
* @link https://vufind.org/wiki/development Wiki
*/
namespace fid\Controller;
use fid\Hydrator\OrderExportHydrator as OrderExportHydrator;
use fid\Service\Client;
use fid\Service\ClientException;
use fid\Service\DataTransferObject\Order;
use fid\Service\UserNotAuthorizedException;
use VuFind\Auth\Manager as AuthManager;
use VuFind\Controller\AbstractBase;
use Zend\Form\Element\Select;
use Zend\Form\Form;
use Zend\Http\PhpEnvironment\Request;
use Zend\Http\PhpEnvironment\Response as HttpResponse;
use Zend\Http\Response;
use Zend\Mvc\Plugin\FlashMessenger\FlashMessenger;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\View\Model\ViewModel;
/**
* User Controller
*
* @category VuFind
* @package Controller
* @author Dorian Merz <merz@ub.uni-leipzig.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU GPLv2
* @link https://vufind.org/wiki/development Wiki
*/
class OrderController extends AbstractBase
{
/**
* AuthManager
*
* @var AuthManager
*/
protected $authManager;
/**
* Fidis client
*
* @var Client
*/
protected $client;
/**
* Configuration
*
* @var array
*/
protected $config;
/**
* RegistrationController constructor.
*
* @param ServiceLocatorInterface $serviceLocator Service Locator
* @param AuthManager $authManager Authentication Manager
* @param Client $client fidis client
* @param array $config Configuration
*/
public function __construct(
ServiceLocatorInterface $serviceLocator,
AuthManager $authManager,
Client $client,
array $config
) {
parent::__construct($serviceLocator);
$this->authManager = $authManager;
$this->client = $client;
$this->config = $config;
}
/**
* Get flash messenger
*
* @return FlashMessenger
*/
protected function getMessenger(): FlashMessenger
{
/* @noinspection PhpUndefinedMethodInspection */
/* @var FlashMessenger $messenger */
return $this->flashMessenger();
}
/**
* Display users order list route action
*
* @return mixed|void|ViewModel
* @throws UserNotAuthorizedException
*/
public function ordersAction()
{
if (!$this->getUser()) {
return $this->forceLogin();
}
try {
$user = $this->client->requestUserDetails(null, true);
$viewModel = $this->createViewModel();
$orders = $user->getOrders();
$displayCols = $this->config['OrderListUser']['displayCols'];
$viewModel->setVariables(compact('orders', 'displayCols'));
$viewModel->setTemplate('fid/order/order-list');
return $viewModel;
} catch (ClientException $exception) {
$this->getMessenger()->addErrorMessage('fid::orders_error');
$this->redirect()->toRoute('myresearch-profile');
}
}
/**
* Edit single order route action
*
* @return mixed|Response|ViewModel
* @throws \Exception
*/
public function editOrderAction()
{
if (!$this->getUser()) {
return $this->forceLogin();
}
/* @var Request $request */
$request = $this->getRequest();
$orderId = $this->params()->fromRoute('orderid');
if (empty($orderId)) {
// if no order ID is set the call is not valid
return $this->redirect()->toRoute('fid/admin/orders');
}
try {
$this->permission()->check('fid.EditOrder', 'exception');
$order = $this->client->requestOrder($orderId);
} catch (\Exception $ex) {
$this->getMessenger()
->addErrorMessage($this->translate('fid::edit_order_error'));
return $this->redirect()->toRoute('fid/admin/orders');
}
$recordId = $order->getRecordId();
$driver = $recordId ? $this->getRecordLoader()->load($recordId) : null;
/* create form only if order type has form configuration in fid.ini */
if (isset($this->config[$order->getType() . "Edit"]['form'])) {
/* @var Form $form */
/* @var Select $statusElement */
$form = $this->serviceLocator->get(
'order-edit-form-' . $this->config[$order->getType() . "Edit"]['form']
);
$this->applyStatusOptions($form, $order->getType());
if ($this->formWasSubmitted()) {
$form->setData($request->getPost());
if ($form->isValid()) {
return $this->updateOrder($form, $order);
}
} else {
$form->setData($form->getHydrator()->extract($order));
}
$action = $this->url()->fromRoute(
'fid/admin/editOrder',
[
'orderid' => $order->getId()
]
);
$form->setAttribute('action', $action);
$form->prepare();
} else {
$form = null;
}
$config = $this->config;
$viewModel = $this->createViewModel();
$viewModel->setVariables(compact('config', 'form', 'order', 'driver'));
$viewModel->setTemplate('fid/order/order-edit');
return $viewModel;
}
/**
* Update order via fidis and redirect to order list
*
* @param Form $form Formular
* @param \fid\Service\DataTransferObject\Order $order Order object
*
* @return Response
*/
protected function updateOrder(Form $form, Order $order)
{
$data = $form->getData();
$messenger = $this->getMessenger();
try {
$form->getHydrator()->hydrate($data, $order);
$this->client->requestOrderUpdate($order);
$message = $this->translate('fid::order_update_success');
$messenger->addSuccessMessage($message);
} catch (\Exception $ex) {
$message = $this->translate('fid::order_update_error');
$messenger->addErrorMessage($message);
}
return $this->redirect()->toRoute('fid/admin/orders');
}
/**
* Helper method for generating status options list and
* applies it to update order status input element.
* Display only options stored in configuration.
*
* @param Form $form Formular
* @param string $type Acquisition type
*
* @return void
*/
protected function applyStatusOptions($form, $type)
{
if ($statusOptions = $this->config["{$type}Edit"]['statusOptions'] ?? null) {
$options = [];
foreach ($statusOptions as $statusOption) {
$options[] = [
'value' => $statusOption,
'label' => $this->translate('fid::status_' . $statusOption),
];
}
$statusElement = $form->get('status');
$statusElement->setValueOptions($options);
}
}
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
/**
* Deletes given Order
*
* @return mixed|void|ViewModel
*
* @throws UserNotAuthorizedException
* @throws ClientException
*/
public function deleteOrderAction()
{
if (!$this->getUser()) {
return $this->forceLogin();
}
$messenger = $this->getMessenger();
try {
$orderId = $this->params()->fromRoute('orderid');
$this->client->requestOrderDelete($orderId);
$message = $this->translate('fid::order_delete_success');
$messenger->addSuccessMessage($message);
} catch (UserNotAuthorizedException $exception) {
$messenger->addErrorMessage('fid::read_order_list_not_allowed');
} catch (ClientException $exception) {
$messenger->addErrorMessage('fid::order_delete_error');
}
return $this->redirect()->toRoute('fid/admin/orders');
}
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
/**
* Display admins order list route action
*
* @return mixed|void|ViewModel
*/
public function adminOrdersAction()
{
if (!$this->getUser()) {
return $this->forceLogin();
}
try {
$orders = $this->client->requestOrderList();
$viewModel = $this->createViewModel();
$displayCols = $this->config['OrderListAdmin']['displayCols'];
$export_button = $this->config['OrderListAdmin']['export'] ?? false;
if ($export_button) {
$viewModel->setVariable("export_button", $export_button);
}
$viewModel->setVariables(compact('orders', 'displayCols'));
$viewModel->setTemplate('fid/order/order-list-admin');
return $viewModel;
} catch (UserNotAuthorizedException $exception) {
$this->getMessenger()
->addErrorMessage('fid::read_order_list_not_allowed');
} catch (ClientException $exception) {
$this->getMessenger()->addErrorMessage('fid::read_order_list_error');
$this->redirect()->toRoute('myresearch-profile');
}
}
/**
* Request download of user list route action
*
* @return HttpResponse
* @throws ClientException
* @throws UserNotAuthorizedException
*/
public function exportAction()
{
$mode = $this->params()->fromRoute('mode') ?? 'default';
$orders = $this->client->requestOrderList();
if ($mode == "json") {
return $this->createJsonExportFile($orders);
} else {
$fields = $this->config['Admin']['order_export_fields'] ?? null;
return $this->createCsvExportFile(
$orders,
$fields,
$mode
);
}
}
/**
* Build user list export file
*
* @param Order[] $orderList User List
* @param string[] $fields Fields to render
* @param string $mode Type of csv/txt that gets exportet
* @param string $separator Separator for CSV
*
* @return HttpResponse
*/
protected function createCsvExportFile($orderList, $fields=null, $mode="default", $separator="\t")
{
$response = new HttpResponse();
$prefix = $this->config['Admin']['order_list_export_file_prefix'] ?? 'export';
if ($mode == "conform" or $mode == "microsoft") {
$response->getHeaders()->addHeaders(
[
'Content-Encoding: UTF-8',
'Content-type' => 'text/csv;charset=utf-8',
'Content-Disposition' => 'attachment; filename="'
. $prefix . '_' . date('Ymd_His') . '.csv"'
]
);
} else {
$response->getHeaders()->addHeaders(
[
'Content-Encoding: UTF-8',
'Content-type' => 'text/plain;charset=utf-8',
'Content-Disposition' => 'attachment; filename="'
. $prefix . '_' . date('Ymd_His') . '.txt"'
]
);
}
if ($mode == "microsoft") {
$output = "sep=\t\n";
} else {
$output = "";
}
$hydrator = new OrderExportHydrator();
if (empty($fields)) {
$fields = $hydrator::FIELDS;
}
# header
foreach ($fields as $rectangle) {
$output .= $this->translate('fid_export_' . $rectangle) . $separator;
}
$output = rtrim($output, $separator) . "\n";
/* @var Order $order */
foreach ($orderList as $order) {
$data = $hydrator->extract($order);
foreach ($fields as $rectangle) {
# translation drive-by
if ($rectangle == "type") {
$output .= $this->translate("fid::acquisition_" . $data[$rectangle]) . $separator;
} else {
$output .= $data[$rectangle] . $separator;
}
}
$output = rtrim($output, $separator) . "\n";
}
$response->setContent($output);
return $response;
}
/**
* Build user list export file
*
* @param Order[] $orderList User List
* @param string[] $fields Fields to include
*
* @return HttpResponse
*/
protected function createJsonExportFile($orderList, $fields=null)
{
$response = new HttpResponse();
$prefix = $this->config['Admin']['order_list_export_file_prefix'] ?? 'export';
$response->getHeaders()->addHeaders(
[
'Content-Encoding: UTF-8',
'Content-type' => 'application/json;charset=utf-8',
'Content-Disposition' => 'attachment; filename="'
. $prefix . '_' . date('Ymd_His') . '.json"'
]
);
$output = [];
$hydrator = new OrderExportHydrator();
foreach ($orderList as $order) {
$data = $hydrator->extract($order);
$output[$data['id']] = [];
//leave out empty fields
foreach ($data as $key => $value) {
if (!empty($value) and $key != 'id') {
$output[$data['id']][$key] = $value;
}
}
}
$response->setContent(json_encode($output, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
return $response;
}
}