]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/SavedSearch.php
22ab866711d09c2d1ea91dfe829526714a48a74c
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/>.
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
34 * @since Available since 0.9.3dev - 2019-03-25
37 namespace Galette\Entity
;
42 use Galette\Core\Login
;
51 * @author Johan Cwiklinski <johan@x-tnd.be>
52 * @copyright 2019 The Galette Team
53 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
54 * @link http://galette.tuxfamily.org
55 * @since Available since 0.9.3dev - 2019-03-25
60 public const TABLE
= 'searches';
61 public const PK
= 'search_id';
66 private $parameters = [];
68 private $creation_date;
77 * @param Db $zdb Database instance
78 * @param Login $login Login instance
79 * @param mixed $args Arguments
81 public function __construct(Db
$zdb, Login
$login, $args = null)
84 $this->login
= $login;
85 $this->creation_date
= date('Y-m-d H:i:s');
89 } elseif ($args !== null && is_object($args)) {
90 $this->loadFromRs($args);
95 * Load a saved search from its identifier
97 * @param integer $id Identifier
101 private function load($id)
104 $select = $this->zdb
->select(self
::TABLE
);
105 $select->limit(1)->where(self
::PK
. ' = ' . $id);
106 if ($this->login
->isSuperAdmin()) {
107 $select->where(Adherent
::PK
. ' IS NULL');
109 $select->where(Adherent
::PK
. ' = ' . (int)$this->login
->id
);
112 $results = $this->zdb
->execute($select);
113 $res = $results->current();
115 $this->loadFromRs($res);
116 } catch (Throwable
$e) {
118 'An error occurred loading saved search #' . $id . "Message:\n" .
126 * Load a saved search from a db ResultSet
128 * @param ResultSet $rs ResultSet
132 private function loadFromRs($rs)
135 $this->id
= $rs->$pk;
136 $this->name
= $rs->name
;
137 $this->parameters
= json_decode($rs->parameters
, true);
138 $this->author_id
= $rs->id_adh
;
139 $this->creation_date
= $rs->creation_date
;
140 $this->form
= $rs->form
;
144 * Check and set values
146 * @param array $values Values to set
150 public function check($values)
154 'form' => _T('Form is mandatory!')
157 foreach ($values as $key => $value) {
158 if (in_array($key, ['nbshow', 'page'])) {
161 if (empty($value) && isset($mandatory[$key])) {
162 $this->errors
[] = $mandatory[$key];
164 $this->$key = $value;
165 unset($mandatory[$key]);
168 if (count($mandatory)) {
169 $this->errors
= array_merge($this->errors
, $mandatory);
172 if ($this->id
=== null && !$this->login
->isSuperAdmin()) {
173 //set author for new searches
174 $this->author_id
= $this->login
->id
;
177 return (count($this->errors
) === 0);
181 * Store saved search in database
183 * @return boolean|null
185 public function store()
187 $parameters = json_encode($this->parameters
);
188 $parameters_sum = sha1($parameters, true);
189 if ($this->zdb
->isPostgres()) {
190 $parameters_sum = pg_escape_bytea($parameters_sum);
193 'name' => $this->name
,
194 'parameters' => $parameters,
195 'parameters_sum' => $parameters_sum,
196 'id_adh' => $this->author_id
,
197 'creation_date' => ($this->creation_date
!== null ?
$this->creation_date
: date('Y-m-d H:i:s')),
198 'form' => $this->form
202 $select = $this->zdb
->select(self
::TABLE
);
205 'form' => $this->form
,
206 'parameters_sum' => $parameters_sum,
207 'id_adh' => $this->author_id
,
211 $results = $this->zdb
->execute($select);
212 if ($results->count() !== 0) {
213 $result = $results->current();
214 //search already exists
216 str_replace('%name', $result->name
, 'Already saved as "%name"!'),
221 $insert = $this->zdb
->insert(self
::TABLE
);
222 $insert->values($data);
223 $add = $this->zdb
->execute($insert);
224 if (!$add->count() > 0) {
225 Analog
::log('Not stored!', Analog
::ERROR
);
230 } catch (Throwable
$e) {
232 'An error occurred storing saved search: ' . $e->getMessage() .
233 "\n" . print_r($data, true),
241 * Remove current saved search
245 public function remove()
247 $id = (int)$this->id
;
249 $delete = $this->zdb
->delete(self
::TABLE
);
251 self
::PK
. ' = ' . $id
253 $this->zdb
->execute($delete);
255 'Saved search #' . $id . ' (' . $this->name
256 . ') deleted successfully.',
260 } catch (\RuntimeException
$re) {
262 } catch (Throwable
$e) {
264 'Unable to delete saved search ' . $id . ' | ' . $e->getMessage(),
274 * @param string $name Property name
278 public function __get($name)
281 $virtuals = ['sparameters'];
283 in_array($name, $virtuals)
284 ||
!in_array($name, $forbidden)
285 && isset($this->$name)
288 case 'creation_date':
289 if ($this->$name != '') {
291 $d = new \
DateTime($this->$name);
292 return $d->format(__("Y-m-d"));
293 } catch (Throwable
$e) {
294 //oops, we've got a bad date :/
296 'Bad date (' . $this->$name . ') | ' .
305 include_once GALETTE_ROOT
. 'includes/fields_defs/members_fields.php';
307 foreach ((array)$this->parameters
as $key => $parameter) {
308 if (isset($members_fields[$key])) {
309 $key = $members_fields[$key]['label'];
311 if (is_array($parameter) ||
is_object($parameter)) {
312 $parameter = json_encode($parameter);
314 $parameters[$key] = $parameter;
319 if (!property_exists($this, $name)) {
321 "Unknown property '$name'",
336 * @param string $name Property name
337 * @param mixed $value Property value
341 public function __set($name, $value)
345 if (!in_array($value, $this->getKnownForms())) {
346 $this->errors
[] = str_replace('%form', $value, _T("Unknown form %form!"));
348 $this->form
= $value;
351 if (!is_array($value)) {
353 'Search parameters must be an array!',
357 $this->parameters
= $value;
360 if (trim($value) === '') {
361 $this->errors
[] = _T("Name cannot be empty!");
363 $this->name
= $value;
366 $this->author_id
= (int)$value;
371 ['%class', '%property'],
372 [self
::class, $name],
373 'Unable to set %class property %property'
386 public function getKnownForms()
398 public function getErrors()
400 return $this->errors
;