20use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
23require_once DOL_DOCUMENT_ROOT.
'/societe/class/client.class.php';
24require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
26require_once DOL_DOCUMENT_ROOT.
'/adherents/class/api_members.class.php';
27require_once DOL_DOCUMENT_ROOT.
'/product/class/api_products.class.php';
28require_once DOL_DOCUMENT_ROOT.
'/societe/class/api_contacts.class.php';
29require_once DOL_DOCUMENT_ROOT.
'/societe/class/api_thirdparties.class.php';
30require_once DOL_DOCUMENT_ROOT.
'/projet/class/api_projects.class.php';
43 public static $FIELDS = array(
51 public static $TYPES = array(
65 13 =>
'knowledgemanagement'
81 $this->category =
new Categorie($this->db);
95 public function get(
$id, $include_childs =
false)
97 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
98 throw new RestException(403);
101 $result = $this->category->fetch(
$id);
103 throw new RestException(404,
'category not found');
107 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
110 if ($include_childs) {
111 $cats = $this->category->get_filles();
112 if (!is_array($cats)) {
113 throw new RestException(500,
'Error when fetching child categories', array_merge(array($this->category->error), $this->category->errors));
115 $this->category->childs = array();
116 foreach ($cats as $cat) {
140 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $type =
'', $sqlfilters =
'', $properties =
'')
144 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
145 throw new RestException(403);
148 $sql =
"SELECT t.rowid";
149 $sql .=
" FROM ".MAIN_DB_PREFIX.
"categorie AS t LEFT JOIN ".MAIN_DB_PREFIX.
"categories_extrafields AS ef ON (ef.fk_object = t.rowid)";
150 $sql .=
' WHERE t.entity IN ('.getEntity(
'category').
')';
152 $sql .=
' AND t.type='.array_search($type, Categories::$TYPES);
159 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
163 $sql .= $this->db->order($sortfield, $sortorder);
168 $offset = $limit * $page;
170 $sql .= $this->db->plimit($limit + 1, $offset);
173 $result = $this->db->query($sql);
176 $num = $this->db->num_rows($result);
177 $min = min($num, ($limit <= 0 ? $num : $limit));
179 $obj = $this->db->fetch_object($result);
180 $category_static =
new Categorie($this->db);
181 if ($category_static->fetch($obj->rowid)) {
187 throw new RestException(503,
'Error when retrieve category list : '.$this->db->lasterror());
199 public function post($request_data =
null)
201 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'creer')) {
202 throw new RestException(403);
208 foreach ($request_data as $field => $value) {
209 if ($field ===
'caller') {
211 $this->category->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
215 $this->category->$field = $this->
_checkValForAPI($field, $value, $this->category);
217 if ($this->category->create(DolibarrApiAccess::$user) < 0) {
218 throw new RestException(500,
'Error when creating category', array_merge(array($this->category->error), $this->category->errors));
220 return $this->category->id;
230 public function put(
$id, $request_data =
null)
232 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'creer')) {
233 throw new RestException(403);
236 $result = $this->category->fetch(
$id);
238 throw new RestException(404,
'category not found');
242 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
245 foreach ($request_data as $field => $value) {
246 if ($field ==
'id') {
249 if ($field ===
'caller') {
251 $this->category->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
255 $this->category->$field = $this->
_checkValForAPI($field, $value, $this->category);
258 if ($this->category->update(DolibarrApiAccess::$user) > 0) {
259 return $this->
get(
$id);
261 throw new RestException(500, $this->category->error);
271 public function delete(
$id)
273 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'supprimer')) {
274 throw new RestException(403);
276 $result = $this->category->fetch(
$id);
278 throw new RestException(404,
'category not found');
282 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
285 if ($this->category->delete(DolibarrApiAccess::$user) <= 0) {
286 throw new RestException(500,
'Error when delete category : ' . $this->category->error);
292 'message' =>
'Category deleted'
314 public function getListForObject(
$id, $type, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
316 if (!in_array($type, [
317 Categorie::TYPE_PRODUCT,
318 Categorie::TYPE_CONTACT,
319 Categorie::TYPE_CUSTOMER,
320 Categorie::TYPE_SUPPLIER,
321 Categorie::TYPE_MEMBER,
322 Categorie::TYPE_PROJECT,
323 Categorie::TYPE_KNOWLEDGEMANAGEMENT,
324 Categorie::TYPE_ACTIONCOMM
326 throw new RestException(403);
329 if ($type == Categorie::TYPE_PRODUCT && !DolibarrApiAccess::$user->hasRight(
'produit',
'lire') && !DolibarrApiAccess::$user->hasRight(
'service',
'lire')) {
330 throw new RestException(403);
331 } elseif ($type == Categorie::TYPE_CONTACT && !DolibarrApiAccess::$user->hasRight(
'contact',
'lire')) {
332 throw new RestException(403);
333 } elseif ($type == Categorie::TYPE_CUSTOMER && !DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
334 throw new RestException(403);
335 } elseif ($type == Categorie::TYPE_SUPPLIER && !DolibarrApiAccess::$user->hasRight(
'fournisseur',
'lire')) {
336 throw new RestException(403);
337 } elseif ($type == Categorie::TYPE_MEMBER && !DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
338 throw new RestException(403);
339 } elseif ($type == Categorie::TYPE_PROJECT && !DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
340 throw new RestException(403);
341 } elseif ($type == Categorie::TYPE_KNOWLEDGEMANAGEMENT && !DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
342 throw new RestException(403);
343 } elseif ($type == Categorie::TYPE_ACTIONCOMM && !DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
344 throw new RestException(403);
347 $categories = $this->category->getListForItem(
$id, $type, $sortfield, $sortorder, $limit, $page);
349 if (!is_array($categories)) {
350 throw new RestException(600,
'Error when fetching object categories', array_merge(array($this->category->error), $this->category->errors));
369 if (empty($type) || empty($object_id)) {
370 throw new RestException(403);
373 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
374 throw new RestException(403);
377 $result = $this->category->fetch(
$id);
379 throw new RestException(404,
'category not found');
382 if ($type === Categorie::TYPE_PRODUCT) {
383 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer') && !DolibarrApiAccess::$user->hasRight(
'service',
'creer')) {
384 throw new RestException(403);
387 } elseif ($type === Categorie::TYPE_CUSTOMER) {
388 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
389 throw new RestException(403);
392 } elseif ($type === Categorie::TYPE_SUPPLIER) {
393 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
394 throw new RestException(403);
397 } elseif ($type === Categorie::TYPE_CONTACT) {
398 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
399 throw new RestException(403);
402 } elseif ($type === Categorie::TYPE_MEMBER) {
403 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
404 throw new RestException(403);
407 } elseif ($type === Categorie::TYPE_ACTIONCOMM) {
408 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
409 throw new RestException(403);
413 throw new RestException(400,
"this type is not recognized yet.");
416 $result =
$object->fetch($object_id);
418 $result = $this->category->add_type(
$object, $type);
420 if ($this->category->error !=
'DB_ERROR_RECORD_ALREADY_EXISTS') {
421 throw new RestException(500,
'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
425 throw new RestException(500,
'Error when fetching object', array_merge(array(
$object->error),
$object->errors));
431 'message' =>
'Objects successfully linked to the category'
450 if (empty($type) || empty($object_ref)) {
451 throw new RestException(403);
454 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
455 throw new RestException(403);
458 $result = $this->category->fetch(
$id);
460 throw new RestException(404,
'category not found');
463 if ($type === Categorie::TYPE_PRODUCT) {
464 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer') && !DolibarrApiAccess::$user->hasRight(
'service',
'creer')) {
465 throw new RestException(403);
468 } elseif ($type === Categorie::TYPE_CUSTOMER) {
469 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
470 throw new RestException(403);
473 } elseif ($type === Categorie::TYPE_SUPPLIER) {
474 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
475 throw new RestException(403);
478 } elseif ($type === Categorie::TYPE_CONTACT) {
479 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
480 throw new RestException(403);
483 } elseif ($type === Categorie::TYPE_MEMBER) {
484 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
485 throw new RestException(403);
488 } elseif ($type === Categorie::TYPE_ACTIONCOMM) {
489 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
490 throw new RestException(403);
494 throw new RestException(400,
"this type is not recognized yet.");
497 $result =
$object->fetch(0, $object_ref);
499 $result = $this->category->add_type(
$object, $type);
501 if ($this->category->error !=
'DB_ERROR_RECORD_ALREADY_EXISTS') {
502 throw new RestException(500,
'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
506 throw new RestException(500,
'Error when fetching object', array_merge(array(
$object->error),
$object->errors));
512 'message' =>
'Objects successfully linked to the category'
531 if (empty($type) || empty($object_id)) {
532 throw new RestException(403);
535 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
536 throw new RestException(403);
539 $result = $this->category->fetch(
$id);
541 throw new RestException(404,
'category not found');
544 if ($type === Categorie::TYPE_PRODUCT) {
545 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer') && !DolibarrApiAccess::$user->hasRight(
'service',
'creer')) {
546 throw new RestException(403);
549 } elseif ($type === Categorie::TYPE_CUSTOMER) {
550 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
551 throw new RestException(403);
554 } elseif ($type === Categorie::TYPE_SUPPLIER) {
555 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
556 throw new RestException(403);
559 } elseif ($type === Categorie::TYPE_CONTACT) {
560 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
561 throw new RestException(403);
564 } elseif ($type === Categorie::TYPE_MEMBER) {
565 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
566 throw new RestException(403);
569 } elseif ($type === Categorie::TYPE_ACTIONCOMM) {
570 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
571 throw new RestException(403);
575 throw new RestException(400,
"this type is not recognized yet.");
578 $result =
$object->fetch((
int) $object_id);
580 $result = $this->category->del_type(
$object, $type);
582 throw new RestException(500,
'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
585 throw new RestException(500,
'Error when fetching object', array_merge(array(
$object->error),
$object->errors));
591 'message' =>
'Objects successfully unlinked from the category'
610 if (empty($type) || empty($object_ref)) {
611 throw new RestException(403);
614 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
615 throw new RestException(403);
618 $result = $this->category->fetch(
$id);
620 throw new RestException(404,
'category not found');
623 if ($type === Categorie::TYPE_PRODUCT) {
624 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer') && !DolibarrApiAccess::$user->hasRight(
'service',
'creer')) {
625 throw new RestException(403);
628 } elseif ($type === Categorie::TYPE_CUSTOMER) {
629 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
630 throw new RestException(403);
633 } elseif ($type === Categorie::TYPE_SUPPLIER) {
634 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
635 throw new RestException(403);
638 } elseif ($type === Categorie::TYPE_CONTACT) {
639 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
640 throw new RestException(403);
643 } elseif ($type === Categorie::TYPE_MEMBER) {
644 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
645 throw new RestException(403);
648 } elseif ($type === Categorie::TYPE_ACTIONCOMM) {
649 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
650 throw new RestException(403);
654 throw new RestException(400,
"this type is not recognized yet.");
657 $result =
$object->fetch(0, (
string) $object_ref);
659 $result = $this->category->del_type(
$object, $type);
661 throw new RestException(500,
'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
664 throw new RestException(500,
'Error when fetching object', array_merge(array(
$object->error),
$object->errors));
670 'message' =>
'Objects successfully unlinked from the category'
698 unset(
$object->total_localtax1);
699 unset(
$object->total_localtax2);
707 unset(
$object->shipping_method_id);
708 unset(
$object->fk_delivery_address);
709 unset(
$object->cond_reglement);
710 unset(
$object->cond_reglement_id);
711 unset(
$object->mode_reglement_id);
712 unset(
$object->barcode_type_coder);
713 unset(
$object->barcode_type_label);
714 unset(
$object->barcode_type_code);
744 foreach (Categories::$FIELDS as $field) {
745 if (!isset($data[$field])) {
746 throw new RestException(400,
"$field field missing");
748 $category[$field] = $data[$field];
766 dol_syslog(
"getObjects($id, $type, $onlyids)", LOG_DEBUG);
768 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
769 throw new RestException(403);
773 throw new RestException(500,
'The "type" parameter is required.');
776 $result = $this->category->fetch(
$id);
778 throw new RestException(404,
'category not found');
782 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
785 $result = $this->category->getObjectsInCateg($type, $onlyids);
788 throw new RestException(503,
'Error when retrieving objects list : '.$this->category->error);
792 $cleaned_objects = array();
794 if ($type ==
'member') {
796 } elseif ($type ==
'customer' || $type ==
'supplier') {
798 } elseif ($type ==
'product') {
800 } elseif ($type ==
'contact') {
802 } elseif ($type ==
'project') {
806 if (is_object($objects_api)) {
807 foreach ($objects as $obj) {
808 $cleaned_objects[] = $objects_api->_cleanObjectDatas($obj);
812 return $cleaned_objects;
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Class to manage agenda events (actions)
Class to manage members of a foundation.
Class to manage categories.
__construct()
Constructor.
put($id, $request_data=null)
Update category.
unlinkObjectById($id, $type, $object_id)
Unlink an object from a category by id.
unlinkObjectByRef($id, $type, $object_ref)
Unlink an object from a category by ref.
_validate($data)
Validate fields before create or update object.
post($request_data=null)
Create category object.
_cleanObjectDatas($object)
Clean sensible object datas.
getObjects($id, $type, $onlyids=0)
Get the list of objects in a category.
getListForObject($id, $type, $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
List categories of an object.
linkObjectById($id, $type, $object_id)
Link an object to a category by id.
linkObjectByRef($id, $type, $object_ref)
Link an object to a category by ref.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $type='', $sqlfilters='', $properties='')
List categories.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Class to manage products or services.
Class to manage third parties objects (customers, suppliers, prospects...)
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.