actions_addupdatedelete.inc.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /* Copyright (C) 2017 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 <http://www.gnu.org/licenses/>.
  16. * or see http://www.gnu.org/
  17. */
  18. /**
  19. * \file htdocs/core/actions_addupdatedelete.inc.php
  20. * \brief Code for common actions cancel / add / update / delete / 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 ($cancel)
  30. {
  31. if (! empty($backtopage))
  32. {
  33. header("Location: ".$backtopage);
  34. exit;
  35. }
  36. $action='';
  37. }
  38. // Action to add record
  39. if ($action == 'add' && ! empty($permissiontoadd))
  40. {
  41. foreach ($object->fields as $key => $val)
  42. {
  43. if (in_array($key, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat', 'fk_user_modif', 'import_key'))) continue; // Ignore special fields
  44. // Set value to insert
  45. if (in_array($object->fields[$key]['type'], array('text', 'html'))) {
  46. $value = GETPOST($key,'none');
  47. } elseif ($object->fields[$key]['type']=='date') {
  48. $value = dol_mktime(12, 0, 0, GETPOST($key.'month'), GETPOST($key.'day'), GETPOST($key.'year'));
  49. } elseif ($object->fields[$key]['type']=='datetime') {
  50. $value = dol_mktime(GETPOST($key.'hour'), GETPOST($key.'min'), 0, GETPOST($key.'month'), GETPOST($key.'day'), GETPOST($key.'year'));
  51. } elseif ($object->fields[$key]['type']=='price') {
  52. $value = price2num(GETPOST($key));
  53. } else {
  54. $value = GETPOST($key,'alpha');
  55. }
  56. if (preg_match('/^integer:/i', $object->fields[$key]['type']) && $value == '-1') $value=''; // This is an implicit foreign key field
  57. if (! empty($object->fields[$key]['foreignkey']) && $value == '-1') $value=''; // This is an explicit foreign key field
  58. $object->$key=$value;
  59. if ($val['notnull'] > 0 && $object->$key == '' && is_null($val['default']))
  60. {
  61. $error++;
  62. setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv($val['label'])), null, 'errors');
  63. }
  64. }
  65. if (! $error)
  66. {
  67. $result=$object->create($user);
  68. if ($result > 0)
  69. {
  70. // Creation OK
  71. $urltogo=$backtopage?str_replace('__ID__', $result, $backtopage):$backurlforlist;
  72. header("Location: ".$urltogo);
  73. exit;
  74. }
  75. else
  76. {
  77. // Creation KO
  78. if (! empty($object->errors)) setEventMessages(null, $object->errors, 'errors');
  79. else setEventMessages($object->error, null, 'errors');
  80. $action='create';
  81. }
  82. }
  83. else
  84. {
  85. $action='create';
  86. }
  87. }
  88. // Action to update record
  89. if ($action == 'update' && ! empty($permissiontoadd))
  90. {
  91. foreach ($object->fields as $key => $val)
  92. {
  93. if (! GETPOSTISSET($key)) continue; // The field was not submited to be edited
  94. if (in_array($key, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat', 'fk_user_modif', 'import_key'))) continue; // Ignore special fields
  95. // Set value to update
  96. if (in_array($object->fields[$key]['type'], array('text', 'html'))) {
  97. $value = GETPOST($key,'none');
  98. } elseif ($object->fields[$key]['type']=='date') {
  99. $value = dol_mktime(12, 0, 0, GETPOST($key.'month'), GETPOST($key.'day'), GETPOST($key.'year'));
  100. } elseif ($object->fields[$key]['type']=='datetime') {
  101. $value = dol_mktime(GETPOST($key.'hour'), GETPOST($key.'min'), 0, GETPOST($key.'month'), GETPOST($key.'day'), GETPOST($key.'year'));
  102. } elseif ($object->fields[$key]['type']=='price') {
  103. $value = price2num(GETPOST($key));
  104. } else {
  105. $value = GETPOST($key,'alpha');
  106. }
  107. if (preg_match('/^integer:/i', $object->fields[$key]['type']) && $value == '-1') $value=''; // This is an implicit foreign key field
  108. if (! empty($object->fields[$key]['foreignkey']) && $value == '-1') $value=''; // This is an explicit foreign key field
  109. $object->$key=$value;
  110. if ($val['notnull'] > 0 && $object->$key == '' && is_null($val['default']))
  111. {
  112. $error++;
  113. setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv($val['label'])), null, 'errors');
  114. }
  115. }
  116. if (! $error)
  117. {
  118. $result=$object->update($user);
  119. if ($result > 0)
  120. {
  121. $action='view';
  122. }
  123. else
  124. {
  125. // Creation KO
  126. setEventMessages($object->error, $object->errors, 'errors');
  127. $action='edit';
  128. }
  129. }
  130. else
  131. {
  132. $action='edit';
  133. }
  134. }
  135. // Action to update one extrafield
  136. if ($action == "update_extras" && ! empty($permissiontoadd))
  137. {
  138. $object->fetch(GETPOST('id','int'));
  139. $attributekey = GETPOST('attribute','alpha');
  140. $attributekeylong = 'options_'.$attributekey;
  141. $object->array_options['options_'.$attributekey] = GETPOST($attributekeylong,' alpha');
  142. $result = $object->insertExtraFields(empty($triggermodname)?'':$triggermodname, $user);
  143. if ($result > 0)
  144. {
  145. setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
  146. $action = 'view';
  147. }
  148. else
  149. {
  150. setEventMessages($object->error, $object->errors, 'errors');
  151. $action = 'edit_extras';
  152. }
  153. }
  154. // Action to delete
  155. if ($action == 'confirm_delete' && ! empty($permissiontodelete))
  156. {
  157. if (! ($object->id > 0))
  158. {
  159. dol_print_error('', 'Error, object must be fetched before being deleted');
  160. exit;
  161. }
  162. $result=$object->delete($user);
  163. if ($result > 0)
  164. {
  165. // Delete OK
  166. setEventMessages("RecordDeleted", null, 'mesgs');
  167. header("Location: ".$backurlforlist);
  168. exit;
  169. }
  170. else
  171. {
  172. if (! empty($object->errors)) setEventMessages(null, $object->errors, 'errors');
  173. else setEventMessages($object->error, null, 'errors');
  174. }
  175. }
  176. // Action clone object
  177. if ($action == 'confirm_clone' && $confirm == 'yes' && ! empty($permissiontoadd))
  178. {
  179. if (1==0 && ! GETPOST('clone_content') && ! GETPOST('clone_receivers'))
  180. {
  181. setEventMessages($langs->trans("NoCloneOptionsSpecified"), null, 'errors');
  182. }
  183. else
  184. {
  185. if ($object->id > 0)
  186. {
  187. // Because createFromClone modifies the object, we must clone it so that we can restore it later if error
  188. $orig = clone $object;
  189. $result=$object->createFromClone($user, $object->id);
  190. if ($result > 0)
  191. {
  192. $newid = 0;
  193. if (is_object($result)) $newid = $result->id;
  194. else $newid = $result;
  195. header("Location: ".$_SERVER['PHP_SELF'].'?id='.$newid); // Open record of new object
  196. exit;
  197. }
  198. else
  199. {
  200. setEventMessages($object->error, $object->errors, 'errors');
  201. $object = $orig;
  202. $action='';
  203. }
  204. }
  205. }
  206. }