dolibarr  13.0.2
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, $conf;
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->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) $search_sale = DolibarrApiAccess::$user->id;
123 
124  $sql = "SELECT t.rowid";
125  if ($restrictonsocid && (!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)
126  $sql .= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." as t";
127 
128  if ($restrictonsocid && (!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
129  $sql .= " WHERE 1 = 1";
130 
131  // Example of use $mode
132  //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
133  //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
134 
135  if ($tmpobject->ismultientitymanaged) $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
136  if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql .= " AND t.fk_soc = sc.fk_soc";
137  if ($restrictonsocid && $socid) $sql .= " AND t.fk_soc = ".$socid;
138  if ($restrictonsocid && $search_sale > 0) $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
139  // Insert sale filter
140  if ($restrictonsocid && $search_sale > 0) {
141  $sql .= " AND sc.fk_user = ".$search_sale;
142  }
143  if ($sqlfilters)
144  {
145  if (!DolibarrApi::_checkFilters($sqlfilters)) {
146  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
147  }
148  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
149  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
150  }
151 
152  $sql .= $this->db->order($sortfield, $sortorder);
153  if ($limit) {
154  if ($page < 0) {
155  $page = 0;
156  }
157  $offset = $limit * $page;
158 
159  $sql .= $this->db->plimit($limit + 1, $offset);
160  }
161 
162  $result = $this->db->query($sql);
163  $i = 0;
164  if ($result)
165  {
166  $num = $this->db->num_rows($result);
167  while ($i < $num)
168  {
169  $obj = $this->db->fetch_object($result);
170  $tmp_object = new MyObject($this->db);
171  if ($tmp_object->fetch($obj->rowid)) {
172  $obj_ret[] = $this->_cleanObjectDatas($tmp_object);
173  }
174  $i++;
175  }
176  } else {
177  throw new RestException(503, 'Error when retrieving myobject list: '.$this->db->lasterror());
178  }
179  if (!count($obj_ret)) {
180  throw new RestException(404, 'No myobject found');
181  }
182  return $obj_ret;
183  }
184 
195  public function post($request_data = null)
196  {
197  if (!DolibarrApiAccess::$user->rights->mymodule->write) {
198  throw new RestException(401);
199  }
200  // Check mandatory fields
201  $result = $this->_validate($request_data);
202 
203  foreach ($request_data as $field => $value) {
204  $this->myobject->$field = $value;
205  }
206  if ($this->myobject->create(DolibarrApiAccess::$user)<0) {
207  throw new RestException(500, "Error creating MyObject", array_merge(array($this->myobject->error), $this->myobject->errors));
208  }
209  return $this->myobject->id;
210  }
211 
223  public function put($id, $request_data = null)
224  {
225  if (!DolibarrApiAccess::$user->rights->mymodule->write) {
226  throw new RestException(401);
227  }
228 
229  $result = $this->myobject->fetch($id);
230  if (!$result) {
231  throw new RestException(404, 'MyObject not found');
232  }
233 
234  if (!DolibarrApi::_checkAccessToResource('myobject', $this->myobject->id, 'mymodule_myobject')) {
235  throw new RestException(401, 'Access to instance id='.$this->myobject->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
236  }
237 
238  foreach ($request_data as $field => $value) {
239  if ($field == 'id') continue;
240  $this->myobject->$field = $value;
241  }
242 
243  if ($this->myobject->update(DolibarrApiAccess::$user, false) > 0)
244  {
245  return $this->get($id);
246  } else {
247  throw new RestException(500, $this->myobject->error);
248  }
249  }
250 
261  public function delete($id)
262  {
263  if (!DolibarrApiAccess::$user->rights->mymodule->delete) {
264  throw new RestException(401);
265  }
266  $result = $this->myobject->fetch($id);
267  if (!$result) {
268  throw new RestException(404, 'MyObject not found');
269  }
270 
271  if (!DolibarrApi::_checkAccessToResource('myobject', $this->myobject->id, 'mymodule_myobject')) {
272  throw new RestException(401, 'Access to instance id='.$this->myobject->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
273  }
274 
275  if (!$this->myobject->delete(DolibarrApiAccess::$user))
276  {
277  throw new RestException(500, 'Error when deleting MyObject : '.$this->myobject->error);
278  }
279 
280  return array(
281  'success' => array(
282  'code' => 200,
283  'message' => 'MyObject deleted'
284  )
285  );
286  }
287 
288 
289  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
296  protected function _cleanObjectDatas($object)
297  {
298  // phpcs:enable
299  $object = parent::_cleanObjectDatas($object);
300 
301  unset($object->rowid);
302  unset($object->canvas);
303 
304  /*unset($object->name);
305  unset($object->lastname);
306  unset($object->firstname);
307  unset($object->civility_id);
308  unset($object->statut);
309  unset($object->state);
310  unset($object->state_id);
311  unset($object->state_code);
312  unset($object->region);
313  unset($object->region_code);
314  unset($object->country);
315  unset($object->country_id);
316  unset($object->country_code);
317  unset($object->barcode_type);
318  unset($object->barcode_type_code);
319  unset($object->barcode_type_label);
320  unset($object->barcode_type_coder);
321  unset($object->total_ht);
322  unset($object->total_tva);
323  unset($object->total_localtax1);
324  unset($object->total_localtax2);
325  unset($object->total_ttc);
326  unset($object->fk_account);
327  unset($object->comments);
328  unset($object->note);
329  unset($object->mode_reglement_id);
330  unset($object->cond_reglement_id);
331  unset($object->cond_reglement);
332  unset($object->shipping_method_id);
333  unset($object->fk_incoterms);
334  unset($object->label_incoterms);
335  unset($object->location_incoterms);
336  */
337 
338  // If object has lines, remove $db property
339  if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
340  $nboflines = count($object->lines);
341  for ($i = 0; $i < $nboflines; $i++)
342  {
343  $this->_cleanObjectDatas($object->lines[$i]);
344 
345  unset($object->lines[$i]->lines);
346  unset($object->lines[$i]->note);
347  }
348  }
349 
350  return $object;
351  }
352 
361  private function _validate($data)
362  {
363  $myobject = array();
364  foreach ($this->myobject->fields as $field => $propfield) {
365  if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) continue; // Not a mandatory field
366  if (!isset($data[$field]))
367  throw new RestException(400, "$field field missing");
368  $myobject[$field] = $data[$field];
369  }
370  return $myobject;
371  }
372 }
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname= '')
Make an include_once using default root and alternate root if it fails.
put($id, $request_data=null)
Update myobject.
Class for MyObject.
$conf db
API class for accounts.
Definition: inc.php:54
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:278
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $sqlfilters= '')
List myobjects.
Class for API REST v1.
Definition: api.class.php:30
post($request_data=null)
Create myobject object.
_cleanObjectDatas($object)
Clean sensible object datas.
_validate($data)
Validate fields before create or update object.
__construct()
Constructor.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
Definition: api.class.php:252