4 * Copyright © 2003-2024 The Galette Team
6 * This file is part of Galette (https://galette.eu).
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
22 namespace Galette\Features
;
25 use Galette\Entity\Adherent
;
26 use Galette\Repository\DynamicFieldsSet
;
29 use Galette\DynamicFields\File
;
30 use Galette\DynamicFields\Date
;
31 use Galette\DynamicFields\Boolean
;
32 use Galette\Entity\DynamicFieldsHandle
;
35 * Dynamics fields trait
37 * @author Johan Cwiklinski <johan@x-tnd.be>
43 * Fields configuration. Each field is an array and must reflect:
49 * @var array<string, array<string, string>>
51 protected array $fields;
54 //protected array $forbidden_fields = [];
57 //protected array $virtual_fields = [];
60 * Set fields, must populate $this->fields
64 abstract protected function setFields(): self
;
69 * @return array<string, array<string, string>>
71 public function getFields(): array
78 * Required for twig to access properties via __get
80 * @param string $name name of the property we want to retrieve
84 public function __isset(string $name): bool
86 if (in_array($name, ($this->forbidden_fields ??
[]))) {
91 if (isset($this->virtual_fields
)) {
92 $virtual_fields = $this->virtual_fields
;
94 return (in_array($name, $virtual_fields) ||
property_exists($this, $name));
100 * @param string $field Field name
101 * @param string $entry Array entry to use (defaults to "label")
105 public function getFieldLabel(string $field, string $entry = 'label'): string
107 $label = $this->fields
[$field][$entry] ??
$field;
109 $label = str_replace(' ', ' ', $label);
110 //remove trailing ':' and then trim
111 $label = trim(trim($label, ':'));
118 * @param string $field Field to store date
119 * @param string $value Date to store
123 protected function setDate(string $field, string $value): self
126 $date = \DateTime
::createFromFormat(__("Y-m-d"), $value);
127 if ($date === false) {
128 //try with non localized date
129 $date = \DateTime
::createFromFormat("Y-m-d", $value);
130 if ($date === false) {
131 throw new \
Exception('Incorrect format');
134 if (isset($this->fields
[$field]['propname'])) {
135 $propname = $this->fields
[$field]['propname'];
139 $this->$propname = $date->format('Y-m-d');
140 } catch (Throwable
$e) {
142 'Wrong date format. field: ' . $field .
143 ', value: ' . $field . ', expected fmt: ' .
144 __("Y-m-d") . ' | ' . $e->getMessage(),
147 $this->errors
[] = sprintf(
148 //TRANS: %1$s is the date format, %2$s is the field name
149 _T('- Wrong date format (%1$s) for %2$s!'),
151 $this->getFieldLabel($field)
160 * @param string $field Field name to retrieve
161 * @param bool $formatted Get formatted date, or DateTime object
162 * @param bool $translated Get translated or db value
164 * @return string|DateTime|null
166 public function getDate(string $field, bool $formatted = true, bool $translated = true): string|DateTime|
null
168 if ($this->$field !== null && $this->$field != '') {
170 $date = new \
DateTime($this->$field);
171 if ($formatted === false) {
174 if ($translated === false) {
175 return $date->format('Y-m-d');
177 return $date->format(__('Y-m-d'));
178 } catch (Throwable
$e) {
179 //oops, we've got a bad date :/
181 'Bad date (' . $this->$field . ') | ' .
185 return $this->$field;