19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
22require_once DOL_DOCUMENT_ROOT.
'/societe/class/client.class.php';
23require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
25require_once DOL_DOCUMENT_ROOT.
'/adherents/class/api_members.class.php';
26require_once DOL_DOCUMENT_ROOT.
'/product/class/api_products.class.php';
27require_once DOL_DOCUMENT_ROOT.
'/societe/class/api_contacts.class.php';
28require_once DOL_DOCUMENT_ROOT.
'/societe/class/api_thirdparties.class.php';
29require_once DOL_DOCUMENT_ROOT.
'/projet/class/api_projects.class.php';
42 public static $FIELDS = array(
47 public static $TYPES = array(
61 13 =>
'knowledgemanagement'
77 $this->category =
new Categorie($this->db);
91 public function get(
$id, $include_childs =
false)
93 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
94 throw new RestException(403);
97 $result = $this->category->fetch(
$id);
99 throw new RestException(404,
'category not found');
103 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
106 if ($include_childs) {
107 $cats = $this->category->get_filles();
108 if (!is_array($cats)) {
109 throw new RestException(500,
'Error when fetching child categories', array_merge(array($this->category->error), $this->category->errors));
111 $this->category->childs = array();
112 foreach ($cats as $cat) {
136 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $type =
'', $sqlfilters =
'', $properties =
'')
140 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
141 throw new RestException(403);
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->hasRight(
'categorie',
'creer')) {
198 throw new RestException(403);
204 foreach ($request_data as $field => $value) {
205 if ($field ===
'caller') {
207 $this->category->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
211 $this->category->$field = $this->
_checkValForAPI($field, $value, $this->category);
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->hasRight(
'categorie',
'creer')) {
229 throw new RestException(403);
232 $result = $this->category->fetch(
$id);
234 throw new RestException(404,
'category not found');
238 throw new RestException(403,
'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'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
251 $this->category->$field = $this->
_checkValForAPI($field, $value, $this->category);
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->hasRight(
'categorie',
'supprimer')) {
270 throw new RestException(403);
272 $result = $this->category->fetch(
$id);
274 throw new RestException(404,
'category not found');
278 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
281 if ($this->category->delete(DolibarrApiAccess::$user) <= 0) {
282 throw new RestException(500,
'Error when delete category : ' . $this->category->error);
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,
320 Categorie::TYPE_ACTIONCOMM
322 throw new RestException(403);
325 if ($type == Categorie::TYPE_PRODUCT && !DolibarrApiAccess::$user->hasRight(
'produit',
'lire') && !DolibarrApiAccess::$user->hasRight(
'service',
'lire')) {
326 throw new RestException(403);
327 } elseif ($type == Categorie::TYPE_CONTACT && !DolibarrApiAccess::$user->hasRight(
'contact',
'lire')) {
328 throw new RestException(403);
329 } elseif ($type == Categorie::TYPE_CUSTOMER && !DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
330 throw new RestException(403);
331 } elseif ($type == Categorie::TYPE_SUPPLIER && !DolibarrApiAccess::$user->hasRight(
'fournisseur',
'lire')) {
332 throw new RestException(403);
333 } elseif ($type == Categorie::TYPE_MEMBER && !DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
334 throw new RestException(403);
335 } elseif ($type == Categorie::TYPE_PROJECT && !DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
336 throw new RestException(403);
337 } elseif ($type == Categorie::TYPE_KNOWLEDGEMANAGEMENT && !DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
338 throw new RestException(403);
339 } elseif ($type == Categorie::TYPE_ACTIONCOMM && !DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
340 throw new RestException(403);
343 $categories = $this->category->getListForItem(
$id, $type, $sortfield, $sortorder, $limit, $page);
345 if (!is_array($categories)) {
346 throw new RestException(600,
'Error when fetching object categories', array_merge(array($this->category->error), $this->category->errors));
365 if (empty($type) || empty($object_id)) {
366 throw new RestException(403);
369 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
370 throw new RestException(403);
373 $result = $this->category->fetch(
$id);
375 throw new RestException(404,
'category not found');
378 if ($type === Categorie::TYPE_PRODUCT) {
379 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer') && !DolibarrApiAccess::$user->hasRight(
'service',
'creer')) {
380 throw new RestException(403);
383 } elseif ($type === Categorie::TYPE_CUSTOMER) {
384 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
385 throw new RestException(403);
388 } elseif ($type === Categorie::TYPE_SUPPLIER) {
389 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
390 throw new RestException(403);
393 } elseif ($type === Categorie::TYPE_CONTACT) {
394 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
395 throw new RestException(403);
398 } elseif ($type === Categorie::TYPE_MEMBER) {
399 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
400 throw new RestException(403);
403 } elseif ($type === Categorie::TYPE_ACTIONCOMM) {
404 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
405 throw new RestException(403);
409 throw new RestException(400,
"this type is not recognized yet.");
412 $result =
$object->fetch($object_id);
414 $result = $this->category->add_type(
$object, $type);
416 if ($this->category->error !=
'DB_ERROR_RECORD_ALREADY_EXISTS') {
417 throw new RestException(500,
'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
421 throw new RestException(500,
'Error when fetching object', array_merge(array(
$object->error),
$object->errors));
427 'message' =>
'Objects successfully linked to the category'
446 if (empty($type) || empty($object_ref)) {
447 throw new RestException(403);
450 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
451 throw new RestException(403);
454 $result = $this->category->fetch(
$id);
456 throw new RestException(404,
'category not found');
459 if ($type === Categorie::TYPE_PRODUCT) {
460 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer') && !DolibarrApiAccess::$user->hasRight(
'service',
'creer')) {
461 throw new RestException(403);
464 } elseif ($type === Categorie::TYPE_CUSTOMER) {
465 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
466 throw new RestException(403);
469 } elseif ($type === Categorie::TYPE_SUPPLIER) {
470 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
471 throw new RestException(403);
474 } elseif ($type === Categorie::TYPE_CONTACT) {
475 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
476 throw new RestException(403);
479 } elseif ($type === Categorie::TYPE_MEMBER) {
480 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
481 throw new RestException(403);
484 } elseif ($type === Categorie::TYPE_ACTIONCOMM) {
485 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
486 throw new RestException(403);
490 throw new RestException(400,
"this type is not recognized yet.");
493 $result =
$object->fetch(0, $object_ref);
495 $result = $this->category->add_type(
$object, $type);
497 if ($this->category->error !=
'DB_ERROR_RECORD_ALREADY_EXISTS') {
498 throw new RestException(500,
'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
502 throw new RestException(500,
'Error when fetching object', array_merge(array(
$object->error),
$object->errors));
508 'message' =>
'Objects successfully linked to the category'
527 if (empty($type) || empty($object_id)) {
528 throw new RestException(403);
531 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
532 throw new RestException(403);
535 $result = $this->category->fetch(
$id);
537 throw new RestException(404,
'category not found');
540 if ($type === Categorie::TYPE_PRODUCT) {
541 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer') && !DolibarrApiAccess::$user->hasRight(
'service',
'creer')) {
542 throw new RestException(403);
545 } elseif ($type === Categorie::TYPE_CUSTOMER) {
546 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
547 throw new RestException(403);
550 } elseif ($type === Categorie::TYPE_SUPPLIER) {
551 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
552 throw new RestException(403);
555 } elseif ($type === Categorie::TYPE_CONTACT) {
556 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
557 throw new RestException(403);
560 } elseif ($type === Categorie::TYPE_MEMBER) {
561 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
562 throw new RestException(403);
565 } elseif ($type === Categorie::TYPE_ACTIONCOMM) {
566 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
567 throw new RestException(403);
571 throw new RestException(400,
"this type is not recognized yet.");
574 $result =
$object->fetch((
int) $object_id);
576 $result = $this->category->del_type(
$object, $type);
578 throw new RestException(500,
'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
581 throw new RestException(500,
'Error when fetching object', array_merge(array(
$object->error),
$object->errors));
587 'message' =>
'Objects successfully unlinked from the category'
606 if (empty($type) || empty($object_ref)) {
607 throw new RestException(403);
610 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
611 throw new RestException(403);
614 $result = $this->category->fetch(
$id);
616 throw new RestException(404,
'category not found');
619 if ($type === Categorie::TYPE_PRODUCT) {
620 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer') && !DolibarrApiAccess::$user->hasRight(
'service',
'creer')) {
621 throw new RestException(403);
624 } elseif ($type === Categorie::TYPE_CUSTOMER) {
625 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
626 throw new RestException(403);
629 } elseif ($type === Categorie::TYPE_SUPPLIER) {
630 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
631 throw new RestException(403);
634 } elseif ($type === Categorie::TYPE_CONTACT) {
635 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
636 throw new RestException(403);
639 } elseif ($type === Categorie::TYPE_MEMBER) {
640 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
641 throw new RestException(403);
644 } elseif ($type === Categorie::TYPE_ACTIONCOMM) {
645 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
646 throw new RestException(403);
650 throw new RestException(400,
"this type is not recognized yet.");
653 $result =
$object->fetch(0, (
string) $object_ref);
655 $result = $this->category->del_type(
$object, $type);
657 throw new RestException(500,
'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
660 throw new RestException(500,
'Error when fetching object', array_merge(array(
$object->error),
$object->errors));
666 'message' =>
'Objects successfully unlinked from the category'
694 unset(
$object->total_localtax1);
695 unset(
$object->total_localtax2);
703 unset(
$object->shipping_method_id);
704 unset(
$object->fk_delivery_address);
705 unset(
$object->cond_reglement);
706 unset(
$object->cond_reglement_id);
707 unset(
$object->mode_reglement_id);
708 unset(
$object->barcode_type_coder);
709 unset(
$object->barcode_type_label);
710 unset(
$object->barcode_type_code);
740 foreach (Categories::$FIELDS as $field) {
741 if (!isset($data[$field])) {
742 throw new RestException(400,
"$field field missing");
744 $category[$field] = $data[$field];
762 dol_syslog(
"getObjects($id, $type, $onlyids)", LOG_DEBUG);
764 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
765 throw new RestException(403);
769 throw new RestException(500,
'The "type" parameter is required.');
772 $result = $this->category->fetch(
$id);
774 throw new RestException(404,
'category not found');
778 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
781 $result = $this->category->getObjectsInCateg($type, $onlyids);
784 throw new RestException(503,
'Error when retrieving objects list : '.$this->category->error);
788 $cleaned_objects = array();
790 if ($type ==
'member') {
792 } elseif ($type ==
'customer' || $type ==
'supplier') {
794 } elseif ($type ==
'product') {
796 } elseif ($type ==
'contact') {
798 } elseif ($type ==
'project') {
802 if (is_object($objects_api)) {
803 foreach ($objects as $obj) {
804 $cleaned_objects[] = $objects_api->_cleanObjectDatas($obj);
808 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.