]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/ImportModel.php
72f5f25894e1fa962610ce08ad3a97df6748a1fb
[galette.git] / galette / lib / Galette / Entity / ImportModel.php
1 <?php
2
3 /**
4 * Copyright © 2003-2024 The Galette Team
5 *
6 * This file is part of Galette (https://galette.eu).
7 *
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.
12 *
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.
17 *
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/>.
20 */
21
22 namespace Galette\Entity;
23
24 use ArrayObject;
25 use Galette\Core\Db;
26 use Galette\Core\Galette;
27 use Throwable;
28 use Analog\Analog;
29 use Laminas\Db\Adapter\Adapter;
30
31 /**
32 * Import model entity
33 *
34 * @author Johan Cwiklinski <johan@x-tnd.be>
35 */
36 class ImportModel
37 {
38 public const TABLE = 'import_model';
39 public const PK = 'model_id';
40
41 private ?int $id;
42 /** @var array<string>|null */
43 private ?array $fields;
44 private ?string $creation_date;
45
46 /**
47 * Loads model
48 *
49 * @return bool true if query succeed, false otherwise
50 */
51 public function load(): bool
52 {
53 global $zdb;
54
55 try {
56 $select = $zdb->select(self::TABLE);
57 $select->limit(1);
58
59 $results = $zdb->execute($select);
60 $result = $results->current();
61
62 if ($result) {
63 $this->loadFromRS($result);
64 return true;
65 } else {
66 return false;
67 }
68 } catch (Throwable $e) {
69 Analog::log(
70 'Cannot load import model | ' . $e->getMessage() .
71 "\n" . $e->__toString(),
72 Analog::ERROR
73 );
74 throw $e;
75 }
76 }
77
78 /**
79 * Populate object from a resultset row
80 *
81 * @param ArrayObject<string, int|string> $r the resultset row
82 *
83 * @return void
84 */
85 private function loadFromRS(ArrayObject $r): void
86 {
87 $this->id = $r->model_id;
88 if (Galette::isSerialized($r->model_fields)) {
89 $this->fields = unserialize($r->model_fields);
90 } else {
91 $this->fields = Galette::jsonDecode($r->model_fields);
92 }
93 $this->creation_date = $r->model_creation_date;
94 }
95
96 /**
97 * Remove model
98 *
99 * @param Db $zdb Database instance
100 *
101 * @return boolean
102 */
103 public function remove(Db $zdb): bool
104 {
105 try {
106 $zdb->db->query(
107 'TRUNCATE TABLE ' . PREFIX_DB . self::TABLE,
108 Adapter::QUERY_MODE_EXECUTE
109 );
110
111 $this->id = null;
112 $this->fields = null;
113 $this->creation_date = null;
114 return true;
115 } catch (Throwable $e) {
116 Analog::log(
117 'Unable to remove import model ' . $e->getMessage(),
118 Analog::ERROR
119 );
120 throw $e;
121 }
122 }
123
124 /**
125 * Store the model
126 *
127 * @param Db $zdb Database instance
128 *
129 * @return boolean
130 */
131 public function store(Db $zdb): bool
132 {
133 try {
134 $values = array(
135 self::PK => $this->id,
136 'model_fields' => Galette::jsonEncode($this->fields)
137 );
138
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;
144
145 $insert = $zdb->insert(self::TABLE);
146 $insert->values($values);
147 $results = $zdb->execute($insert);
148
149 if ($results->count() > 0) {
150 return true;
151 } else {
152 throw new \Exception(
153 'An error occurred inserting new import model!'
154 );
155 }
156 } else {
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);
162 return true;
163 }
164 } catch (Throwable $e) {
165 Analog::log(
166 'Something went wrong storing import model :\'( | ' .
167 $e->getMessage() . "\n" . $e->getTraceAsString(),
168 Analog::ERROR
169 );
170 throw $e;
171 }
172 }
173
174 /**
175 * Get fields
176 *
177 * @return ?array<string>
178 */
179 public function getFields(): ?array
180 {
181 return $this->fields;
182 }
183
184 /**
185 * Get creation date
186 *
187 * @param boolean $formatted Return date formatted, raw if false
188 *
189 * @return string
190 */
191 public function getCreationDate(bool $formatted = true): string
192 {
193 if ($formatted === true) {
194 $date = new \DateTime($this->creation_date);
195 return $date->format(__("Y-m-d"));
196 } else {
197 return $this->creation_date;
198 }
199 }
200
201 /**
202 * Set fields
203 *
204 * @param array<string> $fields Fields list
205 *
206 * @return self
207 */
208 public function setFields(array $fields): self
209 {
210 $this->fields = $fields;
211 return $this;
212 }
213 }