dolibarr 18.0.6
api_mymodule.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
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 <https://www.gnu.org/licenses/>.
17 */
18
19use Luracast\Restler\RestException;
20
21dol_include_once('/mymodule/class/myobject.class.php');
22
23
24
38{
42 public $myobject;
43
50 public function __construct()
51 {
52 global $db;
53 $this->db = $db;
54 $this->myobject = new MyObject($this->db);
55 }
56
57 /*begin methods CRUD*/
58
72 public function get($id)
73 {
74 if (!DolibarrApiAccess::$user->rights->mymodule->myobject->read) {
75 throw new RestException(401);
76 }
77
78 $result = $this->myobject->fetch($id);
79 if (!$result) {
80 throw new RestException(404, 'MyObject not found');
81 }
82
83 if (!DolibarrApi::_checkAccessToResource('myobject', $this->myobject->id, 'mymodule_myobject')) {
84 throw new RestException(401, 'Access to instance id='.$this->myobject->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
85 }
86
87 return $this->_cleanObjectDatas($this->myobject);
88 }
89
90
107 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
108 {
109 global $db, $conf;
110
111 $obj_ret = array();
112 $tmpobject = new MyObject($this->db);
113
114 if (!DolibarrApiAccess::$user->rights->mymodule->myobject->read) {
115 throw new RestException(401);
116 }
117
118 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
119
120 $restrictonsocid = 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 $search_sale = 0;
124 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
125 $search_sale = DolibarrApiAccess::$user->id;
126 }
127
128 $sql = "SELECT t.rowid";
129 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
130 $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)
131 }
132 $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." AS t LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element."_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
133
134 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
135 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
136 }
137 $sql .= " WHERE 1 = 1";
138
139 // Example of use $mode
140 //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
141 //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
142
143 if ($tmpobject->ismultientitymanaged) {
144 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
145 }
146 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
147 $sql .= " AND t.fk_soc = sc.fk_soc";
148 }
149 if ($restrictonsocid && $socid) {
150 $sql .= " AND t.fk_soc = ".((int) $socid);
151 }
152 if ($restrictonsocid && $search_sale > 0) {
153 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
154 }
155 // Insert sale filter
156 if ($restrictonsocid && $search_sale > 0) {
157 $sql .= " AND sc.fk_user = ".((int) $search_sale);
158 }
159 if ($sqlfilters) {
160 $errormessage = '';
161 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
162 if ($errormessage) {
163 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
164 }
165 }
166
167 $sql .= $this->db->order($sortfield, $sortorder);
168 if ($limit) {
169 if ($page < 0) {
170 $page = 0;
171 }
172 $offset = $limit * $page;
173
174 $sql .= $this->db->plimit($limit + 1, $offset);
175 }
176
177 $result = $this->db->query($sql);
178 $i = 0;
179 if ($result) {
180 $num = $this->db->num_rows($result);
181 while ($i < $num) {
182 $obj = $this->db->fetch_object($result);
183 $tmp_object = new MyObject($this->db);
184 if ($tmp_object->fetch($obj->rowid)) {
185 $obj_ret[] = $this->_cleanObjectDatas($tmp_object);
186 }
187 $i++;
188 }
189 } else {
190 throw new RestException(503, 'Error when retrieving myobject list: '.$this->db->lasterror());
191 }
192 if (!count($obj_ret)) {
193 throw new RestException(404, 'No myobject found');
194 }
195 return $obj_ret;
196 }
197
208 public function post($request_data = null)
209 {
210 if (!DolibarrApiAccess::$user->rights->mymodule->myobject->write) {
211 throw new RestException(401);
212 }
213
214 // Check mandatory fields
215 $result = $this->_validate($request_data);
216
217 foreach ($request_data as $field => $value) {
218 $this->myobject->$field = $this->_checkValForAPI($field, $value, $this->myobject);
219 }
220
221 // Clean data
222 // $this->myobject->abc = sanitizeVal($this->myobject->abc, 'alphanohtml');
223
224 if ($this->myobject->create(DolibarrApiAccess::$user)<0) {
225 throw new RestException(500, "Error creating MyObject", array_merge(array($this->myobject->error), $this->myobject->errors));
226 }
227 return $this->myobject->id;
228 }
229
241 public function put($id, $request_data = null)
242 {
243 if (!DolibarrApiAccess::$user->rights->mymodule->myobject->write) {
244 throw new RestException(401);
245 }
246
247 $result = $this->myobject->fetch($id);
248 if (!$result) {
249 throw new RestException(404, 'MyObject not found');
250 }
251
252 if (!DolibarrApi::_checkAccessToResource('myobject', $this->myobject->id, 'mymodule_myobject')) {
253 throw new RestException(401, 'Access to instance id='.$this->myobject->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
254 }
255
256 foreach ($request_data as $field => $value) {
257 if ($field == 'id') {
258 continue;
259 }
260 if ($field == 'array_options' && is_array($value)) {
261 foreach ($value as $index => $val) {
262 $this->myobject->array_options[$index] = $this->_checkValForAPI($field, $val, $this->myobject);
263 }
264 continue;
265 }
266 $this->myobject->$field = $this->_checkValForAPI($field, $value, $this->myobject);
267 }
268
269 // Clean data
270 // $this->myobject->abc = sanitizeVal($this->myobject->abc, 'alphanohtml');
271
272 if ($this->myobject->update(DolibarrApiAccess::$user, false) > 0) {
273 return $this->get($id);
274 } else {
275 throw new RestException(500, $this->myobject->error);
276 }
277 }
278
289 public function delete($id)
290 {
291 if (!DolibarrApiAccess::$user->rights->mymodule->myobject->delete) {
292 throw new RestException(401);
293 }
294 $result = $this->myobject->fetch($id);
295 if (!$result) {
296 throw new RestException(404, 'MyObject not found');
297 }
298
299 if (!DolibarrApi::_checkAccessToResource('myobject', $this->myobject->id, 'mymodule_myobject')) {
300 throw new RestException(401, 'Access to instance id='.$this->myobject->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
301 }
302
303 if ($this->myobject->delete(DolibarrApiAccess::$user) == 0) {
304 throw new RestException(409, 'Error when deleting MyObject : '.$this->myobject->error);
305 } elseif ($this->myobject->delete(DolibarrApiAccess::$user) < 0) {
306 throw new RestException(500, 'Error when deleting MyObject : '.$this->myobject->error);
307 }
308
309 return array(
310 'success' => array(
311 'code' => 200,
312 'message' => 'MyObject deleted'
313 )
314 );
315 }
316
317
326 private function _validate($data)
327 {
328 $myobject = array();
329 foreach ($this->myobject->fields as $field => $propfield) {
330 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
331 continue; // Not a mandatory field
332 }
333 if (!isset($data[$field])) {
334 throw new RestException(400, "$field field missing");
335 }
336 $myobject[$field] = $data[$field];
337 }
338 return $myobject;
339 }
340
341 /*end methods CRUD*/
342
343 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
350 protected function _cleanObjectDatas($object)
351 {
352 // phpcs:enable
353 $object = parent::_cleanObjectDatas($object);
354
355 unset($object->rowid);
356 unset($object->canvas);
357
358 /*unset($object->name);
359 unset($object->lastname);
360 unset($object->firstname);
361 unset($object->civility_id);
362 unset($object->statut);
363 unset($object->state);
364 unset($object->state_id);
365 unset($object->state_code);
366 unset($object->region);
367 unset($object->region_code);
368 unset($object->country);
369 unset($object->country_id);
370 unset($object->country_code);
371 unset($object->barcode_type);
372 unset($object->barcode_type_code);
373 unset($object->barcode_type_label);
374 unset($object->barcode_type_coder);
375 unset($object->total_ht);
376 unset($object->total_tva);
377 unset($object->total_localtax1);
378 unset($object->total_localtax2);
379 unset($object->total_ttc);
380 unset($object->fk_account);
381 unset($object->comments);
382 unset($object->note);
383 unset($object->mode_reglement_id);
384 unset($object->cond_reglement_id);
385 unset($object->cond_reglement);
386 unset($object->shipping_method_id);
387 unset($object->fk_incoterms);
388 unset($object->label_incoterms);
389 unset($object->location_incoterms);
390 */
391
392 // If object has lines, remove $db property
393 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
394 $nboflines = count($object->lines);
395 for ($i = 0; $i < $nboflines; $i++) {
396 $this->_cleanObjectDatas($object->lines[$i]);
397
398 unset($object->lines[$i]->lines);
399 unset($object->lines[$i]->note);
400 }
401 }
402
403 return $object;
404 }
405}
Class for API REST v1.
Definition api.class.php:31
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:86
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List myobjects.
_cleanObjectDatas($object)
Clean sensible object datas.
__construct()
Constructor.
post($request_data=null)
Create myobject object.
put($id, $request_data=null)
Update myobject.
_validate($data)
Validate fields before create or update object.
Class for MyObject.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.