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 $this->myobject->$field = $this->_checkValForAPI($field, $value, $this->myobject);
261 }
262
263 // Clean data
264 // $this->myobject->abc = sanitizeVal($this->myobject->abc, 'alphanohtml');
265
266 if ($this->myobject->update(DolibarrApiAccess::$user, false) > 0) {
267 return $this->get($id);
268 } else {
269 throw new RestException(500, $this->myobject->error);
270 }
271 }
272
283 public function delete($id)
284 {
285 if (!DolibarrApiAccess::$user->rights->mymodule->myobject->delete) {
286 throw new RestException(401);
287 }
288 $result = $this->myobject->fetch($id);
289 if (!$result) {
290 throw new RestException(404, 'MyObject not found');
291 }
292
293 if (!DolibarrApi::_checkAccessToResource('myobject', $this->myobject->id, 'mymodule_myobject')) {
294 throw new RestException(401, 'Access to instance id='.$this->myobject->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
295 }
296
297 if ($this->myobject->delete(DolibarrApiAccess::$user) == 0) {
298 throw new RestException(409, 'Error when deleting MyObject : '.$this->myobject->error);
299 } elseif ($this->myobject->delete(DolibarrApiAccess::$user) < 0) {
300 throw new RestException(500, 'Error when deleting MyObject : '.$this->myobject->error);
301 }
302
303 return array(
304 'success' => array(
305 'code' => 200,
306 'message' => 'MyObject deleted'
307 )
308 );
309 }
310
311
320 private function _validate($data)
321 {
322 $myobject = array();
323 foreach ($this->myobject->fields as $field => $propfield) {
324 if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
325 continue; // Not a mandatory field
326 }
327 if (!isset($data[$field])) {
328 throw new RestException(400, "$field field missing");
329 }
330 $myobject[$field] = $data[$field];
331 }
332 return $myobject;
333 }
334
335 /*end methods CRUD*/
336
337 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
344 protected function _cleanObjectDatas($object)
345 {
346 // phpcs:enable
347 $object = parent::_cleanObjectDatas($object);
348
349 unset($object->rowid);
350 unset($object->canvas);
351
352 /*unset($object->name);
353 unset($object->lastname);
354 unset($object->firstname);
355 unset($object->civility_id);
356 unset($object->statut);
357 unset($object->state);
358 unset($object->state_id);
359 unset($object->state_code);
360 unset($object->region);
361 unset($object->region_code);
362 unset($object->country);
363 unset($object->country_id);
364 unset($object->country_code);
365 unset($object->barcode_type);
366 unset($object->barcode_type_code);
367 unset($object->barcode_type_label);
368 unset($object->barcode_type_coder);
369 unset($object->total_ht);
370 unset($object->total_tva);
371 unset($object->total_localtax1);
372 unset($object->total_localtax2);
373 unset($object->total_ttc);
374 unset($object->fk_account);
375 unset($object->comments);
376 unset($object->note);
377 unset($object->mode_reglement_id);
378 unset($object->cond_reglement_id);
379 unset($object->cond_reglement);
380 unset($object->shipping_method_id);
381 unset($object->fk_incoterms);
382 unset($object->label_incoterms);
383 unset($object->location_incoterms);
384 */
385
386 // If object has lines, remove $db property
387 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
388 $nboflines = count($object->lines);
389 for ($i = 0; $i < $nboflines; $i++) {
390 $this->_cleanObjectDatas($object->lines[$i]);
391
392 unset($object->lines[$i]->lines);
393 unset($object->lines[$i]->note);
394 }
395 }
396
397 return $object;
398 }
399}
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.