]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/Title.php
3218522f0947e7e95223b0e178abae090ad6ba17
[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-2021 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-2021 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 Throwable;
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-2021 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 public const TABLE = 'titles';
58 public const PK = 'id_title';
59
60 private $id;
61 private $short;
62 private $long;
63
64 public const MR = 1;
65 public const MRS = 2;
66 public 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 (Throwable $e) {
103 Analog::log(
104 'An error occurred loading title #' . $id . "Message:\n" .
105 $e->getMessage(),
106 Analog::ERROR
107 );
108 throw $e;
109 }
110 }
111
112 /**
113 * Load title from a db ResultSet
114 *
115 * @param ResultSet $rs ResultSet
116 *
117 * @return void
118 */
119 private function loadFromRs($rs)
120 {
121 $pk = self::PK;
122 $this->id = $rs->$pk;
123 $this->short = $rs->short_label;
124 if ($rs->long_label === 'NULL') {
125 //mysql's null...
126 $this->long = null;
127 } else {
128 $this->long = $rs->long_label;
129 }
130 }
131
132 /**
133 * Store title in database
134 *
135 * @param Db $zdb Database instance
136 *
137 * @return boolean
138 */
139 public function store($zdb)
140 {
141 $data = array(
142 'short_label' => $this->short,
143 'long_label' => $this->long
144 );
145 try {
146 if ($this->id !== null && $this->id > 0) {
147 $update = $zdb->update(self::TABLE);
148 $update->set($data)->where([self::PK => $this->id]);
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 = $zdb->getLastGeneratedValue($this);
160 }
161 return true;
162 } catch (Throwable $e) {
163 Analog::log(
164 'An error occurred storing title: ' . $e->getMessage() .
165 "\n" . print_r($data, true),
166 Analog::ERROR
167 );
168 throw $e;
169 }
170 }
171
172 /**
173 * Remove current title
174 *
175 * @param Db $zdb Database instance
176 *
177 * @return boolean
178 */
179 public function remove($zdb)
180 {
181 $id = (int)$this->id;
182 if ($id === self::MR || $id === self::MRS) {
183 throw new \RuntimeException(_T("You cannot delete Mr. or Mrs. titles!"));
184 }
185
186 try {
187 $delete = $zdb->delete(self::TABLE);
188 $delete->where([self::PK => $id]);
189 $zdb->execute($delete);
190 Analog::log(
191 'Title #' . $id . ' (' . $this->short
192 . ') deleted successfully.',
193 Analog::INFO
194 );
195 return true;
196 } catch (\RuntimeException $re) {
197 throw $re;
198 } catch (Throwable $e) {
199 Analog::log(
200 'Unable to delete title ' . $id . ' | ' . $e->getMessage(),
201 Analog::ERROR
202 );
203 throw $e;
204 }
205 }
206
207 /**
208 * Getter
209 *
210 * @param string $name Property name
211 *
212 * @return mixed
213 */
214 public function __get($name)
215 {
216 global $lang;
217
218 switch ($name) {
219 case 'id':
220 return $this->$name;
221 break;
222 case 'short':
223 case 'long':
224 if (
225 $name === 'long'
226 && ($this->long == null || trim($this->long) === '')
227 ) {
228 $name = 'short';
229 }
230 return $this->$name;
231 break;
232 case 'tshort':
233 case 'tlong':
234 $rname = null;
235 if ($name === 'tshort') {
236 $rname = 'short';
237 } else {
238 if ($this->long !== null && trim($this->long) !== '') {
239 $rname = 'long';
240 } else {
241 //switch back to short version if long does not exists
242 $rname = 'short';
243 }
244 }
245 if (isset($lang) && isset($lang[$this->$rname])) {
246 return _T($this->$rname);
247 } else {
248 return $this->$rname;
249 }
250 break;
251 default:
252 Analog::log(
253 'Unable to get Title property ' . $name,
254 Analog::WARNING
255 );
256 break;
257 }
258 }
259
260 /**
261 * Setter
262 *
263 * @param string $name Property name
264 * @param mixed $value Property value
265 *
266 * @return void
267 */
268 public function __set($name, $value)
269 {
270 switch ($name) {
271 case 'short':
272 case 'long':
273 if (trim($value) === '') {
274 Analog::log(
275 'Trying to set empty value for title' . $name,
276 Analog::WARNING
277 );
278 } else {
279 $this->$name = $value;
280 }
281 break;
282 default:
283 Analog::log(
284 'Unable to set property ' . $name,
285 Analog::WARNING
286 );
287 break;
288 }
289 }
290 }