Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
GetSaveStatuses | |
0.00% |
0 / 31 |
|
0.00% |
0 / 4 |
110 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
formatListData | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
getDataFromUser | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
handleRequest | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | /** |
4 | * "Get Save Statuses" AJAX handler |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2018. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package AJAX |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org/wiki/development Wiki |
28 | */ |
29 | |
30 | namespace VuFind\AjaxHandler; |
31 | |
32 | use Laminas\Mvc\Controller\Plugin\Params; |
33 | use Laminas\Mvc\Controller\Plugin\Url; |
34 | use VuFind\Db\Entity\UserEntityInterface; |
35 | use VuFind\Db\Entity\UserResourceEntityInterface; |
36 | use VuFind\Db\Service\UserResourceServiceInterface; |
37 | use VuFind\I18n\Translator\TranslatorAwareInterface; |
38 | use VuFind\Session\Settings as SessionSettings; |
39 | |
40 | use function is_array; |
41 | |
42 | /** |
43 | * "Get Save Statuses" AJAX handler |
44 | * |
45 | * Check one or more records to see if they are saved in one of the user's list. |
46 | * |
47 | * @category VuFind |
48 | * @package AJAX |
49 | * @author Demian Katz <demian.katz@villanova.edu> |
50 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
51 | * @link https://vufind.org/wiki/development Wiki |
52 | */ |
53 | class GetSaveStatuses extends AbstractBase implements TranslatorAwareInterface |
54 | { |
55 | use \VuFind\I18n\Translator\TranslatorAwareTrait; |
56 | |
57 | /** |
58 | * Constructor |
59 | * |
60 | * @param SessionSettings $ss Session settings |
61 | * @param ?UserEntityInterface $user Logged in user (or null) |
62 | * @param Url $urlHelper URL helper |
63 | * @param UserResourceServiceInterface $userResourceService User resource database service |
64 | */ |
65 | public function __construct( |
66 | SessionSettings $ss, |
67 | protected ?UserEntityInterface $user, |
68 | protected Url $urlHelper, |
69 | protected UserResourceServiceInterface $userResourceService |
70 | ) { |
71 | $this->sessionSettings = $ss; |
72 | } |
73 | |
74 | /** |
75 | * Format UserResourceEntityInterface object into array. |
76 | * |
77 | * @param UserResourceEntityInterface $data UserResourceEntityInterface object |
78 | * |
79 | * @return array |
80 | */ |
81 | protected function formatListData(UserResourceEntityInterface $data): array |
82 | { |
83 | $list = $data->getUserList(); |
84 | return !$list ? [] : [ |
85 | 'list_url' => |
86 | $this->urlHelper->fromRoute('userList', ['id' => $list->getId()]), |
87 | 'list_title' => $list->getTitle(), |
88 | ]; |
89 | } |
90 | |
91 | /** |
92 | * Obtain status data from the current logged-in user. |
93 | * |
94 | * @param array $ids IDs to retrieve |
95 | * @param array $sources Source data for IDs (parallel-indexed) |
96 | * |
97 | * @return array |
98 | */ |
99 | protected function getDataFromUser($ids, $sources) |
100 | { |
101 | $result = $checked = []; |
102 | foreach ($ids as $i => $id) { |
103 | $source = $sources[$i] ?? DEFAULT_SEARCH_BACKEND; |
104 | $selector = $source . '|' . $id; |
105 | |
106 | // We don't want to bother checking the same ID more than once, so |
107 | // use the $checked flag array to avoid duplicates: |
108 | if (!isset($checked[$selector])) { |
109 | $checked[$selector] = true; |
110 | |
111 | $data = $this->userResourceService->getFavoritesForRecord($id, $source, null, $this->user); |
112 | $result[$selector] = array_filter(array_map([$this, 'formatListData'], $data)); |
113 | } |
114 | } |
115 | return $result; |
116 | } |
117 | |
118 | /** |
119 | * Handle a request. |
120 | * |
121 | * @param Params $params Parameter helper from controller |
122 | * |
123 | * @return array [response data, HTTP status code] |
124 | */ |
125 | public function handleRequest(Params $params) |
126 | { |
127 | $this->disableSessionWrites(); // avoid session write timing bug |
128 | // check if user is logged in |
129 | if (!$this->user) { |
130 | return $this->formatResponse( |
131 | $this->translate('You must be logged in first'), |
132 | self::STATUS_HTTP_NEED_AUTH |
133 | ); |
134 | } |
135 | |
136 | // loop through each ID check if it is saved to any of the user's lists |
137 | $ids = $params->fromPost('id', $params->fromQuery('id', [])); |
138 | $sources = $params->fromPost('source', $params->fromQuery('source', [])); |
139 | if (!is_array($ids) || !is_array($sources)) { |
140 | return $this->formatResponse( |
141 | $this->translate('Argument must be array.'), |
142 | self::STATUS_HTTP_BAD_REQUEST |
143 | ); |
144 | } |
145 | $statuses = $this->getDataFromUser($ids, $sources); |
146 | return $this->formatResponse(compact('statuses')); |
147 | } |
148 | } |