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
;
40 use Laminas\Db\Sql\Expression
;
42 use Galette\Core\Login
;
43 use Galette\Core\History
;
44 use Galette\Entity\SavedSearch
;
45 use Galette\Filters\SavedSearchesList
;
46 use Galette\Entity\Adherent
;
51 * @category Repository
54 * @author Johan Cwiklinski <johan@x-tnd.be>
55 * @copyright 2019 The Galette Team
56 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
57 * @link http://galette.tuxfamily.org
62 const TABLE
= SavedSearch
::TABLE
;
63 const PK
= SavedSearch
::PK
;
65 private $count = null;
70 * @param Db $zdb Database
71 * @param Login $login Login
72 * @param SavedSearchesList $filters Filtering
74 public function __construct(Db
$zdb, Login
$login, $filters = null)
77 $this->login
= $login;
79 if ($filters === null) {
80 $this->filters
= new SavedSearchesList();
82 $this->filters
= $filters;
87 * Get saved searches list
89 * @param bool $as_search return the results as an array of
91 * @param array $fields field(s) name(s) to get. Should be a string or
92 * an array. If null, all fields will be
94 * @param boolean $count true if we want to count
96 * @return SavedSearch[]|ResultSet
98 public function getList($as_search = false, $fields = null, $count = true)
101 $select = $this->buildSelect($fields, $count);
102 $this->filters
->setLimits($select);
105 $results = $this->zdb
->execute($select);
107 foreach ($results as $row) {
108 $searches[] = new SavedSearch($this->zdb
, $this->login
, $row);
111 $searches = $results;
114 } catch (\Exception
$e) {
116 'Cannot list saved searches | ' . $e->getMessage(),
124 * Builds the SELECT statement
126 * @param array $fields fields list to retrieve
127 * @param bool $count true if we want to count members
128 * (not applicable from static calls), defaults to false
130 * @return string SELECT statement
132 private function buildSelect($fields, $count = false)
135 $fieldsList = ($fields != null)
136 ?
((!is_array($fields) ||
count($fields) < 1) ?
(array)'*'
137 : implode(', ', $fields)) : (array)'*';
139 $select = $this->zdb
->select(self
::TABLE
, 's');
140 $select->columns($fieldsList);
141 if (0 === $this->login
->id
) {
142 $select->where
->isNull(Adherent
::PK
);
144 $select->where([Adherent
::PK
=> $this->login
->id
]);
147 $select->order(self
::buildOrderClause());
150 $this->proceedCount($select);
154 } catch (\Exception
$e) {
156 'Cannot build SELECT clause for saved searches | ' . $e->getMessage(),
164 * Count searches from the query
166 * @param Select $select Original select
170 private function proceedCount($select)
173 $countSelect = clone $select;
174 $countSelect->reset($countSelect::COLUMNS
);
175 $countSelect->reset($countSelect::JOINS
);
176 $countSelect->reset($countSelect::ORDER
);
177 $countSelect->columns(
179 self
::PK
=> new Expression('COUNT(' . self
::PK
. ')')
183 $results = $this->zdb
->execute($countSelect);
184 $result = $results->current();
187 $this->count
= $result->$k;
189 if ($this->count
> 0) {
190 $this->filters
->setCounter($this->count
);
192 } catch (\Exception
$e) {
194 'Cannot count saved searches | ' . $e->getMessage(),
202 * Builds the order clause
204 * @return string SQL ORDER clause
206 private function buildOrderClause()
209 $order[] = $this->filters
->orderby
. ' ' . $this->filters
->ordered
;
215 * Get count for current query
219 public function getCount()
225 * Remove specified searches
227 * @param integer|array $ids Searches identifiers to delete
228 * @param History $hist History
229 * @param boolean $transaction True to begin a database transaction
233 public function remove($ids, History
$hist, $transaction = true)
236 if (is_numeric($ids)) {
237 //we've got only one identifier
243 if (is_array($list)) {
247 $this->zdb
->connection
->beginTransaction();
249 $select = $this->zdb
->select(self
::TABLE
);
250 $select->where
->in(self
::PK
, $list);
251 $searches = $this->zdb
->execute($select);
252 foreach ($searches as $search) {
253 $s = new SavedSearch($this->zdb
, $this->login
, $search);
254 $res = $s->remove(false);
255 if ($res === false) {
256 throw new \
Exception();
260 $this->zdb
->connection
->commit();
265 print_r($list, true),
266 _T("Searches deleted (%list)")
270 } catch (\Exception
$e) {
272 $this->zdb
->connection
->rollBack();
275 'An error occurred trying to remove searches | ' .
282 //not numeric and not an array: incorrect.
284 'Asking to remove searches, but without providing an array or a single numeric value.',