dolibarr  19.0.0-dev
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  if (strpos($param_ligne, ',')!==false) {
168  list($key, $value) = explode(',', $param_ligne);
169  if (!array_key_exists('options', $params)) {
170  $params['options'] = array();
171  }
172  } else {
173  $key=$param_ligne;
174  $value=null;
175  }
176  $params['options'][$key] = $value;
177  }
178  }
179 
180  // Visibility: -1=not visible by default in list, 1=visible, 0=hidden
181  $visibility = GETPOST('list', 'alpha');
182  if ($type == 'separate') {
183  $visibility = 3;
184  }
185 
186  $result = $extrafields->addExtraField(
187  GETPOST('attrname', 'aZ09'),
188  GETPOST('label', 'alpha'),
189  $type,
190  GETPOST('pos', 'int'),
191  $extrasize,
192  $elementtype,
193  (GETPOST('unique', 'alpha') ? 1 : 0),
194  (GETPOST('required', 'alpha') ? 1 : 0),
195  $default_value,
196  $params,
197  (GETPOST('alwayseditable', 'alpha') ? 1 : 0),
198  (GETPOST('perms', 'alpha') ? GETPOST('perms', 'alpha') : ''),
199  $visibility,
200  GETPOST('help', 'alpha'),
201  GETPOST('computed_value', 'alpha'),
202  (GETPOST('entitycurrentorall', 'alpha') ? 0 : ''),
203  GETPOST('langfile', 'alpha'),
204  1,
205  (GETPOST('totalizable', 'alpha') ? 1 : 0),
206  GETPOST('printable', 'alpha'),
207  array('css' => $css, 'cssview' => $cssview, 'csslist' => $csslist)
208  );
209  if ($result > 0) {
210  setEventMessages($langs->trans('SetupSaved'), null, 'mesgs');
211  header("Location: ".$_SERVER["PHP_SELF"]);
212  exit;
213  } else {
214  $error++;
215  $mesg = $extrafields->error;
216  setEventMessages($mesg, null, 'errors');
217  }
218  } else {
219  $error++;
220  $langs->load("errors");
221  $mesg = $langs->trans("ErrorFieldCanNotContainSpecialNorUpperCharacters", $langs->transnoentities("AttributeCode"));
222  setEventMessages($mesg, null, 'errors');
223  $action = 'create';
224  }
225  } else {
226  setEventMessages($mesg, null, 'errors');
227  }
228  }
229 }
230 
231 // Rename field
232 if ($action == 'update') {
233  if (GETPOST("button") != $langs->trans("Cancel")) {
234  // Check values
235  if (!$type) {
236  $error++;
237  $langs->load("errors");
238  $mesg[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type"));
239  $action = 'edit';
240  }
241  if ($type == 'varchar' && $extrasize <= 0) {
242  $error++;
243  $langs->load("errors");
244  $mesg[] = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Size"));
245  $action = 'edit';
246  }
247  if ($type == 'varchar' && $extrasize > $maxsizestring) {
248  $error++;
249  $langs->load("errors");
250  $mesg[] = $langs->trans("ErrorSizeTooLongForVarcharType", $maxsizestring);
251  $action = 'edit';
252  }
253  if ($type == 'int' && $extrasize > $maxsizeint) {
254  $error++;
255  $langs->load("errors");
256  $mesg[] = $langs->trans("ErrorSizeTooLongForIntType", $maxsizeint);
257  $action = 'edit';
258  }
259  if ($type == 'select' && !$param) {
260  $error++;
261  $langs->load("errors");
262  $mesg[] = $langs->trans("ErrorNoValueForSelectType");
263  $action = 'edit';
264  }
265  if ($type == 'sellist' && !$param) {
266  $error++;
267  $langs->load("errors");
268  $mesg[] = $langs->trans("ErrorNoValueForSelectListType");
269  $action = 'edit';
270  }
271  if ($type == 'checkbox' && !$param) {
272  $error++;
273  $langs->load("errors");
274  $mesg[] = $langs->trans("ErrorNoValueForCheckBoxType");
275  $action = 'edit';
276  }
277  if ($type == 'radio' && !$param) {
278  $error++;
279  $langs->load("errors");
280  $mesg[] = $langs->trans("ErrorNoValueForRadioType");
281  $action = 'edit';
282  }
283  if ((($type == 'radio') || ($type == 'checkbox')) && $param) {
284  // Construct array for parameter (value of select list)
285  $parameters = $param;
286  $parameters_array = explode("\r\n", $parameters);
287  foreach ($parameters_array as $param_ligne) {
288  if (!empty($param_ligne)) {
289  if (preg_match_all('/,/', $param_ligne, $matches)) {
290  if (count($matches[0]) > 1) {
291  $error++;
292  $langs->load("errors");
293  $mesg[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
294  $action = 'edit';
295  }
296  } else {
297  $error++;
298  $langs->load("errors");
299  $mesg[] = $langs->trans("ErrorBadFormatValueList", $param_ligne);
300  $action = 'edit';
301  }
302  }
303  }
304  }
305 
306  if (!$error) {
307  if (strlen(GETPOST('attrname', 'aZ09')) < 3 && empty($conf->global->MAIN_DISABLE_EXTRAFIELDS_CHECK_FOR_UPDATE)) {
308  $error++;
309  $langs->load("errors");
310  $mesg[] = $langs->trans("ErrorValueLength", $langs->transnoentitiesnoconv("AttributeCode"), 3);
311  $action = 'edit';
312  }
313  }
314 
315  // Check reserved keyword with more than 3 characters
316  if (!$error) {
317  if (in_array(GETPOST('attrname', 'aZ09'), array('and', 'keyword', 'table', 'index', 'integer', 'float', 'double', 'position')) && empty($conf->global->MAIN_DISABLE_EXTRAFIELDS_CHECK_FOR_UPDATE)) {
318  $error++;
319  $langs->load("errors");
320  $mesg[] = $langs->trans("ErrorReservedKeyword", GETPOST('attrname', 'aZ09'));
321  $action = 'edit';
322  }
323  }
324 
325  if (!$error) {
326  if (GETPOSTISSET("attrname") && preg_match("/^\w[a-zA-Z0-9-_]*$/", GETPOST('attrname', 'aZ09')) && !is_numeric(GETPOST('attrname', 'aZ09'))) {
327  $pos = GETPOST('pos', 'int');
328  // Construct array for parameter (value of select list)
329  $parameters = $param;
330  $parameters_array = explode("\r\n", $parameters);
331  $params = array();
332  //In sellist we have only one line and it can have come to do SQL expression
333  if ($type == 'sellist' || $type == 'chkbxlst') {
334  foreach ($parameters_array as $param_ligne) {
335  $params['options'] = array($parameters=>null);
336  }
337  } else {
338  //Esle it's separated key/value and coma list
339  foreach ($parameters_array as $param_ligne) {
340  list($key, $value) = explode(',', $param_ligne);
341  if (!array_key_exists('options', $params)) {
342  $params['options'] = array();
343  }
344  $params['options'][$key] = $value;
345  }
346  }
347 
348  // Visibility: -1=not visible by default in list, 1=visible, 0=hidden
349  $visibility = GETPOST('list', 'alpha');
350  if ($type == 'separate') {
351  $visibility = 3;
352  }
353 
354  // Example: is_object($object) ? ($object->id < 10 ? round($object->id / 2, 2) : (2 * $user->id) * (int) substr($mysoc->zip, 1, 2)) : 'objnotdefined'
355  $computedvalue = GETPOST('computed_value', 'nohtml');
356 
357  $result = $extrafields->update(
358  GETPOST('attrname', 'aZ09'),
359  GETPOST('label', 'alpha'),
360  $type,
361  $extrasize,
362  $elementtype,
363  (GETPOST('unique', 'alpha') ? 1 : 0),
364  (GETPOST('required', 'alpha') ? 1 : 0),
365  $pos,
366  $params,
367  (GETPOST('alwayseditable', 'alpha') ? 1 : 0),
368  (GETPOST('perms', 'alpha') ?GETPOST('perms', 'alpha') : ''),
369  $visibility,
370  GETPOST('help', 'alpha'),
371  GETPOST('default_value', 'alpha'),
372  $computedvalue,
373  (GETPOST('entitycurrentorall', 'alpha') ? 0 : ''),
374  GETPOST('langfile'),
375  GETPOST('enabled', 'nohtml'),
376  (GETPOST('totalizable', 'alpha') ? 1 : 0),
377  GETPOST('printable', 'alpha'),
378  array('css' => $css, 'cssview' => $cssview, 'csslist' => $csslist)
379  );
380  if ($result > 0) {
381  setEventMessages($langs->trans('SetupSaved'), null, 'mesgs');
382  header("Location: ".$_SERVER["PHP_SELF"]);
383  exit;
384  } else {
385  $error++;
386  $mesg = $extrafields->error;
387  setEventMessages($mesg, null, 'errors');
388  }
389  } else {
390  $error++;
391  $langs->load("errors");
392  $mesg = $langs->trans("ErrorFieldCanNotContainSpecialCharacters", $langs->transnoentities("AttributeCode"));
393  setEventMessages($mesg, null, 'errors');
394  }
395  } else {
396  setEventMessages($mesg, null, 'errors');
397  }
398  }
399 }
400 
401 // Delete attribute
402 if ($action == 'delete') {
403  if (GETPOSTISSET("attrname") && preg_match("/^\w[a-zA-Z0-9-_]*$/", GETPOST("attrname", 'aZ09'))) {
404  $result = $extrafields->delete(GETPOST("attrname", 'aZ09'), $elementtype);
405  if ($result >= 0) {
406  header("Location: ".$_SERVER["PHP_SELF"]);
407  exit;
408  } else {
409  $mesg = $extrafields->error;
410  }
411  } else {
412  $error++;
413  $langs->load("errors");
414  $mesg = $langs->trans("ErrorFieldCanNotContainSpecialCharacters", $langs->transnoentities("AttributeCode"));
415  }
416 }
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.