]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/Title.php
9277ca62ad2b03b1607218a2fe6e539c66e21247
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.4dev - 2013-01-27
38 namespace Galette\Entity
;
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
57 const TABLE
= 'titles';
58 const PK
= 'id_title';
71 * @param mixed $args Arguments
73 public function __construct($args = null)
77 } elseif ($args !== null && is_object($args)) {
78 $this->loadFromRs($args);
83 * Load a title from its identifier
85 * @param int $id Identifier
89 private function load($id)
93 $select = $zdb->select(self
::TABLE
);
94 $select->limit(1)->where(self
::PK
. ' = ' . $id);
96 $results = $zdb->execute($select);
97 $res = $results->current();
100 $this->short
= $res->short_label
;
101 $this->long
= $res->long_label
;
102 } catch (\Exception
$e) {
104 'An error occurred loading title #' . $id . "Message:\n" .
112 * Load title from a db ResultSet
114 * @param ResultSet $rs ResultSet
118 private function loadFromRs($rs)
121 $this->id
= $rs->$pk;
122 $this->short
= $rs->short_label
;
123 if ($rs->long_label
=== 'NULL') {
127 $this->long
= $rs->long_label
;
132 * Store title in database
134 * @param Db $zdb Database instance
138 public function store($zdb)
141 'short_label' => $this->short
,
142 'long_label' => $this->long
145 if ($this->id
!== null && $this->id
> 0) {
146 $update = $zdb->update(self
::TABLE
);
147 $update->set($data)->where(
148 self
::PK
. '=' . $this->id
150 $zdb->execute($update);
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
);
160 $this->id
= (int)$zdb->driver
->getLastGeneratedValue(
161 PREFIX_DB
. self
::TABLE
. '_id_seq'
165 } catch (\Exception
$e) {
167 'An error occurred storing title: ' . $e->getMessage() .
168 "\n" . print_r($data, true),
176 * Remove current title
178 * @param Db $zdb Database instance
182 public function remove($zdb)
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!"));
190 $delete = $zdb->delete(self
::TABLE
);
192 self
::PK
. ' = ' . $id
194 $zdb->execute($delete);
196 'Title #' . $id . ' (' . $this->short
197 . ') deleted successfully.',
201 } catch (\RuntimeException
$re) {
203 } catch (\Exception
$e) {
205 'Unable to delete title ' . $id . ' | ' . $e->getMessage(),
215 * @param string $name Property name
219 public function __get($name)
230 && ($this->long
== null ||
trim($this->long
) === '')
239 if ($name === 'tshort') {
242 if ($this->long
!== null && trim($this->long
) !== '') {
245 //switch back to short version if long does not exists
249 if (isset($lang) && isset($lang[$this->$rname])) {
250 return _T($this->$rname);
252 return $this->$rname;
257 'Unable to get Title property ' . $name,
267 * @param string $name Property name
268 * @param mixed $value Property value
272 public function __set($name, $value)
277 if (trim($value) === '') {
279 'Trying to set empty value for title' . $name,
283 $this->$name = $value;
288 'Unable to set property ' . $name,