]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/ImportModel.php
e8412747bb710123820458f035e992e464546618
[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-2014 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-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
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7.6dev - 2013-09-26
36 */
37
38 namespace Galette\Entity;
39
40 use Analog\Analog;
41 use Laminas\Db\Adapter\Adapter;
42
43 /**
44 * Import model entity
45 *
46 * @category Entity
47 * @name ImportModel
48 * @package Galette
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
54 */
55 class ImportModel
56 {
57 const TABLE = 'import_model';
58 const PK = 'model_id';
59
60 private $id;
61 private $fields;
62 private $creation_date;
63
64 /**
65 * Loads model
66 *
67 * @return bool true if query succeed, false otherwise
68 */
69 public function load()
70 {
71 global $zdb;
72
73 try {
74 $select = $zdb->select(self::TABLE);
75 $select->limit(1);
76
77 $results = $zdb->execute($select);
78 $result = $results->current();
79
80 if ($result) {
81 $this->loadFromRS($result);
82 return true;
83 } else {
84 return false;
85 }
86 } catch (\Exception $e) {
87 Analog::log(
88 'Cannot load import model | ' . $e->getMessage() .
89 "\n" . $e->__toString(),
90 Analog::ERROR
91 );
92 return false;
93 }
94 }
95
96 /**
97 * Populate object from a resultset row
98 *
99 * @param ResultSet $r the resultset row
100 *
101 * @return void
102 */
103 private function loadFromRS($r)
104 {
105 $this->id = $r->model_id;
106 $this->fields = unserialize($r->model_fields);
107 $this->creation_date = $r->model_creation_date;
108 }
109
110 /**
111 * Remove model
112 *
113 * @param Db $zdb Database instance
114 *
115 * @return boolean
116 */
117 public function remove($zdb)
118 {
119 try {
120 $result = $zdb->db->query(
121 'TRUNCATE TABLE ' . PREFIX_DB . self::TABLE,
122 Adapter::QUERY_MODE_EXECUTE
123 );
124
125 if ($result) {
126 $this->id = null;
127 $this->fields = null;
128 $this->creation_date = null;
129 return true;
130 }
131
132 return false;
133 } catch (\Exception $e) {
134 Analog::log(
135 'Unable to remove import model ' . $e->getMessage(),
136 Analog::ERROR
137 );
138 return false;
139 }
140 }
141
142 /**
143 * Store the model
144 *
145 * @param Db $zdb Database instance
146 *
147 * @return boolean
148 */
149 public function store($zdb)
150 {
151 try {
152 $values = array(
153 self::PK => $this->id,
154 'model_fields' => serialize($this->fields)
155 );
156
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;
162
163 $insert = $zdb->insert(self::TABLE);
164 $insert->values($values);
165 $results = $zdb->execute($insert);
166
167 if ($results->count() > 0) {
168 return true;
169 } else {
170 throw new \Exception(
171 'An error occurred inserting new import model!'
172 );
173 }
174 } else {
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);
180 return true;
181 }
182 } catch (\Exception $e) {
183 Analog::log(
184 'Something went wrong storing import model :\'( | ' .
185 $e->getMessage() . "\n" . $e->getTraceAsString(),
186 Analog::ERROR
187 );
188 return false;
189 }
190 }
191
192 /**
193 * Get fields
194 *
195 * @return array
196 */
197 public function getFields()
198 {
199 return $this->fields;
200 }
201
202 /**
203 * Get creation date
204 *
205 * @param boolean $formatted Return date formatted, raw if false
206 *
207 * @return string
208 */
209 public function getCreationDate($formatted = true)
210 {
211 if ($formatted === true) {
212 $date = new \DateTime($this->creation_date);
213 return $date->format(__("Y-m-d"));
214 } else {
215 return $this->creation_date;
216 }
217 }
218
219 /**
220 * Set fields
221 *
222 * @param array $fields Fields list
223 *
224 * @return void
225 */
226 public function setFields($fields)
227 {
228 $this->fields = $fields;
229 return $this;
230 }
231 }