actions_addupdatedelete.inc.php 21 KB

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