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