|
1
|
|
|
<?php |
|
2
|
|
|
/* Copyright (C) 2015 Jean-François Ferry <[email protected]> |
|
3
|
|
|
* Copyright (C) ---Put here your own copyright and developer email--- |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU General Public License as published by |
|
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Luracast\Restler\RestException; |
|
20
|
|
|
|
|
21
|
|
|
dol_include_once('/mymodule/class/myobject.class.php'); |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* \file htdocs/modulebuilder/template/class/api_mymodule.class.php |
|
27
|
|
|
* \ingroup mymodule |
|
28
|
|
|
* \brief File for API management of myobject. |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* API class for mymodule myobject |
|
33
|
|
|
* |
|
34
|
|
|
* @smart-auto-routing false |
|
35
|
|
|
* @access protected |
|
36
|
|
|
* @class DolibarrApiAccess {@requires user,external} |
|
37
|
|
|
*/ |
|
38
|
|
|
class MyModuleApi extends DolibarrApi |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* @var array $FIELDS Mandatory fields, checked when create and update object |
|
42
|
|
|
*/ |
|
43
|
|
|
static $FIELDS = array( |
|
44
|
|
|
'name', |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var MyObject $myobject {@type MyObject} |
|
50
|
|
|
*/ |
|
51
|
|
|
public $myobject; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Constructor |
|
55
|
|
|
* |
|
56
|
|
|
* @url GET / |
|
57
|
|
|
* |
|
58
|
|
|
*/ |
|
59
|
|
|
function __construct() |
|
60
|
|
|
{ |
|
61
|
|
|
global $db, $conf; |
|
62
|
|
|
$this->db = $db; |
|
63
|
|
|
$this->myobject = new MyObject($this->db); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get properties of a myobject object |
|
68
|
|
|
* |
|
69
|
|
|
* Return an array with myobject informations |
|
70
|
|
|
* |
|
71
|
|
|
* @param int $id ID of myobject |
|
72
|
|
|
* @return array|mixed data without useless information |
|
73
|
|
|
* |
|
74
|
|
|
* @url GET myobjects/{id} |
|
75
|
|
|
* @throws RestException |
|
76
|
|
|
*/ |
|
77
|
|
|
function get($id) |
|
78
|
|
|
{ |
|
79
|
|
|
if(! DolibarrApiAccess::$user->rights->myobject->read) { |
|
80
|
|
|
throw new RestException(401); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$result = $this->myobject->fetch($id); |
|
84
|
|
|
if( ! $result ) { |
|
85
|
|
|
throw new RestException(404, 'MyObject not found'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if( ! DolibarrApi::_checkAccessToResource('myobject',$this->myobject->id)) { |
|
89
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $this->_cleanObjectDatas($this->myobject); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* List myobjects |
|
98
|
|
|
* |
|
99
|
|
|
* Get a list of myobjects |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $sortfield Sort field |
|
102
|
|
|
* @param string $sortorder Sort order |
|
103
|
|
|
* @param int $limit Limit for list |
|
104
|
|
|
* @param int $page Page number |
|
105
|
|
|
* @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" |
|
106
|
|
|
* @return array Array of order objects |
|
107
|
|
|
* |
|
108
|
|
|
* @throws RestException |
|
109
|
|
|
* |
|
110
|
|
|
* @url GET /myobjects/ |
|
111
|
|
|
*/ |
|
112
|
|
|
function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '') |
|
113
|
|
|
{ |
|
114
|
|
|
global $db, $conf; |
|
115
|
|
|
|
|
116
|
|
|
$obj_ret = array(); |
|
117
|
|
|
|
|
118
|
|
|
$socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : ''; |
|
119
|
|
|
|
|
120
|
|
|
$restictonsocid = 0; // Set to 1 if there is a field socid in table of object |
|
121
|
|
|
|
|
122
|
|
|
// If the internal user must only see his customers, force searching by him |
|
123
|
|
|
if ($restictonsocid && ! DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id; |
|
124
|
|
|
|
|
125
|
|
|
$sql = "SELECT t.rowid"; |
|
126
|
|
|
if ($restictonsocid && (!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) |
|
|
|
|
|
|
127
|
|
|
$sql.= " FROM ".MAIN_DB_PREFIX."myobject_mytable as t"; |
|
128
|
|
|
|
|
129
|
|
|
if ($restictonsocid && (!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 |
|
130
|
|
|
$sql.= " WHERE 1 = 1"; |
|
131
|
|
|
|
|
132
|
|
|
// Example of use $mode |
|
133
|
|
|
//if ($mode == 1) $sql.= " AND s.client IN (1, 3)"; |
|
134
|
|
|
//if ($mode == 2) $sql.= " AND s.client IN (2, 3)"; |
|
135
|
|
|
|
|
136
|
|
|
$tmpobject = new MyObject($db); |
|
137
|
|
|
if ($tmpobject->ismultientitymanaged) $sql.= ' AND t.entity IN ('.getEntity('myobject').')'; |
|
138
|
|
|
if ($restictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= " AND t.fk_soc = sc.fk_soc"; |
|
139
|
|
|
if ($restictonsocid && $socid) $sql.= " AND t.fk_soc = ".$socid; |
|
140
|
|
|
if ($restictonsocid && $search_sale > 0) $sql.= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale |
|
141
|
|
|
// Insert sale filter |
|
142
|
|
|
if ($restictonsocid && $search_sale > 0) |
|
143
|
|
|
{ |
|
144
|
|
|
$sql .= " AND sc.fk_user = ".$search_sale; |
|
145
|
|
|
} |
|
146
|
|
|
if ($sqlfilters) |
|
147
|
|
|
{ |
|
148
|
|
|
if (! DolibarrApi::_checkFilters($sqlfilters)) |
|
149
|
|
|
{ |
|
150
|
|
|
throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters); |
|
151
|
|
|
} |
|
152
|
|
|
$regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; |
|
153
|
|
|
$sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$sql.= $db->order($sortfield, $sortorder); |
|
157
|
|
|
if ($limit) { |
|
158
|
|
|
if ($page < 0) |
|
159
|
|
|
{ |
|
160
|
|
|
$page = 0; |
|
161
|
|
|
} |
|
162
|
|
|
$offset = $limit * $page; |
|
163
|
|
|
|
|
164
|
|
|
$sql.= $db->plimit($limit + 1, $offset); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$result = $db->query($sql); |
|
168
|
|
|
if ($result) |
|
169
|
|
|
{ |
|
170
|
|
|
$num = $db->num_rows($result); |
|
171
|
|
|
while ($i < $num) |
|
|
|
|
|
|
172
|
|
|
{ |
|
173
|
|
|
$obj = $db->fetch_object($result); |
|
174
|
|
|
$myobject_static = new MyObject($db); |
|
175
|
|
|
if($myobject_static->fetch($obj->rowid)) { |
|
176
|
|
|
$obj_ret[] = $this->_cleanObjectDatas($myobject_static); |
|
177
|
|
|
} |
|
178
|
|
|
$i++; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
else { |
|
182
|
|
|
throw new RestException(503, 'Error when retrieve myobject list'); |
|
183
|
|
|
} |
|
184
|
|
|
if( ! count($obj_ret)) { |
|
185
|
|
|
throw new RestException(404, 'No myobject found'); |
|
186
|
|
|
} |
|
187
|
|
|
return $obj_ret; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Create myobject object |
|
192
|
|
|
* |
|
193
|
|
|
* @param array $request_data Request datas |
|
194
|
|
|
* @return int ID of myobject |
|
195
|
|
|
* |
|
196
|
|
|
* @url POST myobjects/ |
|
197
|
|
|
*/ |
|
198
|
|
|
function post($request_data = null) |
|
199
|
|
|
{ |
|
200
|
|
|
if(! DolibarrApiAccess::$user->rights->myobject->create) { |
|
201
|
|
|
throw new RestException(401); |
|
202
|
|
|
} |
|
203
|
|
|
// Check mandatory fields |
|
204
|
|
|
$result = $this->_validate($request_data); |
|
205
|
|
|
|
|
206
|
|
|
foreach($request_data as $field => $value) { |
|
207
|
|
|
$this->myobject->$field = $value; |
|
208
|
|
|
} |
|
209
|
|
|
if( ! $this->myobject->create(DolibarrApiAccess::$user)) { |
|
210
|
|
|
throw new RestException(500); |
|
211
|
|
|
} |
|
212
|
|
|
return $this->myobject->id; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Update myobject |
|
217
|
|
|
* |
|
218
|
|
|
* @param int $id Id of myobject to update |
|
219
|
|
|
* @param array $request_data Datas |
|
220
|
|
|
* @return int |
|
221
|
|
|
* |
|
222
|
|
|
* @url PUT myobjects/{id} |
|
223
|
|
|
*/ |
|
224
|
|
|
function put($id, $request_data = null) |
|
225
|
|
|
{ |
|
226
|
|
|
if(! DolibarrApiAccess::$user->rights->myobject->create) { |
|
227
|
|
|
throw new RestException(401); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
$result = $this->myobject->fetch($id); |
|
231
|
|
|
if( ! $result ) { |
|
232
|
|
|
throw new RestException(404, 'MyObject not found'); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
if( ! DolibarrApi::_checkAccessToResource('myobject',$this->myobject->id)) { |
|
236
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
foreach($request_data as $field => $value) { |
|
240
|
|
|
$this->myobject->$field = $value; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
if($this->myobject->update($id, DolibarrApiAccess::$user)) |
|
244
|
|
|
return $this->get($id); |
|
245
|
|
|
|
|
246
|
|
|
return false; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Delete myobject |
|
251
|
|
|
* |
|
252
|
|
|
* @param int $id MyObject ID |
|
253
|
|
|
* @return array |
|
254
|
|
|
* |
|
255
|
|
|
* @url DELETE myobject/{id} |
|
256
|
|
|
*/ |
|
257
|
|
|
function delete($id) |
|
258
|
|
|
{ |
|
259
|
|
|
if(! DolibarrApiAccess::$user->rights->myobject->delete) { |
|
260
|
|
|
throw new RestException(401); |
|
261
|
|
|
} |
|
262
|
|
|
$result = $this->myobject->fetch($id); |
|
263
|
|
|
if( ! $result ) { |
|
264
|
|
|
throw new RestException(404, 'MyObject not found'); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
if( ! DolibarrApi::_checkAccessToResource('myobject',$this->myobject->id)) { |
|
268
|
|
|
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
if( !$this->myobject->delete(DolibarrApiAccess::$user, 0)) |
|
272
|
|
|
{ |
|
273
|
|
|
throw new RestException(500); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
return array( |
|
277
|
|
|
'success' => array( |
|
278
|
|
|
'code' => 200, |
|
279
|
|
|
'message' => 'MyObject deleted' |
|
280
|
|
|
) |
|
281
|
|
|
); |
|
282
|
|
|
|
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Clean sensible object datas |
|
288
|
|
|
* |
|
289
|
|
|
* @param object $object Object to clean |
|
290
|
|
|
* @return array Array of cleaned object properties |
|
291
|
|
|
*/ |
|
292
|
|
|
function _cleanObjectDatas($object) |
|
293
|
|
|
{ |
|
294
|
|
|
$object = parent::_cleanObjectDatas($object); |
|
295
|
|
|
|
|
296
|
|
|
/*unset($object->note); |
|
297
|
|
|
unset($object->address); |
|
298
|
|
|
unset($object->barcode_type); |
|
299
|
|
|
unset($object->barcode_type_code); |
|
300
|
|
|
unset($object->barcode_type_label); |
|
301
|
|
|
unset($object->barcode_type_coder);*/ |
|
302
|
|
|
|
|
303
|
|
|
return $object; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* Validate fields before create or update object |
|
308
|
|
|
* |
|
309
|
|
|
* @param array $data Data to validate |
|
310
|
|
|
* @return array |
|
311
|
|
|
* |
|
312
|
|
|
* @throws RestException |
|
313
|
|
|
*/ |
|
314
|
|
|
function _validate($data) |
|
315
|
|
|
{ |
|
316
|
|
|
$myobject = array(); |
|
317
|
|
|
foreach (MyObjectApi::$FIELDS as $field) { |
|
318
|
|
|
if (!isset($data[$field])) |
|
319
|
|
|
throw new RestException(400, "$field field missing"); |
|
320
|
|
|
$myobject[$field] = $data[$field]; |
|
321
|
|
|
} |
|
322
|
|
|
return $myobject; |
|
323
|
|
|
} |
|
324
|
|
|
} |
|
325
|
|
|
|
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: