diff --git a/composer.json b/composer.json index 346c4a41..e6adebec 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "symfony/validator": "^3.4 | ^4.0", "symfony/serializer": "^3.4 | ^4.0", "symfony/cache": "^3.4 | ^4.0", - "commercetools/php-sdk": "dev-client_factory_refactor", + "commercetools/php-sdk": "dev-develop", "symfony/var-dumper": "^3.4 | ^4.0", "symfony/dotenv": "^3.4 | ^4.0", "symfony/stopwatch": "^3.4 | ^4.0", diff --git a/src/CustomerBundle/Resources/config/services.xml b/src/CustomerBundle/Resources/config/services.xml index fb8985a2..4ecc0c7c 100644 --- a/src/CustomerBundle/Resources/config/services.xml +++ b/src/CustomerBundle/Resources/config/services.xml @@ -52,15 +52,17 @@ + + - + - + diff --git a/src/ExampleBundle/Controller/CatalogController.php b/src/ExampleBundle/Controller/CatalogController.php index 4d49af5f..3b1e725a 100644 --- a/src/ExampleBundle/Controller/CatalogController.php +++ b/src/ExampleBundle/Controller/CatalogController.php @@ -42,7 +42,7 @@ public function __construct(CatalogManager $catalogManager = null, ShoppingListM $this->shoppingListManager = $shoppingListManager; } - public function indexAction(Request $request, $categoryId = null, $productTypeId = null) + public function indexAction(Request $request, $categoryId = null, $productTypeId = null, $categorySlug = null) { $form = $this->createFormBuilder() ->add( @@ -70,16 +70,23 @@ public function indexAction(Request $request, $categoryId = null, $productTypeId $filter = null; if (!is_null($categoryId)) { - $filter['filter.query'][] = Filter::ofName('categories.id')->setValue($categoryId); - $categories = $this->catalogManager->getCategories($request->getLocale()); $category = $categories->getById($categoryId); + + $filter['filter.query'][] = Filter::ofName('categories.id')->setValue($categoryId); } if (!is_null($productTypeId)) { $filter['filter.query'][] = Filter::ofName('productType.id')->setValue($productTypeId); } + if (!is_null($categorySlug)) { + $categories = $this->catalogManager->getCategories($request->getLocale()); + $category = $categories->getBySlug($categorySlug, $request->getLocale()); + + $filter['filter.query'][] = Filter::ofName('categories.id')->setValue($category->getId()); + } + list($products, $facets, $offset) = $this->catalogManager->searchProducts( $request->getLocale(), 12, diff --git a/src/ExampleBundle/Controller/SunriseController.php b/src/ExampleBundle/Controller/SunriseController.php index 8120edd7..b0803ac5 100644 --- a/src/ExampleBundle/Controller/SunriseController.php +++ b/src/ExampleBundle/Controller/SunriseController.php @@ -5,11 +5,20 @@ namespace Commercetools\Symfony\ExampleBundle\Controller; use Commercetools\Core\Model\Cart\Cart; +use Commercetools\Core\Model\Category\Category; +use Commercetools\Core\Model\Category\CategoryCollection; use Commercetools\Symfony\CartBundle\Manager\MeCartManager; use Commercetools\Symfony\CatalogBundle\Manager\CatalogManager; use Commercetools\Symfony\CtpBundle\Model\QueryParams; +use Commercetools\Symfony\CtpBundle\Model\Repository; use Commercetools\Symfony\ExampleBundle\Model\Form\Type\AddToCartType; use Commercetools\Symfony\ExampleBundle\Model\Form\Type\ContactUsType; +use Commercetools\Symfony\ExampleBundle\Model\View\NavMenu; +use Commercetools\Symfony\ExampleBundle\Model\View\Tree; +use Commercetools\Symfony\ExampleBundle\Model\View\Url; +use Commercetools\Symfony\ExampleBundle\Model\ViewDataCollection; +use Psr\Cache\CacheItemInterface; +use Psr\Cache\CacheItemPoolInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -25,13 +34,18 @@ class SunriseController extends AbstractController */ private $cartManager; + /** @var CacheItemPoolInterface */ + private $cache; + /** * CartController constructor. + * @param CacheItemPoolInterface $cache * @param CatalogManager $catalogManager * @param MeCartManager $cartManager */ - public function __construct(CatalogManager $catalogManager, MeCartManager $cartManager) + public function __construct(CacheItemPoolInterface $cache, CatalogManager $catalogManager, MeCartManager $cartManager) { + $this->cache = $cache; $this->catalogManager = $catalogManager; $this->cartManager = $cartManager; } @@ -48,7 +62,7 @@ public function faqAction() public function helpAction() { - return $this->render('@Example/home.html.twig'); + return $this->render('@Example/faq.html.twig'); } public function locateStoreAction() @@ -69,14 +83,13 @@ public function contactAction() public function getNavMenuAction(Request $request, $sort = 'id asc') { $params = QueryParams::of()->add('sort', $sort); + $params->add('limit', 500); $categories = $this->catalogManager->getCategories($request->getLocale(), $params); + $catMenu = $this->getNavMenu($request->getLocale(), $categories); return $this->render('@Example/partials/common/nav-menu.html.twig', [ - 'navMenu' => [ - 'new' => true, - 'categories' => $categories - ] + 'navMenu' => $catMenu ]); } @@ -88,4 +101,75 @@ public function getMiniCartAction(Request $request) 'miniCart' => $cart ]); } + + protected function getNavMenu($locale, CategoryCollection $categoriesCollection) + { + $navMenu = new NavMenu(); + + $cacheKey = 'category-menu-' . $locale; + if ($this->cache->hasItem($cacheKey)) { + /** + * @var CacheItemInterface $item + */ + $item = $this->cache->getItem($cacheKey); + $categoryMenu = $item->get(); + } else { + $categoryMenu = new ViewDataCollection(); + $roots = $this->sortCategoriesByOrderHint($categoriesCollection->getRoots()); + + foreach ($roots as $root) { + /** + * @var Category $root + */ + $menuEntry = new Tree( + (string)$root->getName(), + $this->generateUrl('_ctp_example_products_of_category_with_slug', ['categorySlug' => $root->getSlug()]) + ); +// if ($root->getSlug() == $this->config['sunrise.sale.slug']) { + if ($root->getSlug() == '/sale') { + $menuEntry->sale = true; + } + + $subCategories = $this->sortCategoriesByOrderHint($categoriesCollection->getByParent($root->getId())); + foreach ($subCategories as $children) { + /** + * @var Category $children + */ + $childrenEntry = new Tree( + (string)$children->getName(), + $this->generateUrl('_ctp_example_products_of_category_with_slug', ['categorySlug' => $children->getSlug()]) + ); + + $subChildCategories = $this->sortCategoriesByOrderHint($categoriesCollection->getByParent($children->getId())); + foreach ($subChildCategories as $subChild) { + /** + * @var Category $subChild + */ + $childrenSubEntry = new Url( + (string)$subChild->getName(), + $this->generateUrl('_ctp_example_products_of_category_with_slug', ['categorySlug' => $subChild->getSlug()]) + ); + $childrenEntry->addNode($childrenSubEntry); + } + $menuEntry->addNode($childrenEntry); + } + $categoryMenu->add($menuEntry); + } + $categoryMenu = $categoryMenu->toArray(); + $item = $this->cache->getItem($cacheKey)->set($categoryMenu)->expiresAfter(Repository::CACHE_TTL); + $this->cache->save($item); + } + $navMenu->categories = $categoryMenu; + + return $navMenu; + } + + protected function sortCategoriesByOrderHint($categories) + { + usort($categories, function (Category $a, Category $b) { + return $a->getOrderHint() > $b->getOrderHint(); + }); + + return $categories; + } } diff --git a/src/ExampleBundle/Model/View/NavMenu.php b/src/ExampleBundle/Model/View/NavMenu.php new file mode 100644 index 00000000..f3f98779 --- /dev/null +++ b/src/ExampleBundle/Model/View/NavMenu.php @@ -0,0 +1,12 @@ +children = new ViewDataCollection(); + } + + public function addNode(Url $url) + { + $this->children->add($url); + } +} diff --git a/src/ExampleBundle/Model/View/Url.php b/src/ExampleBundle/Model/View/Url.php new file mode 100644 index 00000000..1f46866d --- /dev/null +++ b/src/ExampleBundle/Model/View/Url.php @@ -0,0 +1,19 @@ +text = $text; + $this->url = $url; + } +} diff --git a/src/ExampleBundle/Resources/config/routing.yml b/src/ExampleBundle/Resources/config/routing.yml index 02d4f01c..2e79f44b 100644 --- a/src/ExampleBundle/Resources/config/routing.yml +++ b/src/ExampleBundle/Resources/config/routing.yml @@ -7,15 +7,19 @@ _ctp_example_catalog: defaults: { _controller: Commercetools\Symfony\ExampleBundle\Controller\CatalogController::indexAction } _ctp_example_product: - path: /product/slug/{slug} + path: /product/slug-{slug} defaults: { _controller: Commercetools\Symfony\ExampleBundle\Controller\CatalogController::detailBySlugAction } _ctp_example_product_by_id: - path: /product/id/{id} + path: /product/id-{id} defaults: { _controller: Commercetools\Symfony\ExampleBundle\Controller\CatalogController::detailByIdAction } _ctp_example_products_of_category: - path: /products/category/{categoryId} + path: /products/catid-{categoryId} + defaults: { _controller: Commercetools\Symfony\ExampleBundle\Controller\CatalogController::indexAction } + +_ctp_example_products_of_category_with_slug: + path: /products/category-{categorySlug}/ defaults: { _controller: Commercetools\Symfony\ExampleBundle\Controller\CatalogController::indexAction } _ctp_example_product_categories: @@ -27,7 +31,7 @@ _ctp_example_productTypes: defaults: { _controller: Commercetools\Symfony\ExampleBundle\Controller\CatalogController::getProductTypesAction } _ctp_example_products_of_productType: - path: /products/type/{productTypeId} + path: /products/type-{productTypeId} defaults: { _controller: Commercetools\Symfony\ExampleBundle\Controller\CatalogController::indexAction } login: diff --git a/src/ExampleBundle/Resources/views/partials/catalog/pop/filters-sidebar.html.twig b/src/ExampleBundle/Resources/views/partials/catalog/pop/filters-sidebar.html.twig index a5927778..8169d9c2 100644 --- a/src/ExampleBundle/Resources/views/partials/catalog/pop/filters-sidebar.html.twig +++ b/src/ExampleBundle/Resources/views/partials/catalog/pop/filters-sidebar.html.twig @@ -9,7 +9,7 @@

{{ 'filters.title'|trans([], 'catalog') }}

- {% include '@Example/partials/catalog/pop/facets.html.twig' with {'facets':content.facets} %} + {% include '@Example/partials/catalog/pop/facets.html.twig' with {'facets':facets} %} diff --git a/src/ExampleBundle/Resources/views/partials/common/nav-menu.html.twig b/src/ExampleBundle/Resources/views/partials/common/nav-menu.html.twig index 05c3e110..30bc2c27 100644 --- a/src/ExampleBundle/Resources/views/partials/common/nav-menu.html.twig +++ b/src/ExampleBundle/Resources/views/partials/common/nav-menu.html.twig @@ -10,13 +10,9 @@ {% for category in navMenu.categories %}