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