3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2013-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 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
37 namespace Galette\Entity
;
43 use Laminas\Db\Adapter\Adapter
;
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
59 public const TABLE
= 'import_model';
60 public const PK
= 'model_id';
64 private $creation_date;
69 * @return bool true if query succeed, false otherwise
71 public function load()
76 $select = $zdb->select(self
::TABLE
);
79 $results = $zdb->execute($select);
80 $result = $results->current();
83 $this->loadFromRS($result);
88 } catch (Throwable
$e) {
90 'Cannot load import model | ' . $e->getMessage() .
91 "\n" . $e->__toString(),
99 * Populate object from a resultset row
101 * @param ArrayObject $r the resultset row
105 private function loadFromRS(ArrayObject
$r)
107 $this->id
= $r->model_id
;
108 $this->fields
= unserialize($r->model_fields
);
109 $this->creation_date
= $r->model_creation_date
;
115 * @param Db $zdb Database instance
119 public function remove(Db
$zdb)
123 'TRUNCATE TABLE ' . PREFIX_DB
. self
::TABLE
,
124 Adapter
::QUERY_MODE_EXECUTE
128 $this->fields
= null;
129 $this->creation_date
= null;
131 } catch (Throwable
$e) {
133 'Unable to remove import model ' . $e->getMessage(),
143 * @param Db $zdb Database instance
147 public function store(Db
$zdb)
151 self
::PK
=> $this->id
,
152 'model_fields' => serialize($this->fields
)
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
;
161 $insert = $zdb->insert(self
::TABLE
);
162 $insert->values($values);
163 $results = $zdb->execute($insert);
165 if ($results->count() > 0) {
168 throw new \
Exception(
169 'An error occurred inserting new import model!'
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);
180 } catch (Throwable
$e) {
182 'Something went wrong storing import model :\'( | ' .
183 $e->getMessage() . "\n" . $e->getTraceAsString(),
195 public function getFields()
197 return $this->fields
;
203 * @param boolean $formatted Return date formatted, raw if false
207 public function getCreationDate($formatted = true)
209 if ($formatted === true) {
210 $date = new \
DateTime($this->creation_date
);
211 return $date->format(__("Y-m-d"));
213 return $this->creation_date
;
220 * @param array $fields Fields list
222 * @return ImportModel
224 public function setFields($fields)
226 $this->fields
= $fields;