]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Features/EntityHelper.php
Add scheduled payments feature
[galette.git] / galette / lib / Galette / Features / EntityHelper.php
1 <?php
2
3 /**
4 * Copyright © 2003-2024 The Galette Team
5 *
6 * This file is part of Galette (https://galette.eu).
7 *
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.
12 *
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.
17 *
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/>.
20 */
21
22 namespace Galette\Features;
23
24 use DateTime;
25 use Galette\Entity\Adherent;
26 use Galette\Repository\DynamicFieldsSet;
27 use Throwable;
28 use Analog\Analog;
29 use Galette\DynamicFields\File;
30 use Galette\DynamicFields\Date;
31 use Galette\DynamicFields\Boolean;
32 use Galette\Entity\DynamicFieldsHandle;
33
34 /**
35 * Dynamics fields trait
36 *
37 * @author Johan Cwiklinski <johan@x-tnd.be>
38 */
39
40 trait EntityHelper
41 {
42 /**
43 * Fields configuration. Each field is an array and must reflect:
44 * array(
45 * (string)label,
46 * (string)propname
47 * )
48 *
49 * @var array<string, array<string, string>>
50 */
51 protected array $fields;
52
53 /** @var string[] */
54 //protected array $forbidden_fields = [];
55
56 /** @var string[] */
57 //protected array $virtual_fields = [];
58
59 /**
60 * Set fields, must populate $this->fields
61 *
62 * @return self
63 */
64 abstract protected function setFields(): self;
65
66 /**
67 * Get fields
68 *
69 * @return array<string, array<string, string>>
70 */
71 public function getFields(): array
72 {
73 return $this->fields;
74 }
75
76 /**
77 * Global isset method
78 * Required for twig to access properties via __get
79 *
80 * @param string $name name of the property we want to retrieve
81 *
82 * @return bool
83 */
84 public function __isset(string $name): bool
85 {
86 if (in_array($name, ($this->forbidden_fields ?? []))) {
87 return false;
88 }
89
90 $virtual_fields = [];
91 if (isset($this->virtual_fields)) {
92 $virtual_fields = $this->virtual_fields;
93 }
94 return (in_array($name, $virtual_fields) || property_exists($this, $name));
95 }
96
97 /**
98 * Get field label
99 *
100 * @param string $field Field name
101 * @param string $entry Array entry to use (defaults to "label")
102 *
103 * @return string
104 */
105 public function getFieldLabel(string $field, string $entry = 'label'): string
106 {
107 $label = $this->fields[$field][$entry] ?? $field;
108 //replace "&nbsp;"
109 $label = str_replace('&nbsp;', ' ', $label);
110 //remove trailing ':' and then trim
111 $label = trim(trim($label, ':'));
112 return $label;
113 }
114
115 /**
116 * Set a Date
117 *
118 * @param string $field Field to store date
119 * @param string $value Date to store
120 *
121 * @return self
122 */
123 protected function setDate(string $field, string $value): self
124 {
125 try {
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');
132 }
133 }
134 if (isset($this->fields[$field]['propname'])) {
135 $propname = $this->fields[$field]['propname'];
136 } else {
137 $propname = $field;
138 }
139 $this->$propname = $date->format('Y-m-d');
140 } catch (Throwable $e) {
141 Analog::log(
142 'Wrong date format. field: ' . $field .
143 ', value: ' . $field . ', expected fmt: ' .
144 __("Y-m-d") . ' | ' . $e->getMessage(),
145 Analog::INFO
146 );
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!'),
150 __("Y-m-d"),
151 $this->getFieldLabel($field)
152 );
153 }
154 return $this;
155 }
156
157 /**
158 * Get a date
159 *
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
163 *
164 * @return string|DateTime|null
165 */
166 public function getDate(string $field, bool $formatted = true, bool $translated = true): string|DateTime|null
167 {
168 if ($this->$field !== null && $this->$field != '') {
169 try {
170 $date = new \DateTime($this->$field);
171 if ($formatted === false) {
172 return $date;
173 }
174 if ($translated === false) {
175 return $date->format('Y-m-d');
176 }
177 return $date->format(__('Y-m-d'));
178 } catch (Throwable $e) {
179 //oops, we've got a bad date :/
180 Analog::log(
181 'Bad date (' . $this->$field . ') | ' .
182 $e->getMessage(),
183 Analog::INFO
184 );
185 return $this->$field;
186 }
187 }
188 return null;
189 }
190 }