3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2019-2023 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
27 * @category Repository
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2019-2023 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
37 namespace Galette\Repository
;
40 use Laminas\Db\Sql\Select
;
43 use Laminas\Db\Sql\Expression
;
45 use Galette\Core\Login
;
46 use Galette\Core\History
;
47 use Galette\Entity\SavedSearch
;
48 use Galette\Filters\SavedSearchesList
;
49 use Galette\Entity\Adherent
;
54 * @category Repository
57 * @author Johan Cwiklinski <johan@x-tnd.be>
58 * @copyright 2019-2023 The Galette Team
59 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
60 * @link http://galette.tuxfamily.org
65 public const TABLE
= SavedSearch
::TABLE
;
66 public const PK
= SavedSearch
::PK
;
68 private $filters = false;
71 private $count = null;
76 * @param Db $zdb Database
77 * @param Login $login Login
78 * @param SavedSearchesList $filters Filtering
80 public function __construct(Db
$zdb, Login
$login, $filters = null)
83 $this->login
= $login;
85 if ($filters === null) {
86 $this->filters
= new SavedSearchesList();
88 $this->filters
= $filters;
93 * Get saved searches list
95 * @param bool $as_search return the results as an array of
97 * @param array $fields field(s) name(s) to get. Should be a string or
98 * an array. If null, all fields will be
100 * @param boolean $count true if we want to count
102 * @return SavedSearch[]|ArrayObject
104 public function getList($as_search = false, $fields = null, $count = true)
107 $select = $this->buildSelect($fields, $count);
108 $this->filters
->setLimits($select);
111 $results = $this->zdb
->execute($select);
113 foreach ($results as $row) {
114 $searches[] = new SavedSearch($this->zdb
, $this->login
, $row);
117 $searches = $results;
120 } catch (Throwable
$e) {
122 'Cannot list saved searches | ' . $e->getMessage(),
130 * Builds the SELECT statement
132 * @param ?array $fields fields list to retrieve
133 * @param bool $count true if we want to count members
134 * (not applicable from static calls), defaults to false
136 * @return Select SELECT statement
138 private function buildSelect(?
array $fields, bool $count = false): Select
142 if ($fields !== null && count($fields)) {
143 $fieldsList = $fields;
146 $select = $this->zdb
->select(self
::TABLE
, 's');
147 $select->columns($fieldsList);
148 if (0 === $this->login
->id
) {
149 $select->where
->isNull(Adherent
::PK
);
151 $select->where([Adherent
::PK
=> $this->login
->id
]);
154 $select->order(self
::buildOrderClause());
157 $this->proceedCount($select);
161 } catch (Throwable
$e) {
163 'Cannot build SELECT clause for saved searches | ' . $e->getMessage(),
171 * Count searches from the query
173 * @param Select $select Original select
177 private function proceedCount(Select
$select)
180 $countSelect = clone $select;
181 $countSelect->reset($countSelect::COLUMNS
);
182 $countSelect->reset($countSelect::JOINS
);
183 $countSelect->reset($countSelect::ORDER
);
184 $countSelect->columns(
186 self
::PK
=> new Expression('COUNT(' . self
::PK
. ')')
190 $results = $this->zdb
->execute($countSelect);
191 $result = $results->current();
194 $this->count
= $result->$k;
195 $this->filters
->setCounter($this->count
);
196 } catch (Throwable
$e) {
198 'Cannot count saved searches | ' . $e->getMessage(),
206 * Builds the order clause
210 private function buildOrderClause()
213 $order[] = $this->filters
->orderby
. ' ' . $this->filters
->ordered
;
219 * Get count for current query
223 public function getCount()
229 * Remove specified searches
231 * @param integer|array $ids Searches identifiers to delete
232 * @param History $hist History
233 * @param boolean $transaction True to begin a database transaction
237 public function remove($ids, History
$hist, $transaction = true)
240 if (is_numeric($ids)) {
241 //we've got only one identifier
250 $this->zdb
->connection
->beginTransaction();
252 $select = $this->zdb
->select(self
::TABLE
);
253 $select->where
->in(self
::PK
, $list);
254 $searches = $this->zdb
->execute($select);
255 foreach ($searches as $search) {
256 $s = new SavedSearch($this->zdb
, $this->login
, $search);
258 if ($res === false) {
259 throw new \
Exception('Cannot remove saved search');
263 $this->zdb
->connection
->commit();
268 print_r($list, true),
269 _T("Searches deleted (%list)")
273 } catch (Throwable
$e) {
275 $this->zdb
->connection
->rollBack();
278 'An error occurred trying to remove searches | ' .
285 //not numeric and not an array: incorrect.
287 'Asking to remove searches, but without providing an array or a single numeric value.',