3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Dynamics fields trait
10 * Copyright © 2017-2021 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 2017-2021 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
37 namespace Galette\Entity
;
41 use Galette\DynamicFields\File
;
42 use Galette\DynamicFields\Date
;
43 use Galette\DynamicFields\Boolean
;
46 * Dynamics fields trait
51 * @author Johan Cwiklinski <johan@x-tnd.be>
52 * @copyright 2017-2021 The Galette Team
53 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
54 * @link http://galette.tuxfamily.org
55 * @since Available since 0.9dev - 2017-05-26
61 protected $name_pattern = 'info_field_';
63 /** @var DynamicFieldsHandle */
67 * Load dynamic fields for member
71 private function loadDynamicFields()
73 if (!property_exists($this, 'login')) {
76 $login = $this->login
;
78 $this->dynamics
= new DynamicFieldsHandle($this->zdb
, $login, $this);
86 public function getDynamicFields()
88 if (null === $this->dynamics
) {
89 $this->loadDynamicFields();
91 return $this->dynamics
;
95 * Extract posted values for dynamic fields
97 * @param array $post Posted values
98 * @param array $required Array of required fields
99 * @param array $disabled Array of disabled fields
103 protected function dynamicsCheck(array $post, array $required, array $disabled)
105 if ($this->dynamics
=== null) {
107 'Dynamics fields have not been loaded, cannot be checked. (from: ' . __METHOD__
. ')',
110 $this->loadDynamicFields();
115 $fields = $this->dynamics
->getFields();
117 $dynamic_fields = [];
119 foreach ($post as $key => $value) {
120 // if the field is enabled, and match patterns check it
121 if (isset($disabled[$key]) ||
substr($key, 0, 11) != $this->name_pattern
) {
125 list($field_id, $val_index) = explode('_', str_replace($this->name_pattern
, '', $key));
126 if (!is_numeric($field_id) ||
!is_numeric($val_index)) {
130 $dynamic_fields[$key] = [
132 'field_id' => $field_id,
133 'val_index' => $val_index
137 //some fields may be mising in posted values (checkboxes)
138 foreach ($fields as $field) {
139 $pattern = '/' . $this->name_pattern
. $field->getId() . '_(\d)/';
140 if ($field instanceof Boolean
&& !preg_grep($pattern, array_keys($dynamic_fields))) {
141 $dynamic_fields[$this->name_pattern
. $field->getId() . '_1'] = [
143 'field_id' => $field->getId(),
149 foreach ($dynamic_fields as $key => $dfield_values) {
150 $field_id = $dfield_values['field_id'];
151 $value = $dfield_values['value'];
152 $val_index = $dfield_values['val_index'];
154 if ($fields[$field_id]->isRequired() && (trim($value) === '' ||
$value == null)) {
155 $this->errors
[] = str_replace(
157 $fields[$field_id]->getName(),
158 _T('Missing required field %field')
161 if ($fields[$field_id] instanceof File
) {
164 'member_%d_field_%d_value_%d',
169 if (file_exists(GALETTE_FILES_PATH
. $filename)) {
170 unlink(GALETTE_FILES_PATH
. $filename);
172 $this->dynamics
->setValue($this->id
, $field_id, $val_index, '');
174 if ($fields[$field_id] instanceof Date
&& !empty(trim($value))) {
177 $d = \DateTime
::createFromFormat(__("Y-m-d"), $value);
179 //try with non localized date
180 $d = \DateTime
::createFromFormat("Y-m-d", $value);
182 throw new \
Exception('Incorrect format');
185 } catch (Throwable
$e) {
188 'Wrong date format. field: ' . $field_id .
189 ', value: ' . $value . ', expected fmt: ' .
190 __("Y-m-d") . ' | ' . $e->getMessage(),
193 $this->errors
[] = str_replace(
200 $fields[$field_id]->getName()
202 _T("- Wrong date format (%date_format) for %field!")
207 if ($value !== null && trim($value) !== '') {
208 $this->dynamics
->setValue($this->id
, $field_id, $val_index, $value);
210 $this->dynamics
->unsetValue($this->id
, $field_id, $val_index);
221 * Stores dynamic fields
223 * @param boolean $transaction True if a transaction already exists
227 protected function dynamicsStore($transaction = false)
229 if ($this->dynamics
=== null) {
231 'Dynamics fields have not been loaded, cannot be stored. (from: ' . __METHOD__
. ')',
234 $this->loadDynamicFields();
236 $return = $this->dynamics
->storeValues($this->id
, $transaction);
237 if (method_exists($this, 'updateModificationDate') && $this->dynamics
->hasChanged()) {
238 $this->updateModificationDate();
244 * Store dynamic Files
246 * @param array $files Posted files
250 protected function dynamicsFiles($files)
252 $this->loadDynamicFields();
253 $fields = $this->dynamics
->getFields();
256 foreach ($files as $key => $file) {
257 // if the field is disabled, skip it
258 if (isset($disabled[$key])) {
262 if (substr($key, 0, 11) != $this->name_pattern
) {
266 list($field_id, $val_index) = explode('_', str_replace($this->name_pattern
, '', $key));
267 if (!is_numeric($field_id) ||
!is_numeric($val_index)) {
272 $file['error'] == UPLOAD_ERR_NO_FILE
273 && $file['name'] == ''
274 && $file['tmp_name'] == ''
278 } elseif ($file['error'] !== UPLOAD_ERR_OK
) {
279 Analog
::log("file upload error", Analog
::ERROR
);
283 $tmp_filename = $file['tmp_name'];
284 if ($tmp_filename == '') {
285 Analog
::log("empty temporary filename", Analog
::ERROR
);
289 if (!is_uploaded_file($tmp_filename)) {
290 Analog
::log("not an uploaded file", Analog
::ERROR
);
295 $fields[$field_id]->getSize() ?
296 $fields[$field_id]->getSize() * 1024 : File
::DEFAULT_MAX_FILE_SIZE
* 1024;
297 if ($file['size'] > $max_size) {
299 "file too large: " . $file['size'] . " Ko, vs $max_size Ko allowed",
302 $this->errors
[] = preg_replace(
305 _T("File is too big. Maximum allowed size is %dKo")
310 $new_filename = sprintf(
311 'member_%d_field_%d_value_%d',
316 Analog
::log("new file: $new_filename", Analog
::DEBUG
);
320 GALETTE_FILES_PATH
. $new_filename
322 $this->dynamics
->setValue($this->id
, $field_id, $val_index, $file['name']);
326 if ($store === true) {
327 $this->dynamicsStore();
332 * Remove dynamic fields values
334 * @param boolean $transaction True if a transaction already exists
338 protected function dynamicsRemove($transaction = false)
340 if ($this->dynamics
=== null) {
342 'Dynamics fields have not been loaded, cannot be removed. (from: ' . __METHOD__
. ')',
345 $this->loadDynamicFields();
347 $return = $this->dynamics
->removeValues($this->id
, $transaction);
358 public function getErrors()
360 return $this->errors
;
364 * Validate data for dynamic fields
365 * Set valid data in current object, also resets errors list
367 * @param array $values Dynamic fields values
368 * @param string $prefix Prefix to replace, default to 'dynfield_'
372 public function dynamicsValidate($values, $prefix = 'dynfield_')
375 foreach ($values as $key => $value) {
376 $dfields[str_replace($prefix, $this->name_pattern
, $key)] = $value;
378 return $this->dynamicsCheck($dfields, [], []);