]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/SavedSearch.php
218f7e8a4a3a435515e1acae1a844b5ed98ff801
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
;
41 use Galette\Core\Login
;
50 * @author Johan Cwiklinski <johan@x-tnd.be>
51 * @copyright 2019 The Galette Team
52 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
53 * @link http://galette.tuxfamily.org
54 * @since Available since 0.9.3dev - 2019-03-25
59 const TABLE
= 'searches';
60 const PK
= 'search_id';
65 private $parameters = [];
67 private $creation_date;
76 * @param Db $zdb Database instance
77 * @param Login $login Login instance
78 * @param mixed $args Arguments
80 public function __construct(Db
$zdb, Login
$login, $args = null)
83 $this->login
= $login;
84 $this->creation_date
= date('Y-m-d H:i:s');
88 } elseif ($args !== null && is_object($args)) {
89 $this->loadFromRs($args);
94 * Load a saved search from its identifier
96 * @param integer $id Identifier
100 private function load($id)
103 $select = $this->zdb
->select(self
::TABLE
);
104 $select->limit(1)->where(self
::PK
. ' = ' . $id);
105 if ($this->login
->isSuperAdmin()) {
106 $select->where(Adherent
::PK
. ' IS NULL');
108 $select->where(Adherent
::PK
. ' = ' . (int)$this->login
->id
);
111 $results = $this->zdb
->execute($select);
112 $res = $results->current();
114 $this->loadFromRs($res);
115 } catch (\Exception
$e) {
117 'An error occurred loading saved search #' . $id . "Message:\n" .
125 * Load a saved search from a db ResultSet
127 * @param ResultSet $rs ResultSet
131 private function loadFromRs($rs)
134 $this->id
= $rs->$pk;
135 $this->name
= $rs->name
;
136 $this->parameters
= json_decode($rs->parameters
, true);
137 $this->author_id
= $rs->id_adh
;
138 $this->creation_date
= $rs->creation_date
;
139 $this->form
= $rs->form
;
143 * Check and set values
145 * @param array $values Values to set
149 public function check($values)
153 'form' => _T('Form is mandatory!')
156 foreach ($values as $key => $value) {
157 if (in_array($key, ['nbshow', 'page'])) {
160 if (empty($value) && isset($mandatory[$key])) {
161 $this->errors
[] = $mandatory[$key];
163 $this->$key = $value;
164 unset($mandatory[$key]);
167 if (count($mandatory)) {
168 $this->errors
= array_merge($this->errors
, $mandatory);
171 if ($this->id
=== null && !$this->login
->isSuperAdmin()) {
172 //set author for new searches
173 $this->author_id
= $this->login
->id
;
176 return (count($this->errors
) === 0);
180 * Store saved search in database
182 * @return boolean|null
184 public function store()
186 $parameters = json_encode($this->parameters
);
187 $parameters_sum = sha1($parameters, true);
188 if ($this->zdb
->isPostgres()) {
189 $parameters_sum = pg_escape_bytea($parameters_sum);
192 'name' => $this->name
,
193 'parameters' => $parameters,
194 'parameters_sum' => $parameters_sum,
195 'id_adh' => $this->author_id
,
196 'creation_date' => ($this->creation_date
!== null ?
$this->creation_date
: date('Y-m-d H:i:s')),
197 'form' => $this->form
201 $select = $this->zdb
->select(self
::TABLE
);
204 'form' => $this->form
,
205 'parameters_sum' => $parameters_sum,
206 'id_adh' => $this->author_id
,
210 $results = $this->zdb
->execute($select);
211 if ($results->count() !== 0) {
212 $result = $results->current();
213 //search already exists
215 str_replace('%name', $result->name
, 'Already saved as "%name"!'),
220 $insert = $this->zdb
->insert(self
::TABLE
);
221 $insert->values($data);
222 $add = $this->zdb
->execute($insert);
223 if (!$add->count() > 0) {
224 Analog
::log('Not stored!', Analog
::ERROR
);
229 } catch (\Exception
$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 (\Exception
$e) {
264 'Unable to delete saved search ' . $id . ' | ' . $e->getMessage(),
274 * @param string $name Property name
278 public function __get($name)
281 $virtuals = ['sparameters'];
282 if (in_array($name, $virtuals)
283 ||
!in_array($name, $forbidden)
284 && isset($this->$name)
287 case 'creation_date':
288 if ($this->$name != '') {
290 $d = new \
DateTime($this->$name);
291 return $d->format(__("Y-m-d"));
292 } catch (\Exception
$e) {
293 //oops, we've got a bad date :/
295 'Bad date (' . $this->$name . ') | ' .
304 include_once GALETTE_ROOT
. 'includes/fields_defs/members_fields.php';
306 foreach ((array)$this->parameters
as $key => $parameter) {
307 if (isset($members_fields[$key])) {
308 $key = $members_fields[$key]['label'];
310 if (is_array($parameter) ||
is_object($parameter)) {
311 $parameter = json_encode($parameter);
313 $parameters[$key] = $parameter;
318 if (!property_exists($this, $name)) {
320 "Unknown property '$name'",
335 * @param string $name Property name
336 * @param mixed $value Property value
340 public function __set($name, $value)
344 if (!in_array($value, $this->getKnownForms())) {
345 $this->errors
[] = str_replace('%form', $value, _T("Unknown form %form!"));
347 $this->form
= $value;
350 if (!is_array($value)) {
352 'Search parameters must be an array!',
356 $this->parameters
= $value;
359 if (trim($value) === '') {
360 $this->errors
[] = _T("Name cannot be empty!");
362 $this->name
= $value;
365 $this->author_id
= (int)$value;
370 ['%class', '%property'],
371 [self
::class, $name],
372 'Unable to set %class property %property'
385 public function getKnownForms()
397 public function getErrors()
399 return $this->errors
;