dolibarr  16.0.5
actions_extrafields.inc.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2011-2020 Laurent Destailleur <eldy@users.sourceforge.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  * or see https://www.gnu.org/
17  *
18  * $elementype must be defined.
19  */
20 
26 $maxsizestring = 255;
27 $maxsizeint = 10;
28 $mesg = array();
29 
30 $extrasize = GETPOST('size', 'intcomma');
31 $type = GETPOST('type', 'alpha');
32 $param = GETPOST('param', 'alpha');
33 
34 if ($type == 'double' && strpos($extrasize, ',') === false) {
35  $extrasize = '24,8';
36 }
37 if ($type == 'date') {
38  $extrasize = '';
39 }
40 if ($type == 'datetime') {
41  $extrasize = '';
42 }
43 if ($type == 'select') {
44  $extrasize = '';
45 }
46 
47 
48 // Add attribute
49 if ($action == 'add') {
50  if (GETPOST("button") != $langs->trans("Cancel")) {
51  // Check values
52  if (!$type) {
53  $error++;
54  $langs->load("errors");
55  $mesg[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type"));
56  $action = 'create';
57  }
58  if ($type == 'varchar' && $extrasize <= 0) {
59  $error++;
60  $langs->load("errors");
61  $mesg[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Size"));
62  $action = 'edit';
63  }
64  if ($type == 'varchar' && $extrasize > $maxsizestring) {
65  $error++;
66  $langs->load("errors");
67  $mesg[] = $langs->trans("ErrorSizeTooLongForVarcharType", $maxsizestring);
68  $action = 'create';
69  }
70  if ($type == 'int' && $extrasize > $maxsizeint) {
71  $error++;
72  $langs->load("errors");
73  $mesg[] = $langs->trans("ErrorSizeTooLongForIntType", $maxsizeint);
74  $action = 'create';
75  }
76  if ($type == 'select' && !$param) {
77  $error++;
78  $langs->load("errors");
79  $mesg[] = $langs->trans("ErrorNoValueForSelectType");
80  $action = 'create';
81  }
82  if ($type == 'sellist' && !$param) {
83  $error++;
84  $langs->load("errors");
85  $mesg[] = $langs->trans("ErrorNoValueForSelectListType");
86  $action = 'create';
87  }
88  if ($type == 'checkbox' && !$param) {
89  $error++;
90  $langs->load("errors");
91  $mesg[] = $langs->trans("ErrorNoValueForCheckBoxType");
92  $action = 'create';
93  }
94  if ($type == 'link' && !$param) {
95  $error++;
96  $langs->load("errors");
97  $mesg[] = $langs->trans("ErrorNoValueForLinkType");
98  $action = 'create';
99  }
100  if ($type == 'radio' && !$param) {
101  $error++;
102  $langs->load("errors");
103  $mesg[] = $langs->trans("ErrorNoValueForRadioType");
104  $action = 'create';
105  }
106  if ((($type == 'radio') || ($type == 'checkbox')) && $param) {
107  // Construct array for parameter (value of select list)
108  $parameters = $param;
109  $parameters_array = explode("\r\n", $parameters);
110  foreach ($parameters_array as $param_ligne) {
111  if (!empty($param_ligne)) {
112  if (preg_match_all('/,/', $param_ligne, $matches)) {
113  if (count($matches[0]) > 1) {
114  $error++;
115  $langs->load("errors");
116  $mesg[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
117  $action = 'create';
118  }
119  } else {
120  $error++;
121  $langs->load("errors");
122  $mesg[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
123  $action = 'create';
124  }
125  }
126  }
127  }
128 
129  if (!$error) {
130  if (strlen(GETPOST('attrname', 'aZ09')) < 3) {
131  $error++;
132  $langs->load("errors");
133  $mesg[] = $langs->trans("ErrorValueLength", $langs->transnoentitiesnoconv("AttributeCode"), 3);
134  $action = 'create';
135  }
136  }
137 
138  // Check reserved keyword with more than 3 characters
139  if (!$error) {
140  if (in_array(GETPOST('attrname', 'aZ09'), array('and', 'keyword', 'table', 'index', 'int', 'integer', 'float', 'double', 'real', 'position'))) {
141  $error++;
142  $langs->load("errors");
143  $mesg[] = $langs->trans("ErrorReservedKeyword", GETPOST('attrname', 'aZ09'));
144  $action = 'create';
145  }
146  }
147 
148  if (!$error) {
149  // attrname must be alphabetical and lower case only
150  if (GETPOSTISSET("attrname") && preg_match("/^[a-z0-9_]+$/", GETPOST('attrname', 'aZ09')) && !is_numeric(GETPOST('attrname', 'aZ09'))) {
151  // Construct array for parameter (value of select list)
152  $default_value = GETPOST('default_value', 'alpha');
153  $parameters = $param;
154  $parameters_array = explode("\r\n", $parameters);
155  $params = array();
156  //In sellist we have only one line and it can have come to do SQL expression
157  if ($type == 'sellist' || $type == 'chkbxlst') {
158  foreach ($parameters_array as $param_ligne) {
159  $params['options'] = array($parameters=>null);
160  }
161  } else {
162  // Else it's separated key/value and coma list
163  foreach ($parameters_array as $param_ligne) {
164  list($key, $value) = explode(',', $param_ligne);
165  if (!array_key_exists('options', $params)) {
166  $params['options'] = array();
167  }
168  $params['options'][$key] = $value;
169  }
170  }
171 
172  // Visibility: -1=not visible by default in list, 1=visible, 0=hidden
173  $visibility = GETPOST('list', 'alpha');
174  if ($type == 'separate') {
175  $visibility = 3;
176  }
177 
178  $result = $extrafields->addExtraField(
179  GETPOST('attrname', 'aZ09'),
180  GETPOST('label', 'alpha'),
181  $type,
182  GETPOST('pos', 'int'),
183  $extrasize,
184  $elementtype,
185  (GETPOST('unique', 'alpha') ? 1 : 0),
186  (GETPOST('required', 'alpha') ? 1 : 0),
187  $default_value,
188  $params,
189  (GETPOST('alwayseditable', 'alpha') ? 1 : 0),
190  (GETPOST('perms', 'alpha') ? GETPOST('perms', 'alpha') : ''),
191  $visibility,
192  GETPOST('help', 'alpha'),
193  GETPOST('computed_value', 'alpha'),
194  (GETPOST('entitycurrentorall', 'alpha') ? 0 : ''),
195  GETPOST('langfile', 'alpha'),
196  1,
197  (GETPOST('totalizable', 'alpha') ? 1 : 0),
198  GETPOST('printable', 'alpha')
199  );
200  if ($result > 0) {
201  setEventMessages($langs->trans('SetupSaved'), null, 'mesgs');
202  header("Location: ".$_SERVER["PHP_SELF"]);
203  exit;
204  } else {
205  $error++;
206  $mesg = $extrafields->error;
207  setEventMessages($mesg, null, 'errors');
208  }
209  } else {
210  $error++;
211  $langs->load("errors");
212  $mesg = $langs->trans("ErrorFieldCanNotContainSpecialNorUpperCharacters", $langs->transnoentities("AttributeCode"));
213  setEventMessages($mesg, null, 'errors');
214  $action = 'create';
215  }
216  } else {
217  setEventMessages($mesg, null, 'errors');
218  }
219  }
220 }
221 
222 // Rename field
223 if ($action == 'update') {
224  if (GETPOST("button") != $langs->trans("Cancel")) {
225  // Check values
226  if (!$type) {
227  $error++;
228  $langs->load("errors");
229  $mesg[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type"));
230  $action = 'edit';
231  }
232  if ($type == 'varchar' && $extrasize <= 0) {
233  $error++;
234  $langs->load("errors");
235  $mesg[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Size"));
236  $action = 'edit';
237  }
238  if ($type == 'varchar' && $extrasize > $maxsizestring) {
239  $error++;
240  $langs->load("errors");
241  $mesg[] = $langs->trans("ErrorSizeTooLongForVarcharType", $maxsizestring);
242  $action = 'edit';
243  }
244  if ($type == 'int' && $extrasize > $maxsizeint) {
245  $error++;
246  $langs->load("errors");
247  $mesg[] = $langs->trans("ErrorSizeTooLongForIntType", $maxsizeint);
248  $action = 'edit';
249  }
250  if ($type == 'select' && !$param) {
251  $error++;
252  $langs->load("errors");
253  $mesg[] = $langs->trans("ErrorNoValueForSelectType");
254  $action = 'edit';
255  }
256  if ($type == 'sellist' && !$param) {
257  $error++;
258  $langs->load("errors");
259  $mesg[] = $langs->trans("ErrorNoValueForSelectListType");
260  $action = 'edit';
261  }
262  if ($type == 'checkbox' && !$param) {
263  $error++;
264  $langs->load("errors");
265  $mesg[] = $langs->trans("ErrorNoValueForCheckBoxType");
266  $action = 'edit';
267  }
268  if ($type == 'radio' && !$param) {
269  $error++;
270  $langs->load("errors");
271  $mesg[] = $langs->trans("ErrorNoValueForRadioType");
272  $action = 'edit';
273  }
274  if ((($type == 'radio') || ($type == 'checkbox')) && $param) {
275  // Construct array for parameter (value of select list)
276  $parameters = $param;
277  $parameters_array = explode("\r\n", $parameters);
278  foreach ($parameters_array as $param_ligne) {
279  if (!empty($param_ligne)) {
280  if (preg_match_all('/,/', $param_ligne, $matches)) {
281  if (count($matches[0]) > 1) {
282  $error++;
283  $langs->load("errors");
284  $mesg[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
285  $action = 'edit';
286  }
287  } else {
288  $error++;
289  $langs->load("errors");
290  $mesg[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
291  $action = 'edit';
292  }
293  }
294  }
295  }
296 
297  if (!$error) {
298  if (strlen(GETPOST('attrname', 'aZ09')) < 3 && empty($conf->global->MAIN_DISABLE_EXTRAFIELDS_CHECK_FOR_UPDATE)) {
299  $error++;
300  $langs->load("errors");
301  $mesg[] = $langs->trans("ErrorValueLength", $langs->transnoentitiesnoconv("AttributeCode"), 3);
302  $action = 'edit';
303  }
304  }
305 
306  // Check reserved keyword with more than 3 characters
307  if (!$error) {
308  if (in_array(GETPOST('attrname', 'aZ09'), array('and', 'keyword', 'table', 'index', 'integer', 'float', 'double', 'position')) && empty($conf->global->MAIN_DISABLE_EXTRAFIELDS_CHECK_FOR_UPDATE)) {
309  $error++;
310  $langs->load("errors");
311  $mesg[] = $langs->trans("ErrorReservedKeyword", GETPOST('attrname', 'aZ09'));
312  $action = 'edit';
313  }
314  }
315 
316  if (!$error) {
317  if (GETPOSTISSET("attrname") && preg_match("/^\w[a-zA-Z0-9-_]*$/", GETPOST('attrname', 'aZ09')) && !is_numeric(GETPOST('attrname', 'aZ09'))) {
318  $pos = GETPOST('pos', 'int');
319  // Construct array for parameter (value of select list)
320  $parameters = $param;
321  $parameters_array = explode("\r\n", $parameters);
322  $params = array();
323  //In sellist we have only one line and it can have come to do SQL expression
324  if ($type == 'sellist' || $type == 'chkbxlst') {
325  foreach ($parameters_array as $param_ligne) {
326  $params['options'] = array($parameters=>null);
327  }
328  } else {
329  //Esle it's separated key/value and coma list
330  foreach ($parameters_array as $param_ligne) {
331  list($key, $value) = explode(',', $param_ligne);
332  if (!array_key_exists('options', $params)) {
333  $params['options'] = array();
334  }
335  $params['options'][$key] = $value;
336  }
337  }
338 
339  // Visibility: -1=not visible by default in list, 1=visible, 0=hidden
340  $visibility = GETPOST('list', 'alpha');
341  if ($type == 'separate') {
342  $visibility = 3;
343  }
344 
345  // Example: is_object($object) ? ($object->id < 10 ? round($object->id / 2, 2) : (2 * $user->id) * (int) substr($mysoc->zip, 1, 2)) : 'objnotdefined'
346  $computedvalue = GETPOST('computed_value', 'nohtml');
347 
348  $result = $extrafields->update(
349  GETPOST('attrname', 'aZ09'),
350  GETPOST('label', 'alpha'),
351  $type,
352  $extrasize,
353  $elementtype,
354  (GETPOST('unique', 'alpha') ? 1 : 0),
355  (GETPOST('required', 'alpha') ? 1 : 0),
356  $pos,
357  $params,
358  (GETPOST('alwayseditable', 'alpha') ? 1 : 0),
359  (GETPOST('perms', 'alpha') ?GETPOST('perms', 'alpha') : ''),
360  $visibility,
361  GETPOST('help', 'alpha'),
362  GETPOST('default_value', 'alpha'),
363  $computedvalue,
364  (GETPOST('entitycurrentorall', 'alpha') ? 0 : ''),
365  GETPOST('langfile'),
366  GETPOST('enabled', 'alpha'),
367  (GETPOST('totalizable', 'alpha') ? 1 : 0),
368  GETPOST('printable', 'alpha')
369  );
370  if ($result > 0) {
371  setEventMessages($langs->trans('SetupSaved'), null, 'mesgs');
372  header("Location: ".$_SERVER["PHP_SELF"]);
373  exit;
374  } else {
375  $error++;
376  $mesg = $extrafields->error;
377  setEventMessages($mesg, null, 'errors');
378  }
379  } else {
380  $error++;
381  $langs->load("errors");
382  $mesg = $langs->trans("ErrorFieldCanNotContainSpecialCharacters", $langs->transnoentities("AttributeCode"));
383  setEventMessages($mesg, null, 'errors');
384  }
385  } else {
386  setEventMessages($mesg, null, 'errors');
387  }
388  }
389 }
390 
391 // Delete attribute
392 if ($action == 'delete') {
393  if (GETPOSTISSET("attrname") && preg_match("/^\w[a-zA-Z0-9-_]*$/", GETPOST("attrname", 'aZ09'))) {
394  $result = $extrafields->delete(GETPOST("attrname", 'aZ09'), $elementtype);
395  if ($result >= 0) {
396  header("Location: ".$_SERVER["PHP_SELF"]);
397  exit;
398  } else {
399  $mesg = $extrafields->error;
400  }
401  } else {
402  $error++;
403  $langs->load("errors");
404  $mesg = $langs->trans("ErrorFieldCanNotContainSpecialCharacters", $langs->transnoentities("AttributeCode"));
405  }
406 }
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137