actions_addupdatedelete.inc.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. /**
  19. * \file htdocs/core/actions_addupdatedelete.inc.php
  20. * \brief Code for common actions cancel / add / update / update_extras / delete / deleteline / validate / cancel / reopen / clone
  21. */
  22. // $action or $cancel must be defined
  23. // $object must be defined
  24. // $permissiontoadd must be defined
  25. // $permissiontodelete must be defined
  26. // $backurlforlist must be defined
  27. // $backtopage may be defined
  28. // $noback may be defined
  29. // $triggermodname may be defined
  30. $hidedetails = isset($hidedetails) ? $hidedetails : '';
  31. $hidedesc = isset($hidedesc) ? $hidedesc : '';
  32. $hideref = isset($hideref) ? $hideref : '';
  33. if (!empty($permissionedit) && empty($permissiontoadd)) {
  34. $permissiontoadd = $permissionedit; // For backward compatibility
  35. }
  36. if ($cancel) {
  37. /*var_dump($cancel);var_dump($backtopage);var_dump($backtopageforcancel);exit;*/
  38. if (!empty($backtopageforcancel)) {
  39. header("Location: ".$backtopageforcancel);
  40. exit;
  41. } elseif (!empty($backtopage)) {
  42. header("Location: ".$backtopage);
  43. exit;
  44. }
  45. $action = '';
  46. }
  47. // Action to add record
  48. if ($action == 'add' && !empty($permissiontoadd)) {
  49. foreach ($object->fields as $key => $val) {
  50. if ($object->fields[$key]['type'] == 'duration') {
  51. if (GETPOST($key.'hour') == '' && GETPOST($key.'min') == '') {
  52. continue; // The field was not submited to be saved
  53. }
  54. } else {
  55. if (!GETPOSTISSET($key) && !preg_match('/^chkbxlst:/', $object->fields[$key]['type'])) {
  56. continue; // The field was not submited to be saved
  57. }
  58. }
  59. // Ignore special fields
  60. if (in_array($key, array('rowid', 'entity', 'import_key'))) {
  61. continue;
  62. }
  63. if (in_array($key, array('date_creation', 'tms', 'fk_user_creat', 'fk_user_modif'))) {
  64. if (!in_array(abs($val['visible']), array(1, 3))) {
  65. continue; // Only 1 and 3 that are case to create
  66. }
  67. }
  68. // Set value to insert
  69. if (preg_match('/^text/', $object->fields[$key]['type'])) {
  70. $tmparray = explode(':', $object->fields[$key]['type']);
  71. if (!empty($tmparray[1])) {
  72. $value = GETPOST($key, $tmparray[1]);
  73. } else {
  74. $value = GETPOST($key, 'nohtml');
  75. }
  76. } elseif (preg_match('/^html/', $object->fields[$key]['type'])) {
  77. $tmparray = explode(':', $object->fields[$key]['type']);
  78. if (!empty($tmparray[1])) {
  79. $value = GETPOST($key, $tmparray[1]);
  80. } else {
  81. $value = GETPOST($key, 'restricthtml');
  82. }
  83. } elseif ($object->fields[$key]['type'] == 'date') {
  84. $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
  85. } elseif ($object->fields[$key]['type'] == 'datetime') {
  86. $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');
  87. } elseif ($object->fields[$key]['type'] == 'duration') {
  88. $value = 60 * 60 * GETPOSTINT($key.'hour') + 60 * GETPOSTINT($key.'min');
  89. } elseif (preg_match('/^(integer|price|real|double)/', $object->fields[$key]['type'])) {
  90. $value = price2num(GETPOST($key, 'alphanohtml')); // To fix decimal separator according to lang setup
  91. } elseif ($object->fields[$key]['type'] == 'boolean') {
  92. $value = ((GETPOST($key) == '1' || GETPOST($key) == 'on') ? 1 : 0);
  93. } elseif ($object->fields[$key]['type'] == 'reference') {
  94. $tmparraykey = array_keys($object->param_list);
  95. $value = $tmparraykey[GETPOST($key)].','.GETPOST($key.'2');
  96. } elseif (preg_match('/^chkbxlst:(.*)/', $object->fields[$key]['type']) || $object->fields[$key]['type'] == 'checkbox') {
  97. $value = '';
  98. $values_arr = GETPOST($key, 'array');
  99. if (!empty($values_arr)) {
  100. $value = implode(',', $values_arr);
  101. }
  102. } else {
  103. if ($key == 'lang') {
  104. $value = GETPOST($key, 'aZ09') ? GETPOST($key, 'aZ09') : "";
  105. } else {
  106. $value = GETPOST($key, 'alphanohtml');
  107. }
  108. }
  109. if (preg_match('/^integer:/i', $object->fields[$key]['type']) && $value == '-1') {
  110. $value = ''; // This is an implicit foreign key field
  111. }
  112. if (!empty($object->fields[$key]['foreignkey']) && $value == '-1') {
  113. $value = ''; // This is an explicit foreign key field
  114. }
  115. //var_dump($key.' '.$value.' '.$object->fields[$key]['type'].' '.$object->fields[$key]['notnull']);
  116. $object->$key = $value;
  117. if (!empty($val['notnull']) && $val['notnull'] > 0 && $object->$key == '' && isset($val['default']) && $val['default'] == '(PROV)') {
  118. $object->$key = '(PROV)';
  119. }
  120. if ($key == 'pass_crypted') {
  121. $object->pass = GETPOST("pass", "none");
  122. // TODO Manadatory for password not yet managed
  123. } else {
  124. if (!empty($val['notnull']) && $val['notnull'] > 0 && $object->$key == '' && !isset($val['default'])) {
  125. $error++;
  126. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv($val['label'])), null, 'errors');
  127. }
  128. }
  129. // Validation of fields values
  130. if (getDolGlobalInt('MAIN_FEATURES_LEVEL') >= 1 || getDolGlobalString('MAIN_ACTIVATE_VALIDATION_RESULT')) {
  131. if (!$error && !empty($val['validate']) && is_callable(array($object, 'validateField'))) {
  132. if (!$object->validateField($object->fields, $key, $value)) {
  133. $error++;
  134. }
  135. }
  136. }
  137. }
  138. // Fill array 'array_options' with data from add form
  139. if (!$error) {
  140. $ret = $extrafields->setOptionalsFromPost(null, $object, '', 1);
  141. if ($ret < 0) {
  142. $error++;
  143. }
  144. }
  145. if (!$error) {
  146. $db->begin();
  147. $result = $object->create($user);
  148. if ($result > 0) {
  149. // Creation OK
  150. if (isModEnabled('categorie') && method_exists($object, 'setCategories')) {
  151. $categories = GETPOST('categories', 'array:int');
  152. $object->setCategories($categories);
  153. }
  154. $urltogo = $backtopage ? str_replace('__ID__', $result, $backtopage) : $backurlforlist;
  155. $urltogo = preg_replace('/--IDFORBACKTOPAGE--/', $object->id, $urltogo); // New method to autoselect project after a New on another form object creation
  156. $db->commit();
  157. if (empty($noback)) {
  158. header("Location: " . $urltogo);
  159. exit;
  160. }
  161. } else {
  162. $db->rollback();
  163. $error++;
  164. // Creation KO
  165. if (!empty($object->errors)) {
  166. setEventMessages(null, $object->errors, 'errors');
  167. } else {
  168. setEventMessages($object->error, null, 'errors');
  169. }
  170. $action = 'create';
  171. }
  172. } else {
  173. $action = 'create';
  174. }
  175. }
  176. // Action to update record
  177. if ($action == 'update' && !empty($permissiontoadd)) {
  178. foreach ($object->fields as $key => $val) {
  179. // Check if field was submited to be edited
  180. if ($object->fields[$key]['type'] == 'duration') {
  181. if (!GETPOSTISSET($key.'hour') || !GETPOSTISSET($key.'min')) {
  182. continue; // The field was not submited to be saved
  183. }
  184. } elseif ($object->fields[$key]['type'] == 'boolean') {
  185. if (!GETPOSTISSET($key)) {
  186. $object->$key = 0; // use 0 instead null if the field is defined as not null
  187. continue;
  188. }
  189. } else {
  190. if (!GETPOSTISSET($key) && !preg_match('/^chkbxlst:/', $object->fields[$key]['type']) && $object->fields[$key]['type']!=='checkbox') {
  191. continue; // The field was not submited to be saved
  192. }
  193. }
  194. // Ignore special fields
  195. if (in_array($key, array('rowid', 'entity', 'import_key'))) {
  196. continue;
  197. }
  198. if (in_array($key, array('date_creation', 'tms', 'fk_user_creat', 'fk_user_modif'))) {
  199. if (!in_array(abs($val['visible']), array(1, 3, 4))) {
  200. continue; // Only 1 and 3 and 4, that are cases to update
  201. }
  202. }
  203. // Set value to update
  204. if (preg_match('/^text/', $object->fields[$key]['type'])) {
  205. $tmparray = explode(':', $object->fields[$key]['type']);
  206. if (!empty($tmparray[1])) {
  207. $value = GETPOST($key, $tmparray[1]);
  208. } else {
  209. $value = GETPOST($key, 'nohtml');
  210. }
  211. } elseif (preg_match('/^html/', $object->fields[$key]['type'])) {
  212. $tmparray = explode(':', $object->fields[$key]['type']);
  213. if (!empty($tmparray[1])) {
  214. $value = GETPOST($key, $tmparray[1]);
  215. } else {
  216. $value = GETPOST($key, 'restricthtml');
  217. }
  218. } elseif ($object->fields[$key]['type'] == 'date') {
  219. $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
  220. } elseif ($object->fields[$key]['type'] == 'datetime') {
  221. $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');
  222. } elseif ($object->fields[$key]['type'] == 'duration') {
  223. if (GETPOST($key.'hour', 'int') != '' || GETPOST($key.'min', 'int') != '') {
  224. $value = 60 * 60 * GETPOST($key.'hour', 'int') + 60 * GETPOST($key.'min', 'int');
  225. } else {
  226. $value = '';
  227. }
  228. } elseif (preg_match('/^(integer|price|real|double)/', $object->fields[$key]['type'])) {
  229. $value = price2num(GETPOST($key, 'alphanohtml')); // To fix decimal separator according to lang setup
  230. } elseif ($object->fields[$key]['type'] == 'boolean') {
  231. $value = ((GETPOST($key, 'aZ09') == 'on' || GETPOST($key, 'aZ09') == '1') ? 1 : 0);
  232. } elseif ($object->fields[$key]['type'] == 'reference') {
  233. $value = array_keys($object->param_list)[GETPOST($key)].','.GETPOST($key.'2');
  234. } elseif (preg_match('/^chkbxlst:/', $object->fields[$key]['type']) || $object->fields[$key]['type'] == 'checkbox') {
  235. $value = '';
  236. $values_arr = GETPOST($key, 'array');
  237. if (!empty($values_arr)) {
  238. $value = implode(',', $values_arr);
  239. }
  240. } else {
  241. if ($key == 'lang') {
  242. $value = GETPOST($key, 'aZ09');
  243. } else {
  244. $value = GETPOST($key, 'alphanohtml');
  245. }
  246. }
  247. if (preg_match('/^integer:/i', $object->fields[$key]['type']) && $value == '-1') {
  248. $value = ''; // This is an implicit foreign key field
  249. }
  250. if (!empty($object->fields[$key]['foreignkey']) && $value == '-1') {
  251. $value = ''; // This is an explicit foreign key field
  252. }
  253. $object->$key = $value;
  254. if ($val['notnull'] > 0 && $object->$key == '' && (!isset($val['default']) || is_null($val['default']))) {
  255. $error++;
  256. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv($val['label'])), null, 'errors');
  257. }
  258. // Validation of fields values
  259. if (getDolGlobalInt('MAIN_FEATURES_LEVEL') >= 1 || getDolGlobalString('MAIN_ACTIVATE_VALIDATION_RESULT')) {
  260. if (!$error && !empty($val['validate']) && is_callable(array($object, 'validateField'))) {
  261. if (!$object->validateField($object->fields, $key, $value)) {
  262. $error++;
  263. }
  264. }
  265. }
  266. if (isModEnabled('categorie')) {
  267. $categories = GETPOST('categories', 'array');
  268. if (method_exists($object, 'setCategories')) {
  269. $object->setCategories($categories);
  270. }
  271. }
  272. }
  273. // Fill array 'array_options' with data from add form
  274. if (!$error) {
  275. $ret = $extrafields->setOptionalsFromPost(null, $object, '@GETPOSTISSET');
  276. if ($ret < 0) {
  277. $error++;
  278. }
  279. }
  280. if (!$error) {
  281. $result = $object->update($user);
  282. if ($result > 0) {
  283. $action = 'view';
  284. $urltogo = $backtopage ? str_replace('__ID__', $result, $backtopage) : $backurlforlist;
  285. $urltogo = preg_replace('/--IDFORBACKTOPAGE--/', $object->id, $urltogo); // New method to autoselect project after a New on another form object creation
  286. if ($urltogo && empty($noback)) {
  287. header("Location: " . $urltogo);
  288. exit;
  289. }
  290. } else {
  291. $error++;
  292. // Creation KO
  293. setEventMessages($object->error, $object->errors, 'errors');
  294. $action = 'edit';
  295. }
  296. } else {
  297. $action = 'edit';
  298. }
  299. }
  300. // Action to update one modulebuilder field
  301. $reg = array();
  302. if (preg_match('/^set(\w+)$/', $action, $reg) && GETPOST('id', 'int') > 0 && !empty($permissiontoadd)) {
  303. $object->fetch(GETPOST('id', 'int'));
  304. $keyforfield = $reg[1];
  305. if (property_exists($object, $keyforfield)) {
  306. if (!empty($object->fields[$keyforfield]) && in_array($object->fields[$keyforfield]['type'], array('date', 'datetime', 'timestamp'))) {
  307. $object->$keyforfield = dol_mktime(GETPOST($keyforfield.'hour'), GETPOST($keyforfield.'min'), GETPOST($keyforfield.'sec'), GETPOST($keyforfield.'month'), GETPOST($keyforfield.'day'), GETPOST($keyforfield.'year'));
  308. } else {
  309. $object->$keyforfield = GETPOST($keyforfield);
  310. }
  311. $result = $object->update($user);
  312. if ($result > 0) {
  313. setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
  314. $action = 'view';
  315. } else {
  316. $error++;
  317. setEventMessages($object->error, $object->errors, 'errors');
  318. $action = 'edit'.$reg[1];
  319. }
  320. }
  321. }
  322. // Action to update one extrafield
  323. if ($action == "update_extras" && GETPOST('id', 'int') > 0 && !empty($permissiontoadd)) {
  324. $object->fetch(GETPOST('id', 'int'));
  325. $object->oldcopy = dol_clone($object, 2);
  326. $attribute = GETPOST('attribute', 'alphanohtml');
  327. $error = 0;
  328. // Fill array 'array_options' with data from update form
  329. $ret = $extrafields->setOptionalsFromPost(null, $object, $attribute);
  330. if ($ret < 0) {
  331. $error++;
  332. setEventMessages($extrafields->error, $object->errors, 'errors');
  333. $action = 'edit_extras';
  334. } else {
  335. $result = $object->updateExtraField($attribute, empty($triggermodname) ? '' : $triggermodname, $user);
  336. if ($result > 0) {
  337. setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
  338. $action = 'view';
  339. } else {
  340. $error++;
  341. setEventMessages($object->error, $object->errors, 'errors');
  342. $action = 'edit_extras';
  343. }
  344. }
  345. }
  346. // Action to delete
  347. if ($action == 'confirm_delete' && !empty($permissiontodelete)) {
  348. if (!($object->id > 0)) {
  349. dol_print_error('', 'Error, object must be fetched before being deleted');
  350. exit;
  351. }
  352. $db->begin();
  353. $result = $object->delete($user);
  354. if ($result > 0) {
  355. $db->commit();
  356. // Delete OK
  357. setEventMessages("RecordDeleted", null, 'mesgs');
  358. if (empty($noback)) {
  359. if (empty($backurlforlist)) {
  360. print 'Error backurlforlist is not defined';
  361. exit;
  362. }
  363. header("Location: " . $backurlforlist);
  364. exit;
  365. }
  366. } else {
  367. $db->rollback();
  368. $error++;
  369. if (!empty($object->errors)) {
  370. setEventMessages(null, $object->errors, 'errors');
  371. } else {
  372. setEventMessages($object->error, null, 'errors');
  373. }
  374. }
  375. $action = '';
  376. }
  377. // Remove a line
  378. if ($action == 'confirm_deleteline' && $confirm == 'yes' && !empty($permissiontoadd)) {
  379. if (method_exists($object, 'deleteline')) {
  380. $result = $object->deleteline($user, $lineid); // For backward compatibility
  381. } else {
  382. $result = $object->deleteLine($user, $lineid);
  383. }
  384. if ($result > 0) {
  385. // Define output language
  386. $outputlangs = $langs;
  387. $newlang = '';
  388. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
  389. $newlang = GETPOST('lang_id', 'aZ09');
  390. }
  391. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && is_object($object->thirdparty)) {
  392. $newlang = $object->thirdparty->default_lang;
  393. }
  394. if (!empty($newlang)) {
  395. $outputlangs = new Translate("", $conf);
  396. $outputlangs->setDefaultLang($newlang);
  397. }
  398. if (!getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) {
  399. if (method_exists($object, 'generateDocument')) {
  400. $ret = $object->fetch($object->id); // Reload to get new records
  401. $object->generateDocument($object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  402. }
  403. }
  404. setEventMessages($langs->trans('RecordDeleted'), null, 'mesgs');
  405. if (empty($noback)) {
  406. header('Location: '.((empty($backtopage)) ? $_SERVER["PHP_SELF"].'?id='.$object->id : $backtopage));
  407. exit;
  408. }
  409. } else {
  410. $error++;
  411. setEventMessages($object->error, $object->errors, 'errors');
  412. }
  413. $action = '';
  414. }
  415. // Action validate object
  416. if ($action == 'confirm_validate' && $confirm == 'yes' && $permissiontoadd) {
  417. if ($object->element == 'inventory' && !empty($include_sub_warehouse)) {
  418. // Can happen when the conf INVENTORY_INCLUDE_SUB_WAREHOUSE is set
  419. $result = $object->validate($user, false, $include_sub_warehouse);
  420. } else {
  421. $result = $object->validate($user);
  422. }
  423. if ($result >= 0) {
  424. // Define output language
  425. if (!getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) {
  426. if (method_exists($object, 'generateDocument')) {
  427. $outputlangs = $langs;
  428. $newlang = '';
  429. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
  430. $newlang = GETPOST('lang_id', 'aZ09');
  431. }
  432. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
  433. $newlang = !empty($object->thirdparty->default_lang) ? $object->thirdparty->default_lang : "";
  434. }
  435. if (!empty($newlang)) {
  436. $outputlangs = new Translate("", $conf);
  437. $outputlangs->setDefaultLang($newlang);
  438. }
  439. $ret = $object->fetch($id); // Reload to get new records
  440. $model = $object->model_pdf;
  441. $retgen = $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  442. if ($retgen < 0) {
  443. setEventMessages($object->error, $object->errors, 'warnings');
  444. }
  445. }
  446. }
  447. } else {
  448. $error++;
  449. setEventMessages($object->error, $object->errors, 'errors');
  450. }
  451. $action = '';
  452. }
  453. // Action close object
  454. if ($action == 'confirm_close' && $confirm == 'yes' && $permissiontoadd) {
  455. $result = $object->cancel($user);
  456. if ($result >= 0) {
  457. // Define output language
  458. if (!getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) {
  459. if (method_exists($object, 'generateDocument')) {
  460. $outputlangs = $langs;
  461. $newlang = '';
  462. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
  463. $newlang = GETPOST('lang_id', 'aZ09');
  464. }
  465. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
  466. $newlang = $object->thirdparty->default_lang;
  467. }
  468. if (!empty($newlang)) {
  469. $outputlangs = new Translate("", $conf);
  470. $outputlangs->setDefaultLang($newlang);
  471. }
  472. $model = $object->model_pdf;
  473. $ret = $object->fetch($id); // Reload to get new records
  474. $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  475. }
  476. }
  477. } else {
  478. $error++;
  479. setEventMessages($object->error, $object->errors, 'errors');
  480. }
  481. $action = '';
  482. }
  483. // Action setdraft object
  484. if ($action == 'confirm_setdraft' && $confirm == 'yes' && $permissiontoadd) {
  485. $result = $object->setDraft($user);
  486. if ($result >= 0) {
  487. // Nothing else done
  488. } else {
  489. $error++;
  490. setEventMessages($object->error, $object->errors, 'errors');
  491. }
  492. $action = '';
  493. }
  494. // Action reopen object
  495. if ($action == 'confirm_reopen' && $confirm == 'yes' && $permissiontoadd) {
  496. $result = $object->reopen($user);
  497. if ($result >= 0) {
  498. // Define output language
  499. if (!getDolGlobalString('MAIN_DISABLE_PDF_AUTOUPDATE')) {
  500. if (method_exists($object, 'generateDocument')) {
  501. $outputlangs = $langs;
  502. $newlang = '';
  503. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
  504. $newlang = GETPOST('lang_id', 'aZ09');
  505. }
  506. if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
  507. $newlang = $object->thirdparty->default_lang;
  508. }
  509. if (!empty($newlang)) {
  510. $outputlangs = new Translate("", $conf);
  511. $outputlangs->setDefaultLang($newlang);
  512. }
  513. $model = $object->model_pdf;
  514. $ret = $object->fetch($id); // Reload to get new records
  515. $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  516. }
  517. }
  518. } else {
  519. $error++;
  520. setEventMessages($object->error, $object->errors, 'errors');
  521. }
  522. $action = '';
  523. }
  524. // Action clone object
  525. if ($action == 'confirm_clone' && $confirm == 'yes' && !empty($permissiontoadd)) {
  526. if (1 == 0 && !GETPOST('clone_content') && !GETPOST('clone_receivers')) {
  527. setEventMessages($langs->trans("NoCloneOptionsSpecified"), null, 'errors');
  528. } else {
  529. // We clone object to avoid to denaturate loaded object when setting some properties for clone or if createFromClone modifies the object.
  530. $objectutil = dol_clone($object, 1);
  531. // We used native clone to keep this->db valid and allow to use later all the methods of object.
  532. //$objectutil->date = dol_mktime(12, 0, 0, GETPOST('newdatemonth', 'int'), GETPOST('newdateday', 'int'), GETPOST('newdateyear', 'int'));
  533. // ...
  534. $result = $objectutil->createFromClone($user, (($object->id > 0) ? $object->id : $id));
  535. if (is_object($result) || $result > 0) {
  536. $newid = 0;
  537. if (is_object($result)) {
  538. $newid = $result->id;
  539. } else {
  540. $newid = $result;
  541. }
  542. if (empty($noback)) {
  543. header("Location: " . $_SERVER['PHP_SELF'] . '?id=' . $newid); // Open record of new object
  544. exit;
  545. }
  546. } else {
  547. $error++;
  548. setEventMessages($objectutil->error, $objectutil->errors, 'errors');
  549. $action = '';
  550. }
  551. }
  552. }