3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2013-2020 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-2020 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.5dev - 2013-02-19
37 namespace Galette\Entity
;
41 use Galette\Core\Preferences
;
42 use Galette\Features\Replacements
;
43 use Galette\Repository\PdfModels
;
45 use Laminas\Db\Sql\Expression
;
53 * @author Johan Cwiklinski <johan@x-tnd.be>
54 * @copyright 2013-2020 The Galette Team
55 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
56 * @link http://galette.tuxfamily.org
57 * @since Available since 0.7.5dev - 2013-02-19
59 * @property integer $id
60 * @property string $name
61 * @property integer $type
62 * @property string $header
63 * @property string $footer
64 * @property string $title
65 * @property string $subtitle
66 * @property string $body
67 * @property string $styles
68 * @property PdfMain $parent
71 abstract class PdfModel
75 public const TABLE
= 'pdfmodels';
76 public const PK
= 'model_id';
78 public const MAIN_MODEL
= 1;
79 public const INVOICE_MODEL
= 2;
80 public const RECEIPT_MODEL
= 3;
81 public const ADHESION_FORM_MODEL
= 4;
97 * @param Db $zdb Database instance
98 * @param Preferences $preferences Galette preferences
99 * @param int $type Model type
100 * @param mixed $args Arguments
102 public function __construct(Db
$zdb, Preferences
$preferences, $type, $args = null)
104 global $container, $login;
105 $this->router
= $container->get('router');
106 $this->preferences
= $preferences;
114 } elseif ($args !== null && is_object($args)) {
115 $this->loadFromRs($args);
120 $this->setPatterns($this->getMainPatterns());
125 * Load a Model from its identifier
127 * @param int $id Identifier
128 * @param boolean $init Init data if required model is missing
132 protected function load($id, $init = true)
137 $select = $this->zdb
->select(self
::TABLE
);
139 ->where(self
::PK
. ' = ' . $id);
141 $results = $this->zdb
->execute($select);
143 $count = $results->count();
145 if ($init === true) {
146 $models = new PdfModels($this->zdb
, $this->preferences
, $login);
147 $models->installInit();
148 $this->load($id, false);
150 throw new \
RuntimeException('Model not found!');
153 $this->loadFromRs($results->current());
155 } catch (Throwable
$e) {
157 'An error occurred loading model #' . $id . "Message:\n" .
166 * Load model from a db ResultSet
168 * @param ResultSet $rs ResultSet
172 protected function loadFromRs($rs)
175 $this->id
= (int)$rs->$pk;
177 $callback = function ($matches) {
178 return _T($matches[1]);
180 $this->name
= preg_replace_callback(
181 '/_T\("([^\"]+)"\)/',
186 $this->title
= $rs->model_title
;
187 $this->subtitle
= $rs->model_subtitle
;
188 $this->header
= $rs->model_header
;
189 $this->footer
= $rs->model_footer
;
190 $this->body
= $rs->model_body
;
191 $this->styles
.= $rs->model_styles
;
193 if ($this->id
> self
::MAIN_MODEL
) {
194 //FIXME: for now, parent will always be a PdfMain
195 $this->parent
= new PdfMain(
198 (int)$rs->model_parent
204 * Store model in database
208 public function store()
210 $title = $this->title
;
211 if (trim($title) === '') {
212 $title = new Expression('NULL');
215 $subtitle = $this->subtitle
;
216 if (trim($subtitle) === '') {
217 $subtitle = new Expression('NULL');
221 'model_header' => $this->header
,
222 'model_footer' => $this->footer
,
223 'model_type' => $this->type
,
224 'model_title' => $title,
225 'model_subtitle' => $subtitle,
226 'model_body' => $this->body
,
227 'model_styles' => $this->styles
231 if ($this->id
!== null) {
232 $update = $this->zdb
->update(self
::TABLE
);
233 $update->set($data)->where(
234 self
::PK
. '=' . $this->id
236 $this->zdb
->execute($update);
238 $data['model_name'] = $this->name
;
239 $insert = $this->zdb
->insert(self
::TABLE
);
240 $insert->values($data);
241 $add = $this->zdb
->execute($insert);
242 if (!($add->count() > 0)) {
243 Analog
::log('Not stored!', Analog
::ERROR
);
248 } catch (Throwable
$e) {
250 'An error occurred storing model: ' . $e->getMessage() .
251 "\n" . print_r($data, true),
259 * Get object class for specified type
261 * @param int $type Type
265 public static function getTypeClass(int $type)
269 case self
::INVOICE_MODEL
:
270 $class = 'PdfInvoice';
272 case self
::RECEIPT_MODEL
:
273 $class = 'PdfReceipt';
275 case self
::ADHESION_FORM_MODEL
:
276 $class = 'PdfAdhesionFormModel';
282 $class = 'Galette\\Entity\\' . $class;
289 * @param string $value The value
290 * @param int $chars Length
291 * @param string $field Field name
292 * @param boolean $empty Can value be empty
296 protected function checkChars($value, $chars, $field, $empty = false)
298 if ($value !== null && trim($value) !== '') {
299 if (mb_strlen($value) > $chars) {
300 throw new \
LengthException(
302 array('%field', '%chars'),
303 array($field, $chars),
304 _T("%field should be less than %chars characters long.")
309 if ($empty === false) {
310 throw new \
UnexpectedValueException(
314 _T("%field should not be empty!")
324 * @param string $name Property name
328 public function __get($name)
349 //get header and footer from parent if not defined in current model
351 $this->id
> self
::MAIN_MODEL
352 && $this->parent
!== null
354 $value = $this->parent
->styles
;
357 $value .= $this->styles
;
365 $pname = substr($name, 1);
366 $prop_value = $this->$pname ??
'';
368 //get header and footer from parent if not defined in current model
370 $this->id
> self
::MAIN_MODEL
371 && $this->parent
!== null
372 && ($pname === 'footer'
373 ||
$pname === 'header')
374 && trim($prop_value) === ''
376 $prop_value = $this->parent
->$pname;
379 $value = $this->proceedReplacements($prop_value);
384 'Unable to get PdfModel property ' . $name,
394 * @param string $name Property name
395 * @param mixed $value Property value
399 public function __set($name, $value)
404 $value === self
::MAIN_MODEL
405 ||
$value === self
::INVOICE_MODEL
406 ||
$value === self
::RECEIPT_MODEL
407 ||
$value === self
::ADHESION_FORM_MODEL
409 $this->$name = $value;
411 throw new \
UnexpectedValueException(
415 _T("Unknown type %type!")
422 $this->checkChars($value, 50, _T("Name"));
423 $this->$name = $value;
424 } catch (Throwable
$e) {
430 if ($name == 'title') {
431 $field = _T("Title");
433 $field = _T("Subtitle");
436 $this->checkChars($value, 100, $field, true);
437 $this->$name = $value;
438 } catch (Throwable
$e) {
445 if ($value === null ||
trim($value) === '') {
446 if ($name !== 'body' && get_class($this) === 'PdfMain') {
447 throw new \
UnexpectedValueException(
448 _T("header and footer should not be empty!")
450 } elseif ($name === 'body' && get_class($this) !== 'PdfMain') {
451 throw new \
UnexpectedValueException(
452 _T("body should not be empty!")
457 $this->$name = $value;
460 $this->styles
= $value;
464 'Unable to set PdfModel property ' . $name,