18use Luracast\Restler\RestException;
20require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
21require_once DOL_DOCUMENT_ROOT.
'/societe/class/client.class.php';
24require_once DOL_DOCUMENT_ROOT.
'/adherents/class/api_members.class.php';
25require_once DOL_DOCUMENT_ROOT.
'/product/class/api_products.class.php';
26require_once DOL_DOCUMENT_ROOT.
'/societe/class/api_contacts.class.php';
27require_once DOL_DOCUMENT_ROOT.
'/societe/class/api_thirdparties.class.php';
28require_once DOL_DOCUMENT_ROOT.
'/projet/class/api_projects.class.php';
41 static $FIELDS = array(
46 static $TYPES = array(
60 13 =>
'knowledgemanagement'
75 $this->category =
new Categorie($this->db);
89 public function get($id, $include_childs =
false)
91 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
92 throw new RestException(401);
95 $result = $this->category->fetch($id);
97 throw new RestException(404,
'category not found');
101 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
104 if ($include_childs) {
105 $cats = $this->category->get_filles();
106 if (!is_array($cats)) {
107 throw new RestException(500,
'Error when fetching child categories', array_merge(array($this->category->error), $this->category->errors));
109 $this->category->childs = array();
110 foreach ($cats as $cat) {
133 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $type =
'', $sqlfilters =
'')
139 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
140 throw new RestException(401);
143 $sql =
"SELECT t.rowid";
144 $sql .=
" FROM ".MAIN_DB_PREFIX.
"categorie AS t LEFT JOIN ".MAIN_DB_PREFIX.
"categories_extrafields AS ef ON (ef.fk_object = t.rowid)";
145 $sql .=
' WHERE t.entity IN ('.getEntity(
'category').
')';
147 $sql .=
' AND t.type='.array_search($type, Categories::$TYPES);
154 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
158 $sql .= $this->db->order($sortfield, $sortorder);
163 $offset = $limit * $page;
165 $sql .= $this->db->plimit($limit + 1, $offset);
168 $result = $this->db->query($sql);
171 $num = $this->db->num_rows($result);
172 $min = min($num, ($limit <= 0 ? $num : $limit));
174 $obj = $this->db->fetch_object($result);
175 $category_static =
new Categorie($this->db);
176 if ($category_static->fetch($obj->rowid)) {
182 throw new RestException(503,
'Error when retrieve category list : '.$this->db->lasterror());
184 if (!count($obj_ret)) {
185 throw new RestException(404,
'No category found');
196 public function post($request_data =
null)
198 if (!DolibarrApiAccess::$user->rights->categorie->creer) {
199 throw new RestException(401);
203 $result = $this->
_validate($request_data);
205 foreach ($request_data as $field => $value) {
206 $this->category->$field = $value;
208 if ($this->category->create(DolibarrApiAccess::$user) < 0) {
209 throw new RestException(500,
'Error when creating category', array_merge(array($this->category->error), $this->category->errors));
211 return $this->category->id;
221 public function put($id, $request_data =
null)
223 if (!DolibarrApiAccess::$user->rights->categorie->creer) {
224 throw new RestException(401);
227 $result = $this->category->fetch($id);
229 throw new RestException(404,
'category not found');
233 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
236 foreach ($request_data as $field => $value) {
237 if ($field ==
'id') {
240 if ($field ==
'array_options' && is_array($value)) {
241 foreach ($value as $index => $val) {
242 $this->category->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->category);
246 $this->category->$field = $value;
249 if ($this->category->update(DolibarrApiAccess::$user) > 0) {
250 return $this->
get($id);
252 throw new RestException(500, $this->category->error);
262 public function delete($id)
264 if (!DolibarrApiAccess::$user->rights->categorie->supprimer) {
265 throw new RestException(401);
267 $result = $this->category->fetch($id);
269 throw new RestException(404,
'category not found');
273 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
276 if ($this->category->delete(DolibarrApiAccess::$user) <= 0) {
277 throw new RestException(500,
'Error when delete category : ' . $this->category->error);
283 'message' =>
'Category deleted'
305 public function getListForObject($id, $type, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
307 if (!in_array($type, [
308 Categorie::TYPE_PRODUCT,
309 Categorie::TYPE_CONTACT,
310 Categorie::TYPE_CUSTOMER,
311 Categorie::TYPE_SUPPLIER,
312 Categorie::TYPE_MEMBER,
313 Categorie::TYPE_PROJECT,
314 Categorie::TYPE_KNOWLEDGEMANAGEMENT
316 throw new RestException(401);
319 if ($type == Categorie::TYPE_PRODUCT && !(DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) {
320 throw new RestException(401);
321 } elseif ($type == Categorie::TYPE_CONTACT && !DolibarrApiAccess::$user->rights->contact->lire) {
322 throw new RestException(401);
323 } elseif ($type == Categorie::TYPE_CUSTOMER && !DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
324 throw new RestException(401);
325 } elseif ($type == Categorie::TYPE_SUPPLIER && !DolibarrApiAccess::$user->rights->fournisseur->lire) {
326 throw new RestException(401);
327 } elseif ($type == Categorie::TYPE_MEMBER && !DolibarrApiAccess::$user->rights->adherent->lire) {
328 throw new RestException(401);
329 } elseif ($type == Categorie::TYPE_PROJECT && !DolibarrApiAccess::$user->rights->projet->lire) {
330 throw new RestException(401);
331 } elseif ($type == Categorie::TYPE_KNOWLEDGEMANAGEMENT && !DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
332 throw new RestException(401);
335 $categories = $this->category->getListForItem($id, $type, $sortfield, $sortorder, $limit, $page);
337 if (!is_array($categories)) {
338 if ($categories == 0) {
339 throw new RestException(404,
'No category found for this object');
341 throw new RestException(600,
'Error when fetching object categories', array_merge(array($this->category->error), $this->category->errors));
360 if (empty($type) || empty($object_id)) {
361 throw new RestException(401);
364 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
365 throw new RestException(401);
368 $result = $this->category->fetch($id);
370 throw new RestException(404,
'category not found');
373 if ($type === Categorie::TYPE_PRODUCT) {
374 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
375 throw new RestException(401);
377 $object =
new Product($this->db);
378 } elseif ($type === Categorie::TYPE_CUSTOMER) {
379 if (!DolibarrApiAccess::$user->rights->societe->creer) {
380 throw new RestException(401);
382 $object =
new Societe($this->db);
383 } elseif ($type === Categorie::TYPE_SUPPLIER) {
384 if (!DolibarrApiAccess::$user->rights->societe->creer) {
385 throw new RestException(401);
387 $object =
new Societe($this->db);
388 } elseif ($type === Categorie::TYPE_CONTACT) {
389 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
390 throw new RestException(401);
392 $object =
new Contact($this->db);
393 } elseif ($type === Categorie::TYPE_MEMBER) {
394 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
395 throw new RestException(401);
399 throw new RestException(401,
"this type is not recognized yet.");
402 if (!empty($object)) {
403 $result = $object->fetch($object_id);
405 $result = $this->category->add_type($object, $type);
407 if ($this->category->error !=
'DB_ERROR_RECORD_ALREADY_EXISTS') {
408 throw new RestException(500,
'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
412 throw new RestException(500,
'Error when fetching object', array_merge(array($object->error), $object->errors));
418 'message' =>
'Objects succefully linked to the category'
423 throw new RestException(401);
440 if (empty($type) || empty($object_ref)) {
441 throw new RestException(401);
444 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
445 throw new RestException(401);
448 $result = $this->category->fetch($id);
450 throw new RestException(404,
'category not found');
453 if ($type === Categorie::TYPE_PRODUCT) {
454 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
455 throw new RestException(401);
457 $object =
new Product($this->db);
458 } elseif ($type === Categorie::TYPE_CUSTOMER) {
459 if (!DolibarrApiAccess::$user->rights->societe->creer) {
460 throw new RestException(401);
462 $object =
new Societe($this->db);
463 } elseif ($type === Categorie::TYPE_SUPPLIER) {
464 if (!DolibarrApiAccess::$user->rights->societe->creer) {
465 throw new RestException(401);
467 $object =
new Societe($this->db);
468 } elseif ($type === Categorie::TYPE_CONTACT) {
469 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
470 throw new RestException(401);
472 $object =
new Contact($this->db);
473 } elseif ($type === Categorie::TYPE_MEMBER) {
474 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
475 throw new RestException(401);
479 throw new RestException(401,
"this type is not recognized yet.");
482 if (!empty($object)) {
483 $result = $object->fetch(
'', $object_ref);
485 $result = $this->category->add_type($object, $type);
487 if ($this->category->error !=
'DB_ERROR_RECORD_ALREADY_EXISTS') {
488 throw new RestException(500,
'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
492 throw new RestException(500,
'Error when fetching object', array_merge(array($object->error), $object->errors));
498 'message' =>
'Objects succefully linked to the category'
503 throw new RestException(401);
520 if (empty($type) || empty($object_id)) {
521 throw new RestException(401);
524 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
525 throw new RestException(401);
528 $result = $this->category->fetch($id);
530 throw new RestException(404,
'category not found');
533 if ($type === Categorie::TYPE_PRODUCT) {
534 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
535 throw new RestException(401);
537 $object =
new Product($this->db);
538 } elseif ($type === Categorie::TYPE_CUSTOMER) {
539 if (!DolibarrApiAccess::$user->rights->societe->creer) {
540 throw new RestException(401);
542 $object =
new Societe($this->db);
543 } elseif ($type === Categorie::TYPE_SUPPLIER) {
544 if (!DolibarrApiAccess::$user->rights->societe->creer) {
545 throw new RestException(401);
547 $object =
new Societe($this->db);
548 } elseif ($type === Categorie::TYPE_CONTACT) {
549 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
550 throw new RestException(401);
552 $object =
new Contact($this->db);
553 } elseif ($type === Categorie::TYPE_MEMBER) {
554 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
555 throw new RestException(401);
559 throw new RestException(401,
"this type is not recognized yet.");
562 if (!empty($object)) {
563 $result = $object->fetch((
int) $object_id);
565 $result = $this->category->del_type($object, $type);
567 throw new RestException(500,
'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
570 throw new RestException(500,
'Error when fetching object', array_merge(array($object->error), $object->errors));
576 'message' =>
'Objects succefully unlinked from the category'
581 throw new RestException(401);
598 if (empty($type) || empty($object_ref)) {
599 throw new RestException(401);
602 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
603 throw new RestException(401);
606 $result = $this->category->fetch($id);
608 throw new RestException(404,
'category not found');
611 if ($type === Categorie::TYPE_PRODUCT) {
612 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
613 throw new RestException(401);
615 $object =
new Product($this->db);
616 } elseif ($type === Categorie::TYPE_CUSTOMER) {
617 if (!DolibarrApiAccess::$user->rights->societe->creer) {
618 throw new RestException(401);
620 $object =
new Societe($this->db);
621 } elseif ($type === Categorie::TYPE_SUPPLIER) {
622 if (!DolibarrApiAccess::$user->rights->societe->creer) {
623 throw new RestException(401);
625 $object =
new Societe($this->db);
626 } elseif ($type === Categorie::TYPE_CONTACT) {
627 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
628 throw new RestException(401);
630 $object =
new Contact($this->db);
631 } elseif ($type === Categorie::TYPE_MEMBER) {
632 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
633 throw new RestException(401);
637 throw new RestException(401,
"this type is not recognized yet.");
640 if (!empty($object)) {
641 $result = $object->fetch(
'', (
string) $object_ref);
643 $result = $this->category->del_type($object, $type);
645 throw new RestException(500,
'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
648 throw new RestException(500,
'Error when fetching object', array_merge(array($object->error), $object->errors));
654 'message' =>
'Objects succefully unlinked from the category'
659 throw new RestException(401);
673 $object = parent::_cleanObjectDatas($object);
676 unset($object->MAP_CAT_FK);
677 unset($object->MAP_CAT_TABLE);
678 unset($object->MAP_OBJ_CLASS);
679 unset($object->MAP_OBJ_TABLE);
680 unset($object->country);
681 unset($object->country_id);
682 unset($object->country_code);
683 unset($object->total_ht);
684 unset($object->total_ht);
685 unset($object->total_localtax1);
686 unset($object->total_localtax2);
687 unset($object->total_ttc);
688 unset($object->total_tva);
689 unset($object->lines);
690 unset($object->civility_id);
691 unset($object->name);
692 unset($object->lastname);
693 unset($object->firstname);
694 unset($object->shipping_method_id);
695 unset($object->fk_delivery_address);
696 unset($object->cond_reglement);
697 unset($object->cond_reglement_id);
698 unset($object->mode_reglement_id);
699 unset($object->barcode_type_coder);
700 unset($object->barcode_type_label);
701 unset($object->barcode_type_code);
702 unset($object->barcode_type);
703 unset($object->canvas);
704 unset($object->cats);
705 unset($object->motherof);
706 unset($object->context);
707 unset($object->socid);
708 unset($object->thirdparty);
709 unset($object->contact);
710 unset($object->contact_id);
711 unset($object->user);
712 unset($object->fk_account);
713 unset($object->fk_project);
714 unset($object->note);
715 unset($object->statut);
731 foreach (Categories::$FIELDS as $field) {
732 if (!isset($data[$field])) {
733 throw new RestException(400,
"$field field missing");
735 $category[$field] = $data[$field];
753 dol_syslog(
"getObjects($id, $type, $onlyids)", LOG_DEBUG);
755 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
756 throw new RestException(401);
760 throw new RestException(500,
'The "type" parameter is required.');
763 $result = $this->category->fetch($id);
765 throw new RestException(404,
'category not found');
769 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
772 $result = $this->category->getObjectsInCateg($type, $onlyids);
775 throw new RestException(503,
'Error when retrieving objects list : '.$this->category->error);
779 $cleaned_objects = array();
781 if ($type ==
'member') {
783 } elseif ($type ==
'customer' || $type ==
'supplier') {
785 } elseif ($type ==
'product') {
787 } elseif ($type ==
'contact') {
789 } elseif ($type ==
'project') {
792 if (is_object($objects_api)) {
793 foreach ($objects as $obj) {
794 $cleaned_objects[] = $objects_api->_cleanObjectDatas($obj);
798 return $cleaned_objects;
Class to manage members of a foundation.
Class to manage categories.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $type='', $sqlfilters='')
List 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.
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
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.