18 use Luracast\Restler\RestException;
20 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
21 require_once DOL_DOCUMENT_ROOT.
'/societe/class/client.class.php';
24 require_once DOL_DOCUMENT_ROOT.
'/adherents/class/api_members.class.php';
25 require_once DOL_DOCUMENT_ROOT.
'/product/class/api_products.class.php';
26 require_once DOL_DOCUMENT_ROOT.
'/societe/class/api_contacts.class.php';
27 require_once DOL_DOCUMENT_ROOT.
'/societe/class/api_thirdparties.class.php';
28 require_once DOL_DOCUMENT_ROOT.
'/projet/class/api_projects.class.php';
41 static $FIELDS = array(
46 static $TYPES = array(
60 13 =>
'knowledgemanagement'
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";
145 $sql .=
' WHERE t.entity IN ('.getEntity(
'category').
')';
147 $sql .=
' AND t.type='.array_search($type, Categories::$TYPES);
153 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
155 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
156 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
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);
177 if ($category_static->fetch($obj->rowid)) {
183 throw new RestException(503,
'Error when retrieve category list : '.$this->
db->lasterror());
185 if (!count($obj_ret)) {
186 throw new RestException(404,
'No category found');
197 public function post($request_data =
null)
199 if (!DolibarrApiAccess::$user->rights->categorie->creer) {
200 throw new RestException(401);
204 $result = $this->
_validate($request_data);
206 foreach ($request_data as $field => $value) {
207 $this->category->$field = $value;
209 if ($this->category->create(DolibarrApiAccess::$user) < 0) {
210 throw new RestException(500,
'Error when creating category', array_merge(array($this->category->error), $this->category->errors));
212 return $this->category->id;
222 public function put($id, $request_data =
null)
224 if (!DolibarrApiAccess::$user->rights->categorie->creer) {
225 throw new RestException(401);
228 $result = $this->category->fetch($id);
230 throw new RestException(404,
'category not found');
234 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
237 foreach ($request_data as $field => $value) {
238 if ($field ==
'id') {
241 $this->category->$field = $value;
244 if ($this->category->update(DolibarrApiAccess::$user) > 0) {
245 return $this->
get($id);
247 throw new RestException(500, $this->category->error);
257 public function delete($id)
259 if (!DolibarrApiAccess::$user->rights->categorie->supprimer) {
260 throw new RestException(401);
262 $result = $this->category->fetch($id);
264 throw new RestException(404,
'category not found');
268 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
271 if (!$this->category->delete(DolibarrApiAccess::$user)) {
272 throw new RestException(401,
'error when delete category');
278 'message' =>
'Category deleted'
300 public function getListForObject($id, $type, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
302 if (!in_array($type, [
303 Categorie::TYPE_PRODUCT,
304 Categorie::TYPE_CONTACT,
305 Categorie::TYPE_CUSTOMER,
306 Categorie::TYPE_SUPPLIER,
307 Categorie::TYPE_MEMBER,
308 Categorie::TYPE_PROJECT,
309 Categorie::TYPE_KNOWLEDGEMANAGEMENT
311 throw new RestException(401);
314 if ($type == Categorie::TYPE_PRODUCT && !(DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) {
315 throw new RestException(401);
316 } elseif ($type == Categorie::TYPE_CONTACT && !DolibarrApiAccess::$user->rights->contact->lire) {
317 throw new RestException(401);
318 } elseif ($type == Categorie::TYPE_CUSTOMER && !DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
319 throw new RestException(401);
320 } elseif ($type == Categorie::TYPE_SUPPLIER && !DolibarrApiAccess::$user->rights->fournisseur->lire) {
321 throw new RestException(401);
322 } elseif ($type == Categorie::TYPE_MEMBER && !DolibarrApiAccess::$user->rights->adherent->lire) {
323 throw new RestException(401);
324 } elseif ($type == Categorie::TYPE_PROJECT && !DolibarrApiAccess::$user->rights->projet->lire) {
325 throw new RestException(401);
326 } elseif ($type == Categorie::TYPE_KNOWLEDGEMANAGEMENT && !DolibarrApiAccess::$user->rights->knowledgemanagement->knowledgerecord->read) {
327 throw new RestException(401);
330 $categories = $this->category->getListForItem($id, $type, $sortfield, $sortorder, $limit, $page);
332 if (!is_array($categories)) {
333 if ($categories == 0) {
334 throw new RestException(404,
'No category found for this object');
336 throw new RestException(600,
'Error when fetching object categories', array_merge(array($this->category->error), $this->category->errors));
355 if (empty($type) || empty($object_id)) {
356 throw new RestException(401);
359 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
360 throw new RestException(401);
363 $result = $this->category->fetch($id);
365 throw new RestException(404,
'category not found');
368 if ($type === Categorie::TYPE_PRODUCT) {
369 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
370 throw new RestException(401);
373 } elseif ($type === Categorie::TYPE_CUSTOMER) {
374 if (!DolibarrApiAccess::$user->rights->societe->creer) {
375 throw new RestException(401);
378 } elseif ($type === Categorie::TYPE_SUPPLIER) {
379 if (!DolibarrApiAccess::$user->rights->societe->creer) {
380 throw new RestException(401);
383 } elseif ($type === Categorie::TYPE_CONTACT) {
384 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
385 throw new RestException(401);
388 } elseif ($type === Categorie::TYPE_MEMBER) {
389 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
390 throw new RestException(401);
394 throw new RestException(401,
"this type is not recognized yet.");
397 if (!empty($object)) {
398 $result = $object->fetch($object_id);
400 $result = $this->category->add_type($object, $type);
402 if ($this->category->error !=
'DB_ERROR_RECORD_ALREADY_EXISTS') {
403 throw new RestException(500,
'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
407 throw new RestException(500,
'Error when fetching object', array_merge(array($object->error), $object->errors));
413 'message' =>
'Objects succefully linked to the category'
418 throw new RestException(401);
435 if (empty($type) || empty($object_ref)) {
436 throw new RestException(401);
439 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
440 throw new RestException(401);
443 $result = $this->category->fetch($id);
445 throw new RestException(404,
'category not found');
448 if ($type === Categorie::TYPE_PRODUCT) {
449 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
450 throw new RestException(401);
453 } elseif ($type === Categorie::TYPE_CUSTOMER) {
454 if (!DolibarrApiAccess::$user->rights->societe->creer) {
455 throw new RestException(401);
458 } elseif ($type === Categorie::TYPE_SUPPLIER) {
459 if (!DolibarrApiAccess::$user->rights->societe->creer) {
460 throw new RestException(401);
463 } elseif ($type === Categorie::TYPE_CONTACT) {
464 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
465 throw new RestException(401);
468 } elseif ($type === Categorie::TYPE_MEMBER) {
469 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
470 throw new RestException(401);
474 throw new RestException(401,
"this type is not recognized yet.");
477 if (!empty($object)) {
478 $result = $object->fetch(
'', $object_ref);
480 $result = $this->category->add_type($object, $type);
482 if ($this->category->error !=
'DB_ERROR_RECORD_ALREADY_EXISTS') {
483 throw new RestException(500,
'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
487 throw new RestException(500,
'Error when fetching object', array_merge(array($object->error), $object->errors));
493 'message' =>
'Objects succefully linked to the category'
498 throw new RestException(401);
515 if (empty($type) || empty($object_id)) {
516 throw new RestException(401);
519 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
520 throw new RestException(401);
523 $result = $this->category->fetch($id);
525 throw new RestException(404,
'category not found');
528 if ($type === Categorie::TYPE_PRODUCT) {
529 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
530 throw new RestException(401);
533 } elseif ($type === Categorie::TYPE_CUSTOMER) {
534 if (!DolibarrApiAccess::$user->rights->societe->creer) {
535 throw new RestException(401);
538 } elseif ($type === Categorie::TYPE_SUPPLIER) {
539 if (!DolibarrApiAccess::$user->rights->societe->creer) {
540 throw new RestException(401);
543 } elseif ($type === Categorie::TYPE_CONTACT) {
544 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
545 throw new RestException(401);
548 } elseif ($type === Categorie::TYPE_MEMBER) {
549 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
550 throw new RestException(401);
554 throw new RestException(401,
"this type is not recognized yet.");
557 if (!empty($object)) {
558 $result = $object->fetch((
int) $object_id);
560 $result = $this->category->del_type($object, $type);
562 throw new RestException(500,
'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
565 throw new RestException(500,
'Error when fetching object', array_merge(array($object->error), $object->errors));
571 'message' =>
'Objects succefully unlinked from the category'
576 throw new RestException(401);
593 if (empty($type) || empty($object_ref)) {
594 throw new RestException(401);
597 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
598 throw new RestException(401);
601 $result = $this->category->fetch($id);
603 throw new RestException(404,
'category not found');
606 if ($type === Categorie::TYPE_PRODUCT) {
607 if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
608 throw new RestException(401);
611 } elseif ($type === Categorie::TYPE_CUSTOMER) {
612 if (!DolibarrApiAccess::$user->rights->societe->creer) {
613 throw new RestException(401);
616 } elseif ($type === Categorie::TYPE_SUPPLIER) {
617 if (!DolibarrApiAccess::$user->rights->societe->creer) {
618 throw new RestException(401);
621 } elseif ($type === Categorie::TYPE_CONTACT) {
622 if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
623 throw new RestException(401);
626 } elseif ($type === Categorie::TYPE_MEMBER) {
627 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
628 throw new RestException(401);
632 throw new RestException(401,
"this type is not recognized yet.");
635 if (!empty($object)) {
636 $result = $object->fetch(
'', (
string) $object_ref);
638 $result = $this->category->del_type($object, $type);
640 throw new RestException(500,
'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
643 throw new RestException(500,
'Error when fetching object', array_merge(array($object->error), $object->errors));
649 'message' =>
'Objects succefully unlinked from the category'
654 throw new RestException(401);
668 $object = parent::_cleanObjectDatas($object);
671 unset($object->MAP_CAT_FK);
672 unset($object->MAP_CAT_TABLE);
673 unset($object->MAP_OBJ_CLASS);
674 unset($object->MAP_OBJ_TABLE);
675 unset($object->country);
676 unset($object->country_id);
677 unset($object->country_code);
678 unset($object->total_ht);
679 unset($object->total_ht);
680 unset($object->total_localtax1);
681 unset($object->total_localtax2);
682 unset($object->total_ttc);
683 unset($object->total_tva);
684 unset($object->lines);
685 unset($object->civility_id);
686 unset($object->name);
687 unset($object->lastname);
688 unset($object->firstname);
689 unset($object->shipping_method_id);
690 unset($object->fk_delivery_address);
691 unset($object->cond_reglement);
692 unset($object->cond_reglement_id);
693 unset($object->mode_reglement_id);
694 unset($object->barcode_type_coder);
695 unset($object->barcode_type_label);
696 unset($object->barcode_type_code);
697 unset($object->barcode_type);
698 unset($object->canvas);
699 unset($object->cats);
700 unset($object->motherof);
701 unset($object->context);
702 unset($object->socid);
703 unset($object->thirdparty);
704 unset($object->contact);
705 unset($object->contact_id);
706 unset($object->user);
707 unset($object->fk_account);
708 unset($object->fk_project);
709 unset($object->note);
710 unset($object->statut);
726 foreach (Categories::$FIELDS as $field) {
727 if (!isset($data[$field])) {
728 throw new RestException(400,
"$field field missing");
730 $category[$field] = $data[$field];
748 dol_syslog(
"getObjects($id, $type, $onlyids)", LOG_DEBUG);
750 if (!DolibarrApiAccess::$user->rights->categorie->lire) {
751 throw new RestException(401);
755 throw new RestException(500,
'The "type" parameter is required.');
758 $result = $this->category->fetch($id);
760 throw new RestException(404,
'category not found');
764 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
767 $result = $this->category->getObjectsInCateg($type, $onlyids);
770 throw new RestException(503,
'Error when retrieving objects list : '.$this->category->error);
774 $cleaned_objects = array();
776 if ($type ==
'member') {
778 } elseif ($type ==
'customer' || $type ==
'supplier') {
780 } elseif ($type ==
'product') {
782 } elseif ($type ==
'contact') {
784 } elseif ($type ==
'project') {
787 if (is_object($objects_api)) {
788 foreach ($objects as $obj) {
789 $cleaned_objects[] = $objects_api->_cleanObjectDatas($obj);
793 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.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Class to manage products or services.
Class to manage third parties objects (customers, suppliers, prospects...)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.