Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
5.41% |
2 / 37 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
Comments | |
5.41% |
2 / 37 |
|
40.00% |
2 / 5 |
113.42 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getForResource | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
deleteIfOwnedByUser | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
42 | |||
deleteByUser | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getStatistics | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Table Definition for comments |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2012. |
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 Site |
28 | */ |
29 | |
30 | namespace VuFind\Db\Table; |
31 | |
32 | use Laminas\Db\Adapter\Adapter; |
33 | use Laminas\Db\Sql\Expression; |
34 | use Laminas\Db\Sql\Select; |
35 | use VuFind\Db\Entity\UserEntityInterface; |
36 | use VuFind\Db\Row\RowGateway; |
37 | use VuFind\Db\Service\DbServiceAwareInterface; |
38 | use VuFind\Db\Service\DbServiceAwareTrait; |
39 | use VuFind\Db\Service\ResourceServiceInterface; |
40 | |
41 | use function count; |
42 | use function is_object; |
43 | |
44 | /** |
45 | * Table Definition for comments |
46 | * |
47 | * @category VuFind |
48 | * @package Db_Table |
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 Main Site |
52 | */ |
53 | class Comments extends Gateway implements DbServiceAwareInterface |
54 | { |
55 | use DbServiceAwareTrait; |
56 | |
57 | /** |
58 | * Constructor |
59 | * |
60 | * @param Adapter $adapter Database adapter |
61 | * @param PluginManager $tm Table manager |
62 | * @param array $cfg Laminas configuration |
63 | * @param RowGateway $rowObj Row prototype object (null for default) |
64 | * @param string $table Name of database table to interface with |
65 | */ |
66 | public function __construct( |
67 | Adapter $adapter, |
68 | PluginManager $tm, |
69 | $cfg, |
70 | ?RowGateway $rowObj = null, |
71 | $table = 'comments' |
72 | ) { |
73 | parent::__construct($adapter, $tm, $cfg, $rowObj, $table); |
74 | } |
75 | |
76 | /** |
77 | * Get tags associated with the specified resource. |
78 | * |
79 | * @param string $id Record ID to look up |
80 | * @param string $source Source of record to look up |
81 | * |
82 | * @return array|\Laminas\Db\ResultSet\AbstractResultSet |
83 | */ |
84 | public function getForResource($id, $source = DEFAULT_SEARCH_BACKEND) |
85 | { |
86 | $resourceService = $this->getDbService(ResourceServiceInterface::class); |
87 | $resource = $resourceService->getResourceByRecordId($id, $source); |
88 | if (!$resource) { |
89 | return []; |
90 | } |
91 | |
92 | $callback = function ($select) use ($resource) { |
93 | $select->columns([Select::SQL_STAR]); |
94 | $select->join( |
95 | ['u' => 'user'], |
96 | 'u.id = comments.user_id', |
97 | ['firstname', 'lastname'], |
98 | $select::JOIN_LEFT |
99 | ); |
100 | $select->where->equalTo('comments.resource_id', $resource->id); |
101 | $select->order('comments.created'); |
102 | }; |
103 | |
104 | return $this->select($callback); |
105 | } |
106 | |
107 | /** |
108 | * Delete a comment if the owner is logged in. Returns true on success. |
109 | * |
110 | * @param int $id ID of row to delete |
111 | * @param UserEntityInterface $user Logged in user object |
112 | * |
113 | * @return bool |
114 | */ |
115 | public function deleteIfOwnedByUser($id, $user) |
116 | { |
117 | // User must be object with ID: |
118 | if (!is_object($user) || !($userId = $user->getId())) { |
119 | return false; |
120 | } |
121 | |
122 | // Comment row must exist: |
123 | $matches = $this->select(['id' => $id]); |
124 | if (count($matches) == 0 || !($row = $matches->current())) { |
125 | return false; |
126 | } |
127 | |
128 | // Row must be owned by user: |
129 | if ($row->user_id != $userId) { |
130 | return false; |
131 | } |
132 | |
133 | // If we got this far, everything is okay: |
134 | $row->delete(); |
135 | return true; |
136 | } |
137 | |
138 | /** |
139 | * Deletes all comments by a user. |
140 | * |
141 | * @param \VuFind\Db\Row\User $user User object |
142 | * |
143 | * @return void |
144 | */ |
145 | public function deleteByUser($user) |
146 | { |
147 | $this->delete(['user_id' => $user->id]); |
148 | } |
149 | |
150 | /** |
151 | * Get statistics on use of comments. |
152 | * |
153 | * @return array |
154 | */ |
155 | public function getStatistics() |
156 | { |
157 | $select = $this->sql->select(); |
158 | $select->columns( |
159 | [ |
160 | 'users' => new Expression( |
161 | 'COUNT(DISTINCT(?))', |
162 | ['user_id'], |
163 | [Expression::TYPE_IDENTIFIER] |
164 | ), |
165 | 'resources' => new Expression( |
166 | 'COUNT(DISTINCT(?))', |
167 | ['resource_id'], |
168 | [Expression::TYPE_IDENTIFIER] |
169 | ), |
170 | 'total' => new Expression('COUNT(*)'), |
171 | ] |
172 | ); |
173 | $statement = $this->sql->prepareStatementForSqlObject($select); |
174 | $result = $statement->execute(); |
175 | return (array)$result->current(); |
176 | } |
177 | } |