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 public static $FIELDS = array(
46 public 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) {
134 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $type =
'', $sqlfilters =
'', $properties =
'')
140 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
141 throw new RestException(401);
144 $sql =
"SELECT t.rowid";
145 $sql .=
" FROM ".MAIN_DB_PREFIX.
"categorie AS t LEFT JOIN ".MAIN_DB_PREFIX.
"categories_extrafields AS ef ON (ef.fk_object = t.rowid)";
146 $sql .=
' WHERE t.entity IN ('.getEntity(
'category').
')';
148 $sql .=
' AND t.type='.array_search($type, Categories::$TYPES);
155 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
159 $sql .= $this->db->order($sortfield, $sortorder);
164 $offset = $limit * $page;
166 $sql .= $this->db->plimit($limit + 1, $offset);
169 $result = $this->db->query($sql);
172 $num = $this->db->num_rows($result);
173 $min = min($num, ($limit <= 0 ? $num : $limit));
175 $obj = $this->db->fetch_object($result);
176 $category_static =
new Categorie($this->db);
177 if ($category_static->fetch($obj->rowid)) {
183 throw new RestException(503,
'Error when retrieve category list : '.$this->db->lasterror());
195 public function post($request_data =
null)
197 if (!DolibarrApiAccess::$user->rights->categorie->creer) {
198 throw new RestException(401);
202 $result = $this->
_validate($request_data);
204 foreach ($request_data as $field => $value) {
205 if ($field ===
'caller') {
207 $this->category->context[
'caller'] = $request_data[
'caller'];
211 $this->category->$field = $value;
213 if ($this->category->create(DolibarrApiAccess::$user) < 0) {
214 throw new RestException(500,
'Error when creating category', array_merge(array($this->category->error), $this->category->errors));
216 return $this->category->id;
226 public function put($id, $request_data =
null)
228 if (!DolibarrApiAccess::$user->rights->categorie->creer) {
229 throw new RestException(401);
232 $result = $this->category->fetch($id);
234 throw new RestException(404,
'category not found');
238 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
241 foreach ($request_data as $field => $value) {
242 if ($field ==
'id') {
245 if ($field ===
'caller') {
247 $this->category->context[
'caller'] = $request_data[
'caller'];
251 $this->category->$field = $value;
254 if ($this->category->update(DolibarrApiAccess::$user) > 0) {
255 return $this->
get($id);
257 throw new RestException(500, $this->category->error);
267 public function delete($id)
269 if (!DolibarrApiAccess::$user->rights->categorie->supprimer) {
270 throw new RestException(401);
272 $result = $this->category->fetch($id);
274 throw new RestException(404,
'category not found');
278 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
281 if (!$this->category->delete(DolibarrApiAccess::$user)) {
282 throw new RestException(401,
'error when delete category');
288 'message' =>
'Category deleted'
310 public function getListForObject($id, $type, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
312 if (!in_array($type, [
313 Categorie::TYPE_PRODUCT,
314 Categorie::TYPE_CONTACT,
315 Categorie::TYPE_CUSTOMER,
316 Categorie::TYPE_SUPPLIER,
317 Categorie::TYPE_MEMBER,
318 Categorie::TYPE_PROJECT,
319 Categorie::TYPE_KNOWLEDGEMANAGEMENT
321 throw new RestException(401);
324 if ($type == Categorie::TYPE_PRODUCT && !(DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) {
325 throw new RestException(401);
326 } elseif ($type == Categorie::TYPE_CONTACT && !DolibarrApiAccess::$user->rights->contact->lire) {
327 throw new RestException(401);
328 } elseif ($type == Categorie::TYPE_CUSTOMER && !DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
329 throw new RestException(401);
330 } elseif ($type == Categorie::TYPE_SUPPLIER && !DolibarrApiAccess::$user->rights->fournisseur->lire) {
331 throw new RestException(401);
332 } elseif ($type == Categorie::TYPE_MEMBER && !DolibarrApiAccess::$user->rights->adherent->lire) {
333 throw new RestException(401);
334 } elseif ($type == Categorie::TYPE_PROJECT && !DolibarrApiAccess::$user->rights->projet->lire) {
335 throw new RestException(401);
336 } elseif ($type == Categorie::TYPE_KNOWLEDGEMANAGEMENT && !DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
337 throw new RestException(401);
340 $categories = $this->category->getListForItem($id, $type, $sortfield, $sortorder, $limit, $page);
342 if (!is_array($categories)) {
343 throw new RestException(600,
'Error when fetching object categories', array_merge(array($this->category->error), $this->category->errors));
362 if (empty($type) || empty($object_id)) {
363 throw new RestException(401);
366 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
367 throw new RestException(401);
370 $result = $this->category->fetch($id);
372 throw new RestException(404,
'category not found');
375 if ($type === Categorie::TYPE_PRODUCT) {
376 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
377 throw new RestException(401);
379 $object =
new Product($this->db);
380 } elseif ($type === Categorie::TYPE_CUSTOMER) {
381 if (!DolibarrApiAccess::$user->rights->societe->creer) {
382 throw new RestException(401);
384 $object =
new Societe($this->db);
385 } elseif ($type === Categorie::TYPE_SUPPLIER) {
386 if (!DolibarrApiAccess::$user->rights->societe->creer) {
387 throw new RestException(401);
389 $object =
new Societe($this->db);
390 } elseif ($type === Categorie::TYPE_CONTACT) {
391 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
392 throw new RestException(401);
394 $object =
new Contact($this->db);
395 } elseif ($type === Categorie::TYPE_MEMBER) {
396 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
397 throw new RestException(401);
401 throw new RestException(401,
"this type is not recognized yet.");
404 if (!empty($object)) {
405 $result = $object->fetch($object_id);
407 $result = $this->category->add_type($object, $type);
409 if ($this->category->error !=
'DB_ERROR_RECORD_ALREADY_EXISTS') {
410 throw new RestException(500,
'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
414 throw new RestException(500,
'Error when fetching object', array_merge(array($object->error), $object->errors));
420 'message' =>
'Objects succefully linked to the category'
425 throw new RestException(401);
442 if (empty($type) || empty($object_ref)) {
443 throw new RestException(401);
446 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
447 throw new RestException(401);
450 $result = $this->category->fetch($id);
452 throw new RestException(404,
'category not found');
455 if ($type === Categorie::TYPE_PRODUCT) {
456 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
457 throw new RestException(401);
459 $object =
new Product($this->db);
460 } elseif ($type === Categorie::TYPE_CUSTOMER) {
461 if (!DolibarrApiAccess::$user->rights->societe->creer) {
462 throw new RestException(401);
464 $object =
new Societe($this->db);
465 } elseif ($type === Categorie::TYPE_SUPPLIER) {
466 if (!DolibarrApiAccess::$user->rights->societe->creer) {
467 throw new RestException(401);
469 $object =
new Societe($this->db);
470 } elseif ($type === Categorie::TYPE_CONTACT) {
471 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
472 throw new RestException(401);
474 $object =
new Contact($this->db);
475 } elseif ($type === Categorie::TYPE_MEMBER) {
476 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
477 throw new RestException(401);
481 throw new RestException(401,
"this type is not recognized yet.");
484 if (!empty($object)) {
485 $result = $object->fetch(
'', $object_ref);
487 $result = $this->category->add_type($object, $type);
489 if ($this->category->error !=
'DB_ERROR_RECORD_ALREADY_EXISTS') {
490 throw new RestException(500,
'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
494 throw new RestException(500,
'Error when fetching object', array_merge(array($object->error), $object->errors));
500 'message' =>
'Objects succefully linked to the category'
505 throw new RestException(401);
522 if (empty($type) || empty($object_id)) {
523 throw new RestException(401);
526 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
527 throw new RestException(401);
530 $result = $this->category->fetch($id);
532 throw new RestException(404,
'category not found');
535 if ($type === Categorie::TYPE_PRODUCT) {
536 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
537 throw new RestException(401);
539 $object =
new Product($this->db);
540 } elseif ($type === Categorie::TYPE_CUSTOMER) {
541 if (!DolibarrApiAccess::$user->rights->societe->creer) {
542 throw new RestException(401);
544 $object =
new Societe($this->db);
545 } elseif ($type === Categorie::TYPE_SUPPLIER) {
546 if (!DolibarrApiAccess::$user->rights->societe->creer) {
547 throw new RestException(401);
549 $object =
new Societe($this->db);
550 } elseif ($type === Categorie::TYPE_CONTACT) {
551 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
552 throw new RestException(401);
554 $object =
new Contact($this->db);
555 } elseif ($type === Categorie::TYPE_MEMBER) {
556 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
557 throw new RestException(401);
561 throw new RestException(401,
"this type is not recognized yet.");
564 if (!empty($object)) {
565 $result = $object->fetch((
int) $object_id);
567 $result = $this->category->del_type($object, $type);
569 throw new RestException(500,
'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
572 throw new RestException(500,
'Error when fetching object', array_merge(array($object->error), $object->errors));
578 'message' =>
'Objects succefully unlinked from the category'
583 throw new RestException(401);
600 if (empty($type) || empty($object_ref)) {
601 throw new RestException(401);
604 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
605 throw new RestException(401);
608 $result = $this->category->fetch($id);
610 throw new RestException(404,
'category not found');
613 if ($type === Categorie::TYPE_PRODUCT) {
614 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
615 throw new RestException(401);
617 $object =
new Product($this->db);
618 } elseif ($type === Categorie::TYPE_CUSTOMER) {
619 if (!DolibarrApiAccess::$user->rights->societe->creer) {
620 throw new RestException(401);
622 $object =
new Societe($this->db);
623 } elseif ($type === Categorie::TYPE_SUPPLIER) {
624 if (!DolibarrApiAccess::$user->rights->societe->creer) {
625 throw new RestException(401);
627 $object =
new Societe($this->db);
628 } elseif ($type === Categorie::TYPE_CONTACT) {
629 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
630 throw new RestException(401);
632 $object =
new Contact($this->db);
633 } elseif ($type === Categorie::TYPE_MEMBER) {
634 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
635 throw new RestException(401);
639 throw new RestException(401,
"this type is not recognized yet.");
642 if (!empty($object)) {
643 $result = $object->fetch(
'', (
string) $object_ref);
645 $result = $this->category->del_type($object, $type);
647 throw new RestException(500,
'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
650 throw new RestException(500,
'Error when fetching object', array_merge(array($object->error), $object->errors));
656 'message' =>
'Objects succefully unlinked from the category'
661 throw new RestException(401);
675 $object = parent::_cleanObjectDatas($object);
678 unset($object->MAP_CAT_FK);
679 unset($object->MAP_CAT_TABLE);
680 unset($object->MAP_OBJ_CLASS);
681 unset($object->MAP_OBJ_TABLE);
682 unset($object->country);
683 unset($object->country_id);
684 unset($object->country_code);
685 unset($object->total_ht);
686 unset($object->total_ht);
687 unset($object->total_localtax1);
688 unset($object->total_localtax2);
689 unset($object->total_ttc);
690 unset($object->total_tva);
691 unset($object->lines);
692 unset($object->civility_id);
693 unset($object->name);
694 unset($object->lastname);
695 unset($object->firstname);
696 unset($object->shipping_method_id);
697 unset($object->fk_delivery_address);
698 unset($object->cond_reglement);
699 unset($object->cond_reglement_id);
700 unset($object->mode_reglement_id);
701 unset($object->barcode_type_coder);
702 unset($object->barcode_type_label);
703 unset($object->barcode_type_code);
704 unset($object->barcode_type);
705 unset($object->canvas);
706 unset($object->cats);
707 unset($object->motherof);
708 unset($object->context);
709 unset($object->socid);
710 unset($object->thirdparty);
711 unset($object->contact);
712 unset($object->contact_id);
713 unset($object->user);
714 unset($object->fk_account);
715 unset($object->fk_project);
716 unset($object->note);
717 unset($object->statut);
733 foreach (Categories::$FIELDS as $field) {
734 if (!isset($data[$field])) {
735 throw new RestException(400,
"$field field missing");
737 $category[$field] = $data[$field];
755 dol_syslog(
"getObjects($id, $type, $onlyids)", LOG_DEBUG);
757 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
758 throw new RestException(401);
762 throw new RestException(500,
'The "type" parameter is required.');
765 $result = $this->category->fetch($id);
767 throw new RestException(404,
'category not found');
771 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
774 $result = $this->category->getObjectsInCateg($type, $onlyids);
777 throw new RestException(503,
'Error when retrieving objects list : '.$this->category->error);
781 $cleaned_objects = array();
783 if ($type ==
'member') {
785 } elseif ($type ==
'customer' || $type ==
'supplier') {
787 } elseif ($type ==
'product') {
789 } elseif ($type ==
'contact') {
791 } elseif ($type ==
'project') {
794 if (is_object($objects_api)) {
795 foreach ($objects as $obj) {
796 $cleaned_objects[] = $objects_api->_cleanObjectDatas($obj);
800 return $cleaned_objects;
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.
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.