Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
8.33% |
5 / 60 |
|
15.00% |
3 / 20 |
CRAP | |
0.00% |
0 / 1 |
UserList | |
8.33% |
5 / 60 |
|
15.00% |
3 / 20 |
924.41 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
editAllowed | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
getResourceTags | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
getListTags | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addListTag | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
setSession | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
rememberLastUsed | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
removeResourcesById | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
isPublic | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
delete | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
getId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setTitle | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getTitle | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setDescription | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getDescription | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setCreated | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getCreated | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setPublic | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
setUser | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getUser | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Row Definition for user_list |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2010. |
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 Db_Row |
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 Main Site |
28 | */ |
29 | |
30 | namespace VuFind\Db\Row; |
31 | |
32 | use DateTime; |
33 | use Laminas\Session\Container; |
34 | use VuFind\Db\Entity\UserEntityInterface; |
35 | use VuFind\Db\Entity\UserListEntityInterface; |
36 | use VuFind\Db\Service\DbServiceAwareInterface; |
37 | use VuFind\Db\Service\DbServiceAwareTrait; |
38 | use VuFind\Db\Service\ResourceServiceInterface; |
39 | use VuFind\Db\Service\ResourceTagsServiceInterface; |
40 | use VuFind\Db\Service\TagServiceInterface; |
41 | use VuFind\Db\Service\UserServiceInterface; |
42 | use VuFind\Exception\ListPermission as ListPermissionException; |
43 | use VuFind\Tags\TagsService; |
44 | |
45 | /** |
46 | * Row Definition for user_list |
47 | * |
48 | * @category VuFind |
49 | * @package Db_Row |
50 | * @author Demian Katz <demian.katz@villanova.edu> |
51 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
52 | * @link https://vufind.org Main Site |
53 | * |
54 | * @property int $id |
55 | * @property int $user_id |
56 | * @property string $title |
57 | * @property string $description |
58 | * @property string $created |
59 | * @property bool $public |
60 | */ |
61 | class UserList extends RowGateway implements |
62 | \VuFind\Db\Table\DbTableAwareInterface, |
63 | UserListEntityInterface, |
64 | DbServiceAwareInterface |
65 | { |
66 | use \VuFind\Db\Table\DbTableAwareTrait; |
67 | use DbServiceAwareTrait; |
68 | |
69 | /** |
70 | * Constructor |
71 | * |
72 | * @param \Laminas\Db\Adapter\Adapter $adapter Database adapter |
73 | * @param TagsService $tagsService Tags service |
74 | * @param ?Container $session Session container for last list information |
75 | */ |
76 | public function __construct($adapter, protected TagsService $tagsService, protected ?Container $session = null) |
77 | { |
78 | parent::__construct('id', 'user_list', $adapter); |
79 | } |
80 | |
81 | /** |
82 | * Is the current user allowed to edit this list? |
83 | * |
84 | * @param ?UserEntityInterface $user Logged-in user (null if none) |
85 | * |
86 | * @return bool |
87 | * |
88 | * @deprecated Use \VuFind\Favorites\FavoritesService::userCanEditList() |
89 | */ |
90 | public function editAllowed($user) |
91 | { |
92 | if ($user && $user->id == $this->user_id) { |
93 | return true; |
94 | } |
95 | return false; |
96 | } |
97 | |
98 | /** |
99 | * Get an array of resource tags associated with this list. |
100 | * |
101 | * @return array |
102 | */ |
103 | public function getResourceTags() |
104 | { |
105 | $table = $this->getDbTable('User'); |
106 | $user = $table->select(['id' => $this->user_id])->current(); |
107 | if (empty($user)) { |
108 | return []; |
109 | } |
110 | return $user->getTags(null, $this->id); |
111 | } |
112 | |
113 | /** |
114 | * Get an array of tags assigned to this list. |
115 | * |
116 | * @return array |
117 | * |
118 | * @deprecated Use TagServiceInterface::getListTags() |
119 | */ |
120 | public function getListTags() |
121 | { |
122 | return $this->getDbTable('Tags')->getForList($this->getId(), $this->getUser()->getId()); |
123 | } |
124 | |
125 | /** |
126 | * Add a tag to the list. |
127 | * |
128 | * @param string $tagText The tag to save. |
129 | * @param UserEntityInterface $user The user posting the tag. |
130 | * |
131 | * @return void |
132 | * |
133 | * @deprecated Use \VuFind\Favorites\FavoritesService::addListTag() |
134 | */ |
135 | public function addListTag($tagText, $user) |
136 | { |
137 | $tagText = trim($tagText); |
138 | if (!empty($tagText)) { |
139 | $tags = $this->getDbTable('tags'); |
140 | $tag = $tags->getByText($tagText); |
141 | $this->getDbService(ResourceTagsServiceInterface::class)->createLink( |
142 | null, |
143 | $tag->id, |
144 | $user, |
145 | $this |
146 | ); |
147 | } |
148 | } |
149 | |
150 | /** |
151 | * Set session container. |
152 | * |
153 | * @param Container $session Session container |
154 | * |
155 | * @return void |
156 | */ |
157 | public function setSession(Container $session) |
158 | { |
159 | $this->session = $session; |
160 | } |
161 | |
162 | /** |
163 | * Remember that this list was used so that it can become the default in |
164 | * dialog boxes. |
165 | * |
166 | * @return void |
167 | * |
168 | * @deprecated Use \VuFind\Favorites\FavoritesService::rememberLastUsedList() |
169 | */ |
170 | public function rememberLastUsed() |
171 | { |
172 | if (null !== $this->session) { |
173 | $this->session->lastUsed = $this->id; |
174 | } |
175 | } |
176 | |
177 | /** |
178 | * Given an array of item ids, remove them from all lists. |
179 | * |
180 | * @param UserEntityInterface|bool $user Logged-in user (false if none) |
181 | * @param array $ids IDs to remove from the list |
182 | * @param string $source Type of resource identified by IDs |
183 | * |
184 | * @return void |
185 | * |
186 | * @deprecated Use \VuFind\Favorites\FavoritesService::removeListResourcesById() |
187 | */ |
188 | public function removeResourcesById( |
189 | $user, |
190 | $ids, |
191 | $source = DEFAULT_SEARCH_BACKEND |
192 | ) { |
193 | if (!$this->editAllowed($user ?: null)) { |
194 | throw new ListPermissionException('list_access_denied'); |
195 | } |
196 | |
197 | // Retrieve a list of resource IDs: |
198 | $resources = $this->getDbService(ResourceServiceInterface::class)->getResourcesByRecordIds($ids, $source); |
199 | |
200 | $resourceIDs = []; |
201 | foreach ($resources as $current) { |
202 | $resourceIDs[] = $current->getId(); |
203 | } |
204 | |
205 | // Remove Resource (related tags are also removed implicitly) |
206 | $userResourceTable = $this->getDbTable('UserResource'); |
207 | $userResourceTable->destroyLinks( |
208 | $resourceIDs, |
209 | $this->user_id, |
210 | $this->id |
211 | ); |
212 | } |
213 | |
214 | /** |
215 | * Is this a public list? |
216 | * |
217 | * @return bool |
218 | */ |
219 | public function isPublic(): bool |
220 | { |
221 | return isset($this->public) && ($this->public == 1); |
222 | } |
223 | |
224 | /** |
225 | * Destroy the list. |
226 | * |
227 | * @param \VuFind\Db\Row\User|bool $user Logged-in user (false if none) |
228 | * @param bool $force Should we force the delete without checking permissions? |
229 | * |
230 | * @return int The number of rows deleted. |
231 | * |
232 | * @deprecated Use \VuFind\Favorites\FavoritesService::destroyList() |
233 | */ |
234 | public function delete($user = false, $force = false) |
235 | { |
236 | if (!$force && !$this->editAllowed($user ?: null)) { |
237 | throw new ListPermissionException('list_access_denied'); |
238 | } |
239 | |
240 | // Remove user_resource and resource_tags rows: |
241 | $userResource = $this->getDbTable('UserResource'); |
242 | $userResource->destroyLinks(null, $this->user_id, $this->id); |
243 | |
244 | // Remove resource_tags rows for list tags: |
245 | $linker = $this->getDbTable('resourcetags'); |
246 | $linker->destroyListLinks($this->id, $user->id); |
247 | |
248 | // Remove the list itself: |
249 | return parent::delete(); |
250 | } |
251 | |
252 | /** |
253 | * Get identifier (returns null for an uninitialized or non-persisted object). |
254 | * |
255 | * @return ?int |
256 | */ |
257 | public function getId(): ?int |
258 | { |
259 | return $this->id ?? null; |
260 | } |
261 | |
262 | /** |
263 | * Set title. |
264 | * |
265 | * @param string $title Title |
266 | * |
267 | * @return UserListEntityInterface |
268 | */ |
269 | public function setTitle(string $title): UserListEntityInterface |
270 | { |
271 | $this->title = $title; |
272 | return $this; |
273 | } |
274 | |
275 | /** |
276 | * Get title. |
277 | * |
278 | * @return string |
279 | */ |
280 | public function getTitle(): string |
281 | { |
282 | return $this->title ?? ''; |
283 | } |
284 | |
285 | /** |
286 | * Set description. |
287 | * |
288 | * @param ?string $description Description |
289 | * |
290 | * @return UserListEntityInterface |
291 | */ |
292 | public function setDescription(?string $description): UserListEntityInterface |
293 | { |
294 | $this->description = $description; |
295 | return $this; |
296 | } |
297 | |
298 | /** |
299 | * Get description. |
300 | * |
301 | * @return ?string |
302 | */ |
303 | public function getDescription(): ?string |
304 | { |
305 | return $this->description ?? null; |
306 | } |
307 | |
308 | /** |
309 | * Set created date. |
310 | * |
311 | * @param DateTime $dateTime Created date |
312 | * |
313 | * @return UserListEntityInterface |
314 | */ |
315 | public function setCreated(DateTime $dateTime): UserListEntityInterface |
316 | { |
317 | $this->created = $dateTime->format('Y-m-d H:i:s'); |
318 | return $this; |
319 | } |
320 | |
321 | /** |
322 | * Get created date. |
323 | * |
324 | * @return DateTime |
325 | */ |
326 | public function getCreated(): DateTime |
327 | { |
328 | return DateTime::createFromFormat('Y-m-d H:i:s', $this->created); |
329 | } |
330 | |
331 | /** |
332 | * Set whether the list is public. |
333 | * |
334 | * @param bool $public Is the list public? |
335 | * |
336 | * @return UserListEntityInterface |
337 | */ |
338 | public function setPublic(bool $public): UserListEntityInterface |
339 | { |
340 | $this->public = $public ? '1' : '0'; |
341 | return $this; |
342 | } |
343 | |
344 | /** |
345 | * Set user. |
346 | * |
347 | * @param ?UserEntityInterface $user User owning the list. |
348 | * |
349 | * @return UserListEntityInterface |
350 | */ |
351 | public function setUser(?UserEntityInterface $user): UserListEntityInterface |
352 | { |
353 | $this->user_id = $user?->getId(); |
354 | return $this; |
355 | } |
356 | |
357 | /** |
358 | * Get user. |
359 | * |
360 | * @return ?UserEntityInterface |
361 | */ |
362 | public function getUser(): ?UserEntityInterface |
363 | { |
364 | return $this->user_id |
365 | ? $this->getDbServiceManager()->get(UserServiceInterface::class)->getUserById($this->user_id) |
366 | : null; |
367 | } |
368 | } |