Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.98% covered (danger)
0.98%
1 / 102
5.56% covered (danger)
5.56%
1 / 18
CRAP
0.00% covered (danger)
0.00%
0 / 1
Resource
0.98% covered (danger)
0.98%
1 / 102
5.56% covered (danger)
5.56%
1 / 18
1294.25
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 deleteTags
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 addTag
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 deleteTag
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
 addComment
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 addOrUpdateRating
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
56
 assignMetadata
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
72
 getId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setRecordId
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getRecordId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setTitle
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setAuthor
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setYear
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setSource
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getSource
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setExtraMetadata
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getExtraMetadata
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Row Definition for resource
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
30namespace VuFind\Db\Row;
31
32use VuFind\Date\DateException;
33use VuFind\Db\Entity\ResourceEntityInterface;
34use VuFind\Db\Entity\UserEntityInterface;
35use VuFind\Db\Service\DbServiceAwareInterface;
36use VuFind\Db\Service\DbServiceAwareTrait;
37use VuFind\Db\Service\ResourceTagsServiceInterface;
38use VuFind\Db\Table\DbTableAwareInterface;
39use VuFind\Db\Table\DbTableAwareTrait;
40use VuFind\Exception\LoginRequired as LoginRequiredException;
41
42use function intval;
43use function strlen;
44
45/**
46 * Row Definition for resource
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 string  $record_id
56 * @property string  $title
57 * @property ?string $author
58 * @property ?int    $year
59 * @property string  $source
60 * @property ?string $extra_metadata
61 */
62class Resource extends RowGateway implements DbServiceAwareInterface, DbTableAwareInterface, ResourceEntityInterface
63{
64    use DbServiceAwareTrait;
65    use DbTableAwareTrait;
66
67    /**
68     * Constructor
69     *
70     * @param \Laminas\Db\Adapter\Adapter $adapter Database adapter
71     */
72    public function __construct($adapter)
73    {
74        parent::__construct('id', 'resource', $adapter);
75    }
76
77    /**
78     * Remove tags from the current resource.
79     *
80     * @param \VuFind\Db\Row\User $user    The user deleting the tags.
81     * @param string              $list_id The list associated with the tags
82     * (optional -- omitting this will delete ALL of the user's tags).
83     *
84     * @return void
85     *
86     * @deprecated Use ResourceTagsServiceInterface::destroyResourceTagsLinksForUser()
87     */
88    public function deleteTags($user, $list_id = null)
89    {
90        $this->getDbService(ResourceTagsServiceInterface::class)
91            ->destroyResourceTagsLinksForUser($this->getId(), $user, $list_id);
92    }
93
94    /**
95     * Add a tag to the current resource.
96     *
97     * @param string              $tagText The tag to save.
98     * @param UserEntityInterface $user    The user posting the tag.
99     * @param string              $list_id The list associated with the tag
100     * (optional).
101     *
102     * @return void
103     *
104     * @deprecated Use \VuFind\Tags\TagService::linkTagToResource()
105     */
106    public function addTag($tagText, $user, $list_id = null)
107    {
108        $tagText = trim($tagText);
109        if (!empty($tagText)) {
110            $tags = $this->getDbTable('Tags');
111            $tag = $tags->getByText($tagText);
112
113            $this->getDbService(ResourceTagsServiceInterface::class)->createLink(
114                $this,
115                $tag->id,
116                $user,
117                $list_id
118            );
119        }
120    }
121
122    /**
123     * Remove a tag from the current resource.
124     *
125     * @param string              $tagText The tag to delete.
126     * @param \VuFind\Db\Row\User $user    The user deleting the tag.
127     * @param string              $list_id The list associated with the tag
128     * (optional).
129     *
130     * @return void
131     *
132     * @deprecated Use \VuFind\Tags\TagsService::unlinkTagFromResource()
133     */
134    public function deleteTag($tagText, $user, $list_id = null)
135    {
136        $tagText = trim($tagText);
137        if (!empty($tagText)) {
138            $tags = $this->getDbTable('Tags');
139            $tagIds = [];
140            foreach ($tags->getByText($tagText, false, false) as $tag) {
141                $tagIds[] = $tag->getId();
142            }
143            if (!empty($tagIds)) {
144                $this->getDbService(ResourceTagsServiceInterface::class)->destroyResourceTagsLinksForUser(
145                    $this->getId(),
146                    $user,
147                    $list_id,
148                    $tagIds
149                );
150            }
151        }
152    }
153
154    /**
155     * Add a comment to the current resource.
156     *
157     * @param string              $comment The comment to save.
158     * @param \VuFind\Db\Row\User $user    The user posting the comment.
159     *
160     * @throws LoginRequiredException
161     * @return int                         ID of newly-created comment.
162     */
163    public function addComment($comment, $user)
164    {
165        if (!isset($user->id)) {
166            throw new LoginRequiredException(
167                "Can't add comments without logging in."
168            );
169        }
170
171        $table = $this->getDbTable('Comments');
172        $row = $table->createRow();
173        $row->setUser($user)
174            ->setResource($this)
175            ->setComment($comment)
176            ->setCreated(new \DateTime());
177        $row->save();
178        return $row->getId();
179    }
180
181    /**
182     * Add or update user's rating for the current resource.
183     *
184     * @param int  $userId User ID
185     * @param ?int $rating Rating (null to delete)
186     *
187     * @throws LoginRequiredException
188     * @throws \Exception
189     * @return int ID of rating added, deleted or updated
190     */
191    public function addOrUpdateRating(int $userId, ?int $rating): int
192    {
193        if (null !== $rating && ($rating < 0 || $rating > 100)) {
194            throw new \Exception('Rating value out of range');
195        }
196
197        $ratings = $this->getDbTable('Ratings');
198        $callback = function ($select) use ($userId) {
199            $select->where->equalTo('ratings.resource_id', $this->id);
200            $select->where->equalTo('ratings.user_id', $userId);
201        };
202        if ($existing = $ratings->select($callback)->current()) {
203            if (null === $rating) {
204                $existing->delete();
205            } else {
206                $existing->rating = $rating;
207                $existing->save();
208            }
209            return $existing->id;
210        }
211
212        if (null === $rating) {
213            return 0;
214        }
215
216        $row = $ratings->createRow();
217        $row->user_id = $userId;
218        $row->resource_id = $this->id;
219        $row->rating = $rating;
220        $row->created = date('Y-m-d H:i:s');
221        $row->save();
222        return $row->id;
223    }
224
225    /**
226     * Use a record driver to assign metadata to the current row. Return the
227     * current object to allow fluent interface.
228     *
229     * @param \VuFind\RecordDriver\AbstractBase $driver    The record driver
230     * @param \VuFind\Date\Converter            $converter Date converter
231     *
232     * @return \VuFind\Db\Row\Resource
233     *
234     * @deprecated Use \VuFind\Record\ResourcePopulator::assignMetadata()
235     */
236    public function assignMetadata($driver, \VuFind\Date\Converter $converter)
237    {
238        // Grab title -- we have to have something in this field!
239        $this->title = mb_substr(
240            $driver->tryMethod('getSortTitle'),
241            0,
242            255,
243            'UTF-8'
244        );
245        if (empty($this->title)) {
246            $this->title = $driver->getBreadcrumb();
247        }
248
249        // Try to find an author; if not available, just leave the default null:
250        $author = mb_substr(
251            $driver->tryMethod('getPrimaryAuthor'),
252            0,
253            255,
254            'UTF-8'
255        );
256        if (!empty($author)) {
257            $this->author = $author;
258        }
259
260        // Try to find a year; if not available, just leave the default null:
261        $dates = $driver->tryMethod('getPublicationDates');
262        if (isset($dates[0]) && strlen($dates[0]) > 4) {
263            try {
264                $year = $converter->convertFromDisplayDate('Y', $dates[0]);
265            } catch (DateException $e) {
266                // If conversion fails, don't store a date:
267                $year = '';
268            }
269        } else {
270            $year = $dates[0] ?? '';
271        }
272        if (!empty($year)) {
273            $this->year = intval($year);
274        }
275
276        if ($extra = $driver->tryMethod('getExtraResourceMetadata')) {
277            $this->extra_metadata = json_encode($extra);
278        }
279        return $this;
280    }
281
282    /**
283     * Id getter
284     *
285     * @return int
286     */
287    public function getId(): int
288    {
289        return $this->id;
290    }
291
292    /**
293     * Record Id setter
294     *
295     * @param string $recordId recordId
296     *
297     * @return ResourceEntityInterface
298     */
299    public function setRecordId(string $recordId): ResourceEntityInterface
300    {
301        $this->record_id = $recordId;
302        return $this;
303    }
304
305    /**
306     * Record Id getter
307     *
308     * @return string
309     */
310    public function getRecordId(): string
311    {
312        return $this->record_id;
313    }
314
315    /**
316     * Title setter
317     *
318     * @param string $title Title of the record.
319     *
320     * @return ResourceEntityInterface
321     */
322    public function setTitle(string $title): ResourceEntityInterface
323    {
324        $this->title = $title;
325        return $this;
326    }
327
328    /**
329     * Title getter
330     *
331     * @return string
332     */
333    public function getTitle(): string
334    {
335        return $this->title;
336    }
337
338    /**
339     * Author setter
340     *
341     * @param ?string $author Author of the title.
342     *
343     * @return ResourceEntityInterface
344     */
345    public function setAuthor(?string $author): ResourceEntityInterface
346    {
347        $this->author = $author;
348        return $this;
349    }
350
351    /**
352     * Year setter
353     *
354     * @param ?int $year Year title is published.
355     *
356     * @return ResourceEntityInterface
357     */
358    public function setYear(?int $year): ResourceEntityInterface
359    {
360        $this->year = $year;
361        return $this;
362    }
363
364    /**
365     * Source setter
366     *
367     * @param string $source Source (a search backend ID).
368     *
369     * @return ResourceEntityInterface
370     */
371    public function setSource(string $source): ResourceEntityInterface
372    {
373        $this->source = $source;
374        return $this;
375    }
376
377    /**
378     * Source getter
379     *
380     * @return string
381     */
382    public function getSource(): string
383    {
384        return $this->source;
385    }
386
387    /**
388     * Extra Metadata setter
389     *
390     * @param ?string $extraMetadata ExtraMetadata.
391     *
392     * @return ResourceEntityInterface
393     */
394    public function setExtraMetadata(?string $extraMetadata): ResourceEntityInterface
395    {
396        $this->extra_metadata = $extraMetadata;
397        return $this;
398    }
399
400    /**
401     * Extra Metadata getter
402     *
403     * @return ?string
404     */
405    public function getExtraMetadata(): ?string
406    {
407        return $this->extra_metadata;
408    }
409}