]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Repository/PaymentTypes.php
1f620dac6bdb5a15da2c0220c3d56d7108ec9a9b
[galette.git] / galette / lib / Galette / Repository / PaymentTypes.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Payment types
7 *
8 * PHP version 5
9 *
10 * Copyright © 2018 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 Repository
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2018 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.9.2dev - 2018-07-23
35 */
36
37 namespace Galette\Repository;
38
39 use Analog\Analog;
40 use Galette\Entity\PaymentType;
41
42 /**
43 * Payment types
44 *
45 * @category Repository
46 * @name PaymentTypes
47 * @package Galette
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2018 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.9.2dev - 2018-07-23
53 */
54 class PaymentTypes extends Repository
55 {
56
57 /**
58 * Get payments types
59 *
60 * @return PaymentType[]
61 */
62 public static function getAll()
63 {
64 global $zdb, $preferences, $login;
65 $ptypes = new self($zdb, $preferences, $login);
66 return $ptypes->getList();
67 }
68
69 /**
70 * Get list
71 *
72 * @return PaymentType[]
73 */
74 public function getList()
75 {
76 try {
77 $select = $this->zdb->select(PaymentType::TABLE, 'a');
78 $select->order(PaymentType::PK);
79
80 $types = array();
81 $results = $this->zdb->execute($select);
82 foreach ($results as $row) {
83 $types[$row->type_id] = new PaymentType($this->zdb, $row);
84 }
85 return $types;
86 } catch (\Exception $e) {
87 Analog::log(
88 'Cannot list payment types | ' . $e->getMessage(),
89 Analog::WARNING
90 );
91 }
92 }
93
94 /**
95 * Add default payment types in database
96 *
97 * @param boolean $check_first Check first if it seem initialized
98 *
99 * @return boolean
100 */
101 public function installInit($check_first = true)
102 {
103 try {
104 $ent = $this->entity;
105 //first of all, let's check if data seem to have already
106 //been initialized
107 $proceed = false;
108 if ($check_first === true) {
109 $select = $this->zdb->select(PaymentType::TABLE);
110 $select->columns(
111 array(
112 'counter' => new Expression('COUNT(' . $ent::PK . ')')
113 )
114 );
115
116 $results = $this->zdb->execute($select);
117 $result = $results->current();
118 $count = $result->counter;
119 if ($count == 0) {
120 //if we got no values in texts table, let's proceed
121 $proceed = true;
122 } else {
123 if ($count < count($this->defaults)) {
124 return $this->checkUpdate();
125 }
126 return false;
127 }
128 } else {
129 $proceed = true;
130 }
131
132 if ($proceed === true) {
133 $this->zdb->connection->beginTransaction();
134
135 //first, we drop all values
136 $delete = $this->zdb->delete($ent::TABLE);
137 $this->zdb->execute($delete);
138 $this->insert($ent::TABLE, $this->defaults);
139
140 $this->zdb->connection->commit();
141 return true;
142 }
143 } catch (\Exception $e) {
144 if ($this->zdb->connection->inTransaction()) {
145 $this->zdb->connection->rollBack();
146 }
147 throw $e;
148 }
149 }
150
151 /**
152 * Checks for missing payment types in the database
153 *
154 * @return boolean
155 */
156 protected function checkUpdate()
157 {
158 try {
159 $ent = $this->entity;
160 $select = $this->zdb->select($ent::TABLE);
161 $list = $this->zdb->execute($select);
162 $list->buffer();
163
164 $missing = array();
165 foreach ($this->defaults as $key => $value) {
166 $exists = false;
167 foreach ($list as $type) {
168 if ($type->type_id == $key) {
169 $exists = true;
170 break;
171 }
172 }
173
174 if ($exists === false) {
175 //model does not exists in database, insert it.
176 $missing[$key] = $value;
177 }
178 }
179
180 if (count($missing) >0) {
181 $this->zdb->connection->beginTransaction();
182 $this->insert($ent::TABLE, $missing);
183 Analog::log(
184 'Missing texts were successfully stored into database.',
185 Analog::INFO
186 );
187 $this->zdb->connection->commit();
188 return true;
189 }
190 } catch (\Exception $e) {
191 if ($this->zdb->connection->inTransaction()) {
192 $this->zdb->connection->rollBack();
193 }
194 throw $e;
195 }
196 }
197
198 /**
199 * Insert values in database
200 *
201 * @param string $table Table name
202 * @param array $values Values to insert
203 *
204 * @return void
205 */
206 private function insert($table, $values)
207 {
208 $insert = $this->zdb->insert($table);
209 $insert->values(
210 array(
211 'type_id' => ':type_id',
212 'type_name' => ':type_name'
213 )
214 );
215 $stmt = $this->zdb->sql->prepareStatementForSqlObject($insert);
216
217 foreach ($values as $k => $v) {
218 $value = [
219 ':type_id' => $k,
220 ':type_name' => $v
221 ];
222 $stmt->execute($value);
223 }
224 }
225
226 /**
227 * Get defaults values
228 *
229 * @return array
230 */
231 protected function loadDefaults()
232 {
233 if (!count($this->defaults)) {
234 $paytype = new PaymentType($this->zdb);
235 $this->defaults = $paytype->getSystemTypes(false);
236 }
237 return parent::loadDefaults();
238 }
239 }