dolibarr  7.0.0-beta
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 
18  use Luracast\Restler\RestException;
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  global $db, $conf;
108 
109  $obj_ret = array();
110 
111  if(! DolibarrApiAccess::$user->rights->categorie->lire) {
112  throw new RestException(401);
113  }
114 
115  $sql = "SELECT t.rowid";
116  $sql.= " FROM ".MAIN_DB_PREFIX."categorie as t";
117  $sql.= ' WHERE t.entity IN ('.getEntity('category').')';
118  if (!empty($type))
119  {
120  $sql.= ' AND t.type='.array_search($type,Categories::$TYPES);
121  }
122  // Add sql filters
123  if ($sqlfilters)
124  {
125  if (! DolibarrApi::_checkFilters($sqlfilters))
126  {
127  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
128  }
129  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
130  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
131  }
132 
133  $sql.= $db->order($sortfield, $sortorder);
134  if ($limit) {
135  if ($page < 0)
136  {
137  $page = 0;
138  }
139  $offset = $limit * $page;
140 
141  $sql.= $db->plimit($limit + 1, $offset);
142  }
143 
144  $result = $db->query($sql);
145  if ($result)
146  {
147  $i=0;
148  $num = $db->num_rows($result);
149  $min = min($num, ($limit <= 0 ? $num : $limit));
150  while ($i < $min)
151  {
152  $obj = $db->fetch_object($result);
153  $category_static = new Categorie($db);
154  if($category_static->fetch($obj->rowid)) {
155  $obj_ret[] = $this->_cleanObjectDatas($category_static);
156  }
157  $i++;
158  }
159  }
160  else {
161  throw new RestException(503, 'Error when retrieve category list : '.$db->lasterror());
162  }
163  if( ! count($obj_ret)) {
164  throw new RestException(404, 'No category found');
165  }
166  return $obj_ret;
167  }
168 
175  function post($request_data = NULL)
176  {
177  if(! DolibarrApiAccess::$user->rights->categorie->creer) {
178  throw new RestException(401);
179  }
180 
181  // Check mandatory fields
182  $result = $this->_validate($request_data);
183 
184  foreach($request_data as $field => $value) {
185  $this->category->$field = $value;
186  }
187  if ($this->category->create(DolibarrApiAccess::$user) < 0) {
188  throw new RestException(500, 'Error when creating category', array_merge(array($this->category->error), $this->category->errors));
189  }
190  return $this->category->id;
191  }
192 
200  function put($id, $request_data = NULL)
201  {
202  if(! DolibarrApiAccess::$user->rights->categorie->creer) {
203  throw new RestException(401);
204  }
205 
206  $result = $this->category->fetch($id);
207  if( ! $result ) {
208  throw new RestException(404, 'category not found');
209  }
210 
211  if( ! DolibarrApi::_checkAccessToResource('category',$this->category->id)) {
212  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
213  }
214 
215  foreach($request_data as $field => $value) {
216  if ($field == 'id') continue;
217  $this->category->$field = $value;
218  }
219 
220  if ($this->category->update(DolibarrApiAccess::$user) > 0)
221  {
222  return $this->get ($id);
223  }
224  else
225  {
226  throw new RestException(500, $this->category->error);
227  }
228  }
229 
236  function delete($id)
237  {
238  if(! DolibarrApiAccess::$user->rights->categorie->supprimer) {
239  throw new RestException(401);
240  }
241  $result = $this->category->fetch($id);
242  if( ! $result ) {
243  throw new RestException(404, 'category not found');
244  }
245 
246  if( ! DolibarrApi::_checkAccessToResource('category',$this->category->id)) {
247  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
248  }
249 
250  if (! $this->category->delete(DolibarrApiAccess::$user)) {
251  throw new RestException(401,'error when delete category');
252  }
253 
254  return array(
255  'success' => array(
256  'code' => 200,
257  'message' => 'Category deleted'
258  )
259  );
260  }
261 
262 
269  function _cleanObjectDatas($object) {
270 
271  $object = parent::_cleanObjectDatas($object);
272 
273  // Remove fields not relevent to categories
274  unset($object->country);
275  unset($object->country_id);
276  unset($object->country_code);
277  unset($object->total_ht);
278  unset($object->total_ht);
279  unset($object->total_localtax1);
280  unset($object->total_localtax2);
281  unset($object->total_ttc);
282  unset($object->total_tva);
283  unset($object->lines);
284  unset($object->fk_incoterms);
285  unset($object->libelle_incoterms);
286  unset($object->location_incoterms);
287  unset($object->civility_id);
288  unset($object->name);
289  unset($object->lastname);
290  unset($object->firstname);
291  unset($object->shipping_method_id);
292  unset($object->fk_delivery_address);
293  unset($object->cond_reglement);
294  unset($object->cond_reglement_id);
295  unset($object->mode_reglement_id);
296  unset($object->barcode_type_coder);
297  unset($object->barcode_type_label);
298  unset($object->barcode_type_code);
299  unset($object->barcode_type);
300  unset($object->canvas);
301  unset($object->cats);
302  unset($object->motherof);
303  unset($object->context);
304  unset($object->socid);
305  unset($object->thirdparty);
306  unset($object->contact);
307  unset($object->contact_id);
308  unset($object->user);
309  unset($object->fk_account);
310  unset($object->fk_project);
311  unset($object->note);
312  unset($object->statut);
313 
314  return $object;
315  }
316 
325  function _validate($data)
326  {
327  $category = array();
328  foreach (Categories::$FIELDS as $field) {
329  if (!isset($data[$field]))
330  throw new RestException(400, "$field field missing");
331  $category[$field] = $data[$field];
332  }
333  return $category;
334  }
335 }
__construct()
Constructor.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $type= '', $sqlfilters= '')
List categories.
put($id, $request_data=NULL)
Update category.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:246
Class for API REST v1.
Definition: api.class.php:29
post($request_data=NULL)
Create category object.
_validate($data)
Validate fields before create or update object.
Class to manage categories.
_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:221