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