actions_addupdatedelete.inc.php 18 KB

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