dolibarr  9.0.0
api_categories.class.php
1 <?php
2 /* Copyright (C) 2015 Jean-Fran├žois Ferry <jfefe@aternatik.fr>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
19 
20  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
21  require_once DOL_DOCUMENT_ROOT.'/societe/class/client.class.php';
22 
29 class Categories extends DolibarrApi
30 {
34  static $FIELDS = array(
35  'label',
36  'type'
37  );
38 
39  static $TYPES = array(
40  0 => 'product',
41  1 => 'supplier',
42  2 => 'customer',
43  3 => 'member',
44  4 => 'contact',
45  5 => 'account',
46  );
47 
51  public $category;
52 
56  function __construct()
57  {
58  global $db, $conf;
59  $this->db = $db;
60  $this->category = new Categorie($this->db);
61  }
62 
73  function get($id)
74  {
75  if(! DolibarrApiAccess::$user->rights->categorie->lire) {
76  throw new RestException(401);
77  }
78 
79  $result = $this->category->fetch($id);
80  if( ! $result ) {
81  throw new RestException(404, 'category not found');
82  }
83 
84  if( ! DolibarrApi::_checkAccessToResource('category',$this->category->id)) {
85  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
86  }
87 
88  return $this->_cleanObjectDatas($this->category);
89  }
90 
106  function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $sqlfilters = '')
107  {
108  global $db, $conf;
109 
110  $obj_ret = array();
111 
112  if(! DolibarrApiAccess::$user->rights->categorie->lire) {
113  throw new RestException(401);
114  }
115 
116  $sql = "SELECT t.rowid";
117  $sql.= " FROM ".MAIN_DB_PREFIX."categorie as t";
118  $sql.= ' WHERE t.entity IN ('.getEntity('category').')';
119  if (!empty($type))
120  {
121  $sql.= ' AND t.type='.array_search($type,Categories::$TYPES);
122  }
123  // Add sql filters
124  if ($sqlfilters)
125  {
126  if (! DolibarrApi::_checkFilters($sqlfilters))
127  {
128  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
129  }
130  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
131  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
132  }
133 
134  $sql.= $db->order($sortfield, $sortorder);
135  if ($limit) {
136  if ($page < 0)
137  {
138  $page = 0;
139  }
140  $offset = $limit * $page;
141 
142  $sql.= $db->plimit($limit + 1, $offset);
143  }
144 
145  $result = $db->query($sql);
146  if ($result)
147  {
148  $i=0;
149  $num = $db->num_rows($result);
150  $min = min($num, ($limit <= 0 ? $num : $limit));
151  while ($i < $min)
152  {
153  $obj = $db->fetch_object($result);
154  $category_static = new Categorie($db);
155  if($category_static->fetch($obj->rowid)) {
156  $obj_ret[] = $this->_cleanObjectDatas($category_static);
157  }
158  $i++;
159  }
160  }
161  else {
162  throw new RestException(503, 'Error when retrieve category list : '.$db->lasterror());
163  }
164  if( ! count($obj_ret)) {
165  throw new RestException(404, 'No category found');
166  }
167  return $obj_ret;
168  }
169 
176  function post($request_data = null)
177  {
178  if(! DolibarrApiAccess::$user->rights->categorie->creer) {
179  throw new RestException(401);
180  }
181 
182  // Check mandatory fields
183  $result = $this->_validate($request_data);
184 
185  foreach($request_data as $field => $value) {
186  $this->category->$field = $value;
187  }
188  if ($this->category->create(DolibarrApiAccess::$user) < 0) {
189  throw new RestException(500, 'Error when creating category', array_merge(array($this->category->error), $this->category->errors));
190  }
191  return $this->category->id;
192  }
193 
201  function put($id, $request_data = null)
202  {
203  if(! DolibarrApiAccess::$user->rights->categorie->creer) {
204  throw new RestException(401);
205  }
206 
207  $result = $this->category->fetch($id);
208  if( ! $result ) {
209  throw new RestException(404, 'category not found');
210  }
211 
212  if( ! DolibarrApi::_checkAccessToResource('category',$this->category->id)) {
213  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
214  }
215 
216  foreach($request_data as $field => $value) {
217  if ($field == 'id') continue;
218  $this->category->$field = $value;
219  }
220 
221  if ($this->category->update(DolibarrApiAccess::$user) > 0)
222  {
223  return $this->get ($id);
224  }
225  else
226  {
227  throw new RestException(500, $this->category->error);
228  }
229  }
230 
237  function delete($id)
238  {
239  if(! DolibarrApiAccess::$user->rights->categorie->supprimer) {
240  throw new RestException(401);
241  }
242  $result = $this->category->fetch($id);
243  if( ! $result ) {
244  throw new RestException(404, 'category not found');
245  }
246 
247  if( ! DolibarrApi::_checkAccessToResource('category',$this->category->id)) {
248  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
249  }
250 
251  if (! $this->category->delete(DolibarrApiAccess::$user)) {
252  throw new RestException(401,'error when delete category');
253  }
254 
255  return array(
256  'success' => array(
257  'code' => 200,
258  'message' => 'Category deleted'
259  )
260  );
261  }
262 
263 
270  function _cleanObjectDatas($object)
271  {
272 
273  $object = parent::_cleanObjectDatas($object);
274 
275  // Remove fields not relevent to categories
276  unset($object->country);
277  unset($object->country_id);
278  unset($object->country_code);
279  unset($object->total_ht);
280  unset($object->total_ht);
281  unset($object->total_localtax1);
282  unset($object->total_localtax2);
283  unset($object->total_ttc);
284  unset($object->total_tva);
285  unset($object->lines);
286  unset($object->fk_incoterms);
287  unset($object->libelle_incoterms);
288  unset($object->location_incoterms);
289  unset($object->civility_id);
290  unset($object->name);
291  unset($object->lastname);
292  unset($object->firstname);
293  unset($object->shipping_method_id);
294  unset($object->fk_delivery_address);
295  unset($object->cond_reglement);
296  unset($object->cond_reglement_id);
297  unset($object->mode_reglement_id);
298  unset($object->barcode_type_coder);
299  unset($object->barcode_type_label);
300  unset($object->barcode_type_code);
301  unset($object->barcode_type);
302  unset($object->canvas);
303  unset($object->cats);
304  unset($object->motherof);
305  unset($object->context);
306  unset($object->socid);
307  unset($object->thirdparty);
308  unset($object->contact);
309  unset($object->contact_id);
310  unset($object->user);
311  unset($object->fk_account);
312  unset($object->fk_project);
313  unset($object->note);
314  unset($object->statut);
315 
316  return $object;
317  }
318 
327  function _validate($data)
328  {
329  $category = array();
330  foreach (Categories::$FIELDS as $field) {
331  if (!isset($data[$field]))
332  throw new RestException(400, "$field field missing");
333  $category[$field] = $data[$field];
334  }
335  return $category;
336  }
337 }
__construct()
Constructor.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:251
Class for API REST v1.
Definition: api.class.php:29
_validate($data)
Validate fields before create or update object.
Class to manage categories.
post($request_data=null)
Create category object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $type='', $sqlfilters='')
List categories.
put($id, $request_data=null)
Update category.
_cleanObjectDatas($object)
Clean sensible object datas.
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:225