actions_addupdatedelete.inc.php 18 KB

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