From 15dad3f4ebd8098b486eeadad471e74a6569ec6c Mon Sep 17 00:00:00 2001 From: Sam Berry Date: Fri, 23 Sep 2022 16:59:30 -0400 Subject: [PATCH 1/2] feat: added getcategories api --- classes/APIRoutes.php | 9 ++ controllers/front/getcategories.php | 130 ++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100755 controllers/front/getcategories.php diff --git a/classes/APIRoutes.php b/classes/APIRoutes.php index 71785c9..803e952 100644 --- a/classes/APIRoutes.php +++ b/classes/APIRoutes.php @@ -320,6 +320,15 @@ public static final function getRoutes(): array 'module' => 'bienoubien' ] ], + 'module-bienoubien-webhook' => [ + 'rule' => 'rest/getcategories', + 'keywords' => [], + 'controller' => 'getcategories', + 'params' => [ + 'fc' => 'module', + 'module' => 'bienoubien' + ] + ], ]; } } diff --git a/controllers/front/getcategories.php b/controllers/front/getcategories.php new file mode 100755 index 0000000..25612d5 --- /dev/null +++ b/controllers/front/getcategories.php @@ -0,0 +1,130 @@ +context->language->id); + $result = array(); + + foreach ($root_categories as $category){ + $root_node = $this->makeNode([ + 'label' => null, + 'type' => 'root_categories', + 'children' => [], + ]); + + $categories = $this->generateCategoriesMenu( + Category::getNestedCategories((int)$category['id_category'], $this->context->language->id, false) + ); + $root_node['children'] = array_merge($root_node['children'], $categories); + + $result[] = $root_node; + } + + $this->ajaxRender(json_encode([ + 'success' => true, + 'code' => 200, + 'psdata' => $result + ])); + die; + } + + protected function generateCategoriesMenu($categories, $is_children = 0) + { + $nodes = []; + + foreach ($categories as $key => $category) { + $node = $this->makeNode([]); + if ($category['level_depth'] > 1) { + $cat = new Category($category['id_category']); + $link = $cat->getLink(); + // Check if customer is set and check access + if (Validate::isLoadedObject($this->context->customer) && !$cat->checkAccess($this->context->customer->id)) { + continue; + } + } else { + $link = $this->context->link->getPageLink('index'); + } + + $node['id'] = $category['id_category']; + $node['slug'] = $category['link_rewrite']; + $node['url'] = $link; + $node['type'] = 'category'; + $node['page_identifier'] = 'category-' . $category['id_category']; + + /* Whenever a category is not active we shouldnt display it to customer */ + if ((bool) $category['active'] === false) { + continue; + } + + $current = $this->page_name == 'category' && (int) Tools::getValue('id_category') == (int) $category['id_category']; + $node['current'] = $current; + $node['label'] = $category['name']; + $node['image_urls'] = []; + + if (isset($category['children']) && !empty($category['children'])) { + $node['children'] = $this->generateCategoriesMenu($category['children'], 1); + } + + $nodes[] = $node; + } + + return $nodes; + } + + protected function makeNode(array $fields) + { + $defaults = [ + 'id' => '', + 'slug' => '', + 'type' => '', + 'label' => '', + 'url' => '', + 'children' => [], + 'image_urls' => [], + 'page_identifier' => null + ]; + + return array_merge($defaults, $fields); + } + + protected function processPostRequest() + { + $this->ajaxRender(json_encode([ + 'success' => true, + 'message' => 'POST not supported on this path' + ])); + die; + } + + protected function processPutRequest() + { + $this->ajaxRender(json_encode([ + 'success' => true, + 'message' => 'put not supported on this path' + ])); + die; + } + + protected function processDeleteRequest() + { + $this->ajaxRender(json_encode([ + 'success' => true, + 'message' => 'delete not supported on this path' + ])); + die; + } +} From 035b211dd4245b7e6fe7c2f9bcbf52bbeb3cfee4 Mon Sep 17 00:00:00 2001 From: Sam Berry Date: Tue, 27 Sep 2022 09:33:49 -0400 Subject: [PATCH 2/2] feat: added pagination --- controllers/front/getcategories.php | 52 +++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/controllers/front/getcategories.php b/controllers/front/getcategories.php index 25612d5..321cde4 100755 --- a/controllers/front/getcategories.php +++ b/controllers/front/getcategories.php @@ -16,22 +16,29 @@ class BienoubienGetcategoriesModuleFrontController extends AbstractRESTController { protected function processGetRequest(){ - $root_categories = Category::getHomeCategories($this->context->language->id); + if (Tools::getValue('total_number')){ + $this->ajaxRender(json_encode([ + 'success' => true, + 'code' => 200, + 'psdata' => count($this->getTopLevelCategories($this->context->language->id)) + ])); + die; + } + + $page = Tools::getValue('page'); + $resultsPerPage = Tools::getValue('resultsPerPage'); + + $start = (($page -1) * $resultsPerPage); + $limit = $resultsPerPage; + + $root_categories = $this->getTopLevelCategories($this->context->language->id, true, false, $start, $limit); $result = array(); foreach ($root_categories as $category){ - $root_node = $this->makeNode([ - 'label' => null, - 'type' => 'root_categories', - 'children' => [], - ]); - $categories = $this->generateCategoriesMenu( Category::getNestedCategories((int)$category['id_category'], $this->context->language->id, false) ); - $root_node['children'] = array_merge($root_node['children'], $categories); - - $result[] = $root_node; + $result = array_merge($result, $categories); } $this->ajaxRender(json_encode([ @@ -42,6 +49,31 @@ protected function processGetRequest(){ die; } + protected function getTopLevelCategories($idLang, $active = true, $idShop = false, $start = 0, $limit = 0){ + $idParent = Configuration::get('PS_HOME_CATEGORY'); + if (!Validate::isBool($active)) { + die(Tools::displayError()); + } + + $cacheId = 'Category::getChildren_' . (int) $idParent . '-' . (int) $idLang . '-' . (bool) $active . '-' . (int) $idShop; + if (!Cache::isStored($cacheId)) { + $query = 'SELECT c.`id_category`, cl.`name`, cl.`link_rewrite`, category_shop.`id_shop` + FROM `' . _DB_PREFIX_ . 'category` c + LEFT JOIN `' . _DB_PREFIX_ . 'category_lang` cl ON (c.`id_category` = cl.`id_category`' . Shop::addSqlRestrictionOnLang('cl') . ') + ' . Shop::addSqlAssociation('category', 'c') . ' + WHERE `id_lang` = ' . (int) $idLang . ' + AND c.`id_parent` = ' . (int) $idParent . ' + ' . ($active ? 'AND `active` = 1' : '') . ' + GROUP BY c.`id_category` + ORDER BY category_shop.`position` ASC ' . ($limit > 0 ? ' LIMIT ' . (int) $start . ',' . (int) $limit : ''); + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query); + Cache::store($cacheId, $result); + + return $result; + } + return Cache::retrieve($cacheId); + } + protected function generateCategoriesMenu($categories, $is_children = 0) { $nodes = [];