From 98f757a87a4e9e37ab1efd6b0c23a827f25b8f65 Mon Sep 17 00:00:00 2001 From: nka11 <nicolas@karageuzian.com> Date: Wed, 27 Apr 2016 08:59:12 +0200 Subject: [PATCH] New API methods add user to group get product list by category id --- htdocs/commande/class/api_commande.class.php | 8 +- htdocs/product/class/api_product.class.php | 86 ++++++++++++++++++++ htdocs/societe/class/api_contact.class.php | 2 +- htdocs/user/class/api_user.class.php | 31 ++++++- 4 files changed, 120 insertions(+), 7 deletions(-) diff --git a/htdocs/commande/class/api_commande.class.php b/htdocs/commande/class/api_commande.class.php index 04d78f30a1d..d6a2a3d0f82 100644 --- a/htdocs/commande/class/api_commande.class.php +++ b/htdocs/commande/class/api_commande.class.php @@ -207,9 +207,9 @@ class CommandeApi extends DolibarrApi */ function post($request_data = NULL) { - if(! DolibarrApiAccess::$user->rights->commande->creer) { - throw new RestException(401); - } + if(! DolibarrApiAccess::$user->rights->commande->creer) { + throw new RestException(401, "Insuffisant rights"); + } // Check mandatory fields $result = $this->_validate($request_data); @@ -224,7 +224,7 @@ class CommandeApi extends DolibarrApi $this->commande->lines = $lines; } if(! $this->commande->create(DolibarrApiAccess::$user) ) { - throw new RestException(401); + throw new RestException(500, "Error while creating order"); } return $this->commande->id; diff --git a/htdocs/product/class/api_product.class.php b/htdocs/product/class/api_product.class.php index 1ce63411cd3..1e485fd6733 100644 --- a/htdocs/product/class/api_product.class.php +++ b/htdocs/product/class/api_product.class.php @@ -18,6 +18,7 @@ use Luracast\Restler\RestException; 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 @@ -165,6 +166,91 @@ class ProductApi extends DolibarrApi } 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 diff --git a/htdocs/societe/class/api_contact.class.php b/htdocs/societe/class/api_contact.class.php index ba7c0d4a211..5144c000b4d 100644 --- a/htdocs/societe/class/api_contact.class.php +++ b/htdocs/societe/class/api_contact.class.php @@ -251,7 +251,7 @@ class ContactApi extends DolibarrApi * * @param int $id Contact ID * @return integer - * + * * @url DELETE contact/{id} */ function delete($id) { diff --git a/htdocs/user/class/api_user.class.php b/htdocs/user/class/api_user.class.php index d09785d3ccb..af0db5bfb2d 100644 --- a/htdocs/user/class/api_user.class.php +++ b/htdocs/user/class/api_user.class.php @@ -159,8 +159,35 @@ class UserApi extends DolibarrApi if ($this->useraccount->update($id, DolibarrApiAccess::$user, 1, '', '', 'update')) 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 -- GitLab