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 if ($field ==
'array_options' && is_array($value)) {
252 foreach ($value as $index => $val) {
253 $this->category->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->category);
258 $this->category->$field = $this->
_checkValForAPI($field, $value, $this->category);
261 if ($this->category->update(DolibarrApiAccess::$user) > 0) {
262 return $this->
get($id);
264 throw new RestException(500, $this->category->error);
274 public function delete($id)
276 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'supprimer')) {
277 throw new RestException(403);
279 $result = $this->category->fetch($id);
281 throw new RestException(404,
'category not found');
285 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
288 if ($this->category->delete(DolibarrApiAccess::$user) <= 0) {
289 throw new RestException(500,
'Error when delete category : ' . $this->category->error);
295 'message' =>
'Category deleted'
317 public function getListForObject($id, $type, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
319 if (!in_array($type, [
320 Categorie::TYPE_PRODUCT,
321 Categorie::TYPE_CONTACT,
322 Categorie::TYPE_CUSTOMER,
323 Categorie::TYPE_SUPPLIER,
324 Categorie::TYPE_MEMBER,
325 Categorie::TYPE_PROJECT,
326 Categorie::TYPE_KNOWLEDGEMANAGEMENT,
327 Categorie::TYPE_ACTIONCOMM
329 throw new RestException(403);
332 if ($type == Categorie::TYPE_PRODUCT && !DolibarrApiAccess::$user->hasRight(
'produit',
'lire') && !DolibarrApiAccess::$user->hasRight(
'service',
'lire')) {
333 throw new RestException(403);
334 } elseif ($type == Categorie::TYPE_CONTACT && !DolibarrApiAccess::$user->hasRight(
'contact',
'lire')) {
335 throw new RestException(403);
336 } elseif ($type == Categorie::TYPE_CUSTOMER && !DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
337 throw new RestException(403);
338 } elseif ($type == Categorie::TYPE_SUPPLIER && !DolibarrApiAccess::$user->hasRight(
'fournisseur',
'lire')) {
339 throw new RestException(403);
340 } elseif ($type == Categorie::TYPE_MEMBER && !DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
341 throw new RestException(403);
342 } elseif ($type == Categorie::TYPE_PROJECT && !DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
343 throw new RestException(403);
344 } elseif ($type == Categorie::TYPE_KNOWLEDGEMANAGEMENT && !DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
345 throw new RestException(403);
346 } elseif ($type == Categorie::TYPE_ACTIONCOMM && !DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
347 throw new RestException(403);
350 $categories = $this->category->getListForItem($id, $type, $sortfield, $sortorder, $limit, $page);
352 if (!is_array($categories)) {
353 throw new RestException(600,
'Error when fetching object categories', array_merge(array($this->category->error), $this->category->errors));
372 if (empty($type) || empty($object_id)) {
373 throw new RestException(403);
376 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
377 throw new RestException(403);
380 $result = $this->category->fetch($id);
382 throw new RestException(404,
'category not found');
385 if ($type === Categorie::TYPE_PRODUCT) {
386 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer') && !DolibarrApiAccess::$user->hasRight(
'service',
'creer')) {
387 throw new RestException(403);
390 } elseif ($type === Categorie::TYPE_CUSTOMER) {
391 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
392 throw new RestException(403);
395 } elseif ($type === Categorie::TYPE_SUPPLIER) {
396 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
397 throw new RestException(403);
400 } elseif ($type === Categorie::TYPE_CONTACT) {
401 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
402 throw new RestException(403);
405 } elseif ($type === Categorie::TYPE_MEMBER) {
406 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
407 throw new RestException(403);
410 } elseif ($type === Categorie::TYPE_ACTIONCOMM) {
411 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
412 throw new RestException(403);
416 throw new RestException(400,
"this type is not recognized yet.");
419 $result =
$object->fetch($object_id);
421 $result = $this->category->add_type(
$object, $type);
423 if ($this->category->error !=
'DB_ERROR_RECORD_ALREADY_EXISTS') {
424 throw new RestException(500,
'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
428 throw new RestException(500,
'Error when fetching object', array_merge(array(
$object->error),
$object->errors));
434 'message' =>
'Objects successfully linked to the category'
453 if (empty($type) || empty($object_ref)) {
454 throw new RestException(403);
457 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
458 throw new RestException(403);
461 $result = $this->category->fetch($id);
463 throw new RestException(404,
'category not found');
466 if ($type === Categorie::TYPE_PRODUCT) {
467 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer') && !DolibarrApiAccess::$user->hasRight(
'service',
'creer')) {
468 throw new RestException(403);
471 } elseif ($type === Categorie::TYPE_CUSTOMER) {
472 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
473 throw new RestException(403);
476 } elseif ($type === Categorie::TYPE_SUPPLIER) {
477 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
478 throw new RestException(403);
481 } elseif ($type === Categorie::TYPE_CONTACT) {
482 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
483 throw new RestException(403);
486 } elseif ($type === Categorie::TYPE_MEMBER) {
487 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
488 throw new RestException(403);
491 } elseif ($type === Categorie::TYPE_ACTIONCOMM) {
492 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
493 throw new RestException(403);
497 throw new RestException(400,
"this type is not recognized yet.");
500 $result =
$object->fetch(0, $object_ref);
502 $result = $this->category->add_type(
$object, $type);
504 if ($this->category->error !=
'DB_ERROR_RECORD_ALREADY_EXISTS') {
505 throw new RestException(500,
'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
509 throw new RestException(500,
'Error when fetching object', array_merge(array(
$object->error),
$object->errors));
515 'message' =>
'Objects successfully linked to the category'
534 if (empty($type) || empty($object_id)) {
535 throw new RestException(403);
538 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
539 throw new RestException(403);
542 $result = $this->category->fetch($id);
544 throw new RestException(404,
'category not found');
547 if ($type === Categorie::TYPE_PRODUCT) {
548 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer') && !DolibarrApiAccess::$user->hasRight(
'service',
'creer')) {
549 throw new RestException(403);
552 } elseif ($type === Categorie::TYPE_CUSTOMER) {
553 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
554 throw new RestException(403);
557 } elseif ($type === Categorie::TYPE_SUPPLIER) {
558 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
559 throw new RestException(403);
562 } elseif ($type === Categorie::TYPE_CONTACT) {
563 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
564 throw new RestException(403);
567 } elseif ($type === Categorie::TYPE_MEMBER) {
568 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
569 throw new RestException(403);
572 } elseif ($type === Categorie::TYPE_ACTIONCOMM) {
573 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
574 throw new RestException(403);
578 throw new RestException(400,
"this type is not recognized yet.");
581 $result =
$object->fetch((
int) $object_id);
583 $result = $this->category->del_type(
$object, $type);
585 throw new RestException(500,
'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
588 throw new RestException(500,
'Error when fetching object', array_merge(array(
$object->error),
$object->errors));
594 'message' =>
'Objects successfully unlinked from the category'
613 if (empty($type) || empty($object_ref)) {
614 throw new RestException(403);
617 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
618 throw new RestException(403);
621 $result = $this->category->fetch($id);
623 throw new RestException(404,
'category not found');
626 if ($type === Categorie::TYPE_PRODUCT) {
627 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer') && !DolibarrApiAccess::$user->hasRight(
'service',
'creer')) {
628 throw new RestException(403);
631 } elseif ($type === Categorie::TYPE_CUSTOMER) {
632 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
633 throw new RestException(403);
636 } elseif ($type === Categorie::TYPE_SUPPLIER) {
637 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
638 throw new RestException(403);
641 } elseif ($type === Categorie::TYPE_CONTACT) {
642 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
643 throw new RestException(403);
646 } elseif ($type === Categorie::TYPE_MEMBER) {
647 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
648 throw new RestException(403);
651 } elseif ($type === Categorie::TYPE_ACTIONCOMM) {
652 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
653 throw new RestException(403);
657 throw new RestException(400,
"this type is not recognized yet.");
660 $result =
$object->fetch(0, (
string) $object_ref);
662 $result = $this->category->del_type(
$object, $type);
664 throw new RestException(500,
'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
667 throw new RestException(500,
'Error when fetching object', array_merge(array(
$object->error),
$object->errors));
673 'message' =>
'Objects successfully unlinked from the category'
701 unset(
$object->total_localtax1);
702 unset(
$object->total_localtax2);
710 unset(
$object->shipping_method_id);
711 unset(
$object->fk_delivery_address);
712 unset(
$object->cond_reglement);
713 unset(
$object->cond_reglement_id);
714 unset(
$object->mode_reglement_id);
715 unset(
$object->barcode_type_coder);
716 unset(
$object->barcode_type_label);
717 unset(
$object->barcode_type_code);
747 foreach (Categories::$FIELDS as $field) {
748 if (!isset($data[$field])) {
749 throw new RestException(400,
"$field field missing");
751 $category[$field] = $data[$field];
769 dol_syslog(
"getObjects($id, $type, $onlyids)", LOG_DEBUG);
771 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
772 throw new RestException(403);
776 throw new RestException(500,
'The "type" parameter is required.');
779 $result = $this->category->fetch($id);
781 throw new RestException(404,
'category not found');
785 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
788 $result = $this->category->getObjectsInCateg($type, $onlyids);
791 throw new RestException(503,
'Error when retrieving objects list : '.$this->category->error);
795 $cleaned_objects = array();
797 if ($type ==
'member') {
799 } elseif ($type ==
'customer' || $type ==
'supplier') {
801 } elseif ($type ==
'product') {
803 } elseif ($type ==
'contact') {
805 } elseif ($type ==
'project') {
809 if (is_object($objects_api)) {
810 foreach ($objects as $obj) {
811 $cleaned_objects[] = $objects_api->_cleanObjectDatas($obj);
815 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.