]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/DynamicsTrait.php
Few fixes, populate on set, more simple code
[galette.git] / galette / lib / Galette / Entity / DynamicsTrait.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Dynamics fields trait
7 *
8 * PHP version 5
9 *
10 * Copyright © 2017-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 Entity
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2017-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.9dev - 2017-05-26
35 */
36
37 namespace Galette\Entity;
38
39 use Analog\Analog;
40 use Galette\DynamicFields\File;
41 use Galette\DynamicFields\Date;
42
43 /**
44 * Dynamics fields trait
45 *
46 * @category Entity
47 * @name DynamicsTrait
48 * @package Galette
49 * @author Johan Cwiklinski <johan@x-tnd.be>
50 * @copyright 2017-2018 The Galette Team
51 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
52 * @link http://galette.tuxfamily.org
53 * @since Available since 0.9dev - 2017-05-26
54 */
55
56 trait DynamicsTrait
57 {
58 /** @var string */
59 protected $name_pattern = 'info_field_';
60
61 /** @var array */
62 protected $dynamics;
63
64 /**
65 * Load dynamic fields for member
66 *
67 * @return void
68 */
69 private function loadDynamicFields()
70 {
71 if (!property_exists($this, 'login')) {
72 global $login;
73 } else {
74 $login = $this->login;
75 }
76 $this->dynamics = new DynamicFieldsHandle($this->zdb, $login, $this);
77 }
78
79 /**
80 * Get dynamic fields
81 *
82 * @return array
83 */
84 public function getDynamicFields()
85 {
86 return $this->dynamics;
87 }
88
89 /**
90 * Extract posted values for dynamic fields
91 *
92 * @param array $post Posted values
93 *
94 * @return boolean
95 */
96 protected function dynamicsCheck($post)
97 {
98 if ($this->dynamics === null) {
99 Analog::log(
100 'Dynamics fields have not been loaded, cannot be checked. (from: ' . __METHOD__ . ')',
101 Analog::WARNING
102 );
103 $this->loadDynamicFields();
104 }
105
106 if ($post != null) {
107 $valid = true;
108 $fields = $this->dynamics->getFields();
109
110 foreach ($post as $key => $value) {
111 // if the field is enabled, check it
112 if (!isset($disabled[$key])) {
113 if (substr($key, 0, 11) != $this->name_pattern) {
114 continue;
115 }
116
117 list($field_id, $val_index) = explode('_', str_replace($this->name_pattern, '', $key));
118 if (!is_numeric($field_id) || !is_numeric($val_index)) {
119 continue;
120 }
121
122 if ($fields[$field_id]->isRequired() && (trim($value) === '' || $value == null)) {
123 $this->errors[] = str_replace(
124 '%field',
125 $fields[$field_id]->getName(),
126 _T('Missing required field %field')
127 );
128 } else {
129 if ($fields[$field_id] instanceof File) {
130 //delete checkbox
131 $filename = sprintf(
132 'member_%d_field_%d_value_%d',
133 $this->id,
134 $field_id,
135 $val_index
136 );
137 unlink(GALETTE_FILES_PATH . $filename);
138 $this->dynamics->setValue($this->id, $field_id, $val_index, '');
139 } else {
140 if ($fields[$field_id] instanceof Date && !empty(trim($value))) {
141 //check date format
142 try {
143 $d = \DateTime::createFromFormat(__("Y-m-d"), $value);
144 if ($d === false) {
145 //try with non localized date
146 $d = \DateTime::createFromFormat("Y-m-d", $value);
147 if ($d === false) {
148 throw new \Exception('Incorrect format');
149 }
150 }
151 } catch (\Exception $e) {
152 $valid = false;
153 Analog::log(
154 'Wrong date format. field: ' . $field_id .
155 ', value: ' . $value . ', expected fmt: ' .
156 __("Y-m-d") . ' | ' . $e->getMessage(),
157 Analog::INFO
158 );
159 $this->errors[] = str_replace(
160 array(
161 '%date_format',
162 '%field'
163 ),
164 array(
165 __("Y-m-d"),
166 $fields[$field_id]->getName()
167 ),
168 _T("- Wrong date format (%date_format) for %field!")
169 );
170 }
171 }
172 //actual field value
173 if ($value !== null && trim($value) !== '') {
174 $this->dynamics->setValue($this->id, $field_id, $val_index, $value);
175 } else {
176 $this->dynamics->unsetValue($this->id, $field_id, $val_index);
177 }
178 }
179 }
180 }
181 }
182
183 return $valid;
184 }
185 }
186
187 /**
188 * Stores dynamic fields
189 *
190 * @param boolean $transaction True if a transaction already exists
191 *
192 * @return boolean
193 */
194 protected function dynamicsStore($transaction = false)
195 {
196 if ($this->dynamics === null) {
197 Analog::log(
198 'Dynamics fields have not been loaded, cannot be stored. (from: ' . __METHOD__ . ')',
199 Analog::WARNING
200 );
201 $this->loadDynamicFields();
202 }
203 $return = $this->dynamics->storeValues($this->id, $transaction);
204 if (method_exists($this, 'updateModificationDate') && $this->dynamics->hasChanged()) {
205 $this->updateModificationDate();
206 }
207 return $return;
208 }
209
210 /**
211 * Store dynamic Files
212 *
213 * @param array $files Posted files
214 *
215 * @return void
216 */
217 protected function dynamicsFiles($files)
218 {
219 if ($this->dynamics === null) {
220 Analog::log(
221 'Dynamics fields have not been loaded, cannot be stored. (from: ' . __METHOD__ . ')',
222 Analog::WARNING
223 );
224 $this->loadDynamicFields();
225 }
226 $fields = $this->dynamics->getFields();
227 $store = false;
228
229 foreach ($files as $key => $file) {
230 // if the field is disabled, skip it
231 if (isset($disabled[$key])) {
232 continue;
233 }
234
235 if (substr($key, 0, 11) != $this->name_pattern) {
236 continue;
237 }
238
239 list($field_id, $val_index) = explode('_', str_replace($this->name_pattern, '', $key));
240 if (!is_numeric($field_id) || !is_numeric($val_index)) {
241 continue;
242 }
243
244 if (
245 $file['error'] == UPLOAD_ERR_NO_FILE
246 && $file['name'] == ''
247 && $file['tmp_name'] == ''
248 ) {
249 //not upload atempt.
250 continue;
251 } elseif ($file['error'] !== UPLOAD_ERR_OK) {
252 Analog::log("file upload error", Analog::ERROR);
253 continue;
254 }
255
256 $tmp_filename = $file['tmp_name'];
257 if ($tmp_filename == '') {
258 Analog::log("empty temporary filename", Analog::ERROR);
259 continue;
260 }
261
262 if (!is_uploaded_file($tmp_filename)) {
263 Analog::log("not an uploaded file", Analog::ERROR);
264 continue;
265 }
266
267 $max_size =
268 $fields[$field_id]->getSize() ?
269 $fields[$field_id]->getSize() * 1024 : File::DEFAULT_MAX_FILE_SIZE * 1024;
270 if ($file['size'] > $max_size) {
271 Analog::log(
272 "file too large: " . $file['size'] . " Ko, vs $max_size Ko allowed",
273 Analog::ERROR
274 );
275 $this->errors[] = preg_replace(
276 '|%d|',
277 $max_size,
278 _T("File is too big. Maximum allowed size is %dKo")
279 );
280 continue;
281 }
282
283 $new_filename = sprintf(
284 'member_%d_field_%d_value_%d',
285 $this->id,
286 $field_id,
287 $val_index
288 );
289 Analog::log("new file: $new_filename", Analog::DEBUG);
290
291 move_uploaded_file(
292 $tmp_filename,
293 GALETTE_FILES_PATH . $new_filename
294 );
295 $this->dynamics->setValue($this->id, $field_id, $val_index, $file['name']);
296 $store = true;
297 }
298
299 if ($store === true) {
300 $this->dynamicsStore();
301 }
302 }
303
304 /**
305 * Remove dynamic fields values
306 *
307 * @param boolean $transaction True if a transaction already exists
308 *
309 * @return boolean
310 */
311 protected function dynamicsRemove($transaction = false)
312 {
313 if ($this->dynamics === null) {
314 Analog::log(
315 'Dynamics fields have not been loaded, cannot be removed. (from: ' . __METHOD__ . ')',
316 Analog::WARNING
317 );
318 $this->loadDynamicFields();
319 }
320 $return = $this->dynamics->removeValues($this->id, $transaction);
321 return $return;
322 }
323
324
325
326 /**
327 * Get errors
328 *
329 * @return array
330 */
331 public function getErrors()
332 {
333 return $this->errors;
334 }
335
336 /**
337 * Validate data for dynamic fields
338 * Set valid data in current object, also resets errors list
339 *
340 * @param array $values Dynamic fields values
341 * @param string $prefix Prefix to replace, default to 'dynfield_'
342 *
343 * @return void
344 */
345 public function dynamicsValidate($values, $prefix = 'dynfield_')
346 {
347 $dfields = [];
348 foreach ($values as $key => $value) {
349 $dfields[str_replace($prefix, $this->name_pattern, $key)] = $value;
350 }
351 return $this->dynamicsCheck($dfields);
352 }
353 }