actions_addupdatedelete.inc.php 20 KB

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