]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/Title.php
9277ca62ad2b03b1607218a2fe6e539c66e21247
[galette.git] / galette / lib / Galette / Entity / Title.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Title
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.4dev - 2013-01-27
36 */
37
38 namespace Galette\Entity;
39
40 use Analog\Analog;
41
42 /**
43 * Title
44 *
45 * @category Entity
46 * @name Title
47 * @package Galette
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2009-2014 The Galette Team
50 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
51 * @link http://galette.tuxfamily.org
52 * @since Available since 0.7dev - 2009-03-04
53 */
54
55 class Title
56 {
57 const TABLE = 'titles';
58 const PK = 'id_title';
59
60 private $id;
61 private $short;
62 private $long;
63
64 const MR = 1;
65 const MRS = 2;
66 const MISS = 3;
67
68 /**
69 * Main constructor
70 *
71 * @param mixed $args Arguments
72 */
73 public function __construct($args = null)
74 {
75 if (is_int($args)) {
76 $this->load($args);
77 } elseif ($args !== null && is_object($args)) {
78 $this->loadFromRs($args);
79 }
80 }
81
82 /**
83 * Load a title from its identifier
84 *
85 * @param int $id Identifier
86 *
87 * @return void
88 */
89 private function load($id)
90 {
91 global $zdb;
92 try {
93 $select = $zdb->select(self::TABLE);
94 $select->limit(1)->where(self::PK . ' = ' . $id);
95
96 $results = $zdb->execute($select);
97 $res = $results->current();
98
99 $this->id = $id;
100 $this->short = $res->short_label;
101 $this->long = $res->long_label;
102 } catch (\Exception $e) {
103 Analog::log(
104 'An error occurred loading title #' . $id . "Message:\n" .
105 $e->getMessage(),
106 Analog::ERROR
107 );
108 }
109 }
110
111 /**
112 * Load title from a db ResultSet
113 *
114 * @param ResultSet $rs ResultSet
115 *
116 * @return void
117 */
118 private function loadFromRs($rs)
119 {
120 $pk = self::PK;
121 $this->id = $rs->$pk;
122 $this->short = $rs->short_label;
123 if ($rs->long_label === 'NULL') {
124 //mysql's null...
125 $this->long = null;
126 } else {
127 $this->long = $rs->long_label;
128 }
129 }
130
131 /**
132 * Store title in database
133 *
134 * @param Db $zdb Database instance
135 *
136 * @return boolean
137 */
138 public function store($zdb)
139 {
140 $data = array(
141 'short_label' => $this->short,
142 'long_label' => $this->long
143 );
144 try {
145 if ($this->id !== null && $this->id > 0) {
146 $update = $zdb->update(self::TABLE);
147 $update->set($data)->where(
148 self::PK . '=' . $this->id
149 );
150 $zdb->execute($update);
151 } else {
152 $insert = $zdb->insert(self::TABLE);
153 $insert->values($data);
154 $add = $zdb->execute($insert);
155 if (!$add->count() > 0) {
156 Analog::log('Not stored!', Analog::ERROR);
157 return false;
158 }
159
160 $this->id = (int)$zdb->driver->getLastGeneratedValue(
161 PREFIX_DB . self::TABLE . '_id_seq'
162 );
163 }
164 return true;
165 } catch (\Exception $e) {
166 Analog::log(
167 'An error occurred storing title: ' . $e->getMessage() .
168 "\n" . print_r($data, true),
169 Analog::ERROR
170 );
171 return false;
172 }
173 }
174
175 /**
176 * Remove current title
177 *
178 * @param Db $zdb Database instance
179 *
180 * @return boolean
181 */
182 public function remove($zdb)
183 {
184 $id = (int)$this->id;
185 if ($id === self::MR || $id === self::MRS) {
186 throw new \RuntimeException(_T("You cannot delete Mr. or Mrs. titles!"));
187 }
188
189 try {
190 $delete = $zdb->delete(self::TABLE);
191 $delete->where(
192 self::PK . ' = ' . $id
193 );
194 $zdb->execute($delete);
195 Analog::log(
196 'Title #' . $id . ' (' . $this->short
197 . ') deleted successfully.',
198 Analog::INFO
199 );
200 return true;
201 } catch (\RuntimeException $re) {
202 throw $re;
203 } catch (\Exception $e) {
204 Analog::log(
205 'Unable to delete title ' . $id . ' | ' . $e->getMessage(),
206 Analog::ERROR
207 );
208 throw $e;
209 }
210 }
211
212 /**
213 * Getter
214 *
215 * @param string $name Property name
216 *
217 * @return mixed
218 */
219 public function __get($name)
220 {
221 global $lang;
222
223 switch ($name) {
224 case 'id':
225 return $this->$name;
226 break;
227 case 'short':
228 case 'long':
229 if ($name === 'long'
230 && ($this->long == null || trim($this->long) === '')
231 ) {
232 $name = 'short';
233 }
234 return $this->$name;
235 break;
236 case 'tshort':
237 case 'tlong':
238 $rname = null;
239 if ($name === 'tshort') {
240 $rname = 'short';
241 } else {
242 if ($this->long !== null && trim($this->long) !== '') {
243 $rname = 'long';
244 } else {
245 //switch back to short version if long does not exists
246 $rname = 'short';
247 }
248 }
249 if (isset($lang) && isset($lang[$this->$rname])) {
250 return _T($this->$rname);
251 } else {
252 return $this->$rname;
253 }
254 break;
255 default:
256 Analog::log(
257 'Unable to get Title property ' . $name,
258 Analog::WARNING
259 );
260 break;
261 }
262 }
263
264 /**
265 * Setter
266 *
267 * @param string $name Property name
268 * @param mixed $value Property value
269 *
270 * @return void
271 */
272 public function __set($name, $value)
273 {
274 switch ($name) {
275 case 'short':
276 case 'long':
277 if (trim($value) === '') {
278 Analog::log(
279 'Trying to set empty value for title' . $name,
280 Analog::WARNING
281 );
282 } else {
283 $this->$name = $value;
284 }
285 break;
286 default:
287 Analog::log(
288 'Unable to set property ' . $name,
289 Analog::WARNING
290 );
291 break;
292 }
293 }
294 }