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 $this->category->$field = $value;
243 if ($this->category->update(DolibarrApiAccess::$user) > 0) {
244 return $this->
get($id);
246 throw new RestException(500, $this->category->error);
256 public function delete($id)
258 if (!DolibarrApiAccess::$user->rights->categorie->supprimer) {
259 throw new RestException(401);
261 $result = $this->category->fetch($id);
263 throw new RestException(404,
'category not found');
267 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
270 if ($this->category->delete(DolibarrApiAccess::$user) <= 0) {
271 throw new RestException(500,
'Error when delete category : ' . $this->category->error);
277 'message' =>
'Category deleted'
299 public function getListForObject($id, $type, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
301 if (!in_array($type, [
302 Categorie::TYPE_PRODUCT,
303 Categorie::TYPE_CONTACT,
304 Categorie::TYPE_CUSTOMER,
305 Categorie::TYPE_SUPPLIER,
306 Categorie::TYPE_MEMBER,
307 Categorie::TYPE_PROJECT,
308 Categorie::TYPE_KNOWLEDGEMANAGEMENT
310 throw new RestException(401);
313 if ($type == Categorie::TYPE_PRODUCT && !(DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) {
314 throw new RestException(401);
315 } elseif ($type == Categorie::TYPE_CONTACT && !DolibarrApiAccess::$user->rights->contact->lire) {
316 throw new RestException(401);
317 } elseif ($type == Categorie::TYPE_CUSTOMER && !DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
318 throw new RestException(401);
319 } elseif ($type == Categorie::TYPE_SUPPLIER && !DolibarrApiAccess::$user->rights->fournisseur->lire) {
320 throw new RestException(401);
321 } elseif ($type == Categorie::TYPE_MEMBER && !DolibarrApiAccess::$user->rights->adherent->lire) {
322 throw new RestException(401);
323 } elseif ($type == Categorie::TYPE_PROJECT && !DolibarrApiAccess::$user->rights->projet->lire) {
324 throw new RestException(401);
325 } elseif ($type == Categorie::TYPE_KNOWLEDGEMANAGEMENT && !DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
326 throw new RestException(401);
329 $categories = $this->category->getListForItem($id, $type, $sortfield, $sortorder, $limit, $page);
331 if (!is_array($categories)) {
332 if ($categories == 0) {
333 throw new RestException(404,
'No category found for this object');
335 throw new RestException(600,
'Error when fetching object categories', array_merge(array($this->category->error), $this->category->errors));
354 if (empty($type) || empty($object_id)) {
355 throw new RestException(401);
358 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
359 throw new RestException(401);
362 $result = $this->category->fetch($id);
364 throw new RestException(404,
'category not found');
367 if ($type === Categorie::TYPE_PRODUCT) {
368 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
369 throw new RestException(401);
371 $object =
new Product($this->db);
372 } elseif ($type === Categorie::TYPE_CUSTOMER) {
373 if (!DolibarrApiAccess::$user->rights->societe->creer) {
374 throw new RestException(401);
376 $object =
new Societe($this->db);
377 } elseif ($type === Categorie::TYPE_SUPPLIER) {
378 if (!DolibarrApiAccess::$user->rights->societe->creer) {
379 throw new RestException(401);
381 $object =
new Societe($this->db);
382 } elseif ($type === Categorie::TYPE_CONTACT) {
383 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
384 throw new RestException(401);
386 $object =
new Contact($this->db);
387 } elseif ($type === Categorie::TYPE_MEMBER) {
388 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
389 throw new RestException(401);
393 throw new RestException(401,
"this type is not recognized yet.");
396 if (!empty($object)) {
397 $result = $object->fetch($object_id);
399 $result = $this->category->add_type($object, $type);
401 if ($this->category->error !=
'DB_ERROR_RECORD_ALREADY_EXISTS') {
402 throw new RestException(500,
'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
406 throw new RestException(500,
'Error when fetching object', array_merge(array($object->error), $object->errors));
412 'message' =>
'Objects succefully linked to the category'
417 throw new RestException(401);
434 if (empty($type) || empty($object_ref)) {
435 throw new RestException(401);
438 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
439 throw new RestException(401);
442 $result = $this->category->fetch($id);
444 throw new RestException(404,
'category not found');
447 if ($type === Categorie::TYPE_PRODUCT) {
448 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
449 throw new RestException(401);
451 $object =
new Product($this->db);
452 } elseif ($type === Categorie::TYPE_CUSTOMER) {
453 if (!DolibarrApiAccess::$user->rights->societe->creer) {
454 throw new RestException(401);
456 $object =
new Societe($this->db);
457 } elseif ($type === Categorie::TYPE_SUPPLIER) {
458 if (!DolibarrApiAccess::$user->rights->societe->creer) {
459 throw new RestException(401);
461 $object =
new Societe($this->db);
462 } elseif ($type === Categorie::TYPE_CONTACT) {
463 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
464 throw new RestException(401);
466 $object =
new Contact($this->db);
467 } elseif ($type === Categorie::TYPE_MEMBER) {
468 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
469 throw new RestException(401);
473 throw new RestException(401,
"this type is not recognized yet.");
476 if (!empty($object)) {
477 $result = $object->fetch(
'', $object_ref);
479 $result = $this->category->add_type($object, $type);
481 if ($this->category->error !=
'DB_ERROR_RECORD_ALREADY_EXISTS') {
482 throw new RestException(500,
'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
486 throw new RestException(500,
'Error when fetching object', array_merge(array($object->error), $object->errors));
492 'message' =>
'Objects succefully linked to the category'
497 throw new RestException(401);
514 if (empty($type) || empty($object_id)) {
515 throw new RestException(401);
518 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
519 throw new RestException(401);
522 $result = $this->category->fetch($id);
524 throw new RestException(404,
'category not found');
527 if ($type === Categorie::TYPE_PRODUCT) {
528 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
529 throw new RestException(401);
531 $object =
new Product($this->db);
532 } elseif ($type === Categorie::TYPE_CUSTOMER) {
533 if (!DolibarrApiAccess::$user->rights->societe->creer) {
534 throw new RestException(401);
536 $object =
new Societe($this->db);
537 } elseif ($type === Categorie::TYPE_SUPPLIER) {
538 if (!DolibarrApiAccess::$user->rights->societe->creer) {
539 throw new RestException(401);
541 $object =
new Societe($this->db);
542 } elseif ($type === Categorie::TYPE_CONTACT) {
543 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
544 throw new RestException(401);
546 $object =
new Contact($this->db);
547 } elseif ($type === Categorie::TYPE_MEMBER) {
548 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
549 throw new RestException(401);
553 throw new RestException(401,
"this type is not recognized yet.");
556 if (!empty($object)) {
557 $result = $object->fetch((
int) $object_id);
559 $result = $this->category->del_type($object, $type);
561 throw new RestException(500,
'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
564 throw new RestException(500,
'Error when fetching object', array_merge(array($object->error), $object->errors));
570 'message' =>
'Objects succefully unlinked from the category'
575 throw new RestException(401);
592 if (empty($type) || empty($object_ref)) {
593 throw new RestException(401);
596 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
597 throw new RestException(401);
600 $result = $this->category->fetch($id);
602 throw new RestException(404,
'category not found');
605 if ($type === Categorie::TYPE_PRODUCT) {
606 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
607 throw new RestException(401);
609 $object =
new Product($this->db);
610 } elseif ($type === Categorie::TYPE_CUSTOMER) {
611 if (!DolibarrApiAccess::$user->rights->societe->creer) {
612 throw new RestException(401);
614 $object =
new Societe($this->db);
615 } elseif ($type === Categorie::TYPE_SUPPLIER) {
616 if (!DolibarrApiAccess::$user->rights->societe->creer) {
617 throw new RestException(401);
619 $object =
new Societe($this->db);
620 } elseif ($type === Categorie::TYPE_CONTACT) {
621 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
622 throw new RestException(401);
624 $object =
new Contact($this->db);
625 } elseif ($type === Categorie::TYPE_MEMBER) {
626 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
627 throw new RestException(401);
631 throw new RestException(401,
"this type is not recognized yet.");
634 if (!empty($object)) {
635 $result = $object->fetch(
'', (
string) $object_ref);
637 $result = $this->category->del_type($object, $type);
639 throw new RestException(500,
'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
642 throw new RestException(500,
'Error when fetching object', array_merge(array($object->error), $object->errors));
648 'message' =>
'Objects succefully unlinked from the category'
653 throw new RestException(401);
667 $object = parent::_cleanObjectDatas($object);
670 unset($object->MAP_CAT_FK);
671 unset($object->MAP_CAT_TABLE);
672 unset($object->MAP_OBJ_CLASS);
673 unset($object->MAP_OBJ_TABLE);
674 unset($object->country);
675 unset($object->country_id);
676 unset($object->country_code);
677 unset($object->total_ht);
678 unset($object->total_ht);
679 unset($object->total_localtax1);
680 unset($object->total_localtax2);
681 unset($object->total_ttc);
682 unset($object->total_tva);
683 unset($object->lines);
684 unset($object->civility_id);
685 unset($object->name);
686 unset($object->lastname);
687 unset($object->firstname);
688 unset($object->shipping_method_id);
689 unset($object->fk_delivery_address);
690 unset($object->cond_reglement);
691 unset($object->cond_reglement_id);
692 unset($object->mode_reglement_id);
693 unset($object->barcode_type_coder);
694 unset($object->barcode_type_label);
695 unset($object->barcode_type_code);
696 unset($object->barcode_type);
697 unset($object->canvas);
698 unset($object->cats);
699 unset($object->motherof);
700 unset($object->context);
701 unset($object->socid);
702 unset($object->thirdparty);
703 unset($object->contact);
704 unset($object->contact_id);
705 unset($object->user);
706 unset($object->fk_account);
707 unset($object->fk_project);
708 unset($object->note);
709 unset($object->statut);
725 foreach (Categories::$FIELDS as $field) {
726 if (!isset($data[$field])) {
727 throw new RestException(400,
"$field field missing");
729 $category[$field] = $data[$field];
747 dol_syslog(
"getObjects($id, $type, $onlyids)", LOG_DEBUG);
749 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
750 throw new RestException(401);
754 throw new RestException(500,
'The "type" parameter is required.');
757 $result = $this->category->fetch($id);
759 throw new RestException(404,
'category not found');
763 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
766 $result = $this->category->getObjectsInCateg($type, $onlyids);
769 throw new RestException(503,
'Error when retrieving objects list : '.$this->category->error);
773 $cleaned_objects = array();
775 if ($type ==
'member') {
777 } elseif ($type ==
'customer' || $type ==
'supplier') {
779 } elseif ($type ==
'product') {
781 } elseif ($type ==
'contact') {
783 } elseif ($type ==
'project') {
786 if (is_object($objects_api)) {
787 foreach ($objects as $obj) {
788 $cleaned_objects[] = $objects_api->_cleanObjectDatas($obj);
792 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.
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.