]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Repository/Titles.php
Handle sequence on PostgreSQL for titles; closes #1374 refs #1158
[galette.git] / galette / lib / Galette / Repository / Titles.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Titles repository management
7 *
8 * PHP version 5
9 *
10 * Copyright © 2009-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 2009-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 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7dev - 2009-03-04
36 */
37
38 namespace Galette\Repository;
39
40 use Galette\Entity\Title;
41 use Analog\Analog;
42
43 /**
44 * Titles repository management
45 *
46 * @category Entity
47 * @name Titles
48 * @package Galette
49 * @author Johan Cwiklinski <johan@x-tnd.be>
50 * @copyright 2009-2014 The Galette Team
51 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
52 * @link http://galette.tuxfamily.org
53 * @since Available since 0.7dev - 2009-03-04
54 */
55
56 class Titles
57 {
58 const TABLE = 'titles';
59 const PK = 'id_title';
60
61 const MR = 1;
62 const MRS = 2;
63 const MISS = 3;
64
65 private static $defaults = array(
66 array(
67 'id_title' => 1,
68 'short_label' => 'Mr.',
69 'long_label' => null
70 ),
71 array(
72 'id_title' => 2,
73 'short_label' => 'Mrs.',
74 'long_label' => null
75 )
76 );
77
78 /**
79 * Get the list of all titles as an array
80 *
81 * @param Db $zdb Database instance
82 *
83 * @return array
84 */
85 public static function getArrayList($zdb)
86 {
87 $otitles = self::getList($zdb);
88 $titles = array();
89 foreach ($otitles as $t) {
90 $titles[$t->id] = $t->short;
91 }
92 return $titles;
93 }
94
95 /**
96 * Get the list of all titles
97 *
98 * @param Db $zdb Database instance
99 *
100 * @return Title[]
101 */
102 public static function getList($zdb)
103 {
104 $select = $zdb->select(self::TABLE);
105 $select->order(self::PK);
106
107 $results = $zdb->execute($select);
108
109 $pols = array();
110 foreach ($results as $r) {
111 $pk = self::PK;
112 $pols[$r->$pk] = new Title($r);
113 }
114 return $pols;
115 }
116
117
118 /**
119 * Set default titles at install time
120 *
121 * @param Db $zdb Database instance
122 *
123 * @return boolean|Exception
124 */
125 public function installInit($zdb)
126 {
127 try {
128 //first, we drop all values
129 $delete = $zdb->delete(self::TABLE);
130 $zdb->execute($delete);
131
132 $insert = $zdb->insert(self::TABLE);
133 $insert->values(
134 array(
135 'id_title' => ':id',
136 'short_label' => ':short',
137 'long_label' => ':long'
138 )
139 );
140 $stmt = $zdb->sql->prepareStatementForSqlObject($insert);
141
142 $zdb->handleSequence(
143 self::TABLE,
144 count(self::$defaults)
145 );
146
147 foreach (self::$defaults as $d) {
148 $short = _T($d['short_label']);
149 $long = null;
150 if ($d['long_label'] !== null) {
151 $long = _T($d['long_label']);
152 }
153 $stmt->execute(
154 array(
155 'id_title' => $d['id_title'],
156 'short_label' => $short,
157 'long_label' => $long
158 )
159 );
160 }
161
162 Analog::log(
163 'Default titles were successfully stored into database.',
164 Analog::INFO
165 );
166 return true;
167 } catch (\Exception $e) {
168 Analog::log(
169 'Unable to initialize default titles. ' . $e->getMessage(),
170 Analog::WARNING
171 );
172 return $e;
173 }
174 }
175
176 /**
177 * Get the title
178 *
179 * @param integer $title The title id to retrieve
180 *
181 * @return translated title short version
182 */
183 public static function getTitle($title)
184 {
185 global $zdb;
186
187 $select = $zdb->select(self::TABLE);
188 $select->limit(1)
189 ->where(array(self::PK => $title));
190
191 $results = $zdb->execute($select);
192 $result = $results->current();
193 $res = $result->short_label;
194 return _T($res);
195 }
196 }