actions_addupdatedelete.inc.php 15 KB

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