]>
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-2021 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-2021 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-2021 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
=> $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" .
127 * Load a saved search from a db ResultSet
129 * @param ResultSet $rs ResultSet
133 private function loadFromRs($rs)
136 $this->id
= $rs->$pk;
137 $this->name
= $rs->name
;
138 $this->parameters
= json_decode($rs->parameters
, true);
139 $this->author_id
= $rs->id_adh
;
140 $this->creation_date
= $rs->creation_date
;
141 $this->form
= $rs->form
;
145 * Check and set values
147 * @param array $values Values to set
151 public function check($values)
155 'form' => _T('Form is mandatory!')
158 foreach ($values as $key => $value) {
159 if (in_array($key, ['nbshow', 'page'])) {
162 if (empty($value) && isset($mandatory[$key])) {
163 $this->errors
[] = $mandatory[$key];
165 $this->$key = $value;
166 unset($mandatory[$key]);
169 if (count($mandatory)) {
170 $this->errors
= array_merge($this->errors
, $mandatory);
173 if ($this->id
=== null && !$this->login
->isSuperAdmin()) {
174 //set author for new searches
175 $this->author_id
= $this->login
->id
;
178 return (count($this->errors
) === 0);
182 * Store saved search in database
184 * @return boolean|null
186 public function store()
188 $parameters = json_encode($this->parameters
);
190 'name' => $this->name
,
191 'parameters' => $parameters,
192 'id_adh' => $this->author_id
,
193 'creation_date' => ($this->creation_date
!== null ?
$this->creation_date
: date('Y-m-d H:i:s')),
194 'form' => $this->form
198 $insert = $this->zdb
->insert(self
::TABLE
);
199 $insert->values($data);
200 $add = $this->zdb
->execute($insert);
201 if (!$add->count() > 0) {
202 Analog
::log('Not stored!', Analog
::ERROR
);
206 } catch (Throwable
$e) {
208 'An error occurred storing saved search: ' . $e->getMessage() .
209 "\n" . print_r($data, true),
217 * Remove current saved search
221 public function remove()
223 $id = (int)$this->id
;
225 $delete = $this->zdb
->delete(self
::TABLE
);
226 $delete->where([self
::PK
=> $id]);
227 $this->zdb
->execute($delete);
229 'Saved search #' . $id . ' (' . $this->name
230 . ') deleted successfully.',
234 } catch (\RuntimeException
$re) {
236 } catch (Throwable
$e) {
238 'Unable to delete saved search ' . $id . ' | ' . $e->getMessage(),
248 * @param string $name Property name
252 public function __get($name)
255 $virtuals = ['sparameters'];
257 in_array($name, $virtuals)
258 ||
!in_array($name, $forbidden)
259 && isset($this->$name)
262 case 'creation_date':
263 if ($this->$name != '') {
265 $d = new \
DateTime($this->$name);
266 return $d->format(__("Y-m-d"));
267 } catch (Throwable
$e) {
268 //oops, we've got a bad date :/
270 'Bad date (' . $this->$name . ') | ' .
279 include_once GALETTE_ROOT
. 'includes/fields_defs/members_fields.php';
281 foreach ((array)$this->parameters
as $key => $parameter) {
282 if (isset($members_fields[$key])) {
283 $key = $members_fields[$key]['label'];
285 if (is_array($parameter) ||
is_object($parameter)) {
286 $parameter = json_encode($parameter);
288 $parameters[$key] = $parameter;
293 if (!property_exists($this, $name)) {
295 "Unknown property '$name'",
310 * @param string $name Property name
311 * @param mixed $value Property value
315 public function __set($name, $value)
319 if (!in_array($value, $this->getKnownForms())) {
320 $this->errors
[] = str_replace('%form', $value, _T("Unknown form %form!"));
322 $this->form
= $value;
325 if (!is_array($value)) {
327 'Search parameters must be an array!',
331 $this->parameters
= $value;
334 if (trim($value) === '') {
335 $this->errors
[] = _T("Name cannot be empty!");
337 $this->name
= $value;
340 $this->author_id
= (int)$value;
345 ['%class', '%property'],
346 [self
::class, $name],
347 'Unable to set %class property %property'
360 public function getKnownForms()
372 public function getErrors()
374 return $this->errors
;