3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2019 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 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
;
41 use Laminas\Db\Sql\Expression
;
43 use Galette\Core\Login
;
44 use Galette\Core\History
;
45 use Galette\Entity\SavedSearch
;
46 use Galette\Filters\SavedSearchesList
;
47 use Galette\Entity\Adherent
;
52 * @category Repository
55 * @author Johan Cwiklinski <johan@x-tnd.be>
56 * @copyright 2019 The Galette Team
57 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
58 * @link http://galette.tuxfamily.org
63 public const TABLE
= SavedSearch
::TABLE
;
64 public const PK
= SavedSearch
::PK
;
66 private $count = null;
71 * @param Db $zdb Database
72 * @param Login $login Login
73 * @param SavedSearchesList $filters Filtering
75 public function __construct(Db
$zdb, Login
$login, $filters = null)
78 $this->login
= $login;
80 if ($filters === null) {
81 $this->filters
= new SavedSearchesList();
83 $this->filters
= $filters;
88 * Get saved searches list
90 * @param bool $as_search return the results as an array of
92 * @param array $fields field(s) name(s) to get. Should be a string or
93 * an array. If null, all fields will be
95 * @param boolean $count true if we want to count
97 * @return SavedSearch[]|ResultSet
99 public function getList($as_search = false, $fields = null, $count = true)
102 $select = $this->buildSelect($fields, $count);
103 $this->filters
->setLimits($select);
106 $results = $this->zdb
->execute($select);
108 foreach ($results as $row) {
109 $searches[] = new SavedSearch($this->zdb
, $this->login
, $row);
112 $searches = $results;
115 } catch (Throwable
$e) {
117 'Cannot list saved searches | ' . $e->getMessage(),
125 * Builds the SELECT statement
127 * @param array $fields fields list to retrieve
128 * @param bool $count true if we want to count members
129 * (not applicable from static calls), defaults to false
131 * @return string SELECT statement
133 private function buildSelect($fields, $count = false)
136 $fieldsList = ($fields != null)
137 ?
((!is_array($fields) ||
count($fields) < 1) ?
(array)'*'
138 : implode(', ', $fields)) : (array)'*';
140 $select = $this->zdb
->select(self
::TABLE
, 's');
141 $select->columns($fieldsList);
142 if (0 === $this->login
->id
) {
143 $select->where
->isNull(Adherent
::PK
);
145 $select->where([Adherent
::PK
=> $this->login
->id
]);
148 $select->order(self
::buildOrderClause());
151 $this->proceedCount($select);
155 } catch (Throwable
$e) {
157 'Cannot build SELECT clause for saved searches | ' . $e->getMessage(),
165 * Count searches from the query
167 * @param Select $select Original select
171 private function proceedCount($select)
174 $countSelect = clone $select;
175 $countSelect->reset($countSelect::COLUMNS
);
176 $countSelect->reset($countSelect::JOINS
);
177 $countSelect->reset($countSelect::ORDER
);
178 $countSelect->columns(
180 self
::PK
=> new Expression('COUNT(' . self
::PK
. ')')
184 $results = $this->zdb
->execute($countSelect);
185 $result = $results->current();
188 $this->count
= $result->$k;
190 if ($this->count
> 0) {
191 $this->filters
->setCounter($this->count
);
193 } catch (Throwable
$e) {
195 'Cannot count saved searches | ' . $e->getMessage(),
203 * Builds the order clause
205 * @return string SQL ORDER clause
207 private function buildOrderClause()
210 $order[] = $this->filters
->orderby
. ' ' . $this->filters
->ordered
;
216 * Get count for current query
220 public function getCount()
226 * Remove specified searches
228 * @param integer|array $ids Searches identifiers to delete
229 * @param History $hist History
230 * @param boolean $transaction True to begin a database transaction
234 public function remove($ids, History
$hist, $transaction = true)
237 if (is_numeric($ids)) {
238 //we've got only one identifier
244 if (is_array($list)) {
248 $this->zdb
->connection
->beginTransaction();
250 $select = $this->zdb
->select(self
::TABLE
);
251 $select->where
->in(self
::PK
, $list);
252 $searches = $this->zdb
->execute($select);
253 foreach ($searches as $search) {
254 $s = new SavedSearch($this->zdb
, $this->login
, $search);
255 $res = $s->remove(false);
256 if ($res === false) {
257 throw new \
Exception();
261 $this->zdb
->connection
->commit();
266 print_r($list, true),
267 _T("Searches deleted (%list)")
271 } catch (Throwable
$e) {
273 $this->zdb
->connection
->rollBack();
276 'An error occurred trying to remove searches | ' .
283 //not numeric and not an array: incorrect.
285 'Asking to remove searches, but without providing an array or a single numeric value.',