Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
16.67% covered (danger)
16.67%
2 / 12
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserList
16.67% covered (danger)
16.67%
2 / 12
25.00% covered (danger)
25.00%
1 / 4
26.83
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getNew
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getExisting
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getListsContainingResource
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * Table 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_Table
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 Page
28 */
29
30namespace VuFind\Db\Table;
31
32use Laminas\Db\Adapter\Adapter;
33use Laminas\Db\Sql\Expression;
34use Laminas\Db\Sql\Select;
35use Laminas\Session\Container;
36use VuFind\Db\Row\RowGateway;
37use VuFind\Db\Service\DbServiceAwareInterface;
38use VuFind\Db\Service\DbServiceAwareTrait;
39use VuFind\Db\Service\UserListServiceInterface;
40use VuFind\Exception\LoginRequired as LoginRequiredException;
41use VuFind\Exception\RecordMissing as RecordMissingException;
42
43/**
44 * Table Definition for user_list
45 *
46 * @category VuFind
47 * @package  Db_Table
48 * @author   Demian Katz <demian.katz@villanova.edu>
49 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
50 * @link     https://vufind.org Main Page
51 */
52class UserList extends Gateway implements DbServiceAwareInterface
53{
54    use DbServiceAwareTrait;
55
56    /**
57     * Session container for last list information.
58     *
59     * @var Container
60     */
61    protected $session;
62
63    /**
64     * Constructor
65     *
66     * @param Adapter       $adapter Database adapter
67     * @param PluginManager $tm      Table manager
68     * @param array         $cfg     Laminas configuration
69     * @param RowGateway    $rowObj  Row prototype object (null for default)
70     * @param Container     $session Session container (must use same
71     * namespace as container provided to \VuFind\View\Helper\Root\UserList).
72     * @param string        $table   Name of database table to interface with
73     */
74    public function __construct(
75        Adapter $adapter,
76        PluginManager $tm,
77        $cfg,
78        ?RowGateway $rowObj = null,
79        Container $session = null,
80        $table = 'user_list'
81    ) {
82        $this->session = $session;
83        parent::__construct($adapter, $tm, $cfg, $rowObj, $table);
84    }
85
86    /**
87     * Create a new list object.
88     *
89     * @param \VuFind\Db\Row\UserList|bool $user User object representing owner of
90     * new list (or false if not logged in)
91     *
92     * @return \VuFind\Db\Row\UserList
93     * @throws LoginRequiredException
94     *
95     * @deprecated Use \VuFind\Favorites\FavoritesService::createListForUser()
96     */
97    public function getNew($user)
98    {
99        if (!$user) {
100            throw new LoginRequiredException('Log in to create lists.');
101        }
102
103        $row = $this->createRow();
104        $row->created = date('Y-m-d H:i:s');    // force creation date
105        $row->user_id = $user->id;
106        return $row;
107    }
108
109    /**
110     * Retrieve a list object.
111     *
112     * @param int $id Numeric ID for existing list.
113     *
114     * @return \VuFind\Db\Row\UserList
115     * @throws RecordMissingException
116     *
117     * @deprecated Use \VuFind\Db\Service\UserListServiceInterface::getUserListById()
118     */
119    public function getExisting($id)
120    {
121        return $this->getDbService(UserListServiceInterface::class)->getUserListById($id);
122    }
123
124    /**
125     * Get lists containing a specific user_resource
126     *
127     * @param string $resourceId ID of record being checked.
128     * @param string $source     Source of record to look up
129     * @param int    $userId     Optional user ID (to limit results to a particular
130     * user).
131     *
132     * @return array
133     */
134    public function getListsContainingResource(
135        $resourceId,
136        $source = DEFAULT_SEARCH_BACKEND,
137        $userId = null
138    ) {
139        // Set up base query:
140        $callback = function ($select) use ($resourceId, $source, $userId) {
141            $select->columns(
142                [
143                    new Expression(
144                        'DISTINCT(?)',
145                        ['user_list.id'],
146                        [Expression::TYPE_IDENTIFIER]
147                    ), Select::SQL_STAR,
148                ]
149            );
150            $select->join(
151                ['ur' => 'user_resource'],
152                'ur.list_id = user_list.id',
153                []
154            );
155            $select->join(
156                ['r' => 'resource'],
157                'r.id = ur.resource_id',
158                []
159            );
160            $select->where->equalTo('r.source', $source)
161                ->equalTo('r.record_id', $resourceId);
162            $select->order(['title']);
163
164            if (null !== $userId) {
165                $select->where->equalTo('ur.user_id', $userId);
166            }
167        };
168        return $this->select($callback);
169    }
170}