dolibarr  7.0.0-beta
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 
32 if ($cancel)
33 {
34  if (! empty($backtopage))
35  {
36  header("Location: ".$backtopage);
37  exit;
38  }
39  $action='';
40 }
41 
42 // Action to add record
43 if ($action == 'add' && ! empty($permissiontoadd))
44 {
45  foreach ($object->fields as $key => $val)
46  {
47  if (in_array($key, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat', 'fk_user_modif', 'import_key'))) continue; // Ignore special fields
48 
49  // Set value to insert
50  if (in_array($object->fields[$key]['type'], array('text', 'html'))) $value = GETPOST($key,'none');
51  else $value = GETPOST($key,'alpha');
52  if (preg_match('/^integer:/i', $object->fields[$key]['type']) && $value == '-1') $value=''; // This is an implicit foreign key field
53  if (! empty($object->fields[$key]['foreignkey']) && $value == '-1') $value=''; // This is an explicit foreign key field
54 
55  $object->$key=$value;
56  if ($val['notnull'] > 0 && $object->$key == '')
57  {
58  $error++;
59  setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv($val['label'])), null, 'errors');
60  }
61  }
62 
63  if (! $error)
64  {
65  $result=$object->createCommon($user);
66  if ($result > 0)
67  {
68  // Creation OK
69  $urltogo=$backtopage?$backtopage:$backurlforlist;
70  header("Location: ".$urltogo);
71  exit;
72  }
73  else
74  {
75  // Creation KO
76  if (! empty($object->errors)) setEventMessages(null, $object->errors, 'errors');
77  else setEventMessages($object->error, null, 'errors');
78  $action='create';
79  }
80  }
81  else
82  {
83  $action='create';
84  }
85 }
86 
87 // Action to update record
88 if ($action == 'update' && ! empty($permissiontoadd))
89 {
90  foreach ($object->fields as $key => $val)
91  {
92  if (! GETPOSTISSET($key)) continue; // The field was not submited to be edited
93  if (in_array($key, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat', 'fk_user_modif', 'import_key'))) continue; // Ignore special fields
94 
95  // Set value to update
96  if (in_array($object->fields[$key]['type'], array('text', 'html'))) $value = GETPOST($key,'none');
97  else $value = GETPOST($key,'alpha');
98  if (preg_match('/^integer:/i', $object->fields[$key]['type']) && $value == '-1') $value=''; // This is an implicit foreign key field
99  if (! empty($object->fields[$key]['foreignkey']) && $value == '-1') $value=''; // This is an explicit foreign key field
100 
101  $object->$key=$value;
102  if ($val['notnull'] > 0 && $object->$key == '')
103  {
104  $error++;
105  setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv($val['label'])), null, 'errors');
106  }
107  }
108 
109  if (! $error)
110  {
111  $result=$object->updateCommon($user);
112  if ($result > 0)
113  {
114  $action='view';
115  }
116  else
117  {
118  // Creation KO
119  if (! empty($object->errors)) setEventMessages(null, $object->errors, 'errors');
120  else setEventMessages($object->error, null, 'errors');
121  $action='edit';
122  }
123  }
124  else
125  {
126  $action='edit';
127  }
128 }
129 
130 // Action to delete
131 if ($action == 'confirm_delete' && ! empty($permissiontodelete))
132 {
133  $result=$object->deleteCommon($user);
134  if ($result > 0)
135  {
136  // Delete OK
137  setEventMessages("RecordDeleted", null, 'mesgs');
138  header("Location: ".$backurlforlist);
139  exit;
140  }
141  else
142  {
143  if (! empty($object->errors)) setEventMessages(null, $object->errors, 'errors');
144  else setEventMessages($object->error, null, 'errors');
145  }
146 }
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting a parameter.