]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/Title.php
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
33 * @link http://galette.tuxfamily.org
34 * @since Available since 0.7.4dev - 2013-01-27
37 namespace Galette\Entity
;
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
56 public const TABLE
= 'titles';
57 public const PK
= 'id_title';
65 public const MISS
= 3;
70 * @param mixed $args Arguments
72 public function __construct($args = null)
76 } elseif ($args !== null && is_object($args)) {
77 $this->loadFromRs($args);
82 * Load a title from its identifier
84 * @param int $id Identifier
88 private function load($id)
92 $select = $zdb->select(self
::TABLE
);
93 $select->limit(1)->where(self
::PK
. ' = ' . $id);
95 $results = $zdb->execute($select);
96 $res = $results->current();
99 $this->short
= $res->short_label
;
100 $this->long
= $res->long_label
;
101 } catch (\Exception
$e) {
103 'An error occurred loading title #' . $id . "Message:\n" .
111 * Load title from a db ResultSet
113 * @param ResultSet $rs ResultSet
117 private function loadFromRs($rs)
120 $this->id
= $rs->$pk;
121 $this->short
= $rs->short_label
;
122 if ($rs->long_label
=== 'NULL') {
126 $this->long
= $rs->long_label
;
131 * Store title in database
133 * @param Db $zdb Database instance
137 public function store($zdb)
140 'short_label' => $this->short
,
141 'long_label' => $this->long
144 if ($this->id
!== null && $this->id
> 0) {
145 $update = $zdb->update(self
::TABLE
);
146 $update->set($data)->where(
147 self
::PK
. '=' . $this->id
149 $zdb->execute($update);
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
);
159 $this->id
= (int)$zdb->driver
->getLastGeneratedValue(
160 PREFIX_DB
. self
::TABLE
. '_id_seq'
164 } catch (\Exception
$e) {
166 'An error occurred storing title: ' . $e->getMessage() .
167 "\n" . print_r($data, true),
175 * Remove current title
177 * @param Db $zdb Database instance
181 public function remove($zdb)
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!"));
189 $delete = $zdb->delete(self
::TABLE
);
191 self
::PK
. ' = ' . $id
193 $zdb->execute($delete);
195 'Title #' . $id . ' (' . $this->short
196 . ') deleted successfully.',
200 } catch (\RuntimeException
$re) {
202 } catch (\Exception
$e) {
204 'Unable to delete title ' . $id . ' | ' . $e->getMessage(),
214 * @param string $name Property name
218 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,