]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/SavedSearch.php
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/>.
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
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-2023 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
57 * @property integer $id
58 * @property string $name
59 * @property array $parameters
60 * @property integer $author_id
61 * @property string $creation_date
62 * @property string $form
67 public const TABLE
= 'searches';
68 public const PK
= 'search_id';
73 private $parameters = [];
75 private $creation_date;
84 * @param Db $zdb Database instance
85 * @param Login $login Login instance
86 * @param mixed $args Arguments
88 public function __construct(Db
$zdb, Login
$login, $args = null)
91 $this->login
= $login;
92 $this->creation_date
= date('Y-m-d H:i:s');
96 } elseif ($args !== null && is_object($args)) {
97 $this->loadFromRs($args);
102 * Load a saved search from its identifier
104 * @param integer $id Identifier
108 private function load($id)
111 $select = $this->zdb
->select(self
::TABLE
);
112 $select->limit(1)->where([self
::PK
=> $id]);
113 if ($this->login
->isSuperAdmin()) {
114 $select->where(Adherent
::PK
. ' IS NULL');
116 $select->where([Adherent
::PK
=> $this->login
->id
]);
119 $results = $this->zdb
->execute($select);
120 /** @var ArrayObject $res */
121 $res = $results->current();
123 $this->loadFromRs($res);
124 } catch (Throwable
$e) {
126 'An error occurred loading saved search #' . $id . "Message:\n" .
135 * Load a saved search from a db ResultSet
137 * @param ArrayObject $rs ResultSet
141 private function loadFromRs(ArrayObject
$rs)
144 $this->id
= $rs->$pk;
145 $this->name
= $rs->name
;
146 $this->parameters
= json_decode($rs->parameters
, true);
147 $this->author_id
= $rs->id_adh
;
148 $this->creation_date
= $rs->creation_date
;
149 $this->form
= $rs->form
;
153 * Check and set values
155 * @param array $values Values to set
159 public function check($values)
163 'form' => _T('Form is mandatory!')
166 foreach ($values as $key => $value) {
167 if (in_array($key, ['nbshow', 'page'])) {
170 if (empty($value) && isset($mandatory[$key])) {
171 $this->errors
[] = $mandatory[$key];
173 $this->$key = $value;
174 unset($mandatory[$key]);
177 if (count($mandatory)) {
178 $this->errors
= array_merge($this->errors
, $mandatory);
181 if ($this->id
=== null && !$this->login
->isSuperAdmin()) {
182 //set author for new searches
183 $this->author_id
= $this->login
->id
;
186 return (count($this->errors
) === 0);
190 * Store saved search in database
192 * @return boolean|null
194 public function store()
196 $parameters = json_encode($this->parameters
);
198 'name' => $this->name
,
199 'parameters' => $parameters,
200 'id_adh' => $this->author_id
,
201 'creation_date' => ($this->creation_date
!== null ?
$this->creation_date
: date('Y-m-d H:i:s')),
202 'form' => $this->form
206 $insert = $this->zdb
->insert(self
::TABLE
);
207 $insert->values($data);
208 $add = $this->zdb
->execute($insert);
209 if (!$add->count() > 0) {
210 Analog
::log('Not stored!', Analog
::ERROR
);
214 } catch (Throwable
$e) {
216 'An error occurred storing saved search: ' . $e->getMessage() .
217 "\n" . print_r($data, true),
225 * Remove current saved search
229 public function remove()
231 $id = (int)$this->id
;
233 $delete = $this->zdb
->delete(self
::TABLE
);
234 $delete->where([self
::PK
=> $id]);
235 $this->zdb
->execute($delete);
237 'Saved search #' . $id . ' (' . $this->name
238 . ') deleted successfully.',
242 } catch (\RuntimeException
$re) {
244 } catch (Throwable
$e) {
246 'Unable to delete saved search ' . $id . ' | ' . $e->getMessage(),
256 * @param string $name Property name
260 public function __get($name)
263 $virtuals = ['sparameters'];
265 in_array($name, $virtuals)
266 ||
!in_array($name, $forbidden)
267 && isset($this->$name)
270 case 'creation_date':
271 if ($this->$name != '') {
273 $d = new \
DateTime($this->$name);
274 return $d->format(__("Y-m-d"));
275 } catch (Throwable
$e) {
276 //oops, we've got a bad date :/
278 'Bad date (' . $this->$name . ') | ' .
287 include_once GALETTE_ROOT
. 'includes/fields_defs/members_fields.php';
289 foreach ((array)$this->parameters
as $key => $parameter) {
290 //@phpstan-ignore-next-line
291 if (isset($members_fields[$key])) {
292 //@phpstan-ignore-next-line
293 $key = $members_fields[$key]['label'];
295 if (is_array($parameter) ||
is_object($parameter)) {
296 $parameter = json_encode($parameter);
298 $parameters[$key] = $parameter;
302 if (!property_exists($this, $name)) {
304 "Unknown property '$name'",
317 * Required for twig to access properties via __get
319 * @param string $name Property name
323 public function __isset($name)
326 $virtuals = ['sparameters'];
328 in_array($name, $virtuals)
329 ||
!in_array($name, $forbidden)
330 && isset($this->$name)
333 case 'creation_date':
337 return property_exists($this, $name);
346 * @param string $name Property name
347 * @param mixed $value Property value
351 public function __set($name, $value)
355 if (!in_array($value, $this->getKnownForms())) {
356 $this->errors
[] = str_replace('%form', $value, _T("Unknown form %form!"));
358 $this->form
= $value;
361 if (!is_array($value)) {
363 'Search parameters must be an array!',
367 $this->parameters
= $value;
370 if (trim($value) === '') {
371 $this->errors
[] = _T("Name cannot be empty!");
373 $this->name
= $value;
376 $this->author_id
= (int)$value;
381 ['%class', '%property'],
382 [self
::class, $name],
383 'Unable to set %class property %property'
396 public function getKnownForms()
408 public function getErrors()
410 return $this->errors
;