actions_addupdatedelete.inc.php 20 KB

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