3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2018 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/>.
27 * @category Repository
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
37 namespace Galette\Repository
;
40 use Galette\Entity\PaymentType
;
45 * @category Repository
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
54 class PaymentTypes
extends Repository
60 * @return PaymentType[]
62 public static function getAll()
64 global $zdb, $preferences, $login;
65 $ptypes = new self($zdb, $preferences, $login);
66 return $ptypes->getList();
72 * @return PaymentType[]
74 public function getList()
77 $select = $this->zdb
->select(PaymentType
::TABLE
, 'a');
78 $select->order(PaymentType
::PK
);
81 $results = $this->zdb
->execute($select);
82 foreach ($results as $row) {
83 $types[$row->type_id
] = new PaymentType($this->zdb
, $row);
86 } catch (\Exception
$e) {
88 'Cannot list payment types | ' . $e->getMessage(),
95 * Add default payment types in database
97 * @param boolean $check_first Check first if it seem initialized
101 public function installInit($check_first = true)
104 $ent = $this->entity
;
105 //first of all, let's check if data seem to have already
108 if ($check_first === true) {
109 $select = $this->zdb
->select(PaymentType
::TABLE
);
112 'counter' => new Expression('COUNT(' . $ent::PK
. ')')
116 $results = $this->zdb
->execute($select);
117 $result = $results->current();
118 $count = $result->counter
;
120 //if we got no values in texts table, let's proceed
123 if ($count < count($this->defaults
)) {
124 return $this->checkUpdate();
132 if ($proceed === true) {
133 $this->zdb
->connection
->beginTransaction();
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
);
140 $this->zdb
->connection
->commit();
143 } catch (\Exception
$e) {
144 if ($this->zdb
->connection
->inTransaction()) {
145 $this->zdb
->connection
->rollBack();
152 * Checks for missing payment types in the database
156 protected function checkUpdate()
159 $ent = $this->entity
;
160 $select = $this->zdb
->select($ent::TABLE
);
161 $list = $this->zdb
->execute($select);
165 foreach ($this->defaults
as $key => $value) {
167 foreach ($list as $type) {
168 if ($type->type_id
== $key) {
174 if ($exists === false) {
175 //model does not exists in database, insert it.
176 $missing[$key] = $value;
180 if (count($missing) >0) {
181 $this->zdb
->connection
->beginTransaction();
182 $this->insert($ent::TABLE
, $missing);
184 'Missing texts were successfully stored into database.',
187 $this->zdb
->connection
->commit();
190 } catch (\Exception
$e) {
191 if ($this->zdb
->connection
->inTransaction()) {
192 $this->zdb
->connection
->rollBack();
199 * Insert values in database
201 * @param string $table Table name
202 * @param array $values Values to insert
206 private function insert($table, $values)
208 $insert = $this->zdb
->insert($table);
211 'type_id' => ':type_id',
212 'type_name' => ':type_name'
215 $stmt = $this->zdb
->sql
->prepareStatementForSqlObject($insert);
217 foreach ($values as $k => $v) {
222 $stmt->execute($value);
227 * Get defaults values
231 protected function loadDefaults()
233 if (!count($this->defaults
)) {
234 $paytype = new PaymentType($this->zdb
);
235 $this->defaults
= $paytype->getSystemTypes(false);
237 return parent
::loadDefaults();