diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index 81e2659d918..d5b5cb77f7a 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -59,15 +59,11 @@ class Products extends DolibarrApi } /** - * Get properties of a product object (from its ID, Ref, Ref_ext or Barcode) + * Get properties of a product object by id * * Return an array with product information. - * TODO implement getting a product by ref or by $ref_ext * * @param int $id ID of product - * @param string $ref Ref of element - * @param string $ref_ext Ref ext of element - * @param string $barcode Barcode of element * @param int $includestockdata Load also information about stock (slower) * @return array|mixed Data without useless information * @@ -75,32 +71,72 @@ class Products extends DolibarrApi * @throws 403 * @throws 404 */ - public function get($id, $ref = '', $ref_ext = '', $barcode = '', $includestockdata = 0) + public function get($id, $includestockdata = 0) { - if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) { - throw new RestException(400, 'bad value for parameter id, ref, ref_ext or barcode'); - } + return $this->_fetch($id, '', '', '', $includestockdata); + } - $id = (empty($id)?0:$id); + /** + * Get properties of a product object by ref + * + * Return an array with product information. + * + * @param string $ref Ref of element + * @param int $includestockdata Load also information about stock (slower) + * + * @return array|mixed Data without useless information + * + * @url GET byRef/{ref} + * + * @throws 401 + * @throws 403 + * @throws 404 + */ + public function getByRef($ref, $includestockdata = 0) + { + return $this->_fetch('', $ref, '', '', $includestockdata); + } - if(! DolibarrApiAccess::$user->rights->produit->lire) { - throw new RestException(403); - } + /** + * Get properties of a product object by ref_ext + * + * Return an array with product information. + * + * @param string $ref_ext Ref_ext of element + * @param int $includestockdata Load also information about stock (slower) + * + * @return array|mixed Data without useless information + * + * @url GET byRefExt/{ref_ext} + * + * @throws 401 + * @throws 403 + * @throws 404 + */ + public function getByRefExt($ref_ext, $includestockdata = 0) + { + return $this->_fetch('', '', $ref_ext, '', $includestockdata); + } - $result = $this->product->fetch($id, $ref, $ref_ext, $barcode); - if(! $result ) { - throw new RestException(404, 'Product not found'); - } - - if(! DolibarrApi::_checkAccessToResource('product', $this->product->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } - - if ($includestockdata) { - $this->product->load_stock(); - } - - return $this->_cleanObjectDatas($this->product); + /** + * Get properties of a product object by barcode + * + * Return an array with product information. + * + * @param string $barcode Barcode of element + * @param int $includestockdata Load also information about stock (slower) + * + * @return array|mixed Data without useless information + * + * @url GET byBarcode/{barcode} + * + * @throws 401 + * @throws 403 + * @throws 404 + */ + public function getByBarcode($barcode, $includestockdata = 0) + { + return $this->_fetch('', '', '', $barcode, $includestockdata); } /** @@ -246,7 +282,7 @@ class Products extends DolibarrApi } $this->product->$field = $value; } - + $updatetype = false; if ($this->product->type != $oldproduct->type && ($this->product->isProduct() || $this->product->isService())) { $updatetype = true; @@ -696,4 +732,48 @@ class Products extends DolibarrApi } return $product; } + + /** + * Get properties of a product object + * + * Return an array with product information. + * + * @param int $id ID of product + * @param string $ref Ref of element + * @param string $ref_ext Ref ext of element + * @param string $barcode Barcode of element + * @param int $includestockdata Load also information about stock (slower) + * @return array|mixed Data without useless information + * + * @throws 401 + * @throws 403 + * @throws 404 + */ + private function _fetch($id, $ref = '', $ref_ext = '', $barcode = '', $includestockdata = 0) + { + if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) { + throw new RestException(400, 'bad value for parameter id, ref, ref_ext or barcode'); + } + + $id = (empty($id)?0:$id); + + if(! DolibarrApiAccess::$user->rights->produit->lire) { + throw new RestException(403); + } + + $result = $this->product->fetch($id, $ref, $ref_ext, $barcode); + if(! $result ) { + throw new RestException(404, 'Product not found'); + } + + if(! DolibarrApi::_checkAccessToResource('product', $this->product->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + if ($includestockdata) { + $this->product->load_stock(); + } + + return $this->_cleanObjectDatas($this->product); + } }