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      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);
 
  257      $this->category->$field = $this->
_checkValForAPI($field, $value, $this->category);
 
  260    if ($this->category->update(DolibarrApiAccess::$user) > 0) {
 
  261      return $this->
get($id);
 
  263      throw new RestException(500, $this->category->error);
 
 
  273  public function delete($id)
 
  275    if (!DolibarrApiAccess::$user->rights->categorie->supprimer) {
 
  276      throw new RestException(401);
 
  278    $result = $this->category->fetch($id);
 
  280      throw new RestException(404, 
'category not found');
 
  284      throw new RestException(401, 
'Access not allowed for login '.DolibarrApiAccess::$user->login);
 
  287    if ($this->category->delete(DolibarrApiAccess::$user) <= 0) {
 
  288      throw new RestException(500, 
'Error when delete category : ' . $this->category->error);
 
  294        'message' => 
'Category deleted' 
 
  316  public function getListForObject($id, $type, $sortfield = 
"s.rowid", $sortorder = 
'ASC', $limit = 0, $page = 0)
 
  318    if (!in_array($type, [
 
  319      Categorie::TYPE_PRODUCT,
 
  320      Categorie::TYPE_CONTACT,
 
  321      Categorie::TYPE_CUSTOMER,
 
  322      Categorie::TYPE_SUPPLIER,
 
  323      Categorie::TYPE_MEMBER,
 
  324      Categorie::TYPE_PROJECT,
 
  325      Categorie::TYPE_KNOWLEDGEMANAGEMENT
 
  327      throw new RestException(401);
 
  330    if ($type == Categorie::TYPE_PRODUCT && !(DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) {
 
  331      throw new RestException(401);
 
  332    } elseif ($type == Categorie::TYPE_CONTACT && !DolibarrApiAccess::$user->rights->contact->lire) {
 
  333      throw new RestException(401);
 
  334    } elseif ($type == Categorie::TYPE_CUSTOMER && !DolibarrApiAccess::$user->hasRight(
'societe', 
'lire')) {
 
  335      throw new RestException(401);
 
  336    } elseif ($type == Categorie::TYPE_SUPPLIER && !DolibarrApiAccess::$user->rights->fournisseur->lire) {
 
  337      throw new RestException(401);
 
  338    } elseif ($type == Categorie::TYPE_MEMBER && !DolibarrApiAccess::$user->rights->adherent->lire) {
 
  339      throw new RestException(401);
 
  340    } elseif ($type == Categorie::TYPE_PROJECT && !DolibarrApiAccess::$user->rights->projet->lire) {
 
  341      throw new RestException(401);
 
  342    } elseif ($type == Categorie::TYPE_KNOWLEDGEMANAGEMENT && !DolibarrApiAccess::$user->hasRight(
'knowledgemanagement', 
'knowledgerecord', 
'read')) {
 
  343      throw new RestException(401);
 
  346    $categories = $this->category->getListForItem($id, $type, $sortfield, $sortorder, $limit, $page);
 
  348    if (!is_array($categories)) {
 
  349      throw new RestException(600, 
'Error when fetching object categories', array_merge(array($this->category->error), $this->category->errors));
 
 
  368    if (empty($type) || empty($object_id)) {
 
  369      throw new RestException(401);
 
  372    if (!DolibarrApiAccess::$user->rights->categorie->lire) {
 
  373      throw new RestException(401);
 
  376    $result = $this->category->fetch($id);
 
  378      throw new RestException(404, 
'category not found');
 
  381    if ($type === Categorie::TYPE_PRODUCT) {
 
  382      if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
 
  383        throw new RestException(401);
 
  385      $object = 
new Product($this->db);
 
  386    } elseif ($type === Categorie::TYPE_CUSTOMER) {
 
  387      if (!DolibarrApiAccess::$user->rights->societe->creer) {
 
  388        throw new RestException(401);
 
  390      $object = 
new Societe($this->db);
 
  391    } elseif ($type === Categorie::TYPE_SUPPLIER) {
 
  392      if (!DolibarrApiAccess::$user->rights->societe->creer) {
 
  393        throw new RestException(401);
 
  395      $object = 
new Societe($this->db);
 
  396    } elseif ($type === Categorie::TYPE_CONTACT) {
 
  397      if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
 
  398        throw new RestException(401);
 
  400      $object = 
new Contact($this->db);
 
  401    } elseif ($type === Categorie::TYPE_MEMBER) {
 
  402      if (!DolibarrApiAccess::$user->hasRight(
'adherent', 
'creer')) {
 
  403        throw new RestException(401);
 
  407      throw new RestException(401, 
"this type is not recognized yet.");
 
  410    if (!empty($object)) {
 
  411      $result = $object->fetch($object_id);
 
  413        $result = $this->category->add_type($object, $type);
 
  415          if ($this->category->error != 
'DB_ERROR_RECORD_ALREADY_EXISTS') {
 
  416            throw new RestException(500, 
'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
 
  420        throw new RestException(500, 
'Error when fetching object', array_merge(array($object->error), $object->errors));
 
  426          'message' => 
'Objects succefully linked to the category' 
  431    throw new RestException(401);
 
 
  448    if (empty($type) || empty($object_ref)) {
 
  449      throw new RestException(401);
 
  452    if (!DolibarrApiAccess::$user->rights->categorie->lire) {
 
  453      throw new RestException(401);
 
  456    $result = $this->category->fetch($id);
 
  458      throw new RestException(404, 
'category not found');
 
  461    if ($type === Categorie::TYPE_PRODUCT) {
 
  462      if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
 
  463        throw new RestException(401);
 
  465      $object = 
new Product($this->db);
 
  466    } elseif ($type === Categorie::TYPE_CUSTOMER) {
 
  467      if (!DolibarrApiAccess::$user->rights->societe->creer) {
 
  468        throw new RestException(401);
 
  470      $object = 
new Societe($this->db);
 
  471    } elseif ($type === Categorie::TYPE_SUPPLIER) {
 
  472      if (!DolibarrApiAccess::$user->rights->societe->creer) {
 
  473        throw new RestException(401);
 
  475      $object = 
new Societe($this->db);
 
  476    } elseif ($type === Categorie::TYPE_CONTACT) {
 
  477      if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
 
  478        throw new RestException(401);
 
  480      $object = 
new Contact($this->db);
 
  481    } elseif ($type === Categorie::TYPE_MEMBER) {
 
  482      if (!DolibarrApiAccess::$user->hasRight(
'adherent', 
'creer')) {
 
  483        throw new RestException(401);
 
  487      throw new RestException(401, 
"this type is not recognized yet.");
 
  490    if (!empty($object)) {
 
  491      $result = $object->fetch(
'', $object_ref);
 
  493        $result = $this->category->add_type($object, $type);
 
  495          if ($this->category->error != 
'DB_ERROR_RECORD_ALREADY_EXISTS') {
 
  496            throw new RestException(500, 
'Error when linking object', array_merge(array($this->category->error), $this->category->errors));
 
  500        throw new RestException(500, 
'Error when fetching object', array_merge(array($object->error), $object->errors));
 
  506          'message' => 
'Objects succefully linked to the category' 
  511    throw new RestException(401);
 
 
  528    if (empty($type) || empty($object_id)) {
 
  529      throw new RestException(401);
 
  532    if (!DolibarrApiAccess::$user->rights->categorie->lire) {
 
  533      throw new RestException(401);
 
  536    $result = $this->category->fetch($id);
 
  538      throw new RestException(404, 
'category not found');
 
  541    if ($type === Categorie::TYPE_PRODUCT) {
 
  542      if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
 
  543        throw new RestException(401);
 
  545      $object = 
new Product($this->db);
 
  546    } elseif ($type === Categorie::TYPE_CUSTOMER) {
 
  547      if (!DolibarrApiAccess::$user->rights->societe->creer) {
 
  548        throw new RestException(401);
 
  550      $object = 
new Societe($this->db);
 
  551    } elseif ($type === Categorie::TYPE_SUPPLIER) {
 
  552      if (!DolibarrApiAccess::$user->rights->societe->creer) {
 
  553        throw new RestException(401);
 
  555      $object = 
new Societe($this->db);
 
  556    } elseif ($type === Categorie::TYPE_CONTACT) {
 
  557      if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
 
  558        throw new RestException(401);
 
  560      $object = 
new Contact($this->db);
 
  561    } elseif ($type === Categorie::TYPE_MEMBER) {
 
  562      if (!DolibarrApiAccess::$user->hasRight(
'adherent', 
'creer')) {
 
  563        throw new RestException(401);
 
  567      throw new RestException(401, 
"this type is not recognized yet.");
 
  570    if (!empty($object)) {
 
  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 succefully unlinked from the category' 
  589    throw new RestException(401);
 
 
  606    if (empty($type) || empty($object_ref)) {
 
  607      throw new RestException(401);
 
  610    if (!DolibarrApiAccess::$user->rights->categorie->lire) {
 
  611      throw new RestException(401);
 
  614    $result = $this->category->fetch($id);
 
  616      throw new RestException(404, 
'category not found');
 
  619    if ($type === Categorie::TYPE_PRODUCT) {
 
  620      if (!(DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) {
 
  621        throw new RestException(401);
 
  623      $object = 
new Product($this->db);
 
  624    } elseif ($type === Categorie::TYPE_CUSTOMER) {
 
  625      if (!DolibarrApiAccess::$user->rights->societe->creer) {
 
  626        throw new RestException(401);
 
  628      $object = 
new Societe($this->db);
 
  629    } elseif ($type === Categorie::TYPE_SUPPLIER) {
 
  630      if (!DolibarrApiAccess::$user->rights->societe->creer) {
 
  631        throw new RestException(401);
 
  633      $object = 
new Societe($this->db);
 
  634    } elseif ($type === Categorie::TYPE_CONTACT) {
 
  635      if (!DolibarrApiAccess::$user->rights->societe->contact->creer) {
 
  636        throw new RestException(401);
 
  638      $object = 
new Contact($this->db);
 
  639    } elseif ($type === Categorie::TYPE_MEMBER) {
 
  640      if (!DolibarrApiAccess::$user->hasRight(
'adherent', 
'creer')) {
 
  641        throw new RestException(401);
 
  645      throw new RestException(401, 
"this type is not recognized yet.");
 
  648    if (!empty($object)) {
 
  649      $result = $object->fetch(
'', (
string) $object_ref);
 
  651        $result = $this->category->del_type($object, $type);
 
  653          throw new RestException(500, 
'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors));
 
  656        throw new RestException(500, 
'Error when fetching object', array_merge(array($object->error), $object->errors));
 
  662          'message' => 
'Objects succefully unlinked from the category' 
  667    throw new RestException(401);
 
 
  681    $object = parent::_cleanObjectDatas($object);
 
  684    unset($object->MAP_CAT_FK);
 
  685    unset($object->MAP_CAT_TABLE);
 
  686    unset($object->MAP_OBJ_CLASS);
 
  687    unset($object->MAP_OBJ_TABLE);
 
  688    unset($object->country);
 
  689    unset($object->country_id);
 
  690    unset($object->country_code);
 
  691    unset($object->total_ht);
 
  692    unset($object->total_ht);
 
  693    unset($object->total_localtax1);
 
  694    unset($object->total_localtax2);
 
  695    unset($object->total_ttc);
 
  696    unset($object->total_tva);
 
  697    unset($object->lines);
 
  698    unset($object->civility_id);
 
  699    unset($object->name);
 
  700    unset($object->lastname);
 
  701    unset($object->firstname);
 
  702    unset($object->shipping_method_id);
 
  703    unset($object->fk_delivery_address);
 
  704    unset($object->cond_reglement);
 
  705    unset($object->cond_reglement_id);
 
  706    unset($object->mode_reglement_id);
 
  707    unset($object->barcode_type_coder);
 
  708    unset($object->barcode_type_label);
 
  709    unset($object->barcode_type_code);
 
  710    unset($object->barcode_type);
 
  711    unset($object->canvas);
 
  712    unset($object->cats);
 
  713    unset($object->motherof);
 
  714    unset($object->context);
 
  715    unset($object->socid);
 
  716    unset($object->thirdparty);
 
  717    unset($object->contact);
 
  718    unset($object->contact_id);
 
  719    unset($object->user);
 
  720    unset($object->fk_account);
 
  721    unset($object->fk_project);
 
  722    unset($object->note);
 
  723    unset($object->statut);
 
 
  739    foreach (Categories::$FIELDS as $field) {
 
  740      if (!isset($data[$field])) {
 
  741        throw new RestException(400, 
"$field field missing");
 
  743      $category[$field] = $data[$field];
 
 
  761    dol_syslog(
"getObjects($id, $type, $onlyids)", LOG_DEBUG);
 
  763    if (!DolibarrApiAccess::$user->rights->categorie->lire) {
 
  764      throw new RestException(401);
 
  768      throw new RestException(500, 
'The "type" parameter is required.');
 
  771    $result = $this->category->fetch($id);
 
  773      throw new RestException(404, 
'category not found');
 
  777      throw new RestException(401, 
'Access not allowed for login '.DolibarrApiAccess::$user->login);
 
  780    $result = $this->category->getObjectsInCateg($type, $onlyids);
 
  783      throw new RestException(503, 
'Error when retrieving objects list : '.$this->category->error);
 
  787    $cleaned_objects = array();
 
  789    if ($type == 
'member') {
 
  791    } elseif ($type == 
'customer' || $type == 
'supplier') {
 
  793    } elseif ($type == 
'product') {
 
  795    } elseif ($type == 
'contact') {
 
  797    } elseif ($type == 
'project') {
 
  800    if (is_object($objects_api)) {
 
  801      foreach ($objects as $obj) {
 
  802        $cleaned_objects[] = $objects_api->_cleanObjectDatas($obj);
 
  806    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.
 
_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
 
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.