actions_addupdatedelete.inc.php 15 KB

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