dolibarr  16.0.5
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  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
160  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
161  }
162  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
163  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
164  }
165 
166  $sql .= $this->db->order($sortfield, $sortorder);
167  if ($limit) {
168  if ($page < 0) {
169  $page = 0;
170  }
171  $offset = $limit * $page;
172 
173  $sql .= $this->db->plimit($limit + 1, $offset);
174  }
175 
176  $result = $this->db->query($sql);
177  $i = 0;
178  if ($result) {
179  $num = $this->db->num_rows($result);
180  while ($i < $num) {
181  $obj = $this->db->fetch_object($result);
182  $tmp_object = new MyObject($this->db);
183  if ($tmp_object->fetch($obj->rowid)) {
184  $obj_ret[] = $this->_cleanObjectDatas($tmp_object);
185  }
186  $i++;
187  }
188  } else {
189  throw new RestException(503, 'Error when retrieving myobject list: '.$this->db->lasterror());
190  }
191  if (!count($obj_ret)) {
192  throw new RestException(404, 'No myobject found');
193  }
194  return $obj_ret;
195  }
196 
207  public function post($request_data = null)
208  {
209  if (!DolibarrApiAccess::$user->rights->mymodule->myobject->write) {
210  throw new RestException(401);
211  }
212 
213  // Check mandatory fields
214  $result = $this->_validate($request_data);
215 
216  foreach ($request_data as $field => $value) {
217  $this->myobject->$field = $this->_checkValForAPI($field, $value, $this->myobject);
218  }
219 
220  // Clean data
221  // $this->myobject->abc = sanitizeVal($this->myobject->abc, 'alphanohtml');
222 
223  if ($this->myobject->create(DolibarrApiAccess::$user)<0) {
224  throw new RestException(500, "Error creating MyObject", array_merge(array($this->myobject->error), $this->myobject->errors));
225  }
226  return $this->myobject->id;
227  }
228 
240  public function put($id, $request_data = null)
241  {
242  if (!DolibarrApiAccess::$user->rights->mymodule->myobject->write) {
243  throw new RestException(401);
244  }
245 
246  $result = $this->myobject->fetch($id);
247  if (!$result) {
248  throw new RestException(404, 'MyObject not found');
249  }
250 
251  if (!DolibarrApi::_checkAccessToResource('myobject', $this->myobject->id, 'mymodule_myobject')) {
252  throw new RestException(401, 'Access to instance id='.$this->myobject->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
253  }
254 
255  foreach ($request_data as $field => $value) {
256  if ($field == 'id') {
257  continue;
258  }
259  $this->myobject->$field = $this->_checkValForAPI($field, $value, $this->myobject);
260  }
261 
262  // Clean data
263  // $this->myobject->abc = sanitizeVal($this->myobject->abc, 'alphanohtml');
264 
265  if ($this->myobject->update(DolibarrApiAccess::$user, false) > 0) {
266  return $this->get($id);
267  } else {
268  throw new RestException(500, $this->myobject->error);
269  }
270  }
271 
282  public function delete($id)
283  {
284  if (!DolibarrApiAccess::$user->rights->mymodule->myobject->delete) {
285  throw new RestException(401);
286  }
287  $result = $this->myobject->fetch($id);
288  if (!$result) {
289  throw new RestException(404, 'MyObject not found');
290  }
291 
292  if (!DolibarrApi::_checkAccessToResource('myobject', $this->myobject->id, 'mymodule_myobject')) {
293  throw new RestException(401, 'Access to instance id='.$this->myobject->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
294  }
295 
296  if (!$this->myobject->delete(DolibarrApiAccess::$user)) {
297  throw new RestException(500, 'Error when deleting MyObject : '.$this->myobject->error);
298  }
299 
300  return array(
301  'success' => array(
302  'code' => 200,
303  'message' => 'MyObject deleted'
304  )
305  );
306  }
307 
308 
309  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
316  protected function _cleanObjectDatas($object)
317  {
318  // phpcs:enable
319  $object = parent::_cleanObjectDatas($object);
320 
321  unset($object->rowid);
322  unset($object->canvas);
323 
324  /*unset($object->name);
325  unset($object->lastname);
326  unset($object->firstname);
327  unset($object->civility_id);
328  unset($object->statut);
329  unset($object->state);
330  unset($object->state_id);
331  unset($object->state_code);
332  unset($object->region);
333  unset($object->region_code);
334  unset($object->country);
335  unset($object->country_id);
336  unset($object->country_code);
337  unset($object->barcode_type);
338  unset($object->barcode_type_code);
339  unset($object->barcode_type_label);
340  unset($object->barcode_type_coder);
341  unset($object->total_ht);
342  unset($object->total_tva);
343  unset($object->total_localtax1);
344  unset($object->total_localtax2);
345  unset($object->total_ttc);
346  unset($object->fk_account);
347  unset($object->comments);
348  unset($object->note);
349  unset($object->mode_reglement_id);
350  unset($object->cond_reglement_id);
351  unset($object->cond_reglement);
352  unset($object->shipping_method_id);
353  unset($object->fk_incoterms);
354  unset($object->label_incoterms);
355  unset($object->location_incoterms);
356  */
357 
358  // If object has lines, remove $db property
359  if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
360  $nboflines = count($object->lines);
361  for ($i = 0; $i < $nboflines; $i++) {
362  $this->_cleanObjectDatas($object->lines[$i]);
363 
364  unset($object->lines[$i]->lines);
365  unset($object->lines[$i]->note);
366  }
367  }
368 
369  return $object;
370  }
371 
380  private function _validate($data)
381  {
382  $myobject = array();
383  foreach ($this->myobject->fields as $field => $propfield) {
384  if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) {
385  continue; // Not a mandatory field
386  }
387  if (!isset($data[$field])) {
388  throw new RestException(400, "$field field missing");
389  }
390  $myobject[$field] = $data[$field];
391  }
392  return $myobject;
393  }
394 }
db
$conf db
API class for accounts.
Definition: inc.php:41
MyModuleApi\_validate
_validate($data)
Validate fields before create or update object.
Definition: api_mymodule.class.php:380
MyModuleApi\put
put($id, $request_data=null)
Update myobject.
Definition: api_mymodule.class.php:240
dol_include_once
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
Definition: functions.lib.php:1033
DolibarrApi\_checkAccessToResource
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
DolibarrApi
Class for API REST v1.
Definition: api.class.php:30
MyModuleApi
Definition: api_mymodule.class.php:37
MyModuleApi\__construct
__construct()
Constructor.
Definition: api_mymodule.class.php:50
DolibarrApi\_checkFilters
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
MyModuleApi\index
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List myobjects.
Definition: api_mymodule.class.php:105
MyObject
Class for MyObject.
Definition: myobject.class.php:33
MyModuleApi\_cleanObjectDatas
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api_mymodule.class.php:316
DolibarrApi\_checkValForAPI
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition: api.class.php:86
MyModuleApi\post
post($request_data=null)
Create myobject object.
Definition: api_mymodule.class.php:207