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