]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/ImportModel.php
Switch to phpstan level 4
[galette.git] / galette / lib / Galette / Entity / ImportModel.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Import model
7 *
8 * PHP version 5
9 *
10 * Copyright © 2013-2023 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
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.
18 *
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.
23 *
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/>.
26 *
27 * @category Entity
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2013-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.7.6dev - 2013-09-26
35 */
36
37 namespace Galette\Entity;
38
39 use ArrayObject;
40 use Galette\Core\Db;
41 use Throwable;
42 use Analog\Analog;
43 use Laminas\Db\Adapter\Adapter;
44
45 /**
46 * Import model entity
47 *
48 * @category Entity
49 * @name ImportModel
50 * @package Galette
51 * @author Johan Cwiklinski <johan@x-tnd.be>
52 * @copyright 2013-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.7.6dev - 2013-09-26
56 */
57 class ImportModel
58 {
59 public const TABLE = 'import_model';
60 public const PK = 'model_id';
61
62 private $id;
63 private $fields;
64 private $creation_date;
65
66 /**
67 * Loads model
68 *
69 * @return bool true if query succeed, false otherwise
70 */
71 public function load()
72 {
73 global $zdb;
74
75 try {
76 $select = $zdb->select(self::TABLE);
77 $select->limit(1);
78
79 $results = $zdb->execute($select);
80 $result = $results->current();
81
82 if ($result) {
83 $this->loadFromRS($result);
84 return true;
85 } else {
86 return false;
87 }
88 } catch (Throwable $e) {
89 Analog::log(
90 'Cannot load import model | ' . $e->getMessage() .
91 "\n" . $e->__toString(),
92 Analog::ERROR
93 );
94 throw $e;
95 }
96 }
97
98 /**
99 * Populate object from a resultset row
100 *
101 * @param ArrayObject $r the resultset row
102 *
103 * @return void
104 */
105 private function loadFromRS(ArrayObject $r)
106 {
107 $this->id = $r->model_id;
108 $this->fields = unserialize($r->model_fields);
109 $this->creation_date = $r->model_creation_date;
110 }
111
112 /**
113 * Remove model
114 *
115 * @param Db $zdb Database instance
116 *
117 * @return boolean
118 */
119 public function remove(Db $zdb)
120 {
121 try {
122 $zdb->db->query(
123 'TRUNCATE TABLE ' . PREFIX_DB . self::TABLE,
124 Adapter::QUERY_MODE_EXECUTE
125 );
126
127 $this->id = null;
128 $this->fields = null;
129 $this->creation_date = null;
130 return true;
131 } catch (Throwable $e) {
132 Analog::log(
133 'Unable to remove import model ' . $e->getMessage(),
134 Analog::ERROR
135 );
136 throw $e;
137 }
138 }
139
140 /**
141 * Store the model
142 *
143 * @param Db $zdb Database instance
144 *
145 * @return boolean
146 */
147 public function store(Db $zdb)
148 {
149 try {
150 $values = array(
151 self::PK => $this->id,
152 'model_fields' => serialize($this->fields)
153 );
154
155 if (!isset($this->id) || $this->id == '') {
156 //we're inserting a new model
157 unset($values[self::PK]);
158 $this->creation_date = date("Y-m-d H:i:s");
159 $values['model_creation_date'] = $this->creation_date;
160
161 $insert = $zdb->insert(self::TABLE);
162 $insert->values($values);
163 $results = $zdb->execute($insert);
164
165 if ($results->count() > 0) {
166 return true;
167 } else {
168 throw new \Exception(
169 'An error occurred inserting new import model!'
170 );
171 }
172 } else {
173 //we're editing an existing model
174 $update = $zdb->update(self::TABLE);
175 $update->set($values);
176 $update->where([self::PK => $this->id]);
177 $zdb->execute($update);
178 return true;
179 }
180 } catch (Throwable $e) {
181 Analog::log(
182 'Something went wrong storing import model :\'( | ' .
183 $e->getMessage() . "\n" . $e->getTraceAsString(),
184 Analog::ERROR
185 );
186 throw $e;
187 }
188 }
189
190 /**
191 * Get fields
192 *
193 * @return ?array
194 */
195 public function getFields()
196 {
197 return $this->fields;
198 }
199
200 /**
201 * Get creation date
202 *
203 * @param boolean $formatted Return date formatted, raw if false
204 *
205 * @return string
206 */
207 public function getCreationDate($formatted = true)
208 {
209 if ($formatted === true) {
210 $date = new \DateTime($this->creation_date);
211 return $date->format(__("Y-m-d"));
212 } else {
213 return $this->creation_date;
214 }
215 }
216
217 /**
218 * Set fields
219 *
220 * @param array $fields Fields list
221 *
222 * @return ImportModel
223 */
224 public function setFields($fields)
225 {
226 $this->fields = $fields;
227 return $this;
228 }
229 }