dolibarr 24.0.3
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 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
27'
28@phan-var-force CommonObject $this
29@phan-var-force ?string $action
30@phan-var-force ?string $cancel
31@phan-var-force CommonObject $object
32@phan-var-force string $permissiontoadd
33@phan-var-force ?string $permissionedit
34@phan-var-force string $permissiontodelete
35@phan-var-force string $backurlforlist
36@phan-var-force ?string $backtopage
37@phan-var-force ?string $noback
38@phan-var-force ?string $triggermodname
39@phan-var-force string $hidedetails
40@phan-var-force string $hidedesc
41@phan-var-force string $hideref
42@phan-var-force ?string $confirm
43@phan-var-force ?int $lineid
44@phan-var-force ?int $id
45';
71// $action or $cancel must be defined
72// $object must be defined
73// $permissiontoadd must be defined
74// $permissiontodelete must be defined
75// $backurlforlist must be defined
76// $backtopage may be defined
77// $noback may be defined
78// $triggermodname may be defined
79
80$hidedetails = isset($hidedetails) ? $hidedetails : '';
81$hidedesc = isset($hidedesc) ? $hidedesc : '';
82$hideref = isset($hideref) ? $hideref : '';
83$error = 0;
84
85if (!empty($permissionedit) && empty($permissiontoadd)) {
86 $permissiontoadd = $permissionedit; // For backward compatibility
87}
88
89if (!empty($cancel)) {
90 /*var_dump($cancel);var_dump($backtopage);var_dump($backtopageforcancel);exit;*/
91 if (!empty($backtopageforcancel)) {
92 header("Location: ".$backtopageforcancel);
93 exit;
94 } elseif (!empty($backtopage)) {
95 header("Location: ".$backtopage);
96 exit;
97 }
98 $action = '';
99}
100
101
102// Action to add record
103if ($action == 'add' && !empty($permissiontoadd)) {
104 foreach ($object->fields as $key => $val) {
105 // Ignore special cases
106 if ($object->fields[$key]['type'] == 'duration') {
107 if (GETPOST($key.'hour') == '' && GETPOST($key.'min') == '') {
108 continue; // The field was not submitted to be saved
109 }
110 } else {
111 if (!GETPOSTISSET($key) && !preg_match('/^chkbxlst:/', $object->fields[$key]['type'])) {
112 continue; // The field was not submitted to be saved
113 }
114 }
115
116 // Ignore special fields
117 if (in_array($key, array('rowid', 'entity', 'import_key'))) {
118 continue;
119 }
120 if (in_array($key, array('date_creation', 'tms', 'fk_user_creat', 'fk_user_modif'))) {
121 if (!in_array(abs($val['visible']), array(1, 3))) {
122 continue; // Only 1 and 3 that are case to create
123 }
124 }
125
126 // Set value to insert
127 if (preg_match('/^text/', $object->fields[$key]['type'])) {
128 $tmparray = explode(':', $object->fields[$key]['type']);
129 if (!empty($tmparray[1])) {
130 $check = $tmparray[1];
131 } else {
132 $check = 'nohtml';
133 }
134 if (getDolGlobalString('MAIN_SECURITY_DISALLOW_UNSECURED_TEXT')) {
135 $security_check = 'alphanohtml';
136 } else {
137 $security_check = $check;
138 }
139 if (!empty($tmparray[1])) {
140 $value = GETPOST($key, $security_check);
141 } else {
142 $value = GETPOST($key, $security_check);
143 if (!empty($object->fields[$key]['arrayofkeyval']) && !empty($object->fields[$key]['multiinput'])) {
144 $tmparraymultiselect = GETPOST($key.'_multiselect', 'array');
145 foreach ($tmparraymultiselect as $tmpvalue) {
146 $value .= (!empty($value) ? "," : "").$tmpvalue;
147 }
148 }
149 }
150 } elseif (preg_match('/^html/', $object->fields[$key]['type'])) {
151 $tmparray = explode(':', $object->fields[$key]['type']);
152 if (!empty($tmparray[1])) {
153 $value = GETPOST($key, $tmparray[1]);
154 } else {
155 $value = GETPOST($key, 'restricthtml');
156 }
157 } elseif ($object->fields[$key]['type'] == 'date') {
158 $value = dol_mktime(12, 0, 0, GETPOSTINT($key.'month'), GETPOSTINT($key.'day'), GETPOSTINT($key.'year')); // for date without hour, we use gmt
159 } elseif ($object->fields[$key]['type'] == 'datetime') {
160 $value = dol_mktime(GETPOSTINT($key.'hour'), GETPOSTINT($key.'min'), GETPOSTINT($key.'sec'), GETPOSTINT($key.'month'), GETPOSTINT($key.'day'), GETPOSTINT($key.'year'), 'tzuserrel');
161 } elseif ($object->fields[$key]['type'] == 'duration') {
162 $value = 60 * 60 * GETPOSTINT($key.'hour') + 60 * GETPOSTINT($key.'min');
163 } elseif (preg_match('/^(integer|price|real|double)/', $object->fields[$key]['type'])) {
164 $value = price2num(GETPOST($key, 'alphanohtml')); // To fix decimal separator according to lang setup
165 } elseif ($object->fields[$key]['type'] == 'boolean') {
166 $value = ((GETPOST($key) == '1' || GETPOST($key) == 'on') ? 1 : 0);
167 } elseif ($object->fields[$key]['type'] == 'reference') {
168 $tmparraykey = array_keys($object->param_list);
169 $value = $tmparraykey[(int) GETPOST($key)].','.GETPOST($key.'2');
170 } elseif (preg_match('/^chkbxlst:(.*)/', $object->fields[$key]['type']) || $object->fields[$key]['type'] == 'checkbox') {
171 $value = '';
172 $values_arr = GETPOST($key, 'array');
173 if (!empty($values_arr)) {
174 $value = implode(',', $values_arr);
175 }
176 } else {
177 if ($key == 'lang') {
178 $value = GETPOST($key, 'aZ09') ? GETPOST($key, 'aZ09') : "";
179 } else {
180 $value = GETPOST($key, 'alphanohtml');
181 }
182 }
183 if (preg_match('/^integer:/i', $object->fields[$key]['type']) && $value == '-1') {
184 $value = ''; // This is an implicit foreign key field
185 }
186 if (!empty($object->fields[$key]['foreignkey']) && $value == '-1') {
187 $value = ''; // This is an explicit foreign key field
188 }
189 if ((preg_match('/^sellist/i', $object->fields[$key]['type']) || $object->fields[$key]['type'] == 'select') && $value === '0') {
190 $value = ''; // sellist / select blank option posts "0"; normalize to '' so notnull check works on PHP 8.0+ (see github.com/Dolibarr/dolibarr/issues/38199)
191 }
192
193 //var_dump($key.' '.$value.' '.$object->fields[$key]['type'].' '.$object->fields[$key]['notnull']);
194
195 $object->$key = $value;
196 if (!empty($val['notnull']) && $val['notnull'] > 0 && $object->$key == '' && isset($val['default']) && $val['default'] === '(PROV)') {
197 $object->$key = '(PROV)';
198 }
199 if ($key == 'pass_crypted') {
200 $object->pass = GETPOST("pass", "password");
201 // TODO Manadatory for password not yet managed
202 } else {
203 if (!empty($val['notnull']) && $val['notnull'] > 0 && $object->$key == '' && !isset($val['default'])) {
204 $error++;
205 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv($val['label'])), null, 'errors');
206 }
207 }
208
209 // Validation of fields values
210 if (getDolGlobalInt('MAIN_FEATURES_LEVEL') >= 1 || getDolGlobalString('MAIN_ACTIVATE_VALIDATION_RESULT')) {
211 if (!$error && !empty($val['validate']) && is_callable(array($object, 'validateField'))) {
212 if (!$object->validateField($object->fields, $key, $value)) {
213 $error++;
214 setEventMessages($object->error, $object->errors, 'errors');
215 }
216 }
217 }
218 }
219
220 // Special field
221 $model_pdf = GETPOST('model');
222 if (!empty($model_pdf) && property_exists($object, 'model_pdf')) {
223 $object->model_pdf = $model_pdf;
224 }
225
226 // Fill array 'array_options' with data from add form
227 if (!$error) {
228 $ret = $extrafields->setOptionalsFromPost(null, $object, '', 1);
229 if ($ret < 0) {
230 $error++;
231 }
232 }
233
234 if (!$error) {
235 $db->begin();
236
237 $result = $object->create($user);
238 if ($result > 0) {
239 // Creation OK
240 if (isModEnabled('category') && method_exists($object, 'setCategories')) {
241 $categories = GETPOST('categories', 'array:int');
242 $object->setCategories($categories);
243 }
244
245 $urltogo = $backtopage ? str_replace('__ID__', $result, $backtopage) : $backurlforlist;
246 $urltogo = preg_replace('/--IDFORBACKTOPAGE--/', (string) $object->id, $urltogo); // New method to autoselect field created after a New on another form object creation
247
248 $db->commit();
249
250 if (empty($noback)) {
251 header("Location: " . $urltogo);
252 exit;
253 }
254 } else {
255 $db->rollback();
256 $error++;
257 // Creation KO
258 if (!empty($object->errors)) {
259 setEventMessages(null, $object->errors, 'errors');
260 } else {
261 setEventMessages($object->error, null, 'errors');
262 }
263 $action = 'create';
264 }
265 } else {
266 $action = 'create';
267 }
268}
269
270// Action to update record
271if ($action == 'update' && !empty($permissiontoadd)) {
272 foreach ($object->fields as $key => $val) {
273 // Check if field was submitted to be edited
274 if ($object->fields[$key]['type'] == 'duration') {
275 if (!GETPOSTISSET($key.'hour') || !GETPOSTISSET($key.'min')) {
276 continue; // The field was not submitted to be saved
277 }
278 } elseif ($object->fields[$key]['type'] == 'boolean') {
279 if (!GETPOSTISSET($key)) {
280 $object->$key = 0; // use 0 instead null if the field is defined as not null
281 continue;
282 }
283 } else {
284 if (!GETPOSTISSET($key) && !preg_match('/^chkbxlst:/', $object->fields[$key]['type']) && $object->fields[$key]['type'] !== 'checkbox') {
285 continue; // The field was not submitted to be saved
286 }
287 }
288 // Ignore special fields
289 if (in_array($key, array('rowid', 'entity', 'import_key'))) {
290 continue;
291 }
292 if (in_array($key, array('date_creation', 'tms', 'fk_user_creat', 'fk_user_modif'))) {
293 if (!in_array(abs($val['visible']), array(1, 3, 4))) {
294 continue; // Only 1 and 3 and 4, that are cases to update
295 }
296 }
297
298 // Set value to update
299 if (preg_match('/^text/', $object->fields[$key]['type'])) {
300 $tmparray = explode(':', $object->fields[$key]['type']);
301 if (!empty($tmparray[1])) {
302 $check = $tmparray[1];
303 } else {
304 $check = 'nohtml';
305 }
306 if (getDolGlobalString('MAIN_SECURITY_DISALLOW_UNSECURED_TEXT')) {
307 $security_check = 'alphanohtml';
308 } else {
309 $security_check = $check;
310 }
311 if (!empty($tmparray[1])) {
312 $value = GETPOST($key, $security_check);
313 } else {
314 $value = GETPOST($key, $security_check);
315 if (!empty($object->fields[$key]['arrayofkeyval']) && !empty($object->fields[$key]['multiinput'])) {
316 $tmparraymultiselect = GETPOST($key.'_multiselect', 'array');
317 foreach ($tmparraymultiselect as $keytmp => $tmpvalue) {
318 $value .= (!empty($value) ? "," : "").$tmpvalue;
319 }
320 }
321 }
322 } elseif (preg_match('/^html/', $object->fields[$key]['type'])) {
323 $tmparray = explode(':', $object->fields[$key]['type']);
324 if (!empty($tmparray[1])) {
325 $value = GETPOST($key, $tmparray[1]);
326 } else {
327 $value = GETPOST($key, 'restricthtml');
328 }
329 } elseif ($object->fields[$key]['type'] == 'date') {
330 $value = dol_mktime(12, 0, 0, GETPOSTINT($key.'month'), GETPOSTINT($key.'day'), GETPOSTINT($key.'year')); // for date without hour, we use gmt
331 } elseif ($object->fields[$key]['type'] == 'datetime') {
332 $value = dol_mktime(GETPOSTINT($key.'hour'), GETPOSTINT($key.'min'), GETPOSTINT($key.'sec'), GETPOSTINT($key.'month'), GETPOSTINT($key.'day'), GETPOSTINT($key.'year'), 'tzuserrel');
333 } elseif ($object->fields[$key]['type'] == 'duration') {
334 if (GETPOSTINT($key.'hour') != '' || GETPOSTINT($key.'min') != '') {
335 $value = 60 * 60 * GETPOSTINT($key.'hour') + 60 * GETPOSTINT($key.'min');
336 } else {
337 $value = '';
338 }
339 } elseif (preg_match('/^(integer|price|real|double)/', $object->fields[$key]['type'])) {
340 $value = price2num(GETPOST($key, 'alphanohtml')); // To fix decimal separator according to lang setup
341 } elseif ($object->fields[$key]['type'] == 'boolean') {
342 $value = ((GETPOST($key, 'aZ09') == 'on' || GETPOST($key, 'aZ09') == '1') ? 1 : 0);
343 } elseif ($object->fields[$key]['type'] == 'reference') {
344 $value = array_keys($object->param_list)[(int) GETPOST($key)].','.GETPOST($key.'2');
345 } elseif (preg_match('/^chkbxlst:/', $object->fields[$key]['type']) || $object->fields[$key]['type'] == 'checkbox') {
346 $value = '';
347 $values_arr = GETPOST($key, 'array');
348 if (!empty($values_arr)) {
349 $value = implode(',', $values_arr);
350 }
351 } else {
352 if ($key == 'lang') {
353 $value = GETPOST($key, 'aZ09');
354 } else {
355 $value = GETPOST($key, 'alphanohtml');
356 }
357 }
358 if (preg_match('/^integer:/i', $object->fields[$key]['type']) && $value == '-1') {
359 $value = ''; // This is an implicit foreign key field
360 }
361 if (!empty($object->fields[$key]['foreignkey']) && $value == '-1') {
362 $value = ''; // This is an explicit foreign key field
363 }
364 if ((preg_match('/^sellist/i', $object->fields[$key]['type']) || $object->fields[$key]['type'] == 'select') && $value === '0') {
365 $value = ''; // sellist / select blank option posts "0"; normalize to '' so notnull check works on PHP 8.0+ (see github.com/Dolibarr/dolibarr/issues/38199)
366 }
367
368 $object->$key = $value;
369
370 if ($key == 'pass_crypted' && property_exists($object, 'pass')) {
371 if (GETPOST("pass", "password")) { // If not provided, we do not change it. We never erase a password with empty.
372 $object->pass = GETPOST("pass", "password");
373 }
374 // TODO Manadatory for password not yet managed
375 } else {
376 if (!empty($val['notnull']) && $val['notnull'] > 0 && $object->$key == '' && (!isset($val['default']) || is_null($val['default']))) {
377 $error++;
378 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv($val['label'])), null, 'errors');
379 }
380 }
381
382 // Validation of fields values
383 if (getDolGlobalInt('MAIN_FEATURES_LEVEL') >= 1 || getDolGlobalString('MAIN_ACTIVATE_VALIDATION_RESULT')) {
384 if (!$error && !empty($val['validate']) && is_callable(array($object, 'validateField'))) {
385 if (!$object->validateField($object->fields, $key, $value)) {
386 $error++;
387 }
388 }
389 }
390
391 if (isModEnabled('category')) {
392 $categories = GETPOST('categories', 'array');
393 if (method_exists($object, 'setCategories')) {
394 $object->setCategories($categories);
395 }
396 }
397 }
398
399
400 // Fill array 'array_options' with data from add form
401 if (!$error) {
402 $ret = $extrafields->setOptionalsFromPost(null, $object, '@GETPOSTISSET');
403 if ($ret < 0) {
404 $error++;
405 }
406 }
407
408 if (!$error) {
409 $result = $object->update($user);
410 if ($result > 0) {
411 $action = 'view';
412 $urltogo = $backtopage ? str_replace('__ID__', (string) $result, $backtopage) : $backurlforlist;
413 $urltogo = preg_replace('/--IDFORBACKTOPAGE--/', (string) $object->id, $urltogo); // New method to autoselect project after a New on another form object creation
414 if ($urltogo && empty($noback)) {
415 header("Location: " . $urltogo);
416 exit;
417 }
418 } else {
419 $error++;
420 // Creation KO
421 setEventMessages($object->error, $object->errors, 'errors');
422 $action = 'edit';
423 }
424 } else {
425 $action = 'edit';
426 }
427}
428
429// Action to update one modulebuilder field
430$reg = array();
431if (preg_match('/^set(\w+)$/', $action, $reg) && GETPOSTINT('id') > 0 && !empty($permissiontoadd)) {
432 $object->fetch(GETPOSTINT('id'));
433
434 $keyforfield = $reg[1];
435 if (property_exists($object, $keyforfield)) {
436 if (!empty($object->fields[$keyforfield]) && in_array($object->fields[$keyforfield]['type'], array('date', 'datetime', 'timestamp'))) {
437 $object->$keyforfield = dol_mktime(GETPOSTINT($keyforfield.'hour'), GETPOSTINT($keyforfield.'min'), GETPOSTINT($keyforfield.'sec'), GETPOSTINT($keyforfield.'month'), GETPOSTINT($keyforfield.'day'), GETPOSTINT($keyforfield.'year'));
438 } else {
439 $object->$keyforfield = GETPOST($keyforfield);
440 }
441
442 $result = $object->update($user);
443
444 if ($result > 0) {
445 setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
446 $action = 'view';
447 } else {
448 $error++;
449 setEventMessages($object->error, $object->errors, 'errors');
450 $action = 'edit'.$reg[1];
451 }
452 }
453}
454
455// Action to update one extrafield
456$permissiontoeditextra = $permissiontoadd;
457if (GETPOST('attribute', 'aZ09') && isset($extrafields->attributes[$object->table_element]['perms'][GETPOST('attribute', 'aZ09')])) {
458 // For action 'update_extras', is there a specific permission set for the attribute to update
459 $permissiontoeditextra = dol_eval((string) $extrafields->attributes[$object->table_element]['perms'][GETPOST('attribute', 'aZ09')]);
460}
461
462if ($action == "update_extras" && GETPOSTINT('id') > 0 && !empty($permissiontoeditextra)) {
463 $object->fetch(GETPOSTINT('id'));
464
465 $object->oldcopy = dol_clone($object, 2); // @phan-suppress-current-line PhanTypeMismatchProperty
466
467 $attribute = GETPOST('attribute', 'aZ09');
468
469 $error = 0;
470
471 // Fill array 'array_options' with data from update form
472 $ret = $extrafields->setOptionalsFromPost(null, $object, $attribute);
473 if ($ret < 0) {
474 $error++;
475 setEventMessages($extrafields->error, $object->errors, 'errors');
476 $action = 'edit_extras';
477 } else {
478 $result = $object->updateExtraField($attribute, empty($triggermodname) ? '' : $triggermodname, $user); // TODO Remove $triggermodname to use $object->TRIGGER_PREFIX.'_MODIFY' instead
479 if ($result > 0) {
480 setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
481 $action = 'view';
482 } else {
483 $error++;
484 setEventMessages($object->error, $object->errors, 'errors');
485 $action = 'edit_extras';
486 }
487 }
488}
489
490// Action to delete
491if ($action == 'confirm_delete' && $confirm == 'yes' && !empty($permissiontodelete)) {
492 if (!($object->id > 0)) {
493 dol_print_error(null, 'Error, object must be fetched before being deleted');
494 exit;
495 }
496
497 $db->begin();
498
499 $result = $object->delete($user);
500
501 if ($result > 0) {
502 $db->commit();
503
504 // Delete OK
505 setEventMessages("RecordDeleted", null, 'mesgs');
506
507 if (empty($noback)) {
508 if (empty($backurlforlist)) {
509 print 'Error backurlforlist is not defined';
510 exit;
511 }
512 header("Location: " . $backurlforlist);
513 exit;
514 }
515 } else {
516 $db->rollback();
517
518 $error++;
519 if (!empty($object->errors)) {
520 setEventMessages(null, $object->errors, 'errors');
521 } else {
522 setEventMessages($object->error, null, 'errors');
523 }
524 }
525
526 $action = '';
527}
528
529// Remove a line
530if ($action == 'confirm_deleteline' && $confirm == 'yes' && !empty($permissiontoadd)) {
531 if (!empty($object->element) && $object->element == 'mo') {
532 $fk_movement = GETPOSTINT('fk_movement');
533 $result = $object->deleteLine($user, $lineid, 0, $fk_movement);
534 } else {
535 $result = $object->deleteLine($user, $lineid);
536 }
537
538 if ($result > 0) {
539 // Define output language
540 $outputlangs = $langs;
541 $newlang = '';
542 if (getDolGlobalInt('MAIN_MULTILANGS') /* && empty($newlang) */ && GETPOST('lang_id', 'aZ09')) {
543 $newlang = GETPOST('lang_id', 'aZ09');
544 }
545 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && is_object($object->thirdparty)) {
546 $newlang = $object->thirdparty->default_lang;
547 }
548 if (!empty($newlang)) {
549 $outputlangs = new Translate("", $conf);
550 $outputlangs->setDefaultLang($newlang);
551 }
552 if (!getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) {
553 if (method_exists($object, 'generateDocument')) {
554 $ret = $object->fetch($object->id); // Reload to get new records
555 $object->generateDocument($object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
556 }
557 }
558
559 setEventMessages($langs->trans('RecordDeleted'), null, 'mesgs');
560
561 if (empty($noback)) {
562 header('Location: '.((empty($backtopage)) ? $_SERVER["PHP_SELF"].'?id='.$object->id : $backtopage));
563 exit;
564 }
565 } else {
566 $error++;
567 setEventMessages($object->error, $object->errors, 'errors');
568 }
569 $action = '';
570}
571
572// Action validate object
573if ($action == 'confirm_validate' && $confirm == 'yes' && $permissiontoadd) {
574 if ($object->element == 'inventory' && !empty($include_sub_warehouse)) {
575 // Can happen when the conf INVENTORY_INCLUDE_SUB_WAREHOUSE is set
576 $result = $object->validate($user, false, $include_sub_warehouse);
577 } else {
578 $result = $object->validate($user);
579 }
580
581 if ($result >= 0) {
582 // Define output language
583 if (!getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) {
584 if (method_exists($object, 'generateDocument')) {
585 $outputlangs = $langs;
586 $newlang = '';
587 if (getDolGlobalInt('MAIN_MULTILANGS') /* && empty($newlang) */ && GETPOST('lang_id', 'aZ09')) {
588 $newlang = GETPOST('lang_id', 'aZ09');
589 }
590 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
591 $newlang = !empty($object->thirdparty->default_lang) ? $object->thirdparty->default_lang : "";
592 }
593 if (!empty($newlang)) {
594 $outputlangs = new Translate("", $conf);
595 $outputlangs->setDefaultLang($newlang);
596 }
597
598 $ret = $object->fetch($id); // Reload to get new records
599
600 $model = $object->model_pdf;
601
602 $retgen = $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
603 if ($retgen < 0) {
604 setEventMessages($object->error, $object->errors, 'warnings');
605 }
606 }
607 }
608 } else {
609 $error++;
610 setEventMessages($object->error, $object->errors, 'errors');
611 }
612 $action = '';
613}
614
615// Action close object
616if ($action == 'confirm_close' && $confirm == 'yes' && $permissiontoadd) {
617 $result = $object->cancel($user);
618 if ($result >= 0) {
619 // Define output language
620 if (!getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) {
621 if (method_exists($object, 'generateDocument')) {
622 $outputlangs = $langs;
623 $newlang = '';
624 if (getDolGlobalInt('MAIN_MULTILANGS') /* && empty($newlang) */ && GETPOST('lang_id', 'aZ09')) {
625 $newlang = GETPOST('lang_id', 'aZ09');
626 }
627 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
628 $newlang = $object->thirdparty->default_lang;
629 }
630 if (!empty($newlang)) {
631 $outputlangs = new Translate("", $conf);
632 $outputlangs->setDefaultLang($newlang);
633 }
634 $model = $object->model_pdf;
635 $ret = $object->fetch($id); // Reload to get new records
636
637 $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
638 }
639 }
640 } else {
641 $error++;
642 setEventMessages($object->error, $object->errors, 'errors');
643 }
644 $action = '';
645}
646
647// Action setdraft object
648if ($action == 'confirm_setdraft' && $confirm == 'yes' && $permissiontoadd) {
649 $result = $object->setDraft($user);
650 if ($result >= 0) {
651 // Nothing else done
652 } else {
653 $error++;
654 setEventMessages($object->error, $object->errors, 'errors');
655 }
656 $action = '';
657}
658
659// Action reopen object
660if ($action == 'confirm_reopen' && $confirm == 'yes' && $permissiontoadd) {
661 $result = $object->reopen($user);
662 if ($result >= 0) {
663 // Define output language
664 if (!getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) {
665 if (method_exists($object, 'generateDocument')) {
666 $outputlangs = $langs;
667 $newlang = '';
668 if (getDolGlobalInt('MAIN_MULTILANGS') /* && empty($newlang) */ && GETPOST('lang_id', 'aZ09')) {
669 $newlang = GETPOST('lang_id', 'aZ09');
670 }
671 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && is_object($object->thirdparty)) {
672 $newlang = $object->thirdparty->default_lang;
673 }
674 if (!empty($newlang)) {
675 $outputlangs = new Translate("", $conf);
676 $outputlangs->setDefaultLang($newlang);
677 }
678 $model = $object->model_pdf;
679 $ret = $object->fetch($id); // Reload to get new records
680
681 $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
682 }
683 }
684 } else {
685 $error++;
686 setEventMessages($object->error, $object->errors, 'errors');
687 }
688 $action = '';
689}
690
691// Action clone object
692if ($action == 'confirm_clone' && $confirm == 'yes' && !empty($permissiontoadd)) {
693 // @phan-suppress-next-line PhanPluginBothLiteralsBinaryOp
694 if (1 == 0 && !GETPOST('clone_content') && !GETPOST('clone_receivers')) {
695 setEventMessages($langs->trans("NoCloneOptionsSpecified"), null, 'errors');
696 } else {
697 // We clone object to avoid to denaturate loaded object when setting some properties for clone or if createFromClone modifies the object.
698 $objectutil = dol_clone($object, 1);
699 // We used native clone to keep this->db valid and allow to use later all the methods of object.
700 // $objectutil->date = dol_mktime(12, 0, 0, GETPOSTINT('newdatemonth', 'int'), GETPOSTINT('newdateday', 'int'), GETPOSTINT('newdateyear', 'int'));
701 // ...
702 $result = $objectutil->createFromClone($user, (($object->id > 0) ? $object->id : $id));
703 if (is_object($result) || $result > 0) {
704 $newid = 0;
705 if (is_object($result)) {
706 $newid = $result->id;
707 } else {
708 $newid = $result;
709 }
710
711 if (empty($noback)) {
712 header("Location: " . $_SERVER['PHP_SELF'] . '?id=' . $newid); // Open record of new object
713 exit;
714 }
715 } else {
716 $error++;
717 setEventMessages($objectutil->error, $objectutil->errors, 'errors');
718 $action = '';
719 }
720 }
721}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage translations.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_eval($s, $returnvalue=1, $hideerrors=1, $onlysimplestring='1')
Replace eval function to add more security.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_clone($srcobject, $native=2)
Create a clone of instance of object (new instance with same value for each properties) With native =...
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.