REST API: improve the API to get/add categories for an item.
Move the GET /xxx/{id}/categories requests to the corresponding classes
(Contacts, Products, Thirdparties).
Move the addCustomerCategory() method to the Thirdparties class. Use the
POST verb for the request instead of GET because the request modifies
the state of the server.
This commit is contained in:
parent
8d86b8b37d
commit
3b662d6ed8
@ -159,10 +159,10 @@ class Categories extends DolibarrApi
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO move to /members/, /products/ and /contacts/
|
||||
* List categories of an entity
|
||||
*
|
||||
* Get a list of categories
|
||||
* Note: This method is not directly exposed in the API, it is used
|
||||
* in the GET /xxx/{id}/categories requests.
|
||||
*
|
||||
* @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact')
|
||||
* @param string $sortfield Sort field
|
||||
@ -172,9 +172,9 @@ class Categories extends DolibarrApi
|
||||
* @param int $item Id of the item to get categories for
|
||||
* @return array Array of category objects
|
||||
*
|
||||
* @url GET /product/{item}/categories
|
||||
* @access private
|
||||
*/
|
||||
function getListForItem($type='product', $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $item = 0) {
|
||||
function getListForItem($type, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $item = 0) {
|
||||
global $db, $conf;
|
||||
|
||||
$obj_ret = array();
|
||||
@ -191,6 +191,9 @@ class Categories extends DolibarrApi
|
||||
$sub_type="societe";
|
||||
$subcol_name="fk_soc";
|
||||
}
|
||||
if ($type=="contact") {
|
||||
$subcol_name="fk_socpeople";
|
||||
}
|
||||
$sql = "SELECT s.rowid";
|
||||
$sql.= " FROM ".MAIN_DB_PREFIX."categorie as s";
|
||||
$sql.= " , ".MAIN_DB_PREFIX."categorie_".$sub_type." as sub ";
|
||||
@ -240,60 +243,7 @@ class Categories extends DolibarrApi
|
||||
}
|
||||
return $obj_ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO move to /thirsparties/ or /customers/
|
||||
* Get categories for a customer
|
||||
*
|
||||
* @param int $cusid Customer id filter
|
||||
* @param string $sortfield Sort field
|
||||
* @param string $sortorder Sort order
|
||||
* @param int $limit Limit for list
|
||||
* @param int $page Page number
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @url GET /customer/{cusid}/categories
|
||||
*/
|
||||
function getListCustomerCategories($cusid, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) {
|
||||
return $this->getListForItem('customer', $sortfield, $sortorder, $limit, $page, $cusid);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO move to /thirsparties/ or /customers/
|
||||
* Add category to customer
|
||||
*
|
||||
* @param int $cusid Id of customer
|
||||
* @param int $catid Id of category
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @url GET /customer/{cusid}/addCategory/{catid}
|
||||
*/
|
||||
function addCustomerCategory($cusid,$catid) {
|
||||
if(! DolibarrApiAccess::$user->rights->societe->creer) {
|
||||
throw new RestException(401);
|
||||
}
|
||||
$customer = new Client($this->db);
|
||||
$customer->fetch($cusid);
|
||||
if( ! $customer ) {
|
||||
throw new RestException(404, 'customer not found');
|
||||
}
|
||||
$result = $this->category->fetch($catid);
|
||||
if( ! $result ) {
|
||||
throw new RestException(404, 'category not found');
|
||||
}
|
||||
|
||||
if( ! DolibarrApi::_checkAccessToResource('societe',$customer->id)) {
|
||||
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
|
||||
}
|
||||
if( ! DolibarrApi::_checkAccessToResource('category',$this->category->id)) {
|
||||
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
|
||||
}
|
||||
$this->category->add_type($customer,'customer');
|
||||
return $customer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create category object
|
||||
*
|
||||
|
||||
@ -250,6 +250,24 @@ class Products extends DolibarrApi
|
||||
return $this->product->delete(DolibarrApiAccess::$user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get categories for a product
|
||||
*
|
||||
* @param int $id ID of product
|
||||
* @param string $sortfield Sort field
|
||||
* @param string $sortorder Sort order
|
||||
* @param int $limit Limit for list
|
||||
* @param int $page Page number
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @url GET {id}/categories
|
||||
*/
|
||||
function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) {
|
||||
$categories = new Categories();
|
||||
return $categories->getListForItem('product', $sortfield, $sortorder, $limit, $page, $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate fields before create or update object
|
||||
*
|
||||
|
||||
@ -300,6 +300,24 @@ class Contacts extends DolibarrApi
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get categories for a contact
|
||||
*
|
||||
* @param int $id ID of contact
|
||||
* @param string $sortfield Sort field
|
||||
* @param string $sortorder Sort order
|
||||
* @param int $limit Limit for list
|
||||
* @param int $page Page number
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @url GET {id}/categories
|
||||
*/
|
||||
function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) {
|
||||
$categories = new Categories();
|
||||
return $categories->getListForItem('contact', $sortfield, $sortorder, $limit, $page, $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate fields before create or update object
|
||||
*
|
||||
|
||||
@ -243,6 +243,64 @@ class Thirdparties extends DolibarrApi
|
||||
return $this->company->delete($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get categories for a thirdparty
|
||||
*
|
||||
* @param int $id ID of thirdparty
|
||||
* @param string $sortfield Sort field
|
||||
* @param string $sortorder Sort order
|
||||
* @param int $limit Limit for list
|
||||
* @param int $page Page number
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @url GET {id}/categories
|
||||
*/
|
||||
function getCategories($id, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) {
|
||||
$categories = new Categories();
|
||||
return $categories->getListForItem('customer', $sortfield, $sortorder, $limit, $page, $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add category to a thirdparty
|
||||
*
|
||||
* @param int $id Id of thirdparty
|
||||
* @param array $request_data Request datas
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @url POST {id}/addCategory
|
||||
*/
|
||||
function addCategory($id, $request_data = NULL) {
|
||||
if (!isset($request_data["category_id"]))
|
||||
throw new RestException(400, "category_id field missing");
|
||||
$category_id = $request_data["category_id"];
|
||||
|
||||
if(! DolibarrApiAccess::$user->rights->societe->creer) {
|
||||
throw new RestException(401);
|
||||
}
|
||||
|
||||
$result = $this->company->fetch($id);
|
||||
if( ! $result ) {
|
||||
throw new RestException(404, 'Thirdparty not found');
|
||||
}
|
||||
$category = new Categorie($this->db);
|
||||
$result = $category->fetch($category_id);
|
||||
if( ! $result ) {
|
||||
throw new RestException(404, 'category not found');
|
||||
}
|
||||
|
||||
if( ! DolibarrApi::_checkAccessToResource('societe',$this->company->id)) {
|
||||
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
|
||||
}
|
||||
if( ! DolibarrApi::_checkAccessToResource('category',$category->id)) {
|
||||
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
|
||||
}
|
||||
|
||||
$category->add_type($this->company,'customer');
|
||||
return $this->company;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate fields before create or update object
|
||||
*
|
||||
|
||||
Loading…
Reference in New Issue
Block a user