dolibarr  7.0.0-beta
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  0,
182  GETPOST('computed_value','alpha'),
183  (GETPOST('entitycurrentorall', 'alpha')?0:''),
184  GETPOST('langfile', 'alpha')
185  );
186  if ($result > 0)
187  {
188  setEventMessages($langs->trans('SetupSaved'), null, 'mesgs');
189  header("Location: ".$_SERVER["PHP_SELF"]);
190  exit;
191  }
192  else
193  {
194  $error++;
195  $mesg=$extrafields->error;
196  setEventMessages($mesg, null, 'errors');
197  }
198  }
199  else
200  {
201  $error++;
202  $langs->load("errors");
203  $mesg=$langs->trans("ErrorFieldCanNotContainSpecialNorUpperCharacters",$langs->transnoentities("AttributeCode"));
204  setEventMessages($mesg, null, 'errors');
205  $action = 'create';
206  }
207  }
208  else
209  {
210  setEventMessages($mesg, null, 'errors');
211  }
212  }
213 }
214 
215 // Rename field
216 if ($action == 'update')
217 {
218  if ($_POST["button"] != $langs->trans("Cancel"))
219  {
220  // Check values
221  if (! $type)
222  {
223  $error++;
224  $langs->load("errors");
225  $mesg[]=$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Type"));
226  $action = 'edit';
227  }
228  if ($type=='varchar' && $extrasize <= 0)
229  {
230  $error++;
231  $langs->load("errors");
232  $mesg[]=$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Size"));
233  $action = 'edit';
234  }
235  if ($type=='varchar' && $extrasize > $maxsizestring)
236  {
237  $error++;
238  $langs->load("errors");
239  $mesg[]=$langs->trans("ErrorSizeTooLongForVarcharType",$maxsizestring);
240  $action = 'edit';
241  }
242  if ($type=='int' && $extrasize > $maxsizeint)
243  {
244  $error++;
245  $langs->load("errors");
246  $mesg[]=$langs->trans("ErrorSizeTooLongForIntType",$maxsizeint);
247  $action = 'edit';
248  }
249  if ($type=='select' && !$param)
250  {
251  $error++;
252  $langs->load("errors");
253  $mesg[]=$langs->trans("ErrorNoValueForSelectType");
254  $action = 'edit';
255  }
256  if ($type=='sellist' && !$param)
257  {
258  $error++;
259  $langs->load("errors");
260  $mesg[]=$langs->trans("ErrorNoValueForSelectListType");
261  $action = 'edit';
262  }
263  if ($type=='checkbox' && !$param)
264  {
265  $error++;
266  $langs->load("errors");
267  $mesg[]=$langs->trans("ErrorNoValueForCheckBoxType");
268  $action = 'edit';
269  }
270  if ($type=='radio' && !$param)
271  {
272  $error++;
273  $langs->load("errors");
274  $mesg[]=$langs->trans("ErrorNoValueForRadioType");
275  $action = 'edit';
276  }
277  if ((($type=='radio') || ($type=='checkbox')) && $param)
278  {
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  {
284  if (!empty($param_ligne)) {
285  if (preg_match_all('/,/',$param_ligne,$matches))
286  {
287  if (count($matches[0])>1) {
288  $error++;
289  $langs->load("errors");
290  $mesg[]=$langs->trans("ErrorBadFormatValueList",$param_ligne);
291  $action = 'edit';
292  }
293  }
294  else
295  {
296  $error++;
297  $langs->load("errors");
298  $mesg[]=$langs->trans("ErrorBadFormatValueList",$param_ligne);
299  $action = 'edit';
300  }
301  }
302  }
303  }
304 
305  if (! $error)
306  {
307  if (isset($_POST["attrname"]) && preg_match("/^\w[a-zA-Z0-9-_]*$/",$_POST['attrname']))
308  {
309  $pos = GETPOST('pos','int');
310  // Construct array for parameter (value of select list)
311  $parameters = $param;
312  $parameters_array = explode("\r\n",$parameters);
313  //In sellist we have only one line and it can have come to do SQL expression
314  if ($type=='sellist') {
315  foreach($parameters_array as $param_ligne)
316  {
317  $params['options'] = array($parameters=>null);
318  }
319  }
320  else
321  {
322  //Esle it's separated key/value and coma list
323  foreach($parameters_array as $param_ligne)
324  {
325  list($key,$value) = explode(',',$param_ligne);
326  $params['options'][$key] = $value;
327  }
328  }
329 
330  // Visibility: -1=not visible by default in list, 1=visible, 0=hidden
331  $visibility = GETPOST('list', 'alpha');
332  if ($type == 'separate') $visibility=3;
333 
334  $result=$extrafields->update(
335  GETPOST('attrname', 'alpha'),
336  GETPOST('label', 'alpha'),
337  $type,
338  $extrasize,
339  $elementtype,
340  (GETPOST('unique', 'alpha')?1:0),
341  (GETPOST('required', 'alpha')?1:0),
342  $pos,
343  $params,
344  (GETPOST('alwayseditable', 'alpha')?1:0),
345  (GETPOST('perms', 'alpha')?GETPOST('perms', 'alpha'):''),
346  $visibility,
347  0,
348  GETPOST('default_value','alpha'),
349  GETPOST('computed_value','alpha'),
350  (GETPOST('entitycurrentorall', 'alpha')?0:''),
351  GETPOST('langfile')
352  );
353  if ($result > 0)
354  {
355  setEventMessages($langs->trans('SetupSaved'), null, 'mesgs');
356  header("Location: ".$_SERVER["PHP_SELF"]);
357  exit;
358  }
359  else
360  {
361  $error++;
362  $mesg=$extrafields->error;
363  setEventMessages($mesg, null, 'errors');
364  }
365  }
366  else
367  {
368  $error++;
369  $langs->load("errors");
370  $mesg=$langs->trans("ErrorFieldCanNotContainSpecialCharacters",$langs->transnoentities("AttributeCode"));
371  setEventMessages($mesg, null, 'errors');
372  }
373  }
374  else
375  {
376  setEventMessages($mesg, null, 'errors');
377  }
378  }
379 }
380 
381 // Delete attribute
382 if ($action == 'delete')
383 {
384  if(isset($_GET["attrname"]) && preg_match("/^\w[a-zA-Z0-9-_]*$/",$_GET["attrname"]))
385  {
386  $result=$extrafields->delete($_GET["attrname"],$elementtype);
387  if ($result >= 0)
388  {
389  header("Location: ".$_SERVER["PHP_SELF"]);
390  exit;
391  }
392  else $mesg=$extrafields->error;
393  }
394  else
395  {
396  $error++;
397  $langs->load("errors");
398  $mesg=$langs->trans("ErrorFieldCanNotContainSpecialCharacters",$langs->transnoentities("AttributeCode"));
399  }
400 }
401 
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.