actions_addupdatedelete.inc.php 20 KB

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