dolibarr  17.0.4
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 
19 use Luracast\Restler\RestException;
20 
21 dol_include_once('/mymodule/class/myobject.class.php');
22 
23 
24 
37 class MyModuleApi extends DolibarrApi
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 
70  public function get($id)
71  {
72  if (!DolibarrApiAccess::$user->rights->mymodule->myobject->read) {
73  throw new RestException(401);
74  }
75 
76  $result = $this->myobject->fetch($id);
77  if (!$result) {
78  throw new RestException(404, 'MyObject not found');
79  }
80 
81  if (!DolibarrApi::_checkAccessToResource('myobject', $this->myobject->id, 'mymodule_myobject')) {
82  throw new RestException(401, 'Access to instance id='.$this->myobject->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
83  }
84 
85  return $this->_cleanObjectDatas($this->myobject);
86  }
87 
88 
105  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
106  {
107  global $db, $conf;
108 
109  $obj_ret = array();
110  $tmpobject = new MyObject($this->db);
111 
112  if (!DolibarrApiAccess::$user->rights->mymodule->myobject->read) {
113  throw new RestException(401);
114  }
115 
116  $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
117 
118  $restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
119 
120  // If the internal user must only see his customers, force searching by him
121  $search_sale = 0;
122  if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
123  $search_sale = DolibarrApiAccess::$user->id;
124  }
125 
126  $sql = "SELECT t.rowid";
127  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
128  $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)
129  }
130  $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." as t";
131 
132  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
133  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
134  }
135  $sql .= " WHERE 1 = 1";
136 
137  // Example of use $mode
138  //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
139  //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
140 
141  if ($tmpobject->ismultientitymanaged) {
142  $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
143  }
144  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
145  $sql .= " AND t.fk_soc = sc.fk_soc";
146  }
147  if ($restrictonsocid && $socid) {
148  $sql .= " AND t.fk_soc = ".((int) $socid);
149  }
150  if ($restrictonsocid && $search_sale > 0) {
151  $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
152  }
153  // Insert sale filter
154  if ($restrictonsocid && $search_sale > 0) {
155  $sql .= " AND sc.fk_user = ".((int) $search_sale);
156  }
157  if ($sqlfilters) {
158  $errormessage = '';
159  $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
160  if ($errormessage) {
161  throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
162  }
163  }
164 
165  $sql .= $this->db->order($sortfield, $sortorder);
166  if ($limit) {
167  if ($page < 0) {
168  $page = 0;
169  }
170  $offset = $limit * $page;
171 
172  $sql .= $this->db->plimit($limit + 1, $offset);
173  }
174 
175  $result = $this->db->query($sql);
176  $i = 0;
177  if ($result) {
178  $num = $this->db->num_rows($result);
179  while ($i < $num) {
180  $obj = $this->db->fetch_object($result);
181  $tmp_object = new MyObject($this->db);
182  if ($tmp_object->fetch($obj->rowid)) {
183  $obj_ret[] = $this->_cleanObjectDatas($tmp_object);
184  }
185  $i++;
186  }
187  } else {
188  throw new RestException(503, 'Error when retrieving myobject list: '.$this->db->lasterror());
189  }
190  if (!count($obj_ret)) {
191  throw new RestException(404, 'No myobject found');
192  }
193  return $obj_ret;
194  }
195 
206  public function post($request_data = null)
207  {
208  if (!DolibarrApiAccess::$user->rights->mymodule->myobject->write) {
209  throw new RestException(401);
210  }
211 
212  // Check mandatory fields
213  $result = $this->_validate($request_data);
214 
215  foreach ($request_data as $field => $value) {
216  $this->myobject->$field = $this->_checkValForAPI($field, $value, $this->myobject);
217  }
218 
219  // Clean data
220  // $this->myobject->abc = sanitizeVal($this->myobject->abc, 'alphanohtml');
221 
222  if ($this->myobject->create(DolibarrApiAccess::$user)<0) {
223  throw new RestException(500, "Error creating MyObject", array_merge(array($this->myobject->error), $this->myobject->errors));
224  }
225  return $this->myobject->id;
226  }
227 
239  public function put($id, $request_data = null)
240  {
241  if (!DolibarrApiAccess::$user->rights->mymodule->myobject->write) {
242  throw new RestException(401);
243  }
244 
245  $result = $this->myobject->fetch($id);
246  if (!$result) {
247  throw new RestException(404, 'MyObject not found');
248  }
249 
250  if (!DolibarrApi::_checkAccessToResource('myobject', $this->myobject->id, 'mymodule_myobject')) {
251  throw new RestException(401, 'Access to instance id='.$this->myobject->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
252  }
253 
254  foreach ($request_data as $field => $value) {
255  if ($field == 'id') {
256  continue;
257  }
258  $this->myobject->$field = $this->_checkValForAPI($field, $value, $this->myobject);
259  }
260 
261  // Clean data
262  // $this->myobject->abc = sanitizeVal($this->myobject->abc, 'alphanohtml');
263 
264  if ($this->myobject->update(DolibarrApiAccess::$user, false) > 0) {
265  return $this->get($id);
266  } else {
267  throw new RestException(500, $this->myobject->error);
268  }
269  }
270 
281  public function delete($id)
282  {
283  if (!DolibarrApiAccess::$user->rights->mymodule->myobject->delete) {
284  throw new RestException(401);
285  }
286  $result = $this->myobject->fetch($id);
287  if (!$result) {
288  throw new RestException(404, 'MyObject not found');
289  }
290 
291  if (!DolibarrApi::_checkAccessToResource('myobject', $this->myobject->id, 'mymodule_myobject')) {
292  throw new RestException(401, 'Access to instance id='.$this->myobject->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
293  }
294 
295  if ($this->myobject->delete(DolibarrApiAccess::$user) == 0) {
296  throw new RestException(409, 'Error when deleting MyObject : '.$this->myobject->error);
297  } elseif ($this->myobject->delete(DolibarrApiAccess::$user) < 0) {
298  throw new RestException(500, 'Error when deleting MyObject : '.$this->myobject->error);
299  }
300 
301  return array(
302  'success' => array(
303  'code' => 200,
304  'message' => 'MyObject deleted'
305  )
306  );
307  }
308 
309 
310  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
317  protected function _cleanObjectDatas($object)
318  {
319  // phpcs:enable
320  $object = parent::_cleanObjectDatas($object);
321 
322  unset($object->rowid);
323  unset($object->canvas);
324 
325  /*unset($object->name);
326  unset($object->lastname);
327  unset($object->firstname);
328  unset($object->civility_id);
329  unset($object->statut);
330  unset($object->state);
331  unset($object->state_id);
332  unset($object->state_code);
333  unset($object->region);
334  unset($object->region_code);
335  unset($object->country);
336  unset($object->country_id);
337  unset($object->country_code);
338  unset($object->barcode_type);
339  unset($object->barcode_type_code);
340  unset($object->barcode_type_label);
341  unset($object->barcode_type_coder);
342  unset($object->total_ht);
343  unset($object->total_tva);
344  unset($object->total_localtax1);
345  unset($object->total_localtax2);
346  unset($object->total_ttc);
347  unset($object->fk_account);
348  unset($object->comments);
349  unset($object->note);
350  unset($object->mode_reglement_id);
351  unset($object->cond_reglement_id);
352  unset($object->cond_reglement);
353  unset($object->shipping_method_id);
354  unset($object->fk_incoterms);
355  unset($object->label_incoterms);
356  unset($object->location_incoterms);
357  */
358 
359  // If object has lines, remove $db property
360  if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
361  $nboflines = count($object->lines);
362  for ($i = 0; $i < $nboflines; $i++) {
363  $this->_cleanObjectDatas($object->lines[$i]);
364 
365  unset($object->lines[$i]->lines);
366  unset($object->lines[$i]->note);
367  }
368  }
369 
370  return $object;
371  }
372 
381  private function _validate($data)
382  {
383  $myobject = array();
384  foreach ($this->myobject->fields as $field => $propfield) {
385  if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
386  continue; // Not a mandatory field
387  }
388  if (!isset($data[$field])) {
389  throw new RestException(400, "$field field missing");
390  }
391  $myobject[$field] = $data[$field];
392  }
393  return $myobject;
394  }
395 }
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.
Definition: api.class.php:283
_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, &$error='')
forgeSQLFromUniversalSearchCriteria
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
$conf db
API class for accounts.
Definition: inc.php:41