Issues (3936)

Classes/Controller/AjaxBackofficeController.php (8 issues)

1
<?php
2
namespace EWW\Dpf\Controller;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use EWW\Dpf\Domain\Model\FrontendUser;
18
use EWW\Dpf\Domain\Model\MetadataGroup;
19
use EWW\Dpf\Services\FeUser\FisDataService;
20
use EWW\Dpf\Services\FeUser\GndDataService;
21
22
use EWW\Dpf\Services\FeUser\OrcidDataService;
23
use EWW\Dpf\Services\FeUser\RorDataService;
24
use EWW\Dpf\Services\FeUser\UnpaywallDataService;
25
use EWW\Dpf\Services\FeUser\ZdbDataService;
26
use EWW\Dpf\Session\BulkImportSessionData;
27
use EWW\Dpf\Session\SearchSessionData;
28
29
/**
30
 * AjaxBackofficeController
31
 */
32
class AjaxBackofficeController extends \EWW\Dpf\Controller\AbstractController
33
{
34
    /**
35
     * bookmarkRepository
36
     *
37
     * @var \EWW\Dpf\Domain\Repository\BookmarkRepository
38
     * @TYPO3\CMS\Extbase\Annotation\Inject
39
     */
40
    protected $bookmarkRepository = null;
41
42
    /**
43
     * frontendUserRepository
44
     *
45
     * @var \EWW\Dpf\Domain\Repository\FrontendUserRepository
46
     * @TYPO3\CMS\Extbase\Annotation\Inject
47
     */
48
    protected $frontendUserRepository = null;
49
50
    /**
51
     * metadataGroupRepository
52
     *
53
     * @var \EWW\Dpf\Domain\Repository\MetadataGroupRepository
54
     * @TYPO3\CMS\Extbase\Annotation\Inject
55
     */
56
    protected $metadataGroupRepository = null;
57
58
    /**
59
     * externalMetadataRepository
60
     *
61
     * @var \EWW\Dpf\Domain\Repository\ExternalMetadataRepository
62
     * @TYPO3\CMS\Extbase\Annotation\Inject
63
     */
64
    protected $externalMetadataRepository = null;
65
66
    /**
67
     * Adds a the given document identifier to the bookmark list of the current fe user.
68
     *
69
     * @param string $identifier
70
     * @return bool
71
     */
72
    public function addBookmarkAction($identifier)
73
    {
74
        /** @var \EWW\Dpf\Domain\Model\Bookmark $bookmark */
75
        $bookmark = $this->bookmarkRepository->findBookmark($this->security->getUser()->getUid(), $identifier);
76
        if (!$bookmark) {
0 ignored issues
show
$bookmark is of type EWW\Dpf\Domain\Model\Bookmark, thus it always evaluated to true.
Loading history...
77
            $bookmark = $this->objectManager->get(\EWW\Dpf\Domain\Model\Bookmark::class);
78
            $bookmark->setDocumentIdentifier($identifier);
79
            $bookmark->setFeUserUid($this->security->getUser()->getUid());
80
            $this->bookmarkRepository->add($bookmark);
81
            return true;
82
        }
83
84
        return false;
85
    }
86
87
    /**
88
     * Removes the given document from the bookmark list of the current fe user.
89
     *
90
     * @param string $identifier
91
     * @return bool
92
     */
93
    public function removeBookmarkAction($identifier)
94
    {
95
        /** @var \EWW\Dpf\Domain\Model\Bookmark $bookmark */
96
        $bookmark = $this->bookmarkRepository->findBookmark($this->security->getUser()->getUid(), $identifier);
97
        if ($bookmark) {
0 ignored issues
show
$bookmark is of type EWW\Dpf\Domain\Model\Bookmark, thus it always evaluated to true.
Loading history...
98
            $this->bookmarkRepository->remove($bookmark);
99
            return true;
100
        }
101
102
        return false;
103
    }
104
105
    /**
106
     * Adds a workspace filter to the session.
107
     *
108
     * @param string $name
109
     * @param array $values
110
     * @return bool
111
     */
112
    public function addWorkspaceFilterAction($name, $values = [])
113
    {
114
        /** @var SearchSessionData $workspaceSessionData */
115
        $workspaceSessionData = $this->session->getWorkspaceData();
116
        $workspaceSessionData->setFilter($name, $values);
117
        $this->session->setWorkspaceData($workspaceSessionData);
118
        return true;
119
    }
120
121
    /**
122
     * Adds a workspace sort to the session.
123
     *
124
     * @param string $field
125
     * @param string $order
126
     * @return bool
127
     */
128
    public function addWorkspaceSortAction($field, $order)
129
    {
130
        /** @var SearchSessionData $workspaceSessionData */
131
        $workspaceSessionData = $this->session->getWorkspaceData();
132
        $workspaceSessionData->setSortField($field);
133
        $workspaceSessionData->setSortOrder($order);
134
        $this->session->setWorkspaceData($workspaceSessionData);
135
        return true;
136
    }
137
138
    /**
139
     * Toggles the filter to exclude discarded documents.
140
     *
141
     * @return bool
142
     */
143
    public function toggleWorkspaceExcludeDiscardedAction()
144
    {
145
        /** @var SearchSessionData $workspaceSessionData */
146
        $workspaceSessionData = $this->session->getWorkspaceData();
147
        $workspaceSessionData->toggleExcludeDiscardedFilter();
148
        $this->session->setWorkspaceData($workspaceSessionData);
149
        return true;
150
    }
151
152
    /**
153
     * Toggles the filter to hide bookmarked documents.
154
     *
155
     * @return bool
156
     */
157
    public function toggleWorkspaceBookmarksOnlyAction()
158
    {
159
        /** @var SearchSessionData $workspaceSessionData */
160
        $workspaceSessionData = $this->session->getWorkspaceData();
161
        $workspaceSessionData->toggleBookmarksOnlyFilter();
162
        $this->session->setWorkspaceData($workspaceSessionData);
163
        return true;
164
    }
165
166
    /**
167
     * Sets the items per page for the workspace list.
168
     *
169
     * @param int $itemsPerPage
170
     * @return bool
171
     */
172
    public function setWorkspaceItemsPerPageAction($itemsPerPage)
173
    {
174
        /** @var SearchSessionData $workspaceSessionData */
175
        $workspaceSessionData = $this->session->getWorkspaceData();
176
        $workspaceSessionData->setItemsPerPage($itemsPerPage);
177
        $this->session->setWorkspaceData($workspaceSessionData);
178
        return true;
179
    }
180
181
182
    /**
183
     * Save an extended search query.
184
     *
185
     * @param string $name
186
     * @param string $query
187
     * @return bool
188
     */
189
    public function saveExtendedSearchAction($name, $query)
190
    {
191
        $search = new \EWW\Dpf\Domain\Model\StoredSearch();
192
        $search->setName($name);
193
        $search->setQuery($query);
194
195
        /** @var FrontendUser $feUser */
196
        $feUser = $this->security->getUser();
197
        $feUser->addStoredSearch($search);
198
        $this->frontendUserRepository->update($feUser);
199
200
        return true;
201
    }
202
203
    /**
204
     * Loads a stored extended search query.
205
     *
206
     * @param int $id
207
     * @return bool
208
     */
209
    public function loadExtendedSearchAction($id)
210
    {
211
        /** @var FrontendUser $feUser */
212
        $feUser = $this->security->getUser();
213
        $searches = $feUser->getStoredSearches();
214
215
        foreach ($searches as $search) {
216
            if ($search->getUid() == $id) {
217
                return $search->getQuery();
218
            }
219
        }
220
221
        return false;
222
    }
223
224
    /**
225
     * Loads a list of all stored extended search queries.
226
     *
227
     * @return string
228
     */
229
    public function loadExtendedSearchListAction()
230
    {
231
        /** @var FrontendUser $feUser */
232
        $feUser = $this->security->getUser();
233
234
        $searches = [];
235
        foreach ($feUser->getStoredSearches() as $search) {
236
            $searches[] = [
237
                'uid' => $search->getUid(),
238
                'name' => $search->getName(),
239
                'query' => $search->getQuery()
240
            ];
241
        }
242
243
        return json_encode($searches);
244
    }
245
246
    /**
247
     * @param string $searchTerm
248
     * @param string $type
249
     * @return false|string
250
     */
251
    public function searchFisAction($searchTerm, $type = 'person') {
252
        $fisUserDataService = new FisDataService();
253
        $methodName = 'search'.ucfirst($type).'Request';
254
        $result = $fisUserDataService->{$methodName}($searchTerm);
255
256
        return json_encode($result);
257
    }
258
259
    /**
260
     * @param string $dataId
261
     * @param int $groupId
262
     * @param int $groupIndex
263
     * @param int $fieldIndex
264
     * @param int $pageId
265
     * @param string $type
266
     * @return false|string
267
     */
268
    public function getFisDataAction($dataId, $groupId, $groupIndex, $fieldIndex, $pageId, $type = 'person') {
269
        $fisDataService = new FisDataService();
270
        $methodName = 'get'.ucfirst($type).'Data';
271
        $fisData = $fisDataService->{$methodName}($dataId);
272
273
        $result = $this->getApiMappingArray($groupId, $fisData, $groupIndex, $fieldIndex, $pageId, 'getFis'.ucfirst($type).'Mapping');
274
275
        return json_encode($result);
276
    }
277
278
    /**
279
     * @param string $searchTerm
280
     * @param string $type
281
     * @return false|string
282
     */
283
    public function searchGndAction($searchTerm, $type = 'person') {
284
        $gndUserDataService = new GndDataService();
285
        $methodName = 'search'.ucfirst($type).'Request';
286
        $result = $gndUserDataService->{$methodName}($searchTerm);
287
288
        return json_encode($result);
289
    }
290
291
    /**
292
     * @param string $dataId
293
     * @param int $groupId
294
     * @param int $groupIndex
295
     * @param int $fieldIndex
296
     * @param int $pageId
297
     * @param string $type
298
     */
299
    public function getGndDataAction($dataId, $groupId, $groupIndex, $fieldIndex, $pageId, $type = 'person') {
300
        $gndUserDataService = new GndDataService();
301
        $methodName = 'get'.ucfirst($type).'Data';
302
        $gndData = $gndUserDataService->{$methodName}($dataId);
303
304
        $result = $this->getApiMappingArray($groupId, $gndData, $groupIndex, $fieldIndex, $pageId, 'getGnd'.ucfirst($type).'Mapping');
305
306
        return json_encode($result);
307
    }
308
309
    /**
310
     * search ROR API
311
     * API is organisation only!
312
     * @param string $searchTerm
313
     * @return false|string
314
     */
315
    public function searchRorAction($searchTerm) {
316
        $rorUserDataService = new RorDataService();
317
        $result = $rorUserDataService->searchOrganisationRequest($searchTerm);
318
319
        return json_encode($result);
320
    }
321
322
    /**
323
     * @param string $dataId
324
     * @param int $groupId
325
     * @param int $groupIndex
326
     * @param int $fieldIndex
327
     * @param int $pageId
328
     */
329
    public function getRorDataAction($dataId, $groupId, $groupIndex, $fieldIndex, $pageId) {
330
        $rorUserDataService = new RorDataService();
331
        $rorData = $rorUserDataService->getOrganisationData($dataId);
332
333
        $result = $this->getApiMappingArray($groupId, $rorData, $groupIndex, $fieldIndex, $pageId, 'getRorMapping');
334
335
        return json_encode($result);
336
    }
337
338
    /**
339
     * @param string $searchTerm
340
     * @return false|string
341
     */
342
    public function searchZdbAction($searchTerm) {
343
        $zdbUserDataService = new ZdbDataService();
344
        $result = $zdbUserDataService->searchRequest($searchTerm);
345
346
        return json_encode($result);
347
    }
348
349
    /**
350
     * @param string $dataId
351
     * @param int $groupId
352
     * @param int $groupIndex
353
     * @param int $fieldIndex
354
     * @param int $pageId
355
     * @return false|string
356
     */
357
    public function getZdbDataAction($dataId, $groupId, $groupIndex, $fieldIndex, $pageId) {
358
        $zdbDataService = new ZdbDataService();
359
        $zdbData = $zdbDataService->getDataRequest($dataId);
360
361
        $result = $this->getApiMappingArray($groupId, $zdbData, $groupIndex, $fieldIndex, $pageId, 'getZdbMapping');
362
363
        return json_encode($result);
364
    }
365
366
    /**
367
     * @param string $searchTerm
368
     */
369
    public function searchUnpaywallAction($searchTerm) {
370
        $unpaywallUserDataService = new UnpaywallDataService();
371
        $result = $unpaywallUserDataService->searchRequest($searchTerm);
372
373
        return json_encode($result);
374
    }
375
376
    /**
377
     * @param string $dataId
378
     * @param int $groupId
379
     * @param int $groupIndex
380
     * @param int $fieldIndex
381
     * @param int $pageId
382
     * @return false|string
383
     */
384
    public function getUnpaywallDataAction($dataId, $groupId, $groupIndex, $fieldIndex, $pageId) {
385
        $unpaywallDataService = new UnpaywallDataService();
386
        $unpaywallData = $unpaywallDataService->getDataRequest($dataId);
387
388
        $result = $this->getApiMappingArray($groupId, $unpaywallData, $groupIndex, $fieldIndex, $pageId, 'getUnpaywallMapping');
389
390
        return json_encode($result);
391
    }
392
393
    /**
394
     * @param string $searchTerm
395
     * @param string $type
396
     */
397
    public function searchOrcidAction($searchTerm, $type = 'person') {
398
        $orcidUserDataService = new OrcidDataService();
399
        $methodName = 'search'.ucfirst($type).'Request';
400
        $result = $orcidUserDataService->{$methodName}($searchTerm);
401
402
        return json_encode($result);
403
    }
404
405
    /**
406
     * @param string $dataId
407
     * @param int $groupId
408
     * @param int $groupIndex
409
     * @param int $fieldIndex
410
     * @param int $pageId
411
     * @param string $type
412
     * @return false|string
413
     */
414
    public function getOrcidDataAction($dataId, $groupId, $groupIndex, $fieldIndex, $pageId, $type = 'person') {
415
        $orcidDataService = new OrcidDataService();
416
        $methodName = 'get'.ucfirst($type).'Data';
417
        $orcidData = $orcidDataService->{$methodName}($dataId);
418
419
        $result = $this->getApiMappingArray($groupId, $orcidData, $groupIndex, $fieldIndex, $pageId, 'getOrcid'.ucfirst($type).'Mapping');
420
421
        return json_encode($result);
422
    }
423
424
    /**
425
     * Preparing data from api and returning an array to identify specific field in frontend
426
     * @param $groupId
427
     * @param $data
428
     * @param $groupIndex
429
     * @param $fieldIndex
430
     * @param $pageId
431
     * @param $methodMappingName
432
     * @return mixed
433
     */
434
    public function getApiMappingArray($groupId, $data, $groupIndex, $fieldIndex, $pageId, $methodMappingName) {
435
        // get mapping
436
        /** @var MetadataGroup $group */
437
        $group = $this->metadataGroupRepository->findByUid($groupId);
438
439
        foreach ($group->getChildren() as $key => $value) {
440
            if (!empty($value->{$methodMappingName}())) {
441
                // for configuration field1->field1a
442
                $mappingPart = explode('->', $value->{$methodMappingName}());
443
                $apiData = '';
444
                $i = 0;
445
                foreach ($mappingPart as $mapping) {
446
                    if ($i == 0) {
447
                        $apiData = $data->{$mapping};
448
                    } else {
449
                        if (is_array($apiData)) {
450
                            foreach ($apiData as $fisArrayValue) {
451
                                $apiDataArray[] = $fisArrayValue->{$mapping};
452
                            }
453
                        } else {
454
                            $apiData = $apiData->{$mapping};
455
                        }
456
                    }
457
                    $i++;
458
                }
459
460
                if (!empty($apiData) || !empty($apiDataArray)) {
461
                    if (!empty($apiDataArray)) {
462
                        foreach ($apiDataArray as $key => $apiDataValue) {
0 ignored issues
show
Comprehensibility Bug introduced by
$key is overwriting a variable from outer foreach loop.
Loading history...
463
                            $result[$pageId . '-' . $groupId . '-' . $groupIndex . '-' . $value->getUid() . '-' . $key] = $apiDataValue;
464
                        }
465
                        $apiDataArray = [];
466
                    } else {
467
                        $result[$pageId . '-' . $groupId . '-' . $groupIndex . '-' . $value->getUid() . '-' . $fieldIndex] = $apiData;
468
                    }
469
                }
470
            }
471
        }
472
473
        return $result;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $result does not seem to be defined for all execution paths leading up to this point.
Loading history...
474
    }
475
476
477
    /**
478
     * Selects or unselects an external metadata record to be imported later as a document.
479
     *
480
     * @param string $identifier
481
     * @return bool
482
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
483
     */
484
    public function toggleBulkImportRecordAction($identifier)
485
    {
486
        $externalMetaData = $this->externalMetadataRepository->findOneByPublicationIdentifier($identifier);
0 ignored issues
show
The method findOneByPublicationIdentifier() does not exist on EWW\Dpf\Domain\Repositor...ernalMetadataRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

486
        /** @scrutinizer ignore-call */ 
487
        $externalMetaData = $this->externalMetadataRepository->findOneByPublicationIdentifier($identifier);
Loading history...
487
488
        if ($externalMetaData) {
489
            $this->externalMetadataRepository->remove($externalMetaData);
490
        } else {
491
            /** @var BulkImportSessionData $bulkImportSessionData */
492
            $bulkImportSessionData = $this->session->getBulkImportData();
493
            $currentResults = $bulkImportSessionData->getCurrentMetadataItems();
494
            if ($currentResults && is_array($currentResults)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $currentResults of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
495
                $this->externalMetadataRepository->add($currentResults[$identifier]);
496
            }
497
        }
498
499
        return true;
500
    }
501
502
    /**
503
     * Activates/deactivates the author search for the bulk import.
504
     *
505
     * @param string $apiName
506
     * @return bool
507
     */
508
    public function toggleBulkImportAuthorSearchAction($apiName)
509
    {
510
        /** @var BulkImportSessionData $bulkImportData */
511
        $bulkImportSessionData = $this->session->getBulkImportData();
512
513
        switch ($apiName) {
514
            case 'CrossRef':
515
                $searchField = $bulkImportSessionData->getCrossRefSearchField();
516
                if ($searchField === 'author') {
517
                    $searchField = '';
518
                } else {
519
                    $searchField = 'author';
520
                }
521
                $bulkImportSessionData->setCrossRefSearchField($searchField);
522
                break;
523
            case 'PubMed':
524
                $searchField = $bulkImportSessionData->getPubMedSearchField();
525
                if ($searchField === 'author') {
526
                    $searchField = '';
527
                } else {
528
                    $searchField = 'author';
529
                }
530
                $bulkImportSessionData->setPubMedSearchField($searchField);
531
                break;
532
            default:
533
                return false;
534
        }
535
536
        $this->session->setBulkImportData($bulkImportSessionData);
537
538
        return true;
539
    }
540
541
    public function initializeAction()
542
    {
543
        $this->authorizationChecker->denyAccessUnlessLoggedIn();
544
545
        parent::initializeAction();
546
    }
547
548
    /**
549
     * @param int $feUser
550
     * @return false|string
551
     */
552
    public function generateApiTokenAction($feUser) {
553
        $currentUser = $this->security->getUser();
554
        if ($currentUser->getUid() === $feUser) {
555
            $string = md5(substr(md5(time()), 0, 14)).date("Y-m-dH:i:s");
556
            $hash = hash('sha256', $string);
557
558
            $currentUser->setApiToken($hash);
559
            $this->frontendUserRepository->update($currentUser);
560
561
            return json_encode(['apiToken' => $hash]);
562
        } else {
563
            return json_encode(['failed' => 'wrong user id']);
564
        }
565
    }
566
567
    /**
568
     * @param int $feUser
569
     * @return bool
570
     */
571
    public function removeApiTokenAction($feUser) {
572
        $currentUser = $this->security->getUser();
573
        if ($currentUser->getUid() === $feUser) {
574
            $currentUser->setApiToken('');
575
            $this->frontendUserRepository->update($currentUser);
576
577
            return json_encode(['success' => '1']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return json_encode(array('success' => '1')) returns the type string which is incompatible with the documented return type boolean.
Loading history...
578
        } else {
579
            return json_encode(['failed' => 'wrong user id']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return json_encode(array...d' => 'wrong user id')) returns the type string which is incompatible with the documented return type boolean.
Loading history...
580
        }
581
582
    }
583
584
}
585