actions_addupdatedelete.inc.php 16 KB

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