actions_addupdatedelete.inc.php 20 KB

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