]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/Title.php
3218522f0947e7e95223b0e178abae090ad6ba17
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2013-2021 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-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
37 namespace Galette\Entity
;
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
57 public const TABLE
= 'titles';
58 public const PK
= 'id_title';
66 public const MISS
= 3;
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 (Throwable
$e) {
104 'An error occurred loading title #' . $id . "Message:\n" .
113 * Load title from a db ResultSet
115 * @param ResultSet $rs ResultSet
119 private function loadFromRs($rs)
122 $this->id
= $rs->$pk;
123 $this->short
= $rs->short_label
;
124 if ($rs->long_label
=== 'NULL') {
128 $this->long
= $rs->long_label
;
133 * Store title in database
135 * @param Db $zdb Database instance
139 public function store($zdb)
142 'short_label' => $this->short
,
143 'long_label' => $this->long
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);
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
= $zdb->getLastGeneratedValue($this);
162 } catch (Throwable
$e) {
164 'An error occurred storing title: ' . $e->getMessage() .
165 "\n" . print_r($data, true),
173 * Remove current title
175 * @param Db $zdb Database instance
179 public function remove($zdb)
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!"));
187 $delete = $zdb->delete(self
::TABLE
);
188 $delete->where([self
::PK
=> $id]);
189 $zdb->execute($delete);
191 'Title #' . $id . ' (' . $this->short
192 . ') deleted successfully.',
196 } catch (\RuntimeException
$re) {
198 } catch (Throwable
$e) {
200 'Unable to delete title ' . $id . ' | ' . $e->getMessage(),
210 * @param string $name Property name
214 public function __get($name)
226 && ($this->long
== null ||
trim($this->long
) === '')
235 if ($name === 'tshort') {
238 if ($this->long
!== null && trim($this->long
) !== '') {
241 //switch back to short version if long does not exists
245 if (isset($lang) && isset($lang[$this->$rname])) {
246 return _T($this->$rname);
248 return $this->$rname;
253 'Unable to get Title property ' . $name,
263 * @param string $name Property name
264 * @param mixed $value Property value
268 public function __set($name, $value)
273 if (trim($value) === '') {
275 'Trying to set empty value for title' . $name,
279 $this->$name = $value;
284 'Unable to set property ' . $name,