From 480ac1a926286c29c4518f8a23502b150a077353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Tue, 12 Nov 2019 18:08:09 +0100 Subject: [PATCH 1/3] API New link/unlink products with categories Need it for V11 please :) --- .../categories/class/api_categories.class.php | 240 ++++++++++++++++++ 1 file changed, 240 insertions(+) diff --git a/htdocs/categories/class/api_categories.class.php b/htdocs/categories/class/api_categories.class.php index d289874d1ee..f7a2addef3d 100644 --- a/htdocs/categories/class/api_categories.class.php +++ b/htdocs/categories/class/api_categories.class.php @@ -264,6 +264,246 @@ class Categories extends DolibarrApi ) ); } + + /** + * Link an object to a category by id + * + * @param int $id ID of category + * @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact') + * @param int $object_id ID of object + * + * @return array + * @throws RestException + * + * @url POST {id}/objects/{type}/{object_id} + */ + public function link_object_by_id($id, $type, $object_id) + { + if (empty($type) || empty($object_id)) { + throw new RestException(401); + } + + if(! DolibarrApiAccess::$user->rights->categorie->lire) { + throw new RestException(401); + } + + $result = $this->category->fetch($id); + if( ! $result ) { + throw new RestException(404, 'category not found'); + } + + # TODO Add all types + if ($type === "product") { + if(! (DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) { + throw new RestException(401); + } + $object = new Product($this->db); + } else { + throw new RestException(401, "this type is not recognized yet."); + } + + if (!empty($object)) { + $result = $object->fetch($object_id); + if ($result > 0) { + $result=$this->category->add_type($object, $type); + if ($result < 0) { + if ($this->category->error != 'DB_ERROR_RECORD_ALREADY_EXISTS') { + throw new RestException(500, 'Error when linking object', array_merge(array($this->category->error), $this->category->errors)); + } + } + } else { + throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors)); + } + + return array( + 'success' => array( + 'code' => 200, + 'message' => 'Objects succefully linked to the category' + ) + ); + } + + throw new RestException(401); + } + + /** + * Link an object to a category by ref + * + * @param int $id ID of category + * @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact') + * @param string $object_ref Reference of object + * + * @return array + * @throws RestException + * + * @url POST {id}/objects/{type}/ref/{object_ref} + */ + public function link_object_by_ref($id, $type, $object_ref) + { + if (empty($type) || empty($object_ref)) { + throw new RestException(401); + } + + if(! DolibarrApiAccess::$user->rights->categorie->lire) { + throw new RestException(401); + } + + $result = $this->category->fetch($id); + if( ! $result ) { + throw new RestException(404, 'category not found'); + } + + # TODO Add all types + if ($type === "product") { + if(! (DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) { + throw new RestException(401); + } + $object = new Product($this->db); + } else { + throw new RestException(401, "this type is not recognized yet."); + } + + if (!empty($object)) { + $result = $object->fetch('', $object_ref); + if ($result > 0) { + $result=$this->category->add_type($object, $type); + if ($result < 0) { + if ($this->category->error != 'DB_ERROR_RECORD_ALREADY_EXISTS') { + throw new RestException(500, 'Error when linking object', array_merge(array($this->category->error), $this->category->errors)); + } + } + } else { + throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors)); + } + + return array( + 'success' => array( + 'code' => 200, + 'message' => 'Objects succefully linked to the category' + ) + ); + } + + throw new RestException(401); + } + + /** + * Unlink an object from a category by id + * + * @param int $id ID of category + * @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact') + * @param int $object_id ID of the object + * + * @return array + * @throws RestException + * + * @url DELETE {id}/objects/{type}/{object_id} + */ + public function unlink_object_by_id($id, $type, $object_id) + { + if (empty($type) || empty($object_id)) { + throw new RestException(401); + } + + if(! DolibarrApiAccess::$user->rights->categorie->lire) { + throw new RestException(401); + } + + $result = $this->category->fetch($id); + if( ! $result ) { + throw new RestException(404, 'category not found'); + } + + # TODO Add all types + if ($type === "product") { + if(! (DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) { + throw new RestException(401); + } + $object = new Product($this->db); + } else { + throw new RestException(401, "this type is not recognized yet."); + } + + if (!empty($object)) { + $result = $object->fetch((int) $object_id); + if ($result > 0) { + $result=$this->category->del_type($object, $type); + if ($result < 0) { + throw new RestException(500, 'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors)); + } + } else { + throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors)); + } + + return array( + 'success' => array( + 'code' => 200, + 'message' => 'Objects succefully unlinked from the category' + ) + ); + } + + throw new RestException(401); + } + + /** + * Unlink an object from a category by ref + * + * @param int $id ID of category + * @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact') + * @param string $object_ref Reference of the object + * + * @return array + * @throws RestException + * + * @url DELETE {id}/objects/{type}/ref/{object_ref} + */ + public function unlink_object_by_ref($id, $type, $object_ref) + { + if (empty($type) || empty($object_ref)) { + throw new RestException(401); + } + + if(! DolibarrApiAccess::$user->rights->categorie->lire) { + throw new RestException(401); + } + + $result = $this->category->fetch($id); + if( ! $result ) { + throw new RestException(404, 'category not found'); + } + + # TODO Add all types + if ($type === "product") { + if(! (DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) { + throw new RestException(401); + } + $object = new Product($this->db); + } else { + throw new RestException(401, "this type is not recognized yet."); + } + + if (!empty($object)) { + $result = $object->fetch('', (string) $object_ref); + if ($result > 0) { + $result=$this->category->del_type($object, $type); + if ($result < 0) { + throw new RestException(500, 'Error when unlinking object', array_merge(array($this->category->error), $this->category->errors)); + } + } else { + throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors)); + } + + return array( + 'success' => array( + 'code' => 200, + 'message' => 'Objects succefully unlinked from the category' + ) + ); + } + + throw new RestException(401); + } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore From fcb50841ccf35a298fb9c145f4be21445381e4e0 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Tue, 12 Nov 2019 17:10:20 +0000 Subject: [PATCH 2/3] Fixing style errors. --- .../categories/class/api_categories.class.php | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/htdocs/categories/class/api_categories.class.php b/htdocs/categories/class/api_categories.class.php index f7a2addef3d..36b3e10174a 100644 --- a/htdocs/categories/class/api_categories.class.php +++ b/htdocs/categories/class/api_categories.class.php @@ -264,17 +264,17 @@ class Categories extends DolibarrApi ) ); } - + /** * Link an object to a category by id * * @param int $id ID of category * @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact') * @param int $object_id ID of object - * + * * @return array * @throws RestException - * + * * @url POST {id}/objects/{type}/{object_id} */ public function link_object_by_id($id, $type, $object_id) @@ -282,17 +282,17 @@ class Categories extends DolibarrApi if (empty($type) || empty($object_id)) { throw new RestException(401); } - + if(! DolibarrApiAccess::$user->rights->categorie->lire) { throw new RestException(401); } - + $result = $this->category->fetch($id); if( ! $result ) { throw new RestException(404, 'category not found'); } - - # TODO Add all types + + // TODO Add all types if ($type === "product") { if(! (DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) { throw new RestException(401); @@ -301,7 +301,7 @@ class Categories extends DolibarrApi } else { throw new RestException(401, "this type is not recognized yet."); } - + if (!empty($object)) { $result = $object->fetch($object_id); if ($result > 0) { @@ -314,7 +314,7 @@ class Categories extends DolibarrApi } else { throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors)); } - + return array( 'success' => array( 'code' => 200, @@ -322,10 +322,10 @@ class Categories extends DolibarrApi ) ); } - + throw new RestException(401); } - + /** * Link an object to a category by ref * @@ -343,17 +343,17 @@ class Categories extends DolibarrApi if (empty($type) || empty($object_ref)) { throw new RestException(401); } - + if(! DolibarrApiAccess::$user->rights->categorie->lire) { throw new RestException(401); } - + $result = $this->category->fetch($id); if( ! $result ) { throw new RestException(404, 'category not found'); } - - # TODO Add all types + + // TODO Add all types if ($type === "product") { if(! (DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) { throw new RestException(401); @@ -362,7 +362,7 @@ class Categories extends DolibarrApi } else { throw new RestException(401, "this type is not recognized yet."); } - + if (!empty($object)) { $result = $object->fetch('', $object_ref); if ($result > 0) { @@ -375,7 +375,7 @@ class Categories extends DolibarrApi } else { throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors)); } - + return array( 'success' => array( 'code' => 200, @@ -383,10 +383,10 @@ class Categories extends DolibarrApi ) ); } - + throw new RestException(401); } - + /** * Unlink an object from a category by id * @@ -404,17 +404,17 @@ class Categories extends DolibarrApi if (empty($type) || empty($object_id)) { throw new RestException(401); } - + if(! DolibarrApiAccess::$user->rights->categorie->lire) { throw new RestException(401); } - + $result = $this->category->fetch($id); if( ! $result ) { throw new RestException(404, 'category not found'); } - - # TODO Add all types + + // TODO Add all types if ($type === "product") { if(! (DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) { throw new RestException(401); @@ -423,7 +423,7 @@ class Categories extends DolibarrApi } else { throw new RestException(401, "this type is not recognized yet."); } - + if (!empty($object)) { $result = $object->fetch((int) $object_id); if ($result > 0) { @@ -434,7 +434,7 @@ class Categories extends DolibarrApi } else { throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors)); } - + return array( 'success' => array( 'code' => 200, @@ -442,10 +442,10 @@ class Categories extends DolibarrApi ) ); } - + throw new RestException(401); } - + /** * Unlink an object from a category by ref * @@ -463,17 +463,17 @@ class Categories extends DolibarrApi if (empty($type) || empty($object_ref)) { throw new RestException(401); } - + if(! DolibarrApiAccess::$user->rights->categorie->lire) { throw new RestException(401); } - + $result = $this->category->fetch($id); if( ! $result ) { throw new RestException(404, 'category not found'); } - - # TODO Add all types + + // TODO Add all types if ($type === "product") { if(! (DolibarrApiAccess::$user->rights->produit->creer || DolibarrApiAccess::$user->rights->service->creer)) { throw new RestException(401); @@ -482,7 +482,7 @@ class Categories extends DolibarrApi } else { throw new RestException(401, "this type is not recognized yet."); } - + if (!empty($object)) { $result = $object->fetch('', (string) $object_ref); if ($result > 0) { @@ -493,7 +493,7 @@ class Categories extends DolibarrApi } else { throw new RestException(500, 'Error when fetching object', array_merge(array($object->error), $object->errors)); } - + return array( 'success' => array( 'code' => 200, @@ -501,7 +501,7 @@ class Categories extends DolibarrApi ) ); } - + throw new RestException(401); } From c4c463fb96eb9a207db5a3ac684cafc99b0f807d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= <35066297+c3do@users.noreply.github.com> Date: Wed, 13 Nov 2019 11:36:42 +0100 Subject: [PATCH 3/3] camelCaps --- htdocs/categories/class/api_categories.class.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/htdocs/categories/class/api_categories.class.php b/htdocs/categories/class/api_categories.class.php index 36b3e10174a..0f2a425fe87 100644 --- a/htdocs/categories/class/api_categories.class.php +++ b/htdocs/categories/class/api_categories.class.php @@ -277,7 +277,7 @@ class Categories extends DolibarrApi * * @url POST {id}/objects/{type}/{object_id} */ - public function link_object_by_id($id, $type, $object_id) + public function linkObjectById($id, $type, $object_id) { if (empty($type) || empty($object_id)) { throw new RestException(401); @@ -338,7 +338,7 @@ class Categories extends DolibarrApi * * @url POST {id}/objects/{type}/ref/{object_ref} */ - public function link_object_by_ref($id, $type, $object_ref) + public function linkObjectByRef($id, $type, $object_ref) { if (empty($type) || empty($object_ref)) { throw new RestException(401); @@ -399,7 +399,7 @@ class Categories extends DolibarrApi * * @url DELETE {id}/objects/{type}/{object_id} */ - public function unlink_object_by_id($id, $type, $object_id) + public function unlinkObjectById($id, $type, $object_id) { if (empty($type) || empty($object_id)) { throw new RestException(401); @@ -458,7 +458,7 @@ class Categories extends DolibarrApi * * @url DELETE {id}/objects/{type}/ref/{object_ref} */ - public function unlink_object_by_ref($id, $type, $object_ref) + public function unlinkObjectByRef($id, $type, $object_ref) { if (empty($type) || empty($object_ref)) { throw new RestException(401);