4 * Copyright © 2003-2024 The Galette Team
6 * This file is part of Galette (https://galette.eu).
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
22 namespace Galette\Entity
;
26 use Galette\Core\Galette
;
29 use Laminas\Db\Adapter\Adapter
;
34 * @author Johan Cwiklinski <johan@x-tnd.be>
38 public const TABLE
= 'import_model';
39 public const PK
= 'model_id';
42 /** @var array<string>|null */
43 private ?
array $fields;
44 private ?
string $creation_date;
49 * @return bool true if query succeed, false otherwise
51 public function load(): bool
56 $select = $zdb->select(self
::TABLE
);
59 $results = $zdb->execute($select);
60 $result = $results->current();
63 $this->loadFromRS($result);
68 } catch (Throwable
$e) {
70 'Cannot load import model | ' . $e->getMessage() .
71 "\n" . $e->__toString(),
79 * Populate object from a resultset row
81 * @param ArrayObject<string, int|string> $r the resultset row
85 private function loadFromRS(ArrayObject
$r): void
87 $this->id
= $r->model_id
;
88 if (Galette
::isSerialized($r->model_fields
)) {
89 $this->fields
= unserialize($r->model_fields
);
91 $this->fields
= Galette
::jsonDecode($r->model_fields
);
93 $this->creation_date
= $r->model_creation_date
;
99 * @param Db $zdb Database instance
103 public function remove(Db
$zdb): bool
107 'TRUNCATE TABLE ' . PREFIX_DB
. self
::TABLE
,
108 Adapter
::QUERY_MODE_EXECUTE
112 $this->fields
= null;
113 $this->creation_date
= null;
115 } catch (Throwable
$e) {
117 'Unable to remove import model ' . $e->getMessage(),
127 * @param Db $zdb Database instance
131 public function store(Db
$zdb): bool
135 self
::PK
=> $this->id
,
136 'model_fields' => Galette
::jsonEncode($this->fields
)
139 if (!isset($this->id
) ||
$this->id
== '') {
140 //we're inserting a new model
141 unset($values[self
::PK
]);
142 $this->creation_date
= date("Y-m-d H:i:s");
143 $values['model_creation_date'] = $this->creation_date
;
145 $insert = $zdb->insert(self
::TABLE
);
146 $insert->values($values);
147 $results = $zdb->execute($insert);
149 if ($results->count() > 0) {
152 throw new \
Exception(
153 'An error occurred inserting new import model!'
157 //we're editing an existing model
158 $update = $zdb->update(self
::TABLE
);
159 $update->set($values);
160 $update->where([self
::PK
=> $this->id
]);
161 $zdb->execute($update);
164 } catch (Throwable
$e) {
166 'Something went wrong storing import model :\'( | ' .
167 $e->getMessage() . "\n" . $e->getTraceAsString(),
177 * @return ?array<string>
179 public function getFields(): ?
array
181 return $this->fields
;
187 * @param boolean $formatted Return date formatted, raw if false
191 public function getCreationDate(bool $formatted = true): string
193 if ($formatted === true) {
194 $date = new \
DateTime($this->creation_date
);
195 return $date->format(__("Y-m-d"));
197 return $this->creation_date
;
204 * @param array<string> $fields Fields list
208 public function setFields(array $fields): self
210 $this->fields
= $fields;