20use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
23require_once DOL_DOCUMENT_ROOT.
'/societe/class/client.class.php';
24require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
26require_once DOL_DOCUMENT_ROOT.
'/adherents/class/api_members.class.php';
27require_once DOL_DOCUMENT_ROOT.
'/product/class/api_products.class.php';
28require_once DOL_DOCUMENT_ROOT.
'/societe/class/api_contacts.class.php';
29require_once DOL_DOCUMENT_ROOT.
'/societe/class/api_thirdparties.class.php';
30require_once DOL_DOCUMENT_ROOT.
'/projet/class/api_projects.class.php';
43 public static $FIELDS = array(
61 $this->category =
new Categorie($this->db);
75 public function get(
$id, $include_childs =
false)
77 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
78 throw new RestException(403);
81 $result = $this->category->fetch(
$id);
83 throw new RestException(404,
'category not found');
87 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
90 if ($include_childs) {
91 $cats = $this->category->get_filles();
92 if (!is_array($cats)) {
93 throw new RestException(500,
'Error when fetching child categories', array_merge(array($this->category->error), $this->category->errors));
95 $this->category->childs = array();
96 foreach ($cats as $cat) {
120 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $type =
'', $sqlfilters =
'', $properties =
'')
124 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
125 throw new RestException(403);
128 $sql =
"SELECT t.rowid";
129 $sql .=
" FROM ".MAIN_DB_PREFIX.
"categorie AS t LEFT JOIN ".MAIN_DB_PREFIX.
"categories_extrafields AS ef ON (ef.fk_object = t.rowid)";
130 $sql .=
' WHERE t.entity IN ('.getEntity(
'category').
')';
132 $category_static =
new Categorie($this->db);
133 if (is_numeric($type)) {
134 $sql .=
' AND t.type = '.((int) $type);
136 $sql .=
' AND t.type = '.((int) (array_key_exists($type, $category_static->MAP_ID) ? $category_static->MAP_ID[$type] : -1));
145 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
149 $sql .= $this->db->order($sortfield, $sortorder);
154 $offset = $limit * $page;
156 $sql .= $this->db->plimit($limit + 1, $offset);
159 $result = $this->db->query($sql);
162 $num = $this->db->num_rows($result);
163 $min = min($num, ($limit <= 0 ? $num : $limit));
165 $obj = $this->db->fetch_object($result);
166 $category_static =
new Categorie($this->db);
167 if ($category_static->fetch($obj->rowid)) {
173 throw new RestException(503,
'Error when retrieve category list : '.$this->db->lasterror());
185 public function post($request_data =
null)
187 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'creer')) {
188 throw new RestException(403);
194 foreach ($request_data as $field => $value) {
195 if ($field ===
'caller') {
197 $this->category->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
201 $this->category->$field = $this->
_checkValForAPI($field, $value, $this->category);
203 if ($this->category->create(DolibarrApiAccess::$user) < 0) {
204 throw new RestException(500,
'Error when creating category', array_merge(array($this->category->error), $this->category->errors));
206 return $this->category->id;
216 public function put(
$id, $request_data =
null)
218 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'creer')) {
219 throw new RestException(403);
222 $result = $this->category->fetch(
$id);
224 throw new RestException(404,
'category not found');
228 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
231 foreach ($request_data as $field => $value) {
232 if ($field ==
'id') {
235 if ($field ===
'caller') {
237 $this->category->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
241 if ($field ==
'array_options' && is_array($value)) {
242 foreach ($value as $index => $val) {
243 $this->category->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->category);
248 $this->category->$field = $this->
_checkValForAPI($field, $value, $this->category);
251 if ($this->category->update(DolibarrApiAccess::$user) > 0) {
252 return $this->
get(
$id);
254 throw new RestException(500, $this->category->error);
264 public function delete(
$id)
266 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'supprimer')) {
267 throw new RestException(403);
269 $result = $this->category->fetch(
$id);
271 throw new RestException(404,
'category not found');
275 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
278 if ($this->category->delete(DolibarrApiAccess::$user) <= 0) {
279 throw new RestException(500,
'Error when delete category : ' . $this->category->error);
285 'message' =>
'Category deleted'
307 public function getListForObject(
$id, $type, $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
309 if (!in_array($type, [
310 Categorie::TYPE_PRODUCT,
311 Categorie::TYPE_CONTACT,
312 Categorie::TYPE_CUSTOMER,
313 Categorie::TYPE_SUPPLIER,
314 Categorie::TYPE_MEMBER,
315 Categorie::TYPE_PROJECT,
316 Categorie::TYPE_KNOWLEDGEMANAGEMENT,
317 Categorie::TYPE_ACTIONCOMM
319 throw new RestException(403);
322 if ($type == Categorie::TYPE_PRODUCT && !DolibarrApiAccess::$user->hasRight(
'produit',
'lire') && !DolibarrApiAccess::$user->hasRight(
'service',
'lire')) {
323 throw new RestException(403);
324 } elseif ($type == Categorie::TYPE_CONTACT && !DolibarrApiAccess::$user->hasRight(
'contact',
'lire')) {
325 throw new RestException(403);
326 } elseif ($type == Categorie::TYPE_CUSTOMER && !DolibarrApiAccess::$user->hasRight(
'societe',
'lire')) {
327 throw new RestException(403);
328 } elseif ($type == Categorie::TYPE_SUPPLIER && !DolibarrApiAccess::$user->hasRight(
'fournisseur',
'lire')) {
329 throw new RestException(403);
330 } elseif ($type == Categorie::TYPE_MEMBER && !DolibarrApiAccess::$user->hasRight(
'adherent',
'lire')) {
331 throw new RestException(403);
332 } elseif ($type == Categorie::TYPE_PROJECT && !DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
333 throw new RestException(403);
334 } elseif ($type == Categorie::TYPE_KNOWLEDGEMANAGEMENT && !DolibarrApiAccess::$user->hasRight(
'knowledgemanagement',
'knowledgerecord',
'read')) {
335 throw new RestException(403);
336 } elseif ($type == Categorie::TYPE_ACTIONCOMM && !DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
337 throw new RestException(403);
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(403);
366 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
367 throw new RestException(403);
370 $result = $this->category->fetch(
$id);
372 throw new RestException(404,
'category not found');
375 if ($type === Categorie::TYPE_PRODUCT) {
376 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer') && !DolibarrApiAccess::$user->hasRight(
'service',
'creer')) {
377 throw new RestException(403);
380 } elseif ($type === Categorie::TYPE_CUSTOMER) {
381 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
382 throw new RestException(403);
385 } elseif ($type === Categorie::TYPE_SUPPLIER) {
386 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
387 throw new RestException(403);
390 } elseif ($type === Categorie::TYPE_CONTACT) {
391 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
392 throw new RestException(403);
395 } elseif ($type === Categorie::TYPE_MEMBER) {
396 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
397 throw new RestException(403);
400 } elseif ($type === Categorie::TYPE_ACTIONCOMM) {
401 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
402 throw new RestException(403);
406 throw new RestException(400,
"this type is not recognized yet.");
409 $result =
$object->fetch($object_id);
411 $result = $this->category->add_type(
$object, $type);
413 if ($this->category->error !=
'DB_ERROR_RECORD_ALREADY_EXISTS') {
414 throw new RestException(500,
'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
418 throw new RestException(500,
'Error when fetching object', array_merge(array(
$object->error),
$object->errors));
424 'message' =>
'Objects successfully linked to the category'
443 if (empty($type) || empty($object_ref)) {
444 throw new RestException(403);
447 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
448 throw new RestException(403);
451 $result = $this->category->fetch(
$id);
453 throw new RestException(404,
'category not found');
456 if ($type === Categorie::TYPE_PRODUCT) {
457 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer') && !DolibarrApiAccess::$user->hasRight(
'service',
'creer')) {
458 throw new RestException(403);
461 } elseif ($type === Categorie::TYPE_CUSTOMER) {
462 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
463 throw new RestException(403);
466 } elseif ($type === Categorie::TYPE_SUPPLIER) {
467 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
468 throw new RestException(403);
471 } elseif ($type === Categorie::TYPE_CONTACT) {
472 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
473 throw new RestException(403);
476 } elseif ($type === Categorie::TYPE_MEMBER) {
477 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
478 throw new RestException(403);
481 } elseif ($type === Categorie::TYPE_ACTIONCOMM) {
482 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
483 throw new RestException(403);
487 throw new RestException(400,
"this type is not recognized yet.");
490 $result =
$object->fetch(0, $object_ref);
492 $result = $this->category->add_type(
$object, $type);
494 if ($this->category->error !=
'DB_ERROR_RECORD_ALREADY_EXISTS') {
495 throw new RestException(500,
'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
499 throw new RestException(500,
'Error when fetching object', array_merge(array(
$object->error),
$object->errors));
505 'message' =>
'Objects successfully linked to the category'
524 if (empty($type) || empty($object_id)) {
525 throw new RestException(403);
528 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
529 throw new RestException(403);
532 $result = $this->category->fetch(
$id);
534 throw new RestException(404,
'category not found');
537 if ($type === Categorie::TYPE_PRODUCT) {
538 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer') && !DolibarrApiAccess::$user->hasRight(
'service',
'creer')) {
539 throw new RestException(403);
542 } elseif ($type === Categorie::TYPE_CUSTOMER) {
543 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
544 throw new RestException(403);
547 } elseif ($type === Categorie::TYPE_SUPPLIER) {
548 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
549 throw new RestException(403);
552 } elseif ($type === Categorie::TYPE_CONTACT) {
553 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
554 throw new RestException(403);
557 } elseif ($type === Categorie::TYPE_MEMBER) {
558 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
559 throw new RestException(403);
562 } elseif ($type === Categorie::TYPE_ACTIONCOMM) {
563 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
564 throw new RestException(403);
568 throw new RestException(400,
"this type is not recognized yet.");
571 $result =
$object->fetch((
int) $object_id);
573 $result = $this->category->del_type(
$object, $type);
575 throw new RestException(500,
'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
578 throw new RestException(500,
'Error when fetching object', array_merge(array(
$object->error),
$object->errors));
584 'message' =>
'Objects successfully unlinked from the category'
603 if (empty($type) || empty($object_ref)) {
604 throw new RestException(403);
607 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
608 throw new RestException(403);
611 $result = $this->category->fetch(
$id);
613 throw new RestException(404,
'category not found');
616 if ($type === Categorie::TYPE_PRODUCT) {
617 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer') && !DolibarrApiAccess::$user->hasRight(
'service',
'creer')) {
618 throw new RestException(403);
621 } elseif ($type === Categorie::TYPE_CUSTOMER) {
622 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
623 throw new RestException(403);
626 } elseif ($type === Categorie::TYPE_SUPPLIER) {
627 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'creer')) {
628 throw new RestException(403);
631 } elseif ($type === Categorie::TYPE_CONTACT) {
632 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'contact',
'creer')) {
633 throw new RestException(403);
636 } elseif ($type === Categorie::TYPE_MEMBER) {
637 if (!DolibarrApiAccess::$user->hasRight(
'adherent',
'creer')) {
638 throw new RestException(403);
641 } elseif ($type === Categorie::TYPE_ACTIONCOMM) {
642 if (!DolibarrApiAccess::$user->hasRight(
'agenda',
'allactions',
'read')) {
643 throw new RestException(403);
647 throw new RestException(400,
"this type is not recognized yet.");
650 $result =
$object->fetch(0, (
string) $object_ref);
652 $result = $this->category->del_type(
$object, $type);
654 throw new RestException(500,
'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
657 throw new RestException(500,
'Error when fetching object', array_merge(array(
$object->error),
$object->errors));
663 'message' =>
'Objects successfully unlinked from the category'
691 unset(
$object->total_localtax1);
692 unset(
$object->total_localtax2);
700 unset(
$object->shipping_method_id);
701 unset(
$object->fk_delivery_address);
702 unset(
$object->cond_reglement);
703 unset(
$object->cond_reglement_id);
704 unset(
$object->mode_reglement_id);
705 unset(
$object->barcode_type_coder);
706 unset(
$object->barcode_type_label);
707 unset(
$object->barcode_type_code);
737 foreach (Categories::$FIELDS as $field) {
738 if (!isset($data[$field])) {
739 throw new RestException(400,
"$field field missing");
741 $category[$field] = $data[$field];
759 dol_syslog(
"getObjects($id, $type, $onlyids)", LOG_DEBUG);
761 if (!DolibarrApiAccess::$user->hasRight(
'categorie',
'lire')) {
762 throw new RestException(403);
766 throw new RestException(500,
'The "type" parameter is required.');
769 $result = $this->category->fetch(
$id);
771 throw new RestException(404,
'category not found');
775 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
778 $result = $this->category->getObjectsInCateg($type, $onlyids);
781 throw new RestException(503,
'Error when retrieving objects list : '.$this->category->error);
785 $cleaned_objects = array();
787 if ($type ==
'member') {
789 } elseif ($type ==
'customer' || $type ==
'supplier') {
791 } elseif ($type ==
'product') {
793 } elseif ($type ==
'contact') {
795 } elseif ($type ==
'project') {
799 if (is_object($objects_api)) {
800 foreach ($objects as $obj) {
801 $cleaned_objects[] = $objects_api->_cleanObjectDatas($obj);
805 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.