From 8d6e6e115dd7f7df3738dd858766fff8cc806548 Mon Sep 17 00:00:00 2001 From: Xebax Date: Mon, 15 Apr 2019 12:39:02 +0200 Subject: [PATCH] NEW REST API: get the list of objects in a category. --- .../categories/class/api_categories.class.php | 68 ++++++++++++++++++- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/htdocs/categories/class/api_categories.class.php b/htdocs/categories/class/api_categories.class.php index c975236f930..9b59e1f001b 100644 --- a/htdocs/categories/class/api_categories.class.php +++ b/htdocs/categories/class/api_categories.class.php @@ -15,10 +15,15 @@ * along with this program. If not, see . */ - use Luracast\Restler\RestException; +use Luracast\Restler\RestException; - require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; - require_once DOL_DOCUMENT_ROOT.'/societe/class/client.class.php'; +require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; +require_once DOL_DOCUMENT_ROOT.'/societe/class/client.class.php'; + +require_once DOL_DOCUMENT_ROOT.'/adherents/class/api_members.class.php'; +require_once DOL_DOCUMENT_ROOT.'/product/class/api_products.class.php'; +require_once DOL_DOCUMENT_ROOT.'/societe/class/api_contacts.class.php'; +require_once DOL_DOCUMENT_ROOT.'/societe/class/api_thirdparties.class.php'; /** * API class for categories @@ -335,4 +340,61 @@ class Categories extends DolibarrApi } return $category; } + + /** + * Get the list of objects in a category. + * + * @param int $id ID of category + * @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact') + * @param int $onlyids Return only ids of objects (consume less memory) + * + * @return mixed + * + * @url GET {id}/objects + */ + public function getObjects($id, $type, $onlyids = 0) + { + dol_syslog("getObjects($id, $type, $onlyids)", LOG_DEBUG); + + if (! DolibarrApiAccess::$user->rights->categorie->lire) { + throw new RestException(401); + } + + if (empty($type)) + { + throw new RestException(500, 'The "type" parameter is required.'); + } + + $result = $this->category->fetch($id); + if (! $result) { + throw new RestException(404, 'category not found'); + } + + if (! DolibarrApi::_checkAccessToResource('category', $this->category->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $result = $this->category->getObjectsInCateg($type, $onlyids); + + if ($result < 0) { + throw new RestException(503, 'Error when retrieving objects list : '.$this->category->error); + } + + $objects = $result; + $cleaned_objects = array(); + if ($type == 'member') { + $objects_api = new Members(); + } elseif ($type == 'customer' || $type == 'supplier') { + $objects_api = new Thirdparties(); + } elseif ($type == 'product') { + $objects_api = new Products(); + } elseif ($type == 'contact') { + $objects_api = new Contacts(); + } + foreach ($objects as $obj) { + $cleaned_objects[] = $objects_api->_cleanObjectDatas($obj); + } + + return $cleaned_objects; + } }