3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2013-2014 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-2014 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
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7.6dev - 2013-09-26
38 namespace Galette\Entity
;
41 use Laminas\Db\Adapter\Adapter
;
49 * @author Johan Cwiklinski <johan@x-tnd.be>
50 * @copyright 2013-2014 The Galette Team
51 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
52 * @link http://galette.tuxfamily.org
53 * @since Available since 0.7.6dev - 2013-09-26
57 const TABLE
= 'import_model';
58 const PK
= 'model_id';
62 private $creation_date;
67 * @return bool true if query succeed, false otherwise
69 public function load()
74 $select = $zdb->select(self
::TABLE
);
77 $results = $zdb->execute($select);
78 $result = $results->current();
81 $this->loadFromRS($result);
86 } catch (\Exception
$e) {
88 'Cannot load import model | ' . $e->getMessage() .
89 "\n" . $e->__toString(),
97 * Populate object from a resultset row
99 * @param ResultSet $r the resultset row
103 private function loadFromRS($r)
105 $this->id
= $r->model_id
;
106 $this->fields
= unserialize($r->model_fields
);
107 $this->creation_date
= $r->model_creation_date
;
113 * @param Db $zdb Database instance
117 public function remove($zdb)
120 $result = $zdb->db
->query(
121 'TRUNCATE TABLE ' . PREFIX_DB
. self
::TABLE
,
122 Adapter
::QUERY_MODE_EXECUTE
127 $this->fields
= null;
128 $this->creation_date
= null;
133 } catch (\Exception
$e) {
135 'Unable to remove import model ' . $e->getMessage(),
145 * @param Db $zdb Database instance
149 public function store($zdb)
153 self
::PK
=> $this->id
,
154 'model_fields' => serialize($this->fields
)
157 if (!isset($this->id
) ||
$this->id
== '') {
158 //we're inserting a new model
159 unset($values[self
::PK
]);
160 $this->creation_date
= date("Y-m-d H:i:s");
161 $values['model_creation_date'] = $this->creation_date
;
163 $insert = $zdb->insert(self
::TABLE
);
164 $insert->values($values);
165 $results = $zdb->execute($insert);
167 if ($results->count() > 0) {
170 throw new \
Exception(
171 'An error occurred inserting new import model!'
175 //we're editing an existing model
176 $update = $zdb->update(self
::TABLE
);
177 $update->set($values);
178 $update->where(self
::PK
. '=' . $this->id
);
179 $zdb->execute($update);
182 } catch (\Exception
$e) {
184 'Something went wrong storing import model :\'( | ' .
185 $e->getMessage() . "\n" . $e->getTraceAsString(),
197 public function getFields()
199 return $this->fields
;
205 * @param boolean $formatted Return date formatted, raw if false
209 public function getCreationDate($formatted = true)
211 if ($formatted === true) {
212 $date = new \
DateTime($this->creation_date
);
213 return $date->format(__("Y-m-d"));
215 return $this->creation_date
;
222 * @param array $fields Fields list
226 public function setFields($fields)
228 $this->fields
= $fields;