From f620b165d3b87d4c7f25c495375cd2e613ce11cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Fri, 15 Nov 2019 22:17:53 +0100 Subject: [PATCH 01/23] API New get / add / remove product childs --- htdocs/product/class/api_products.class.php | 136 ++++++++++++++++++-- 1 file changed, 127 insertions(+), 9 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index f9a4a12b0df..c2f61604cd9 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -66,15 +66,16 @@ class Products extends DolibarrApi * * @param int $id ID of product * @param int $includestockdata Load also information about stock (slower) + * @param bool $includesousproduits Load information about virtual product components * @return array|mixed Data without useless information * * @throws 401 * @throws 403 * @throws 404 */ - public function get($id, $includestockdata = 0) + public function get($id, $includestockdata = 0, $includesousproduits = false) { - return $this->_fetch($id, '', '', '', $includestockdata); + return $this->_fetch($id, '', '', '', $includestockdata, $includesousproduits); } /** @@ -84,6 +85,7 @@ class Products extends DolibarrApi * * @param string $ref Ref of element * @param int $includestockdata Load also information about stock (slower) + * @param bool $includesousproduits Load information about virtual product components * * @return array|mixed Data without useless information * @@ -93,9 +95,9 @@ class Products extends DolibarrApi * @throws 403 * @throws 404 */ - public function getByRef($ref, $includestockdata = 0) + public function getByRef($ref, $includestockdata = 0, $includesousproduits = false) { - return $this->_fetch('', $ref, '', '', $includestockdata); + return $this->_fetch('', $ref, '', '', $includestockdata, $includesousproduits); } /** @@ -105,6 +107,7 @@ class Products extends DolibarrApi * * @param string $ref_ext Ref_ext of element * @param int $includestockdata Load also information about stock (slower) + * @param bool $includesousproduits Load information about virtual product components * * @return array|mixed Data without useless information * @@ -114,9 +117,9 @@ class Products extends DolibarrApi * @throws 403 * @throws 404 */ - public function getByRefExt($ref_ext, $includestockdata = 0) + public function getByRefExt($ref_ext, $includestockdata = 0, $includesousproduits = false) { - return $this->_fetch('', '', $ref_ext, '', $includestockdata); + return $this->_fetch('', '', $ref_ext, '', $includestockdata, $includesousproduits); } /** @@ -126,6 +129,7 @@ class Products extends DolibarrApi * * @param string $barcode Barcode of element * @param int $includestockdata Load also information about stock (slower) + * @param bool $includesousproduits Load information about virtual product components * * @return array|mixed Data without useless information * @@ -135,9 +139,9 @@ class Products extends DolibarrApi * @throws 403 * @throws 404 */ - public function getByBarcode($barcode, $includestockdata = 0) + public function getByBarcode($barcode, $includestockdata = 0, $includesousproduits = false) { - return $this->_fetch('', '', '', $barcode, $includestockdata); + return $this->_fetch('', '', '', $barcode, $includestockdata, $includesousproduits); } /** @@ -369,6 +373,105 @@ class Products extends DolibarrApi return $this->product->delete(DolibarrApiAccess::$user); } + + /** + * Get the list of children of the product. + * + * @param int $id Id of parent product/service + * @return array + * + * @throws RestException + * @throws 401 + * @throws 404 + * + * @url GET {id}/childs + */ + public function getChilds($id) + { + if(! DolibarrApiAccess::$user->rights->produit->lire) { + throw new RestException(401); + } + + if(! DolibarrApi::_checkAccessToResource('product', $id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $childsArbo = $this->product->getChildsArbo($id, 1); + + $keys = ['rowid', 'qty', 'fk_product_type', 'label', 'incdec']; + $childs = []; + foreach ($childsArbo as $values) { + $childs[] = array_combine($keys, $values); + } + + return $childs; + } + + /** + * Add product child. + * + * Link a product/service to a parent product/service + * + * @param int $id Id of parent product/service + * @param int $id_fils Id of child product/service + * @param int $qty Quantity + * @param int $incdec 1=Increase/decrease stock of child when parent stock increase/decrease + * @return int + * + * @throws RestException + * @throws 401 + * @throws 404 + * + * @url POST {id}/childs/add + */ + public function addChild($id, $child_id, $qty, $incdec = 1) + { + if(! DolibarrApiAccess::$user->rights->produit->creer) { + throw new RestException(401); + } + + if(! DolibarrApi::_checkAccessToResource('product', $id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $result = $this->product->add_sousproduit($id, $child_id, $qty, $incdec); + if ($result <= 0) { + throw new RestException(500, "Error adding product child"); + } + return $result; + } + + /** + * Remove product child. + * + * Unlink a product/service from a parent product/service + * + * @param int $id Id of parent product/service + * @param int $child_id Id of child product/service + * @return int + * + * @throws RestException + * @throws 401 + * @throws 404 + * + * @url DELETE {id}/childs/remove + */ + public function delChild($id, $child_id) + { + if(! DolibarrApiAccess::$user->rights->produit->creer) { + throw new RestException(401); + } + + if(! DolibarrApi::_checkAccessToResource('product', $id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $result = $this->product->del_sousproduit($id, $child_id); + if ($result <= 0) { + throw new RestException(500, "Error while removing product child"); + } + return $result; + } /** @@ -753,13 +856,14 @@ class Products extends DolibarrApi * @param string $ref_ext Ref ext of element * @param string $barcode Barcode of element * @param int $includestockdata Load also information about stock (slower) + * @param bool $includesousproduits Load information about virtual product components * @return array|mixed Data without useless information * * @throws 401 * @throws 403 * @throws 404 */ - private function _fetch($id, $ref = '', $ref_ext = '', $barcode = '', $includestockdata = 0) + private function _fetch($id, $ref = '', $ref_ext = '', $barcode = '', $includestockdata = 0, $includesousproduits = false) { if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) { throw new RestException(400, 'bad value for parameter id, ref, ref_ext or barcode'); @@ -783,6 +887,20 @@ class Products extends DolibarrApi if ($includestockdata) { $this->product->load_stock(); } + + + + if ($includesousproduits) { + $childsArbo = $this->product->getChildsArbo($id, 1); + + $keys = ['rowid', 'qty', 'fk_product_type', 'label', 'incdec']; + $childs = []; + foreach ($childsArbo as $values) { + $childs[] = array_combine($keys, $values); + } + + $this->product->sousprods = $childs; + } return $this->_cleanObjectDatas($this->product); } From aa7353d3cf25020b627fc7434a2defe5e9e6a67c Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Fri, 15 Nov 2019 21:18:40 +0000 Subject: [PATCH 02/23] Fixing style errors. --- htdocs/product/class/api_products.class.php | 36 ++++++++++----------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index c2f61604cd9..1ec1479f24c 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -373,7 +373,7 @@ class Products extends DolibarrApi return $this->product->delete(DolibarrApiAccess::$user); } - + /** * Get the list of children of the product. * @@ -391,22 +391,22 @@ class Products extends DolibarrApi if(! DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } - + if(! DolibarrApi::_checkAccessToResource('product', $id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - + $childsArbo = $this->product->getChildsArbo($id, 1); - + $keys = ['rowid', 'qty', 'fk_product_type', 'label', 'incdec']; $childs = []; foreach ($childsArbo as $values) { $childs[] = array_combine($keys, $values); } - + return $childs; } - + /** * Add product child. * @@ -421,26 +421,26 @@ class Products extends DolibarrApi * @throws RestException * @throws 401 * @throws 404 - * + * * @url POST {id}/childs/add */ public function addChild($id, $child_id, $qty, $incdec = 1) - { + { if(! DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } - + if(! DolibarrApi::_checkAccessToResource('product', $id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - + $result = $this->product->add_sousproduit($id, $child_id, $qty, $incdec); if ($result <= 0) { throw new RestException(500, "Error adding product child"); } return $result; } - + /** * Remove product child. * @@ -461,11 +461,11 @@ class Products extends DolibarrApi if(! DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } - + if(! DolibarrApi::_checkAccessToResource('product', $id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - + $result = $this->product->del_sousproduit($id, $child_id); if ($result <= 0) { throw new RestException(500, "Error while removing product child"); @@ -887,18 +887,18 @@ class Products extends DolibarrApi if ($includestockdata) { $this->product->load_stock(); } - - - + + + if ($includesousproduits) { $childsArbo = $this->product->getChildsArbo($id, 1); - + $keys = ['rowid', 'qty', 'fk_product_type', 'label', 'incdec']; $childs = []; foreach ($childsArbo as $values) { $childs[] = array_combine($keys, $values); } - + $this->product->sousprods = $childs; } From 04f58f5d32e7b9b4e82c7db4949478b2a4021731 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Sat, 16 Nov 2019 11:17:56 +0100 Subject: [PATCH 03/23] $includesubproducts and path consistency --- htdocs/product/class/api_products.class.php | 64 ++++++++++----------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index 1ec1479f24c..ccfe8f1acf9 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -64,18 +64,18 @@ class Products extends DolibarrApi * * Return an array with product information. * - * @param int $id ID of product - * @param int $includestockdata Load also information about stock (slower) - * @param bool $includesousproduits Load information about virtual product components + * @param int $id ID of product + * @param int $includestockdata Load also information about stock (slower) + * @param bool $includesubproducts Load information about subproducts * @return array|mixed Data without useless information * * @throws 401 * @throws 403 * @throws 404 */ - public function get($id, $includestockdata = 0, $includesousproduits = false) + public function get($id, $includestockdata = 0, $includesubproducts = false) { - return $this->_fetch($id, '', '', '', $includestockdata, $includesousproduits); + return $this->_fetch($id, '', '', '', $includestockdata, $includesubproducts); } /** @@ -83,21 +83,21 @@ class Products extends DolibarrApi * * Return an array with product information. * - * @param string $ref Ref of element - * @param int $includestockdata Load also information about stock (slower) - * @param bool $includesousproduits Load information about virtual product components + * @param string $ref Ref of element + * @param int $includestockdata Load also information about stock (slower) + * @param bool $includesubproducts Load information about subproducts * * @return array|mixed Data without useless information * - * @url GET byRef/{ref} + * @url GET ref/{ref} * * @throws 401 * @throws 403 * @throws 404 */ - public function getByRef($ref, $includestockdata = 0, $includesousproduits = false) + public function getByRef($ref, $includestockdata = 0, $includesubproducts = false) { - return $this->_fetch('', $ref, '', '', $includestockdata, $includesousproduits); + return $this->_fetch('', $ref, '', '', $includestockdata, $includesubproducts); } /** @@ -105,21 +105,21 @@ class Products extends DolibarrApi * * Return an array with product information. * - * @param string $ref_ext Ref_ext of element - * @param int $includestockdata Load also information about stock (slower) - * @param bool $includesousproduits Load information about virtual product components + * @param string $ref_ext Ref_ext of element + * @param int $includestockdata Load also information about stock (slower) + * @param bool $includesubproducts Load information about subproducts * * @return array|mixed Data without useless information * - * @url GET byRefExt/{ref_ext} + * @url GET ref_ext/{ref_ext} * * @throws 401 * @throws 403 * @throws 404 */ - public function getByRefExt($ref_ext, $includestockdata = 0, $includesousproduits = false) + public function getByRefExt($ref_ext, $includestockdata = 0, $includesubproducts = false) { - return $this->_fetch('', '', $ref_ext, '', $includestockdata, $includesousproduits); + return $this->_fetch('', '', $ref_ext, '', $includestockdata, $includesubproducts); } /** @@ -127,21 +127,21 @@ class Products extends DolibarrApi * * Return an array with product information. * - * @param string $barcode Barcode of element - * @param int $includestockdata Load also information about stock (slower) - * @param bool $includesousproduits Load information about virtual product components + * @param string $barcode Barcode of element + * @param int $includestockdata Load also information about stock (slower) + * @param bool $includesubproducts Load information about subproducts * * @return array|mixed Data without useless information * - * @url GET byBarcode/{barcode} + * @url GET barcode/{barcode} * * @throws 401 * @throws 403 * @throws 404 */ - public function getByBarcode($barcode, $includestockdata = 0, $includesousproduits = false) + public function getByBarcode($barcode, $includestockdata = 0, $includesubproducts = false) { - return $this->_fetch('', '', '', $barcode, $includestockdata, $includesousproduits); + return $this->_fetch('', '', '', $barcode, $includestockdata, $includesubproducts); } /** @@ -851,19 +851,19 @@ class Products extends DolibarrApi * * 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) - * @param bool $includesousproduits Load information about virtual product components - * @return array|mixed Data without useless 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) + * @param bool $includesubproducts Load information about subproducts + * @return array|mixed Data without useless information * * @throws 401 * @throws 403 * @throws 404 */ - private function _fetch($id, $ref = '', $ref_ext = '', $barcode = '', $includestockdata = 0, $includesousproduits = false) + private function _fetch($id, $ref = '', $ref_ext = '', $barcode = '', $includestockdata = 0, $includesubproducts = false) { if (empty($id) && empty($ref) && empty($ref_ext) && empty($barcode)) { throw new RestException(400, 'bad value for parameter id, ref, ref_ext or barcode'); @@ -890,7 +890,7 @@ class Products extends DolibarrApi - if ($includesousproduits) { + if ($includesubproducts) { $childsArbo = $this->product->getChildsArbo($id, 1); $keys = ['rowid', 'qty', 'fk_product_type', 'label', 'incdec']; From 33d4c4b73cd09a064612438f9b3a1c07dad4379c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Sat, 16 Nov 2019 11:23:50 +0100 Subject: [PATCH 04/23] Doc for $child_id --- htdocs/product/class/api_products.class.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index ccfe8f1acf9..06cf17ea66b 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -412,10 +412,10 @@ class Products extends DolibarrApi * * Link a product/service to a parent product/service * - * @param int $id Id of parent product/service - * @param int $id_fils Id of child product/service - * @param int $qty Quantity - * @param int $incdec 1=Increase/decrease stock of child when parent stock increase/decrease + * @param int $id Id of parent product/service + * @param int $child_id Id of child product/service + * @param int $qty Quantity + * @param int $incdec 1=Increase/decrease stock of child when parent stock increase/decrease * @return int * * @throws RestException From 256e256f62c00a8b0cdaff829a045f704833a960 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Sat, 16 Nov 2019 12:30:47 +0100 Subject: [PATCH 05/23] Paths and vars: subproducts instead of childs --- htdocs/product/class/api_products.class.php | 34 ++++++++++----------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index 06cf17ea66b..58d53583645 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -375,7 +375,7 @@ class Products extends DolibarrApi } /** - * Get the list of children of the product. + * Get the list of subproducts of the product. * * @param int $id Id of parent product/service * @return array @@ -384,9 +384,9 @@ class Products extends DolibarrApi * @throws 401 * @throws 404 * - * @url GET {id}/childs + * @url GET {id}/subproducts */ - public function getChilds($id) + public function getSubproducts($id) { if(! DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); @@ -408,23 +408,23 @@ class Products extends DolibarrApi } /** - * Add product child. + * Add subproduct. * * Link a product/service to a parent product/service * - * @param int $id Id of parent product/service - * @param int $child_id Id of child product/service - * @param int $qty Quantity - * @param int $incdec 1=Increase/decrease stock of child when parent stock increase/decrease + * @param int $id Id of parent product/service + * @param int $subproduct_id Id of child product/service + * @param int $qty Quantity + * @param int $incdec 1=Increase/decrease stock of child when parent stock increase/decrease * @return int * * @throws RestException * @throws 401 * @throws 404 * - * @url POST {id}/childs/add + * @url POST {id}/subproducts/add */ - public function addChild($id, $child_id, $qty, $incdec = 1) + public function addSubproducts($id, $subproduct_id, $qty, $incdec = 1) { if(! DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); @@ -434,7 +434,7 @@ class Products extends DolibarrApi throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - $result = $this->product->add_sousproduit($id, $child_id, $qty, $incdec); + $result = $this->product->add_sousproduit($id, $subproduct_id, $qty, $incdec); if ($result <= 0) { throw new RestException(500, "Error adding product child"); } @@ -442,21 +442,21 @@ class Products extends DolibarrApi } /** - * Remove product child. + * Remove subproduct. * * Unlink a product/service from a parent product/service * - * @param int $id Id of parent product/service - * @param int $child_id Id of child product/service + * @param int $id Id of parent product/service + * @param int $subproduct_id Id of child product/service * @return int * * @throws RestException * @throws 401 * @throws 404 * - * @url DELETE {id}/childs/remove + * @url DELETE {id}/subproducts/remove */ - public function delChild($id, $child_id) + public function delSubproducts($id, $subproduct_id) { if(! DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); @@ -466,7 +466,7 @@ class Products extends DolibarrApi throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - $result = $this->product->del_sousproduit($id, $child_id); + $result = $this->product->del_sousproduit($id, $subproduct_id); if ($result <= 0) { throw new RestException(500, "Error while removing product child"); } From 8c057f1975d07aaa1b1c14dd506196bd07932361 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Sun, 17 Nov 2019 01:20:15 +0100 Subject: [PATCH 06/23] API New Product variants --- htdocs/product/class/api_products.class.php | 546 ++++++++++++++++++++ 1 file changed, 546 insertions(+) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index 58d53583645..e3fb4d6079c 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -21,6 +21,10 @@ use Luracast\Restler\RestException; require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php'; require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; +require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttribute.class.php'; +require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttributeValue.class.php'; +require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination.class.php'; +require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination2ValuePair.class.php'; /** * API class for products @@ -798,6 +802,548 @@ class Products extends DolibarrApi return $this->_cleanObjectDatas($this->productsupplier); } + + /** + * Get attributes. + * + * @return array + * + * @throws RestException + * + * @url GET attributes + */ + public function getAttributes() + { + if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + throw new RestException(401); + } + + $prodattr = new ProductAttribute($this->db); + return $prodattr->fetchAll(); + } + + /** + * Get attribute by ID. + * + * @param int $id ID of Attribute + * @return array + * + * @throws RestException + * @throws 401 + * @throws 404 + * + * @url GET attributes/{id} + */ + public function getAttributeById($id) + { + if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + throw new RestException(401); + } + + $prodattr = new ProductAttribute($this->db); + $result = $prodattr->fetch((int) $id); + + if($result < 0) { + throw new RestException(404, "Attribute not found"); + } + + return $prodattr; + } + + /** + * Add attributes. + * + * @param string $ref Reference of Attribute + * @param string $label Label of Attribute + * @return int + * + * @throws RestException + * @throws 401 + * + * @url POST attributes + */ + public function addAttributes($ref, $label) + { + if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + throw new RestException(401); + } + + $prodattr = new ProductAttribute($this->db); + $prodattr->label = $label; + $prodattr->ref = $ref; + + $resid = $prodattr->create(DolibarrApiAccess::$user); + if ($resid <= 0) { + throw new RestException(500, "Error creating new attribute"); + } + return $resid; + } + + /** + * Update attributes by id. + * + * @param int $id ID of Attribute + * @param string $ref Reference of Attribute + * @param string $label Label of Attribute + * @return int + * + * @throws RestException + * @throws 401 + * @throws 404 + * + * @url PUT attributes/{id} + */ + public function putAttributes($id, $ref, $label) + { + if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + throw new RestException(401); + } + + $prodattr = new ProductAttribute($this->db); + + $result = $prodattr->fetch((int) $id); + if ($result == 0) { + throw new RestException(404, 'Attribute not found'); + } elseif ($result < 0) { + throw new RestException(500, "Error fetching attribute"); + } + + + $prodattr->label = $label; + $prodattr->ref = $ref; + + if ($prodattr->update(DolibarrApiAccess::$user) > 0) { + return 1; + } + throw new RestException(500, "Error updating attribute"); + } + + /** + * Delete attributes by id. + * + * @param int $id ID of Attribute + * @return int + * + * @throws RestException + * @throws 401 + * + * @url DELETE attributes/{id} + */ + public function deleteAttributes($id) + { + if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + throw new RestException(401); + } + + $prodattr = new ProductAttribute($this->db); + $prodattr->id = (int) $id; + $result = $prodattr->delete(); + + if ($result > 0) { + return 1; + } + throw new RestException(500, "Error deleting attribute"); + } + + /** + * Delete attributes by ref. + * + * @param string $ref Reference of Attribute + * @return int + * + * @throws RestException + * @throws 401 + * + * @url DELETE attributes/ref/{ref} + */ + public function deleteAttributesByRef($ref) + { + if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + throw new RestException(401); + } + + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute WHERE ref LIKE '". trim($ref) ."'"; + + if ($this->db->query($sql)) { + return 1; + } + throw new RestException(500, "Error deleting attribute"); + } + + /** + * Get all values for an attribute id. + * + * @param int $id ID of an Attribute + * @return array + * + * @throws RestException + * @throws 401 + * + * @url GET attributes/{id}/values + */ + public function getAttributeValues($id) + { + if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + throw new RestException(401); + } + + $objectval = new ProductAttributeValue($this->db); + return $objectval->fetchAllByProductAttribute((int) $id); + } + + /** + * Get all values for an attribute ref. + * + * @param string $ref Ref of an Attribute + * @return array + * + * @throws RestException + * @throws 401 + * + * @url GET attributes/ref/{ref}/values + */ + public function getAttributeValuesByRef($ref) + { + if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + throw new RestException(401); + } + + $return = array(); + + $sql = 'SELECT '; + $sql .= 'v.fk_product_attribute, v.rowid, v.ref, v.value FROM '.MAIN_DB_PREFIX.'product_attribute_value v '; + $sql .= "WHERE v.fk_product_attribute = ( SELECT rowid FROM ".MAIN_DB_PREFIX."product_attribute WHERE ref LIKE '". strtoupper(trim($ref)) ."' LIMIT 1)"; + + $query = $this->db->query($sql); + + while ($result = $this->db->fetch_object($query)) { + $tmp = new ProductAttributeValue($this->db); + $tmp->fk_product_attribute = $result->fk_product_attribute; + $tmp->id = $result->rowid; + $tmp->ref = $result->ref; + $tmp->value = $result->value; + + $return[] = $tmp; + } + + return $return; + } + + /** + * Add attribute value. + * + * @param int $id ID of Attribute + * @param string $ref Reference of Attribute value + * @param string $value Value of Attribute value + * @return int + * + * @throws RestException + * @throws 401 + * + * @url POST attributes/{id}/values + */ + public function addAttributeValue($id, $ref, $value) + { + if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + throw new RestException(401); + } + + if (empty($ref) || empty($value)) { + throw new RestException(401); + } + + $objectval = new ProductAttributeValue($this->db); + $objectval->fk_product_attribute = $id; + $objectval->ref = $ref; + $objectval->value = $value; + + if ($objectval->create(DolibarrApiAccess::$user) > 0) { + return $objectval->id; + } + throw new RestException(500, "Error creating new attribute value"); + } + + /** + * Delete attribute value by id. + * + * @param int $id ID of Attribute value + * @return int + * + * @throws RestException + * @throws 401 + * + * @url DELETE attributes/values/{id} + */ + public function deleteAttributeValueById($id) + { + if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + throw new RestException(401); + } + + $objectval = new ProductAttributeValue($this->db); + $objectval->id = (int) $id; + + if ($objectval->delete() > 0) { + return 1; + } + throw new RestException(500, "Error deleting attribute value"); + } + + /** + * Delete attribute value by ref. + * + * @param string $ref Ref of Attribute value + * @return int + * + * @throws RestException + * @throws 401 + * + * @url DELETE attributes/values/ref/{ref} + */ + public function deleteAttributeValueByRef($ref) + { + if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + throw new RestException(401); + } + + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE ref LIKE '". trim($ref) ."'"; + + if ($this->db->query($sql)) { + return 1; + } + + throw new RestException(500, "Error deleting attribute value"); + } + + /** + * Get product variants. + * + * @param int $id ID of Product + * @return array + * + * @throws RestException + * @throws 401 + * + * @url GET {id}/variants + */ + public function getVariants($id) + { + if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + throw new RestException(401); + } + + $prodcomb = new ProductCombination($this->db); + return $prodcomb->fetchAllByFkProductParent((int) $id); + } + + /** + * Get product variants by Product ref. + * + * @param string $ref Ref of Product + * @return array + * + * @throws RestException + * @throws 401 + * + * @url GET ref/{ref}/variants + */ + public function getVariantsByProdRef($ref) + { + if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + throw new RestException(401); + } + + $result = $this->product->fetch('', $ref); + if(! $result ) { + throw new RestException(404, 'Product not found'); + } + + $prodcomb = new ProductCombination($this->db); + return $prodcomb->fetchAllByFkProductParent((int) $this->product->id); + } + + /** + * Add variant. + * + * "features" is a list of attributes pairs id_attribute=>id_value. Example: array(id_color=>id_Blue, id_size=>id_small, id_option=>id_val_a, ...) + * + * @param int $id ID of Product + * @param float $weight_impact Weight impact of variant + * @param float $price_impact Price impact of variant + * @param bool $price_impact_is_percent Price impact in percent (true or false) + * @param array $features List of attributes pairs id_attribute->id_value. Example: array(id_color=>id_Blue, id_size=>id_small, id_option=>id_val_a, ...) + * @return int + * + * @throws RestException + * @throws 401 + * @throws 404 + * + * @url POST {id}/variants + */ + public function addVariant($id, $weight_impact, $price_impact, $price_impact_is_percent, $features) + { + if(! (DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) { + throw new RestException(401); + } + + if (empty($id) || empty($weight_impact) || empty($price_impact) || empty($features) || !is_array($features)) { + throw new RestException(401); + } + + $prodattr = new ProductAttribute($this->db); + $prodattr_val = new ProductAttributeValue($this->db); + foreach ($features as $id_attr => $id_value) { + if ($prodattr->fetch((int) $id_attr) < 0) { + throw new RestException(401); + } + if ($prodattr_val->fetch((int) $id_value) < 0) { + throw new RestException(401); + } + } + + $result = $this->product->fetch((int) $id); + if(! $result ) { + throw new RestException(404, 'Product not found'); + } + + $prodcomb = new ProductCombination($this->db); + if (! $prodcomb->fetchByProductCombination2ValuePairs($id, $features)) + { + $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact); + if ($result > 0) + { + return $result; + } else { + throw new RestException(500, "Error creating new product variant"); + } + } else { + return $prodcomb->id; + } + } + + /** + * Add variant by product ref. + * + * "features" is a list of attributes pairs id_attribute=>id_value. Example: array(id_color=>id_Blue, id_size=>id_small, id_option=>id_val_a, ...) + * + * @param string $ref Ref of Product + * @param float $weight_impact Weight impact of variant + * @param float $price_impact Price impact of variant + * @param bool $price_impact_is_percent Price impact in percent (true or false) + * @param array $features List of attributes pairs id_attribute->id_value. Example: array(id_color=>id_Blue, id_size=>id_small, id_option=>id_val_a, ...) + * @return int + * + * @throws RestException + * @throws 401 + * @throws 404 + * + * @url POST ref/{ref}/variants + */ + public function addVariantByProductRef($ref, $weight_impact, $price_impact, $price_impact_is_percent, $features) + { + if(! (DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) { + throw new RestException(401); + } + + if (empty($ref) || empty($weight_impact) || empty($price_impact) || empty($features) || !is_array($features)) { + throw new RestException(401); + } + + $prodattr = new ProductAttribute($this->db); + $prodattr_val = new ProductAttributeValue($this->db); + foreach ($features as $id_attr => $id_value) { + if ($prodattr->fetch((int) $id_attr) < 0) { + throw new RestException(404); + } + if ($prodattr_val->fetch((int) $id_value) < 0) { + throw new RestException(404); + } + } + + $result = $this->product->fetch('', trim($ref)); + if(! $result ) { + throw new RestException(404, 'Product not found'); + } + + $prodcomb = new ProductCombination($this->db); + if (! $prodcomb->fetchByProductCombination2ValuePairs($this->product->id, $features)) + { + $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact); + if ($result > 0) + { + return $result; + } else { + throw new RestException(500, "Error creating new product variant"); + } + } else { + return $prodcomb->id; + } + } + + /** + * Put product variants. + * + * @param int $id ID of Variant + * @param array $request_data Datas + * @return int + * + * @throws RestException + * @throws 401 + * + * @url PUT variants/{id} + */ + public function putVariant($id, $request_data = null) + { + if(! (DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) { + throw new RestException(401); + } + + $prodcomb = new ProductCombination($this->db); + $prodcomb->fetch((int) $id); + + $old_prodcomb = dol_clone($prodcomb, 0); + + foreach($request_data as $field => $value) { + if ($field == 'rowid') { continue; + } + $prodcomb->$field = $value; + } + + $result = $prodcomb->update(DolibarrApiAccess::$user); + if ($result > 0) + { + return 1; + } + throw new RestException(500, "Error editing variant"); + } + + /** + * Delete product variants. + * + * @param int $id ID of Variant + * @return int + * + * @throws RestException + * @throws 401 + * + * @url DELETE variants/{id} + */ + public function deleteVariant($id) + { + if(! (DolibarrApiAccess::$user->rights->produit->supprimer || DolibarrApiAccess::$user->rights->service->supprimer)) { + throw new RestException(401); + } + + $prodcomb = new ProductCombination($this->db); + $prodcomb->id = (int) $id; + $result = $prodcomb->delete(DolibarrApiAccess::$user); + return $result; + if ($result > 0) + { + return 1; + } + throw new RestException(500, "Error deleting variant"); + } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore /** From a17e183da906fddfc0f1a663f8365bcf2482e892 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Sun, 17 Nov 2019 00:18:43 +0000 Subject: [PATCH 07/23] Fixing style errors. --- htdocs/product/class/api_products.class.php | 132 ++++++++++---------- 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index e3fb4d6079c..fd564b17278 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -802,7 +802,7 @@ class Products extends DolibarrApi return $this->_cleanObjectDatas($this->productsupplier); } - + /** * Get attributes. * @@ -813,15 +813,15 @@ class Products extends DolibarrApi * @url GET attributes */ public function getAttributes() - { + { if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { throw new RestException(401); } - + $prodattr = new ProductAttribute($this->db); return $prodattr->fetchAll(); } - + /** * Get attribute by ID. * @@ -839,17 +839,17 @@ class Products extends DolibarrApi if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { throw new RestException(401); } - + $prodattr = new ProductAttribute($this->db); $result = $prodattr->fetch((int) $id); - + if($result < 0) { throw new RestException(404, "Attribute not found"); } - + return $prodattr; } - + /** * Add attributes. * @@ -867,18 +867,18 @@ class Products extends DolibarrApi if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { throw new RestException(401); } - + $prodattr = new ProductAttribute($this->db); $prodattr->label = $label; $prodattr->ref = $ref; - + $resid = $prodattr->create(DolibarrApiAccess::$user); if ($resid <= 0) { throw new RestException(500, "Error creating new attribute"); } return $resid; } - + /** * Update attributes by id. * @@ -898,26 +898,26 @@ class Products extends DolibarrApi if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { throw new RestException(401); } - + $prodattr = new ProductAttribute($this->db); - + $result = $prodattr->fetch((int) $id); if ($result == 0) { throw new RestException(404, 'Attribute not found'); } elseif ($result < 0) { throw new RestException(500, "Error fetching attribute"); } - - + + $prodattr->label = $label; $prodattr->ref = $ref; - + if ($prodattr->update(DolibarrApiAccess::$user) > 0) { return 1; } throw new RestException(500, "Error updating attribute"); } - + /** * Delete attributes by id. * @@ -934,17 +934,17 @@ class Products extends DolibarrApi if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { throw new RestException(401); } - + $prodattr = new ProductAttribute($this->db); $prodattr->id = (int) $id; $result = $prodattr->delete(); - + if ($result > 0) { return 1; } throw new RestException(500, "Error deleting attribute"); } - + /** * Delete attributes by ref. * @@ -961,15 +961,15 @@ class Products extends DolibarrApi if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { throw new RestException(401); } - + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute WHERE ref LIKE '". trim($ref) ."'"; - + if ($this->db->query($sql)) { return 1; } throw new RestException(500, "Error deleting attribute"); } - + /** * Get all values for an attribute id. * @@ -986,11 +986,11 @@ class Products extends DolibarrApi if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { throw new RestException(401); } - + $objectval = new ProductAttributeValue($this->db); return $objectval->fetchAllByProductAttribute((int) $id); } - + /** * Get all values for an attribute ref. * @@ -1007,28 +1007,28 @@ class Products extends DolibarrApi if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { throw new RestException(401); } - + $return = array(); - + $sql = 'SELECT '; $sql .= 'v.fk_product_attribute, v.rowid, v.ref, v.value FROM '.MAIN_DB_PREFIX.'product_attribute_value v '; $sql .= "WHERE v.fk_product_attribute = ( SELECT rowid FROM ".MAIN_DB_PREFIX."product_attribute WHERE ref LIKE '". strtoupper(trim($ref)) ."' LIMIT 1)"; $query = $this->db->query($sql); - + while ($result = $this->db->fetch_object($query)) { $tmp = new ProductAttributeValue($this->db); $tmp->fk_product_attribute = $result->fk_product_attribute; $tmp->id = $result->rowid; $tmp->ref = $result->ref; $tmp->value = $result->value; - + $return[] = $tmp; } - + return $return; } - + /** * Add attribute value. * @@ -1047,22 +1047,22 @@ class Products extends DolibarrApi if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { throw new RestException(401); } - + if (empty($ref) || empty($value)) { throw new RestException(401); } - + $objectval = new ProductAttributeValue($this->db); $objectval->fk_product_attribute = $id; $objectval->ref = $ref; $objectval->value = $value; - + if ($objectval->create(DolibarrApiAccess::$user) > 0) { return $objectval->id; } throw new RestException(500, "Error creating new attribute value"); } - + /** * Delete attribute value by id. * @@ -1079,16 +1079,16 @@ class Products extends DolibarrApi if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { throw new RestException(401); } - + $objectval = new ProductAttributeValue($this->db); $objectval->id = (int) $id; - + if ($objectval->delete() > 0) { return 1; } throw new RestException(500, "Error deleting attribute value"); } - + /** * Delete attribute value by ref. * @@ -1105,16 +1105,16 @@ class Products extends DolibarrApi if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { throw new RestException(401); } - + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE ref LIKE '". trim($ref) ."'"; - + if ($this->db->query($sql)) { return 1; } - + throw new RestException(500, "Error deleting attribute value"); } - + /** * Get product variants. * @@ -1131,11 +1131,11 @@ class Products extends DolibarrApi if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { throw new RestException(401); } - + $prodcomb = new ProductCombination($this->db); return $prodcomb->fetchAllByFkProductParent((int) $id); } - + /** * Get product variants by Product ref. * @@ -1152,19 +1152,19 @@ class Products extends DolibarrApi if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { throw new RestException(401); } - + $result = $this->product->fetch('', $ref); if(! $result ) { throw new RestException(404, 'Product not found'); } - + $prodcomb = new ProductCombination($this->db); return $prodcomb->fetchAllByFkProductParent((int) $this->product->id); } - + /** * Add variant. - * + * * "features" is a list of attributes pairs id_attribute=>id_value. Example: array(id_color=>id_Blue, id_size=>id_small, id_option=>id_val_a, ...) * * @param int $id ID of Product @@ -1185,11 +1185,11 @@ class Products extends DolibarrApi if(! (DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) { throw new RestException(401); } - + if (empty($id) || empty($weight_impact) || empty($price_impact) || empty($features) || !is_array($features)) { throw new RestException(401); } - + $prodattr = new ProductAttribute($this->db); $prodattr_val = new ProductAttributeValue($this->db); foreach ($features as $id_attr => $id_value) { @@ -1200,12 +1200,12 @@ class Products extends DolibarrApi throw new RestException(401); } } - + $result = $this->product->fetch((int) $id); if(! $result ) { throw new RestException(404, 'Product not found'); } - + $prodcomb = new ProductCombination($this->db); if (! $prodcomb->fetchByProductCombination2ValuePairs($id, $features)) { @@ -1220,10 +1220,10 @@ class Products extends DolibarrApi return $prodcomb->id; } } - + /** * Add variant by product ref. - * + * * "features" is a list of attributes pairs id_attribute=>id_value. Example: array(id_color=>id_Blue, id_size=>id_small, id_option=>id_val_a, ...) * * @param string $ref Ref of Product @@ -1244,11 +1244,11 @@ class Products extends DolibarrApi if(! (DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) { throw new RestException(401); } - + if (empty($ref) || empty($weight_impact) || empty($price_impact) || empty($features) || !is_array($features)) { throw new RestException(401); } - + $prodattr = new ProductAttribute($this->db); $prodattr_val = new ProductAttributeValue($this->db); foreach ($features as $id_attr => $id_value) { @@ -1259,16 +1259,16 @@ class Products extends DolibarrApi throw new RestException(404); } } - + $result = $this->product->fetch('', trim($ref)); if(! $result ) { throw new RestException(404, 'Product not found'); } - + $prodcomb = new ProductCombination($this->db); if (! $prodcomb->fetchByProductCombination2ValuePairs($this->product->id, $features)) { - $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact); + $result = $prodcomb->createProductCombination(DolibarrApiAccess::$user, $this->product, $features, array(), $price_impact_is_percent, $price_impact, $weight_impact); if ($result > 0) { return $result; @@ -1279,7 +1279,7 @@ class Products extends DolibarrApi return $prodcomb->id; } } - + /** * Put product variants. * @@ -1297,18 +1297,18 @@ class Products extends DolibarrApi if(! (DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) { throw new RestException(401); } - + $prodcomb = new ProductCombination($this->db); $prodcomb->fetch((int) $id); - + $old_prodcomb = dol_clone($prodcomb, 0); - + foreach($request_data as $field => $value) { if ($field == 'rowid') { continue; } $prodcomb->$field = $value; } - + $result = $prodcomb->update(DolibarrApiAccess::$user); if ($result > 0) { @@ -1316,7 +1316,7 @@ class Products extends DolibarrApi } throw new RestException(500, "Error editing variant"); } - + /** * Delete product variants. * @@ -1333,7 +1333,7 @@ class Products extends DolibarrApi if(! (DolibarrApiAccess::$user->rights->produit->supprimer || DolibarrApiAccess::$user->rights->service->supprimer)) { throw new RestException(401); } - + $prodcomb = new ProductCombination($this->db); $prodcomb->id = (int) $id; $result = $prodcomb->delete(DolibarrApiAccess::$user); From 40e4af234de3fc0568bd9c9abd6b615f1b9b1d4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Sun, 17 Nov 2019 01:24:07 +0100 Subject: [PATCH 08/23] Inject $user instead of global var --- htdocs/variants/class/ProductCombination.class.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/htdocs/variants/class/ProductCombination.class.php b/htdocs/variants/class/ProductCombination.class.php index 4dfd40a8f53..1c09aae62df 100644 --- a/htdocs/variants/class/ProductCombination.class.php +++ b/htdocs/variants/class/ProductCombination.class.php @@ -495,6 +495,7 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1"; * [...] * ) * + * @param User $user Object user * @param Product $product Parent product * @param array $combinations Attribute and value combinations. * @param array $variations Price and weight variations @@ -503,9 +504,9 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1"; * @param bool|float $forced_weightvar If the weight variation is forced * @return int <0 KO, >0 OK */ - public function createProductCombination(Product $product, array $combinations, array $variations, $price_var_percent = false, $forced_pricevar = false, $forced_weightvar = false) + public function createProductCombination(User $user, Product $product, array $combinations, array $variations, $price_var_percent = false, $forced_pricevar = false, $forced_weightvar = false) { - global $db, $user, $conf; + global $db, $conf; require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttribute.class.php'; require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductAttributeValue.class.php'; @@ -660,11 +661,12 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1"; /** * Copies all product combinations from the origin product to the destination product * + * @param User $user Object user * @param int $origProductId Origin product id * @param Product $destProduct Destination product * @return int >0 OK <0 KO */ - public function copyAll($origProductId, Product $destProduct) + public function copyAll(User $user, $origProductId, Product $destProduct) { require_once DOL_DOCUMENT_ROOT.'/variants/class/ProductCombination2ValuePair.class.php'; @@ -686,6 +688,7 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1"; } if ($this->createProductCombination( + $user, $destProduct, $variations, array(), From a7f42b3fdfeba4783575e303d370877d665e949f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Sun, 17 Nov 2019 01:25:44 +0100 Subject: [PATCH 09/23] Inject $user for createProductCombination --- htdocs/variants/combinations.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/variants/combinations.php b/htdocs/variants/combinations.php index 714ee07ed3f..f8a8b9cc9e5 100644 --- a/htdocs/variants/combinations.php +++ b/htdocs/variants/combinations.php @@ -141,7 +141,7 @@ if ($_POST) { if (!$prodcomb->fetchByProductCombination2ValuePairs($id, $sanit_features)) { - $result = $prodcomb->createProductCombination($object, $sanit_features, array(), $price_impact_percent, $price_impact, $weight_impact); + $result = $prodcomb->createProductCombination($user, $object, $sanit_features, array(), $price_impact_percent, $price_impact, $weight_impact); if ($result > 0) { setEventMessages($langs->trans('RecordSaved'), null, 'mesgs'); @@ -268,7 +268,7 @@ if ($action === 'confirm_deletecombination') { if ($prodstatic->fetch('', $dest_product) > 0) { //To prevent from copying to the same product if ($prodstatic->ref != $object->ref) { - if ($prodcomb->copyAll($object->id, $prodstatic) > 0) { + if ($prodcomb->copyAll($user, $object->id, $prodstatic) > 0) { header('Location: '.dol_buildpath('/variants/combinations.php?id='.$prodstatic->id, 2)); exit(); } else { From 398f0e00f75bca7958db73ece512da9b28e7f381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Sun, 17 Nov 2019 01:27:23 +0100 Subject: [PATCH 10/23] inject $user for createProductCombination --- htdocs/variants/generator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/variants/generator.php b/htdocs/variants/generator.php index 27f811fdafe..137186bd5b8 100644 --- a/htdocs/variants/generator.php +++ b/htdocs/variants/generator.php @@ -110,7 +110,7 @@ if ($_POST) $cartesianarray = cartesianArray($adapted_values); foreach ($cartesianarray as $currcomb) { - $res = $combination->createProductCombination($product, $currcomb, $sanitized_values, $price_var_percent); + $res = $combination->createProductCombination($user, $product, $currcomb, $sanitized_values, $price_var_percent); if ($res < 0) { $error++; setEventMessages($combination->error, $combination->errors, 'errors'); From 8fbf853b9421d0788469d72f9de18caeecfc0ca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Tue, 19 Nov 2019 14:40:50 +0100 Subject: [PATCH 11/23] Display attributes infos when get product variants --- htdocs/product/class/api_products.class.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index fd564b17278..1d71246cab7 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -1133,7 +1133,14 @@ class Products extends DolibarrApi } $prodcomb = new ProductCombination($this->db); - return $prodcomb->fetchAllByFkProductParent((int) $id); + $combinations = $prodcomb->fetchAllByFkProductParent((int) $id); + + foreach ($combinations as $key => $combination) { + $prodc2vp = new ProductCombination2ValuePair($this->db); + $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((int) $combination->id); + } + + return $combinations; } /** @@ -1159,7 +1166,14 @@ class Products extends DolibarrApi } $prodcomb = new ProductCombination($this->db); - return $prodcomb->fetchAllByFkProductParent((int) $this->product->id); + $combinations = $prodcomb->fetchAllByFkProductParent((int) $this->product->id); + + foreach ($combinations as $key => $combination) { + $prodc2vp = new ProductCombination2ValuePair($this->db); + $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((int) $combination->id); + } + + return $combinations; } /** From c33506b4cc567d8e1134746227c161868769905c Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Tue, 19 Nov 2019 13:41:30 +0000 Subject: [PATCH 12/23] Fixing style errors. --- htdocs/product/class/api_products.class.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index 1d71246cab7..c9e95cb5fe0 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -1134,12 +1134,12 @@ class Products extends DolibarrApi $prodcomb = new ProductCombination($this->db); $combinations = $prodcomb->fetchAllByFkProductParent((int) $id); - + foreach ($combinations as $key => $combination) { $prodc2vp = new ProductCombination2ValuePair($this->db); $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((int) $combination->id); } - + return $combinations; } @@ -1167,12 +1167,12 @@ class Products extends DolibarrApi $prodcomb = new ProductCombination($this->db); $combinations = $prodcomb->fetchAllByFkProductParent((int) $this->product->id); - + foreach ($combinations as $key => $combination) { $prodc2vp = new ProductCombination2ValuePair($this->db); $combinations[$key]->attributes = $prodc2vp->fetchByFkCombination((int) $combination->id); } - + return $combinations; } From d6ba06e271f7b98a410243416438f163a8678de1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Thu, 21 Nov 2019 15:50:06 +0100 Subject: [PATCH 13/23] API New get attribute by ref + security update --- htdocs/product/class/api_products.class.php | 70 ++++++++++++++++----- 1 file changed, 53 insertions(+), 17 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index c9e95cb5fe0..6010f6dbcdf 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -814,7 +814,7 @@ class Products extends DolibarrApi */ public function getAttributes() { - if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + if(! DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } @@ -836,7 +836,7 @@ class Products extends DolibarrApi */ public function getAttributeById($id) { - if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + if(! DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } @@ -849,6 +849,42 @@ class Products extends DolibarrApi return $prodattr; } + + /** + * Get attributes by ref. + * + * @param string $ref Reference of Attribute + * @return array + * + * @throws RestException + * @throws 401 + * + * @url GET attributes/ref/{ref} + */ + public function getAttributesByRef($ref) + { + if(! DolibarrApiAccess::$user->rights->produit->lire) { + throw new RestException(401); + } + + $sql = "SELECT rowid, ref, label, rang FROM ".MAIN_DB_PREFIX."product_attribute WHERE ref LIKE '". trim($ref) ."' AND entity IN (".getEntity('product').")"; + + $query = $this->db->query($sql); + + if (!$this->db->num_rows($query)) { + throw new RestException(404); + } + + $result = $this->db->fetch_object($query); + + $attr = []; + $attr['id'] = $result->rowid; + $attr['ref'] = $result->ref; + $attr['label'] = $result->label; + $attr['rang'] = $result->rang; + + return $attr; + } /** * Add attributes. @@ -864,7 +900,7 @@ class Products extends DolibarrApi */ public function addAttributes($ref, $label) { - if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + if(! DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } @@ -895,7 +931,7 @@ class Products extends DolibarrApi */ public function putAttributes($id, $ref, $label) { - if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + if(! DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } @@ -931,7 +967,7 @@ class Products extends DolibarrApi */ public function deleteAttributes($id) { - if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + if(! DolibarrApiAccess::$user->rights->produit->supprimer) { throw new RestException(401); } @@ -958,7 +994,7 @@ class Products extends DolibarrApi */ public function deleteAttributesByRef($ref) { - if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + if(! DolibarrApiAccess::$user->rights->produit->supprimer) { throw new RestException(401); } @@ -983,7 +1019,7 @@ class Products extends DolibarrApi */ public function getAttributeValues($id) { - if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + if(! DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } @@ -1004,7 +1040,7 @@ class Products extends DolibarrApi */ public function getAttributeValuesByRef($ref) { - if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + if(! DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } @@ -1044,7 +1080,7 @@ class Products extends DolibarrApi */ public function addAttributeValue($id, $ref, $value) { - if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + if(! DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } @@ -1076,7 +1112,7 @@ class Products extends DolibarrApi */ public function deleteAttributeValueById($id) { - if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + if(! DolibarrApiAccess::$user->rights->produit->supprimer) { throw new RestException(401); } @@ -1102,7 +1138,7 @@ class Products extends DolibarrApi */ public function deleteAttributeValueByRef($ref) { - if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + if(! DolibarrApiAccess::$user->rights->produit->supprimer) { throw new RestException(401); } @@ -1128,7 +1164,7 @@ class Products extends DolibarrApi */ public function getVariants($id) { - if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + if(! DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } @@ -1156,7 +1192,7 @@ class Products extends DolibarrApi */ public function getVariantsByProdRef($ref) { - if(! (DolibarrApiAccess::$user->rights->produit->lire || DolibarrApiAccess::$user->rights->service->lire)) { + if(! DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } @@ -1196,7 +1232,7 @@ class Products extends DolibarrApi */ public function addVariant($id, $weight_impact, $price_impact, $price_impact_is_percent, $features) { - if(! (DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) { + if(! DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } @@ -1255,7 +1291,7 @@ class Products extends DolibarrApi */ public function addVariantByProductRef($ref, $weight_impact, $price_impact, $price_impact_is_percent, $features) { - if(! (DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) { + if(! DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } @@ -1308,7 +1344,7 @@ class Products extends DolibarrApi */ public function putVariant($id, $request_data = null) { - if(! (DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) { + if(! DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } @@ -1344,7 +1380,7 @@ class Products extends DolibarrApi */ public function deleteVariant($id) { - if(! (DolibarrApiAccess::$user->rights->produit->supprimer || DolibarrApiAccess::$user->rights->service->supprimer)) { + if(! DolibarrApiAccess::$user->rights->produit->supprimer) { throw new RestException(401); } From 4f747e2eb8e5054a8836eb99e0e4ef7fd1f623e2 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Thu, 21 Nov 2019 14:50:47 +0000 Subject: [PATCH 14/23] Fixing style errors. --- htdocs/product/class/api_products.class.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index 6010f6dbcdf..faf312e77ff 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -849,7 +849,7 @@ class Products extends DolibarrApi return $prodattr; } - + /** * Get attributes by ref. * @@ -866,23 +866,23 @@ class Products extends DolibarrApi if(! DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } - + $sql = "SELECT rowid, ref, label, rang FROM ".MAIN_DB_PREFIX."product_attribute WHERE ref LIKE '". trim($ref) ."' AND entity IN (".getEntity('product').")"; - + $query = $this->db->query($sql); - + if (!$this->db->num_rows($query)) { throw new RestException(404); } - + $result = $this->db->fetch_object($query); - + $attr = []; $attr['id'] = $result->rowid; $attr['ref'] = $result->ref; $attr['label'] = $result->label; $attr['rang'] = $result->rang; - + return $attr; } From 1068d9a62388606adceabcac241c681155ebc2d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Thu, 21 Nov 2019 17:23:46 +0100 Subject: [PATCH 15/23] FIX deleteAttributeValueByRef / ADD getAttributeValueById, getAttributeValueByRef --- htdocs/product/class/api_products.class.php | 102 ++++++++++++++++++-- 1 file changed, 93 insertions(+), 9 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index faf312e77ff..5be52795437 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -980,30 +980,114 @@ class Products extends DolibarrApi } throw new RestException(500, "Error deleting attribute"); } - + /** - * Delete attributes by ref. + * Get attribute value by id. * - * @param string $ref Reference of Attribute + * @param int $id ID of Attribute value + * @param string $ref Ref of Attribute value + * @return array + * + * @throws RestException + * @throws 401 + * + * @url GET attributes/values/{id} + */ + public function getAttributeValueById($id) + { + if(! DolibarrApiAccess::$user->rights->produit->lire) { + throw new RestException(401); + } + + $sql = "SELECT rowid, fk_product_attribute, ref, value FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE rowid = ".(int) $id." AND entity IN (".getEntity('product').")"; + + $query = $this->db->query($sql); + + if (!$query) { + throw new RestException(401); + } + + if (!$this->db->num_rows($query)) { + throw new RestException(404, 'Attribute value not found'); + } + + $result = $this->db->fetch_object($query); + + $attrval = []; + $attrval['id'] = $result->rowid; + $attrval['fk_product_attribute'] = $result->fk_product_attribute; + $attrval['ref'] = $result->ref; + $attrval['value'] = $result->value; + + return $attrval; + } + + /** + * Get attribute value by ref. + * + * @param int $id ID of Attribute value + * @param string $ref Ref of Attribute value + * @return array + * + * @throws RestException + * @throws 401 + * + * @url GET attributes/{id}/values/ref/{ref} + */ + public function getAttributeValueByRef($id, $ref) + { + if(! DolibarrApiAccess::$user->rights->produit->lire) { + throw new RestException(401); + } + + $sql = "SELECT rowid, fk_product_attribute, ref, value FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE ref LIKE '". trim($ref) ."' AND fk_product_attribute = ". (int) $id ." AND entity IN (".getEntity('product').")"; + + $query = $this->db->query($sql); + + if (!$query) { + throw new RestException(401); + } + + if (!$this->db->num_rows($query)) { + throw new RestException(404, 'Attribute value not found'); + } + + $result = $this->db->fetch_object($query); + + $attrval = []; + $attrval['id'] = $result->rowid; + $attrval['fk_product_attribute'] = $result->fk_product_attribute; + $attrval['ref'] = $result->ref; + $attrval['value'] = $result->value; + + return $attrval; + } + + /** + * Delete attribute value by ref. + * + * @param int $id ID of Attribute + * @param string $ref Ref of Attribute value * @return int * * @throws RestException * @throws 401 * - * @url DELETE attributes/ref/{ref} + * @url DELETE attributes/{id}/values/ref/{ref} */ - public function deleteAttributesByRef($ref) + public function deleteAttributeValueByRef($id, $ref) { if(! DolibarrApiAccess::$user->rights->produit->supprimer) { throw new RestException(401); } - - $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute WHERE ref LIKE '". trim($ref) ."'"; - + + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE ref LIKE '". trim($ref) ."' AND fk_product_attribute = ". (int) $id; + if ($this->db->query($sql)) { return 1; } - throw new RestException(500, "Error deleting attribute"); + + throw new RestException(500, "Error deleting attribute value"); } /** From 0074589b6fa689ca1c355a78e153690292c3bb6f Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Thu, 21 Nov 2019 16:24:25 +0000 Subject: [PATCH 16/23] Fixing style errors. --- htdocs/product/class/api_products.class.php | 40 ++++++++++----------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index 5be52795437..c614b013f83 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -980,7 +980,7 @@ class Products extends DolibarrApi } throw new RestException(500, "Error deleting attribute"); } - + /** * Get attribute value by id. * @@ -998,30 +998,30 @@ class Products extends DolibarrApi if(! DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } - + $sql = "SELECT rowid, fk_product_attribute, ref, value FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE rowid = ".(int) $id." AND entity IN (".getEntity('product').")"; - + $query = $this->db->query($sql); - + if (!$query) { throw new RestException(401); } - + if (!$this->db->num_rows($query)) { throw new RestException(404, 'Attribute value not found'); } - + $result = $this->db->fetch_object($query); - + $attrval = []; $attrval['id'] = $result->rowid; $attrval['fk_product_attribute'] = $result->fk_product_attribute; $attrval['ref'] = $result->ref; $attrval['value'] = $result->value; - + return $attrval; } - + /** * Get attribute value by ref. * @@ -1039,30 +1039,30 @@ class Products extends DolibarrApi if(! DolibarrApiAccess::$user->rights->produit->lire) { throw new RestException(401); } - + $sql = "SELECT rowid, fk_product_attribute, ref, value FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE ref LIKE '". trim($ref) ."' AND fk_product_attribute = ". (int) $id ." AND entity IN (".getEntity('product').")"; - + $query = $this->db->query($sql); - + if (!$query) { throw new RestException(401); } - + if (!$this->db->num_rows($query)) { throw new RestException(404, 'Attribute value not found'); } - + $result = $this->db->fetch_object($query); - + $attrval = []; $attrval['id'] = $result->rowid; $attrval['fk_product_attribute'] = $result->fk_product_attribute; $attrval['ref'] = $result->ref; $attrval['value'] = $result->value; - + return $attrval; } - + /** * Delete attribute value by ref. * @@ -1080,13 +1080,13 @@ class Products extends DolibarrApi if(! DolibarrApiAccess::$user->rights->produit->supprimer) { throw new RestException(401); } - + $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE ref LIKE '". trim($ref) ."' AND fk_product_attribute = ". (int) $id; - + if ($this->db->query($sql)) { return 1; } - + throw new RestException(500, "Error deleting attribute value"); } From 416a2f7f1c0056c3cdebe6eb4505c8526bf10655 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Thu, 21 Nov 2019 17:41:09 +0100 Subject: [PATCH 17/23] FIX putAttributes, ADD putAttributeValue --- htdocs/product/class/api_products.class.php | 80 +++++++++++++++++---- 1 file changed, 67 insertions(+), 13 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index c614b013f83..2f3f7463b8d 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -914,14 +914,13 @@ class Products extends DolibarrApi } return $resid; } - + /** * Update attributes by id. * * @param int $id ID of Attribute - * @param string $ref Reference of Attribute - * @param string $label Label of Attribute - * @return int + * @param array $request_data Datas + * @return array * * @throws RestException * @throws 401 @@ -929,27 +928,36 @@ class Products extends DolibarrApi * * @url PUT attributes/{id} */ - public function putAttributes($id, $ref, $label) + public function putAttributes($id, $request_data = null) { if(! DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } - + $prodattr = new ProductAttribute($this->db); - + $result = $prodattr->fetch((int) $id); if ($result == 0) { throw new RestException(404, 'Attribute not found'); } elseif ($result < 0) { throw new RestException(500, "Error fetching attribute"); } - - - $prodattr->label = $label; - $prodattr->ref = $ref; - + + foreach($request_data as $field => $value) { + if ($field == 'rowid') { continue; + } + $prodattr->$field = $value; + } + if ($prodattr->update(DolibarrApiAccess::$user) > 0) { - return 1; + $result = $prodattr->fetch((int) $id); + if ($result == 0) { + throw new RestException(404, 'Attribute not found'); + } elseif ($result < 0) { + throw new RestException(500, "Error fetching attribute"); + } else { + return $prodattr; + } } throw new RestException(500, "Error updating attribute"); } @@ -1182,6 +1190,52 @@ class Products extends DolibarrApi } throw new RestException(500, "Error creating new attribute value"); } + + /** + * Update attribute value. + * + * @param int $id ID of Attribute + * @param array $request_data Datas + * @return array + * + * @throws RestException + * @throws 401 + * + * @url PUT attributes/values/{id} + */ + public function putAttributeValue($id, $request_data) + { + if(! DolibarrApiAccess::$user->rights->produit->creer) { + throw new RestException(401); + } + + $objectval = new ProductAttributeValue($this->db); + $result = $objectval->fetch((int) $id); + + if ($result == 0) { + throw new RestException(404, 'Attribute value not found'); + } elseif ($result < 0) { + throw new RestException(500, "Error fetching attribute value"); + } + + foreach($request_data as $field => $value) { + if ($field == 'rowid') { continue; + } + $objectval->$field = $value; + } + + if ($objectval->update(DolibarrApiAccess::$user) > 0) { + $result = $objectval->fetch((int) $id); + if ($result == 0) { + throw new RestException(404, 'Attribute not found'); + } elseif ($result < 0) { + throw new RestException(500, "Error fetching attribute"); + } else { + return $objectval; + } + } + throw new RestException(500, "Error updating attribute"); + } /** * Delete attribute value by id. From cfa0860d244896fe64ca5e2e29b01b210e915a0a Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Thu, 21 Nov 2019 16:41:45 +0000 Subject: [PATCH 18/23] Fixing style errors. --- htdocs/product/class/api_products.class.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index 2f3f7463b8d..04d9979cefe 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -914,7 +914,7 @@ class Products extends DolibarrApi } return $resid; } - + /** * Update attributes by id. * @@ -933,22 +933,22 @@ class Products extends DolibarrApi if(! DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } - + $prodattr = new ProductAttribute($this->db); - + $result = $prodattr->fetch((int) $id); if ($result == 0) { throw new RestException(404, 'Attribute not found'); } elseif ($result < 0) { throw new RestException(500, "Error fetching attribute"); } - + foreach($request_data as $field => $value) { if ($field == 'rowid') { continue; } $prodattr->$field = $value; } - + if ($prodattr->update(DolibarrApiAccess::$user) > 0) { $result = $prodattr->fetch((int) $id); if ($result == 0) { @@ -1190,7 +1190,7 @@ class Products extends DolibarrApi } throw new RestException(500, "Error creating new attribute value"); } - + /** * Update attribute value. * @@ -1208,22 +1208,22 @@ class Products extends DolibarrApi if(! DolibarrApiAccess::$user->rights->produit->creer) { throw new RestException(401); } - + $objectval = new ProductAttributeValue($this->db); $result = $objectval->fetch((int) $id); - + if ($result == 0) { throw new RestException(404, 'Attribute value not found'); } elseif ($result < 0) { throw new RestException(500, "Error fetching attribute value"); } - + foreach($request_data as $field => $value) { if ($field == 'rowid') { continue; } $objectval->$field = $value; } - + if ($objectval->update(DolibarrApiAccess::$user) > 0) { $result = $objectval->fetch((int) $id); if ($result == 0) { From 351ba58bbe65f9ddf6e1f85d852ea39d620c613f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Thu, 21 Nov 2019 17:43:40 +0100 Subject: [PATCH 19/23] Fix Superfluous parameter comment --- htdocs/product/class/api_products.class.php | 1 - 1 file changed, 1 deletion(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index 04d9979cefe..32423c015fc 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -993,7 +993,6 @@ class Products extends DolibarrApi * Get attribute value by id. * * @param int $id ID of Attribute value - * @param string $ref Ref of Attribute value * @return array * * @throws RestException From 5a3f78088e2c42fd763d6493a4143783164c42fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Thu, 21 Nov 2019 18:18:11 +0100 Subject: [PATCH 20/23] Fix double deleteAttributeValueByRef --- htdocs/product/class/api_products.class.php | 26 --------------------- 1 file changed, 26 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index 32423c015fc..2fa4f600b11 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -1262,32 +1262,6 @@ class Products extends DolibarrApi throw new RestException(500, "Error deleting attribute value"); } - /** - * Delete attribute value by ref. - * - * @param string $ref Ref of Attribute value - * @return int - * - * @throws RestException - * @throws 401 - * - * @url DELETE attributes/values/ref/{ref} - */ - public function deleteAttributeValueByRef($ref) - { - if(! DolibarrApiAccess::$user->rights->produit->supprimer) { - throw new RestException(401); - } - - $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE ref LIKE '". trim($ref) ."'"; - - if ($this->db->query($sql)) { - return 1; - } - - throw new RestException(500, "Error deleting attribute value"); - } - /** * Get product variants. * From 1bc8c57d6d2486d5f3919bf12dc5ff5204968f85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Thu, 21 Nov 2019 22:54:13 +0100 Subject: [PATCH 21/23] Fix $weight_impact $price_impact --- htdocs/product/class/api_products.class.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index 2fa4f600b11..bb83630bb72 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -1347,9 +1347,12 @@ class Products extends DolibarrApi throw new RestException(401); } - if (empty($id) || empty($weight_impact) || empty($price_impact) || empty($features) || !is_array($features)) { + if (empty($id) || empty($features) || !is_array($features)) { throw new RestException(401); } + + $weight_impact = price2num($weight_impact); + $price_impact = price2num($price_impact); $prodattr = new ProductAttribute($this->db); $prodattr_val = new ProductAttributeValue($this->db); @@ -1406,9 +1409,12 @@ class Products extends DolibarrApi throw new RestException(401); } - if (empty($ref) || empty($weight_impact) || empty($price_impact) || empty($features) || !is_array($features)) { + if (empty($ref) || empty($features) || !is_array($features)) { throw new RestException(401); } + + $weight_impact = price2num($weight_impact); + $price_impact = price2num($price_impact); $prodattr = new ProductAttribute($this->db); $prodattr_val = new ProductAttributeValue($this->db); From 821ca724a5031f0825efec5a94334596505d8292 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Thu, 21 Nov 2019 21:54:51 +0000 Subject: [PATCH 22/23] Fixing style errors. --- htdocs/product/class/api_products.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index bb83630bb72..c5ca4271f75 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -1350,7 +1350,7 @@ class Products extends DolibarrApi if (empty($id) || empty($features) || !is_array($features)) { throw new RestException(401); } - + $weight_impact = price2num($weight_impact); $price_impact = price2num($price_impact); @@ -1412,7 +1412,7 @@ class Products extends DolibarrApi if (empty($ref) || empty($features) || !is_array($features)) { throw new RestException(401); } - + $weight_impact = price2num($weight_impact); $price_impact = price2num($price_impact); From e76a112099ec5eff39d77a3f4f87ba049e78146e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Thu, 21 Nov 2019 23:09:18 +0100 Subject: [PATCH 23/23] Remove unecessary dol_clone in ::putVariant --- htdocs/product/class/api_products.class.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/htdocs/product/class/api_products.class.php b/htdocs/product/class/api_products.class.php index c5ca4271f75..37c950e9956 100644 --- a/htdocs/product/class/api_products.class.php +++ b/htdocs/product/class/api_products.class.php @@ -1468,8 +1468,6 @@ class Products extends DolibarrApi $prodcomb = new ProductCombination($this->db); $prodcomb->fetch((int) $id); - $old_prodcomb = dol_clone($prodcomb, 0); - foreach($request_data as $field => $value) { if ($field == 'rowid') { continue; }