Skip to content
Snippets Groups Projects
Commit 98f757a8 authored by nka11's avatar nka11
Browse files

New API methods

add user to group
get product list by category id
parent 56d25a7c
No related branches found
No related tags found
No related merge requests found
...@@ -207,9 +207,9 @@ class CommandeApi extends DolibarrApi ...@@ -207,9 +207,9 @@ class CommandeApi extends DolibarrApi
*/ */
function post($request_data = NULL) function post($request_data = NULL)
{ {
if(! DolibarrApiAccess::$user->rights->commande->creer) { if(! DolibarrApiAccess::$user->rights->commande->creer) {
throw new RestException(401); throw new RestException(401, "Insuffisant rights");
} }
// Check mandatory fields // Check mandatory fields
$result = $this->_validate($request_data); $result = $this->_validate($request_data);
...@@ -224,7 +224,7 @@ class CommandeApi extends DolibarrApi ...@@ -224,7 +224,7 @@ class CommandeApi extends DolibarrApi
$this->commande->lines = $lines; $this->commande->lines = $lines;
} }
if(! $this->commande->create(DolibarrApiAccess::$user) ) { if(! $this->commande->create(DolibarrApiAccess::$user) ) {
throw new RestException(401); throw new RestException(500, "Error while creating order");
} }
return $this->commande->id; return $this->commande->id;
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
use Luracast\Restler\RestException; use Luracast\Restler\RestException;
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
/** /**
* API class for product object * API class for product object
...@@ -165,6 +166,91 @@ class ProductApi extends DolibarrApi ...@@ -165,6 +166,91 @@ class ProductApi extends DolibarrApi
} }
return $obj_ret; return $obj_ret;
} }
/**
* List products in a category
*
* Get a list of products
*
* @param int $mode Use this param to filter list (0 for all, 1 for only product, 2 for only service)
* @param int $category Use this param to filter list by category
* @param mixed $to_sell Filter products to sell (1) or not to sell (0)
* @param mixed $to_buy Filter products to nuy (1) or not to buy (0)
* @param string $sortfield Sort field
* @param string $sortorder Sort order
* @param int $limit Limit for list
* @param int $page Page number
*
* @return array Array of product objects
*
* @url GET /product/list/category/{category}
*/
function getByCategory($mode=0, $category=0, $to_sell='', $to_buy='', $sortfield = "p.ref", $sortorder = 'ASC', $limit = 0, $page = 0) {
global $db, $conf;
$obj_ret = array();
$socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : '';
$sql = "SELECT rowid, ref, ref_ext";
$sql.= " FROM ".MAIN_DB_PREFIX."product as p, ";
$sql.= MAIN_DB_PREFIX."categorie_product as c";
$sql.= ' WHERE p.entity IN ('.getEntity('product', 1).')';
// Select products of given category
$sql.= " AND c.fk_categorie = ".$db->escape($category);
$sql.= " AND c.fk_product = p.rowid ";
// Show products
if ($mode == 1) $sql.= " AND p.fk_product_type = 0";
// Show services
if ($mode == 2) $sql.= " AND p.fk_product_type = 1";
// Show product on sell
if ($to_sell) $sql.= " AND p.to_sell = ".$db->escape($to_sell);
// Show product on buy
if ($to_buy) $sql.= " AND p.to_nuy = ".$db->escape($to_nuy);
$nbtotalofrecords = 0;
if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
{
$result = $db->query($sql);
$nbtotalofrecords = $db->num_rows($result);
}
$sql.= $db->order($sortfield, $sortorder);
if ($limit) {
if ($page < 0)
{
$page = 0;
}
$offset = $limit * $page;
$sql.= $db->plimit($limit + 1, $offset);
}
$result = $db->query($sql);
if ($result)
{
$num = $db->num_rows($result);
while ($i < $num)
{
$obj = $db->fetch_object($result);
$product_static = new Product($db);
if($product_static->fetch($obj->rowid)) {
$obj_ret[] = parent::_cleanObjectDatas($product_static);
}
$i++;
}
}
else {
throw new RestException(503, 'Error when retrieve product list');
}
if( ! count($obj_ret)) {
throw new RestException(404, 'No product found');
}
return $obj_ret;
}
/** /**
* Create product object * Create product object
......
...@@ -251,7 +251,7 @@ class ContactApi extends DolibarrApi ...@@ -251,7 +251,7 @@ class ContactApi extends DolibarrApi
* *
* @param int $id Contact ID * @param int $id Contact ID
* @return integer * @return integer
* *
* @url DELETE contact/{id} * @url DELETE contact/{id}
*/ */
function delete($id) { function delete($id) {
......
...@@ -159,8 +159,35 @@ class UserApi extends DolibarrApi ...@@ -159,8 +159,35 @@ class UserApi extends DolibarrApi
if ($this->useraccount->update($id, DolibarrApiAccess::$user, 1, '', '', 'update')) if ($this->useraccount->update($id, DolibarrApiAccess::$user, 1, '', '', 'update'))
return $this->get($id); return $this->get($id);
return false; return false;
} }
/**
* add user to group
*
* @param int $id User ID
* @param int $group Group ID
* @return int
*
* @url GET user/{id}/setGroup/{group}
*/
function setGroup($id,$group) {
//if (!DolibarrApiAccess::$user->rights->user->user->supprimer) {
//throw new RestException(401);
//}
$result = $this->useraccount->fetch($id);
if (!$result)
{
throw new RestException(404, 'User not found');
}
if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user'))
{
throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
}
return $this->useraccount->SetInGroup($group,1);
}
/** /**
* Delete account * Delete account
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment