|
1
|
|
|
<?php |
|
2
|
|
|
/* Copyright (C) 2015 Jean-François Ferry <[email protected]> |
|
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
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
use Luracast\Restler\RestException; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* API class for skeleton object |
|
23
|
|
|
* |
|
24
|
|
|
* @smart-auto-routing false |
|
25
|
|
|
* @access protected |
|
26
|
|
|
* @class DolibarrApiAccess {@requires user,external} |
|
27
|
|
|
* |
|
28
|
|
|
* |
|
29
|
|
|
*/ |
|
30
|
|
|
class SkeletonApi extends DolibarrApi |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var array $FIELDS Mandatory fields, checked when create and update object |
|
34
|
|
|
*/ |
|
35
|
|
|
static $FIELDS = array( |
|
36
|
|
|
'name' |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Skeleton $skeleton {@type Skeleton} |
|
41
|
|
|
*/ |
|
42
|
|
|
public $skeleton; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Constructor |
|
46
|
|
|
* |
|
47
|
|
|
* @url GET skeleton/ |
|
48
|
|
|
* |
|
49
|
|
|
*/ |
|
50
|
|
|
function __construct() |
|
51
|
|
|
{ |
|
52
|
|
|
global $db, $conf; |
|
53
|
|
|
$this->db = $db; |
|
54
|
|
|
$this->skeleton = new Skeleton($this->db); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get properties of a skeleton object |
|
59
|
|
|
* |
|
60
|
|
|
* Return an array with skeleton informations |
|
61
|
|
|
* |
|
62
|
|
|
* @param int $id ID of skeleton |
|
63
|
|
|
* @return array|mixed data without useless information |
|
64
|
|
|
* |
|
65
|
|
|
* @url GET skeleton/{id} |
|
66
|
|
|
* @throws RestException |
|
67
|
|
|
*/ |
|
68
|
|
|
function get($id) |
|
69
|
|
|
{ |
|
70
|
|
|
if(! DolibarrApiAccess::$user->rights->skeleton->read) { |
|
71
|
|
|
throw new RestException(401); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$result = $this->skeleton->fetch($id); |
|
75
|
|
|
if( ! $result ) { |
|
76
|
|
|
throw new RestException(404, 'Skeleton not found'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if( ! DolibarrApi::_checkAccessToResource('skeleton',$this->skeleton->id)) { |
|
80
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->_cleanObjectDatas($this->skeleton); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* List skeletons |
|
88
|
|
|
* |
|
89
|
|
|
* Get a list of skeletons |
|
90
|
|
|
* |
|
91
|
|
|
* @param int $mode Use this param to filter list |
|
92
|
|
|
* @param string $sortfield Sort field |
|
93
|
|
|
* @param string $sortorder Sort order |
|
94
|
|
|
* @param int $limit Limit for list |
|
95
|
|
|
* @param int $page Page number |
|
96
|
|
|
* @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101') or (t.import_key:=:'20160101')" |
|
97
|
|
|
* @return array Array of skeleton objects |
|
98
|
|
|
* |
|
99
|
|
|
* @url GET /skeletons/ |
|
100
|
|
|
*/ |
|
101
|
|
|
function index($mode, $sortfield = "t.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $sqlfilters = '') { |
|
102
|
|
|
global $db, $conf; |
|
103
|
|
|
|
|
104
|
|
|
$obj_ret = array(); |
|
105
|
|
|
|
|
106
|
|
|
$socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : ''; |
|
107
|
|
|
|
|
108
|
|
|
// If the internal user must only see his customers, force searching by him |
|
109
|
|
|
if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id; |
|
110
|
|
|
|
|
111
|
|
|
$sql = "SELECT s.rowid"; |
|
112
|
|
|
if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects) |
|
|
|
|
|
|
113
|
|
|
$sql.= " FROM ".MAIN_DB_PREFIX."skeleton as s"; |
|
114
|
|
|
|
|
115
|
|
|
if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale |
|
116
|
|
|
$sql.= ", ".MAIN_DB_PREFIX."c_stcomm as st"; |
|
117
|
|
|
$sql.= " WHERE s.fk_stcomm = st.id"; |
|
118
|
|
|
|
|
119
|
|
|
// Example of use $mode |
|
120
|
|
|
//if ($mode == 1) $sql.= " AND s.client IN (1, 3)"; |
|
121
|
|
|
//if ($mode == 2) $sql.= " AND s.client IN (2, 3)"; |
|
122
|
|
|
|
|
123
|
|
|
$sql.= ' AND s.entity IN ('.getEntity('skeleton', 1).')'; |
|
124
|
|
|
if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= " AND s.fk_soc = sc.fk_soc"; |
|
125
|
|
|
if ($socid) $sql.= " AND s.fk_soc = ".$socid; |
|
126
|
|
|
if ($search_sale > 0) $sql.= " AND s.rowid = sc.fk_soc"; // Join for the needed table to filter by sale |
|
127
|
|
|
// Insert sale filter |
|
128
|
|
|
if ($search_sale > 0) |
|
129
|
|
|
{ |
|
130
|
|
|
$sql .= " AND sc.fk_user = ".$search_sale; |
|
131
|
|
|
} |
|
132
|
|
|
if ($sqlfilters) |
|
133
|
|
|
{ |
|
134
|
|
|
if (! DolibarrApi::_checkFilters($sqlfilters)) |
|
135
|
|
|
{ |
|
136
|
|
|
throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters); |
|
137
|
|
|
} |
|
138
|
|
|
$regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; |
|
139
|
|
|
$sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$sql.= $db->order($sortfield, $sortorder); |
|
143
|
|
|
if ($limit) { |
|
144
|
|
|
if ($page < 0) |
|
145
|
|
|
{ |
|
146
|
|
|
$page = 0; |
|
147
|
|
|
} |
|
148
|
|
|
$offset = $limit * $page; |
|
149
|
|
|
|
|
150
|
|
|
$sql.= $db->plimit($limit + 1, $offset); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$result = $db->query($sql); |
|
154
|
|
|
if ($result) |
|
155
|
|
|
{ |
|
156
|
|
|
$num = $db->num_rows($result); |
|
157
|
|
|
while ($i < $num) |
|
|
|
|
|
|
158
|
|
|
{ |
|
159
|
|
|
$obj = $db->fetch_object($result); |
|
160
|
|
|
$skeleton_static = new Skeleton($db); |
|
161
|
|
|
if($skeleton_static->fetch($obj->rowid)) { |
|
162
|
|
|
$obj_ret[] = parent::_cleanObjectDatas($skeleton_static); |
|
163
|
|
|
} |
|
164
|
|
|
$i++; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
else { |
|
168
|
|
|
throw new RestException(503, 'Error when retrieve skeleton list'); |
|
169
|
|
|
} |
|
170
|
|
|
if( ! count($obj_ret)) { |
|
171
|
|
|
throw new RestException(404, 'No skeleton found'); |
|
172
|
|
|
} |
|
173
|
|
|
return $obj_ret; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Create skeleton object |
|
178
|
|
|
* |
|
179
|
|
|
* @param array $request_data Request datas |
|
180
|
|
|
* @return int ID of skeleton |
|
181
|
|
|
* |
|
182
|
|
|
* @url POST skeleton/ |
|
183
|
|
|
*/ |
|
184
|
|
|
function post($request_data = NULL) |
|
185
|
|
|
{ |
|
186
|
|
|
if(! DolibarrApiAccess::$user->rights->skeleton->create) { |
|
187
|
|
|
throw new RestException(401); |
|
188
|
|
|
} |
|
189
|
|
|
// Check mandatory fields |
|
190
|
|
|
$result = $this->_validate($request_data); |
|
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
foreach($request_data as $field => $value) { |
|
193
|
|
|
$this->skeleton->$field = $value; |
|
194
|
|
|
} |
|
195
|
|
|
if( ! $this->skeleton->create(DolibarrApiAccess::$user)) { |
|
196
|
|
|
throw new RestException(500); |
|
197
|
|
|
} |
|
198
|
|
|
return $this->skeleton->id; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Update skeleton |
|
203
|
|
|
* |
|
204
|
|
|
* @param int $id Id of skeleton to update |
|
205
|
|
|
* @param array $request_data Datas |
|
206
|
|
|
* @return int |
|
207
|
|
|
* |
|
208
|
|
|
* @url PUT skeleton/{id} |
|
209
|
|
|
*/ |
|
210
|
|
|
function put($id, $request_data = NULL) |
|
211
|
|
|
{ |
|
212
|
|
|
if(! DolibarrApiAccess::$user->rights->skeleton->create) { |
|
213
|
|
|
throw new RestException(401); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
$result = $this->skeleton->fetch($id); |
|
217
|
|
|
if( ! $result ) { |
|
218
|
|
|
throw new RestException(404, 'Skeleton not found'); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
if( ! DolibarrApi::_checkAccessToResource('skeleton',$this->skeleton->id)) { |
|
222
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
foreach($request_data as $field => $value) { |
|
226
|
|
|
$this->skeleton->$field = $value; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
if($this->skeleton->update($id, DolibarrApiAccess::$user)) |
|
230
|
|
|
return $this->get ($id); |
|
231
|
|
|
|
|
232
|
|
|
return false; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Delete skeleton |
|
237
|
|
|
* |
|
238
|
|
|
* @param int $id Skeleton ID |
|
239
|
|
|
* @return array |
|
240
|
|
|
* |
|
241
|
|
|
* @url DELETE skeleton/{id} |
|
242
|
|
|
*/ |
|
243
|
|
|
function delete($id) |
|
244
|
|
|
{ |
|
245
|
|
|
if(! DolibarrApiAccess::$user->rights->skeleton->supprimer) { |
|
246
|
|
|
throw new RestException(401); |
|
247
|
|
|
} |
|
248
|
|
|
$result = $this->skeleton->fetch($id); |
|
249
|
|
|
if( ! $result ) { |
|
250
|
|
|
throw new RestException(404, 'Skeleton not found'); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
if( ! DolibarrApi::_checkAccessToResource('skeleton',$this->skeleton->id)) { |
|
254
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
if( !$this->skeleton->delete($id)) |
|
258
|
|
|
{ |
|
259
|
|
|
throw new RestException(500); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
return array( |
|
263
|
|
|
'success' => array( |
|
264
|
|
|
'code' => 200, |
|
265
|
|
|
'message' => 'Skeleton deleted' |
|
266
|
|
|
) |
|
267
|
|
|
); |
|
268
|
|
|
|
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Validate fields before create or update object |
|
273
|
|
|
* |
|
274
|
|
|
* @param array $data Data to validate |
|
275
|
|
|
* @return array |
|
276
|
|
|
* |
|
277
|
|
|
* @throws RestException |
|
278
|
|
|
*/ |
|
279
|
|
|
function _validate($data) |
|
280
|
|
|
{ |
|
281
|
|
|
$skeleton = array(); |
|
282
|
|
|
foreach (SkeletonApi::$FIELDS as $field) { |
|
283
|
|
|
if (!isset($data[$field])) |
|
284
|
|
|
throw new RestException(400, "$field field missing"); |
|
285
|
|
|
$skeleton[$field] = $data[$field]; |
|
286
|
|
|
} |
|
287
|
|
|
return $skeleton; |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: