]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/FieldsCategories.php
50a127e597b50e5514c70e5d9a0ed91dfba98579
[galette.git] / galette / lib / Galette / Entity / FieldsCategories.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Fields categories handling
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-28
36 */
37
38 namespace Galette\Entity;
39
40 use Analog\Analog;
41 use Galette\Core\Db;
42
43 /**
44 * Fields categories class for galette
45 *
46 * @category Entity
47 * @name FieldsCategories
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-28
54 */
55
56 class FieldsCategories
57 {
58 const TABLE = 'fields_categories';
59 const PK = 'id_field_category';
60
61 private $defaults;
62
63 private $zdb;
64
65 const ADH_CATEGORY_IDENTITY = 1;
66 const ADH_CATEGORY_GALETTE = 2;
67 const ADH_CATEGORY_CONTACT = 3;
68
69 /**
70 * Default constructor
71 *
72 * @param Db $zdb Database
73 * @param array $defaults default values
74 */
75 public function __construct(Db $zdb, $defaults)
76 {
77 $this->zdb = $zdb;
78 $this->defaults = $defaults;
79 }
80
81 /**
82 * Get list of categories
83 *
84 * @param Db $zdb Database
85 *
86 * @return array
87 */
88 public static function getList($zdb)
89 {
90 try {
91 $select = $zdb->select(self::TABLE);
92 $select->order('position');
93
94 $categories = [];
95 $results = $zdb->execute($select);
96 foreach ($results as $result) {
97 $categories[] = $result;
98 }
99 return $categories;
100 } catch (\Exception $e) {
101 Analog::log(
102 '[' . static::class . '] Cannot get fields categories list | ' .
103 $e->getMessage(),
104 Analog::WARNING
105 );
106 return false;
107 }
108 }
109
110 /**
111 * Store the categories
112 *
113 * @param Db $zdb Database
114 * @param array $categories Categories
115 *
116 * @return boolean
117 */
118 public static function setCategories(Db $zdb, $categories)
119 {
120 try {
121 $zdb->connection->beginTransaction();
122
123 $update = $zdb->update(self::TABLE);
124 $update->set(
125 array(
126 'position' => ':position'
127 )
128 )->where(
129 array(
130 self::PK => ':pk'
131 )
132 );
133 $stmt = $zdb->sql->prepareStatementForSqlObject($update);
134
135 foreach ($categories as $k => $v) {
136 $params = array(
137 'position' => $k,
138 'where1' => $v
139 );
140 $stmt->execute($params);
141 }
142 $zdb->connection->commit();
143 } catch (\Exception $e) {
144 $zdb->connection->rollBack();
145 throw $e;
146 }
147 }
148
149 /**
150 * Set default fields categories at install time
151 *
152 *
153 * @return boolean|Exception
154 */
155 public function installInit()
156 {
157 try {
158 //first, we drop all values
159 $delete = $this->zdb->delete(self::TABLE);
160 $this->zdb->execute($delete);
161
162 $insert = $this->zdb->insert(self::TABLE);
163 $insert->values(
164 array(
165 self::PK => ':id',
166 'table_name' => ':table_name',
167 'category' => ':category',
168 'position' => ':position'
169 )
170 );
171 $stmt = $this->zdb->sql->prepareStatementForSqlObject($insert);
172
173 foreach ($this->defaults as $d) {
174 $stmt->execute(
175 array(
176 self::PK => $d['id'],
177 'table_name' => $d['table_name'],
178 'category' => $d['category'],
179 'position' => $d['position']
180 )
181 );
182 }
183
184 Analog::log(
185 'Default fields configurations were successfully stored.',
186 Analog::INFO
187 );
188 return true;
189 } catch (\Exception $e) {
190 Analog::log(
191 'Unable to initialize default fields configuration.' .
192 $e->getMessage(),
193 Analog::WARNING
194 );
195 return $e;
196 }
197 }
198 }