dolibarr  13.0.2
actions_addupdatedelete.inc.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2017-2019 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 
25 // $action or $cancel must be defined
26 // $object must be defined
27 // $permissiontoadd must be defined
28 // $permissiontodelete must be defined
29 // $backurlforlist must be defined
30 // $backtopage may be defined
31 // $triggermodname may be defined
32 
33 if (!empty($permissionedit) && empty($permissiontoadd)) $permissiontoadd = $permissionedit; // For backward compatibility
34 
35 if ($cancel)
36 {
37  /*var_dump($cancel);
38  var_dump($backtopage);exit;*/
39  if (!empty($backtopageforcancel))
40  {
41  header("Location: ".$backtopageforcancel);
42  exit;
43  } elseif (!empty($backtopage))
44  {
45  header("Location: ".$backtopage);
46  exit;
47  }
48  $action = '';
49 }
50 
51 
52 // Action to add record
53 if ($action == 'add' && !empty($permissiontoadd))
54 {
55  foreach ($object->fields as $key => $val)
56  {
57  if ($object->fields[$key]['type'] == 'duration') {
58  if (GETPOST($key.'hour') == '' && GETPOST($key.'min') == '') continue; // The field was not submited to be edited
59  } else {
60  if (!GETPOSTISSET($key)) continue; // The field was not submited to be edited
61  }
62  // Ignore special fields
63  if (in_array($key, array('rowid', 'entity', 'import_key'))) continue;
64  if (in_array($key, array('date_creation', 'tms', 'fk_user_creat', 'fk_user_modif'))) {
65  if (!in_array(abs($val['visible']), array(1, 3))) continue; // Only 1 and 3 that are case to create
66  }
67 
68  // Set value to insert
69  if (in_array($object->fields[$key]['type'], array('text', 'html'))) {
70  $value = GETPOST($key, 'restricthtml');
71  } elseif ($object->fields[$key]['type'] == 'date') {
72  $value = dol_mktime(12, 0, 0, GETPOST($key.'month', 'int'), GETPOST($key.'day', 'int'), GETPOST($key.'year', 'int')); // for date without hour, we use gmt
73  } elseif ($object->fields[$key]['type'] == 'datetime') {
74  $value = dol_mktime(GETPOST($key.'hour', 'int'), GETPOST($key.'min', 'int'), GETPOST($key.'sec', 'int'), GETPOST($key.'month', 'int'), GETPOST($key.'day', 'int'), GETPOST($key.'year', 'int'), 'tzuserrel');
75  } elseif ($object->fields[$key]['type'] == 'duration') {
76  $value = 60 * 60 * GETPOST($key.'hour', 'int') + 60 * GETPOST($key.'min', 'int');
77  } elseif (preg_match('/^(integer|price|real|double)/', $object->fields[$key]['type'])) {
78  $value = price2num(GETPOST($key, 'alphanohtml')); // To fix decimal separator according to lang setup
79  } elseif ($object->fields[$key]['type'] == 'boolean') {
80  $value = ((GETPOST($key) == '1' || GETPOST($key) == 'on') ? 1 : 0);
81  } elseif ($object->fields[$key]['type'] == 'reference') {
82  $tmparraykey = array_keys($object->param_list);
83  $value = $tmparraykey[GETPOST($key)].','.GETPOST($key.'2');
84  } else {
85  $value = GETPOST($key, 'alphanohtml');
86  }
87  if (preg_match('/^integer:/i', $object->fields[$key]['type']) && $value == '-1') $value = ''; // This is an implicit foreign key field
88  if (!empty($object->fields[$key]['foreignkey']) && $value == '-1') $value = ''; // This is an explicit foreign key field
89 
90  //var_dump($key.' '.$value.' '.$object->fields[$key]['type']);
91  $object->$key = $value;
92  if ($val['notnull'] > 0 && $object->$key == '' && !is_null($val['default']) && $val['default'] == '(PROV)')
93  {
94  $object->$key = '(PROV)';
95  }
96  if ($val['notnull'] > 0 && $object->$key == '' && is_null($val['default']))
97  {
98  $error++;
99  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv($val['label'])), null, 'errors');
100  }
101  }
102 
103  // Fill array 'array_options' with data from add form
104  if (!$error) {
105  $ret = $extrafields->setOptionalsFromPost(null, $object);
106  if ($ret < 0) $error++;
107  }
108 
109  if (!$error)
110  {
111  $result = $object->create($user);
112  if ($result > 0)
113  {
114  // Creation OK
115  $urltogo = $backtopage ? str_replace('__ID__', $result, $backtopage) : $backurlforlist;
116  $urltogo = preg_replace('/--IDFORBACKTOPAGE--/', $object->id, $urltogo); // New method to autoselect project after a New on another form object creation
117  header("Location: ".$urltogo);
118  exit;
119  } else {
120  // Creation KO
121  if (!empty($object->errors)) setEventMessages(null, $object->errors, 'errors');
122  else setEventMessages($object->error, null, 'errors');
123  $action = 'create';
124  }
125  } else {
126  $action = 'create';
127  }
128 }
129 
130 // Action to update record
131 if ($action == 'update' && !empty($permissiontoadd))
132 {
133  foreach ($object->fields as $key => $val)
134  {
135  // Check if field was submited to be edited
136  if ($object->fields[$key]['type'] == 'duration') {
137  if (!GETPOSTISSET($key.'hour') || !GETPOSTISSET($key.'min')) continue; // The field was not submited to be edited
138  } elseif ($object->fields[$key]['type'] == 'boolean') {
139  if (!GETPOSTISSET($key)) {
140  $object->$key = 0; // use 0 instead null if the field is defined as not null
141  continue;
142  }
143  } else {
144  if (!GETPOSTISSET($key)) continue; // The field was not submited to be edited
145  }
146  // Ignore special fields
147  if (in_array($key, array('rowid', 'entity', 'import_key'))) continue;
148  if (in_array($key, array('date_creation', 'tms', 'fk_user_creat', 'fk_user_modif'))) {
149  if (!in_array(abs($val['visible']), array(1, 3, 4))) continue; // Only 1 and 3 and 4 that are case to update
150  }
151 
152  // Set value to update
153  if (preg_match('/^(text|html)/', $object->fields[$key]['type'])) {
154  $tmparray = explode(':', $object->fields[$key]['type']);
155  if (!empty($tmparray[1])) {
156  $value = GETPOST($key, $tmparray[1]);
157  } else {
158  $value = GETPOST($key, 'restricthtml');
159  }
160  } elseif ($object->fields[$key]['type'] == 'date') {
161  $value = dol_mktime(12, 0, 0, GETPOST($key.'month', 'int'), GETPOST($key.'day', 'int'), GETPOST($key.'year', 'int')); // for date without hour, we use gmt
162  } elseif ($object->fields[$key]['type'] == 'datetime') {
163  $value = dol_mktime(GETPOST($key.'hour', 'int'), GETPOST($key.'min', 'int'), GETPOST($key.'sec', 'int'), GETPOST($key.'month', 'int'), GETPOST($key.'day', 'int'), GETPOST($key.'year', 'int'), 'tzuserrel');
164  } elseif ($object->fields[$key]['type'] == 'duration') {
165  if (GETPOST($key.'hour', 'int') != '' || GETPOST($key.'min', 'int') != '') {
166  $value = 60 * 60 * GETPOST($key.'hour', 'int') + 60 * GETPOST($key.'min', 'int');
167  } else {
168  $value = '';
169  }
170  } elseif (preg_match('/^(integer|price|real|double)/', $object->fields[$key]['type'])) {
171  $value = price2num(GETPOST($key, 'alphanohtml')); // To fix decimal separator according to lang setup
172  } elseif ($object->fields[$key]['type'] == 'boolean') {
173  $value = ((GETPOST($key, 'aZ09') == 'on' || GETPOST($key, 'aZ09') == '1') ? 1 : 0);
174  } elseif ($object->fields[$key]['type'] == 'reference') {
175  $value = array_keys($object->param_list)[GETPOST($key)].','.GETPOST($key.'2');
176  } else {
177  $value = GETPOST($key, 'alpha');
178  }
179  if (preg_match('/^integer:/i', $object->fields[$key]['type']) && $value == '-1') $value = ''; // This is an implicit foreign key field
180  if (!empty($object->fields[$key]['foreignkey']) && $value == '-1') $value = ''; // This is an explicit foreign key field
181 
182  $object->$key = $value;
183  if ($val['notnull'] > 0 && $object->$key == '' && is_null($val['default']))
184  {
185  $error++;
186  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv($val['label'])), null, 'errors');
187  }
188  }
189 
190  // Fill array 'array_options' with data from add form
191  if (!$error) {
192  $ret = $extrafields->setOptionalsFromPost(null, $object, '@GETPOSTISSET');
193  if ($ret < 0) $error++;
194  }
195 
196  if (!$error)
197  {
198  $result = $object->update($user);
199  if ($result > 0)
200  {
201  $action = 'view';
202  } else {
203  // Creation KO
204  setEventMessages($object->error, $object->errors, 'errors');
205  $action = 'edit';
206  }
207  } else {
208  $action = 'edit';
209  }
210 }
211 
212 // Action to update one extrafield
213 if ($action == "update_extras" && !empty($permissiontoadd))
214 {
215  $object->fetch(GETPOST('id', 'int'));
216 
217  $attributekey = GETPOST('attribute', 'alpha');
218  $attributekeylong = 'options_'.$attributekey;
219 
220  if (GETPOSTISSET($attributekeylong.'day') && GETPOSTISSET($attributekeylong.'month') && GETPOSTISSET($attributekeylong.'year')) {
221  // This is properties of a date
222  $object->array_options['options_'.$attributekey] = dol_mktime(GETPOST($attributekeylong.'hour', 'int'), GETPOST($attributekeylong.'min', 'int'), GETPOST($attributekeylong.'sec', 'int'), GETPOST($attributekeylong.'month', 'int'), GETPOST($attributekeylong.'day', 'int'), GETPOST($attributekeylong.'year', 'int'));
223  //var_dump(dol_print_date($object->array_options['options_'.$attributekey]));exit;
224  } else {
225  $object->array_options['options_'.$attributekey] = GETPOST($attributekeylong, 'alpha');
226  }
227 
228  $result = $object->insertExtraFields(empty($triggermodname) ? '' : $triggermodname, $user);
229  if ($result > 0)
230  {
231  setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
232  $action = 'view';
233  } else {
234  setEventMessages($object->error, $object->errors, 'errors');
235  $action = 'edit_extras';
236  }
237 }
238 
239 // Action to delete
240 if ($action == 'confirm_delete' && !empty($permissiontodelete))
241 {
242  if (!($object->id > 0))
243  {
244  dol_print_error('', 'Error, object must be fetched before being deleted');
245  exit;
246  }
247 
248  $result = $object->delete($user);
249  if ($result > 0)
250  {
251  // Delete OK
252  setEventMessages("RecordDeleted", null, 'mesgs');
253  header("Location: ".$backurlforlist);
254  exit;
255  } else {
256  if (!empty($object->errors)) setEventMessages(null, $object->errors, 'errors');
257  else setEventMessages($object->error, null, 'errors');
258  }
259 }
260 
261 // Remove a line
262 if ($action == 'confirm_deleteline' && $confirm == 'yes' && !empty($permissiontoadd))
263 {
264  if (method_exists($object, 'deleteline')) {
265  $result = $object->deleteline($user, $lineid); // For backward compatibility
266  } else {
267  $result = $object->deleteLine($user, $lineid);
268  }
269  if ($result > 0)
270  {
271  // Define output language
272  $outputlangs = $langs;
273  $newlang = '';
274  if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id', 'aZ09'))
275  {
276  $newlang = GETPOST('lang_id', 'aZ09');
277  }
278  if ($conf->global->MAIN_MULTILANGS && empty($newlang) && is_object($object->thirdparty))
279  {
280  $newlang = $object->thirdparty->default_lang;
281  }
282  if (!empty($newlang)) {
283  $outputlangs = new Translate("", $conf);
284  $outputlangs->setDefaultLang($newlang);
285  }
286  if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
287  if (method_exists($object, 'generateDocument')) {
288  $ret = $object->fetch($object->id); // Reload to get new records
289  $object->generateDocument($object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
290  }
291  }
292 
293  setEventMessages($langs->trans('RecordDeleted'), null, 'mesgs');
294  header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id);
295  exit;
296  } else {
297  setEventMessages($object->error, $object->errors, 'errors');
298  }
299 }
300 
301 // Action validate object
302 if ($action == 'confirm_validate' && $confirm == 'yes' && $permissiontoadd)
303 {
304  $result = $object->validate($user);
305  if ($result >= 0)
306  {
307  // Define output language
308  if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
309  if (method_exists($object, 'generateDocument')) {
310  $outputlangs = $langs;
311  $newlang = '';
312  if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
313  if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
314  if (!empty($newlang)) {
315  $outputlangs = new Translate("", $conf);
316  $outputlangs->setDefaultLang($newlang);
317  }
318 
319  $ret = $object->fetch($id); // Reload to get new records
320 
321  $model = $object->model_pdf;
322 
323  $retgen = $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
324  if ($retgen < 0) {
325  setEventMessages($object->error, $object->errors, 'warnings');
326  }
327  }
328  }
329  } else {
330  setEventMessages($object->error, $object->errors, 'errors');
331  }
332 }
333 
334 // Action close object
335 if ($action == 'confirm_close' && $confirm == 'yes' && $permissiontoadd)
336 {
337  $result = $object->cancel($user);
338  if ($result >= 0)
339  {
340  // Define output language
341  if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
342  if (method_exists($object, 'generateDocument')) {
343  $outputlangs = $langs;
344  $newlang = '';
345  if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
346  if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
347  if (!empty($newlang)) {
348  $outputlangs = new Translate("", $conf);
349  $outputlangs->setDefaultLang($newlang);
350  }
351  $model = $object->model_pdf;
352  $ret = $object->fetch($id); // Reload to get new records
353 
354  $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
355  }
356  }
357  } else {
358  setEventMessages($object->error, $object->errors, 'errors');
359  }
360 }
361 
362 // Action setdraft object
363 if ($action == 'confirm_setdraft' && $confirm == 'yes' && $permissiontoadd)
364 {
365  $result = $object->setDraft($user);
366  if ($result >= 0)
367  {
368  // Nothing else done
369  } else {
370  setEventMessages($object->error, $object->errors, 'errors');
371  }
372 }
373 
374 // Action reopen object
375 if ($action == 'confirm_reopen' && $confirm == 'yes' && $permissiontoadd)
376 {
377  $result = $object->reopen($user);
378  if ($result >= 0)
379  {
380  // Define output language
381  if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
382  if (method_exists($object, 'generateDocument')) {
383  $outputlangs = $langs;
384  $newlang = '';
385  if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id', 'aZ09')) $newlang = GETPOST('lang_id', 'aZ09');
386  if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
387  if (!empty($newlang)) {
388  $outputlangs = new Translate("", $conf);
389  $outputlangs->setDefaultLang($newlang);
390  }
391  $model = $object->model_pdf;
392  $ret = $object->fetch($id); // Reload to get new records
393 
394  $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
395  }
396  }
397  } else {
398  setEventMessages($object->error, $object->errors, 'errors');
399  }
400 }
401 
402 // Action clone object
403 if ($action == 'confirm_clone' && $confirm == 'yes' && !empty($permissiontoadd))
404 {
405  if (1 == 0 && !GETPOST('clone_content') && !GETPOST('clone_receivers'))
406  {
407  setEventMessages($langs->trans("NoCloneOptionsSpecified"), null, 'errors');
408  } else {
409  $objectutil = dol_clone($object, 1); // To avoid to denaturate loaded object when setting some properties for clone or if createFromClone modifies the object. We use native clone to keep this->db valid.
410  //$objectutil->date = dol_mktime(12, 0, 0, GETPOST('newdatemonth', 'int'), GETPOST('newdateday', 'int'), GETPOST('newdateyear', 'int'));
411  // ...
412  $result = $objectutil->createFromClone($user, (($object->id > 0) ? $object->id : $id));
413  if (is_object($result) || $result > 0)
414  {
415  $newid = 0;
416  if (is_object($result)) $newid = $result->id;
417  else $newid = $result;
418  header("Location: ".$_SERVER['PHP_SELF'].'?id='.$newid); // Open record of new object
419  exit;
420  } else {
421  setEventMessages($objectutil->error, $objectutil->errors, 'errors');
422  $action = '';
423  }
424  }
425 }
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm= 'auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
dol_clone($object, $native=0)
Create a clone of instance of object (new instance with same value for properties) With native = 0: P...
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
Class to manage translations.
print $_SERVER["PHP_SELF"]
Edit parameters.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...