dolibarr  9.0.0
actions_addupdatedelete.inc.php
Go to the documentation of this file.
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 
25 // $action or $cancel must be defined
26 // $object must be defined
27 // $permissiontoadd must be defined
28 // $permissiontodelete must be defined
29 // $backurlforlist must be defined
30 // $backtopage may be defined
31 // $triggermodname may be defined
32 
33 if ($cancel)
34 {
35  if (! empty($backtopage))
36  {
37  header("Location: ".$backtopage);
38  exit;
39  }
40  $action='';
41 }
42 
43 // Action to add record
44 if ($action == 'add' && ! empty($permissiontoadd))
45 {
46  foreach ($object->fields as $key => $val)
47  {
48  if (in_array($key, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat', 'fk_user_modif', 'import_key'))) continue; // Ignore special fields
49 
50  // Set value to insert
51  if (in_array($object->fields[$key]['type'], array('text', 'html'))) {
52  $value = GETPOST($key,'none');
53  } elseif ($object->fields[$key]['type']=='date') {
54  $value = dol_mktime(12, 0, 0, GETPOST($key.'month'), GETPOST($key.'day'), GETPOST($key.'year'));
55  } elseif ($object->fields[$key]['type']=='datetime') {
56  $value = dol_mktime(GETPOST($key.'hour'), GETPOST($key.'min'), 0, GETPOST($key.'month'), GETPOST($key.'day'), GETPOST($key.'year'));
57  } elseif ($object->fields[$key]['type']=='price') {
58  $value = price2num(GETPOST($key));
59  } else {
60  $value = GETPOST($key,'alpha');
61  }
62  if (preg_match('/^integer:/i', $object->fields[$key]['type']) && $value == '-1') $value=''; // This is an implicit foreign key field
63  if (! empty($object->fields[$key]['foreignkey']) && $value == '-1') $value=''; // This is an explicit foreign key field
64 
65  $object->$key=$value;
66  if ($val['notnull'] > 0 && $object->$key == '' && is_null($val['default']))
67  {
68  $error++;
69  setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv($val['label'])), null, 'errors');
70  }
71  }
72 
73  if (! $error)
74  {
75  $result=$object->create($user);
76  if ($result > 0)
77  {
78  // Creation OK
79  $urltogo=$backtopage?str_replace('__ID__', $result, $backtopage):$backurlforlist;
80  header("Location: ".$urltogo);
81  exit;
82  }
83  else
84  {
85  // Creation KO
86  if (! empty($object->errors)) setEventMessages(null, $object->errors, 'errors');
87  else setEventMessages($object->error, null, 'errors');
88  $action='create';
89  }
90  }
91  else
92  {
93  $action='create';
94  }
95 }
96 
97 // Action to update record
98 if ($action == 'update' && ! empty($permissiontoadd))
99 {
100  foreach ($object->fields as $key => $val)
101  {
102  if (! GETPOSTISSET($key)) continue; // The field was not submited to be edited
103  if (in_array($key, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat', 'fk_user_modif', 'import_key'))) continue; // Ignore special fields
104 
105  // Set value to update
106  if (in_array($object->fields[$key]['type'], array('text', 'html'))) {
107  $value = GETPOST($key,'none');
108  } elseif ($object->fields[$key]['type']=='date') {
109  $value = dol_mktime(12, 0, 0, GETPOST($key.'month'), GETPOST($key.'day'), GETPOST($key.'year'));
110  } elseif ($object->fields[$key]['type']=='datetime') {
111  $value = dol_mktime(GETPOST($key.'hour'), GETPOST($key.'min'), 0, GETPOST($key.'month'), GETPOST($key.'day'), GETPOST($key.'year'));
112  } elseif ($object->fields[$key]['type']=='price') {
113  $value = price2num(GETPOST($key));
114  } else {
115  $value = GETPOST($key,'alpha');
116  }
117  if (preg_match('/^integer:/i', $object->fields[$key]['type']) && $value == '-1') $value=''; // This is an implicit foreign key field
118  if (! empty($object->fields[$key]['foreignkey']) && $value == '-1') $value=''; // This is an explicit foreign key field
119 
120  $object->$key=$value;
121  if ($val['notnull'] > 0 && $object->$key == '' && is_null($val['default']))
122  {
123  $error++;
124  setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv($val['label'])), null, 'errors');
125  }
126  }
127 
128  if (! $error)
129  {
130  $result=$object->update($user);
131  if ($result > 0)
132  {
133  $action='view';
134  }
135  else
136  {
137  // Creation KO
138  setEventMessages($object->error, $object->errors, 'errors');
139  $action='edit';
140  }
141  }
142  else
143  {
144  $action='edit';
145  }
146 }
147 
148 // Action to update one extrafield
149 if ($action == "update_extras" && ! empty($permissiontoadd))
150 {
151  $object->fetch(GETPOST('id','int'));
152 
153  $attributekey = GETPOST('attribute','alpha');
154  $attributekeylong = 'options_'.$attributekey;
155  $object->array_options['options_'.$attributekey] = GETPOST($attributekeylong,' alpha');
156 
157  $result = $object->insertExtraFields(empty($triggermodname)?'':$triggermodname, $user);
158  if ($result > 0)
159  {
160  setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
161  $action = 'view';
162  }
163  else
164  {
165  setEventMessages($object->error, $object->errors, 'errors');
166  $action = 'edit_extras';
167  }
168 }
169 
170 // Action to delete
171 if ($action == 'confirm_delete' && ! empty($permissiontodelete))
172 {
173  if (! ($object->id > 0))
174  {
175  dol_print_error('', 'Error, object must be fetched before being deleted');
176  exit;
177  }
178 
179  $result=$object->delete($user);
180  if ($result > 0)
181  {
182  // Delete OK
183  setEventMessages("RecordDeleted", null, 'mesgs');
184  header("Location: ".$backurlforlist);
185  exit;
186  }
187  else
188  {
189  if (! empty($object->errors)) setEventMessages(null, $object->errors, 'errors');
190  else setEventMessages($object->error, null, 'errors');
191  }
192 }
193 
194 // Action clone object
195 if ($action == 'confirm_clone' && $confirm == 'yes' && ! empty($permissiontoadd))
196 {
197  if (1==0 && ! GETPOST('clone_content') && ! GETPOST('clone_receivers'))
198  {
199  setEventMessages($langs->trans("NoCloneOptionsSpecified"), null, 'errors');
200  }
201  else
202  {
203  if ($object->id > 0)
204  {
205  // Because createFromClone modifies the object, we must clone it so that we can restore it later if error
206  $orig = clone $object;
207 
208  $result=$object->createFromClone($user, $object->id);
209  if ($result > 0)
210  {
211  $newid = 0;
212  if (is_object($result)) $newid = $result->id;
213  else $newid = $result;
214  header("Location: ".$_SERVER['PHP_SELF'].'?id='.$newid); // Open record of new object
215  exit;
216  }
217  else
218  {
219  setEventMessages($object->error, $object->errors, 'errors');
220  $object = $orig;
221  $action='';
222  }
223  }
224  }
225 }
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm=false, $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
GETPOSTISSET($paramname)
Return true if we are in a context of submitting a parameter.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...