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