Skip to content

Commit

Permalink
WIP: fix navigation menu
Browse files Browse the repository at this point in the history
  • Loading branch information
nikossvnk committed Sep 13, 2019
1 parent 17a207b commit 450b3b0
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 25 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 4 additions & 2 deletions src/CustomerBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@
<argument id="session" type="service"/>
</service>

<service id="commercetools.http.providerfactory" class="Commercetools\Core\Client\ProviderFactory" />

<service id="Commercetools\Core\Client\OAuth\TokenStorageProvider">
<factory service="commercetools.http.clientfactory" method="createTokenStorageProviderFor" />
<factory service="commercetools.http.providerfactory" method="createTokenStorageProviderFor" />
<argument type="service" id="commercetools.client.me.config"/>
<argument type="service" id="commercetools.oauth.client" />
<argument type="service" id="Commercetools\Symfony\CtpBundle\Service\SessionTokenStorage"/>
</service>

<service id="Commercetools\Core\Client\OAuth\PasswordFlowTokenProvider">
<factory service="commercetools.http.clientfactory" method="createPasswordFlowProviderFor" />
<factory service="commercetools.http.providerfactory" method="createPasswordFlowProviderFor" />
<argument type="service" id="commercetools.client.me.config"/>
<argument type="service" id="commercetools.oauth.client"/>
<argument type="service" id="Commercetools\Symfony\CtpBundle\Service\SessionTokenStorage" />
Expand Down
13 changes: 10 additions & 3 deletions src/ExampleBundle/Controller/CatalogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
96 changes: 90 additions & 6 deletions src/ExampleBundle/Controller/SunriseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand All @@ -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()
Expand All @@ -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
]);
}

Expand All @@ -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;
}
}
12 changes: 12 additions & 0 deletions src/ExampleBundle/Model/View/NavMenu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
*/

namespace Commercetools\Symfony\ExampleBundle\Model\View;

use Commercetools\Symfony\ExampleBundle\Model\ViewData;

class NavMenu extends ViewData
{
public $categories;
}
26 changes: 26 additions & 0 deletions src/ExampleBundle/Model/View/Tree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
*/

namespace Commercetools\Symfony\ExampleBundle\Model\View;

use Commercetools\Symfony\ExampleBundle\Model\ViewDataCollection;

class Tree extends Url
{
/**
* @var ViewDataCollection
*/
protected $children;

public function __construct($text, $url)
{
parent::__construct($text, $url);
$this->children = new ViewDataCollection();
}

public function addNode(Url $url)
{
$this->children->add($url);
}
}
19 changes: 19 additions & 0 deletions src/ExampleBundle/Model/View/Url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
*/

namespace Commercetools\Symfony\ExampleBundle\Model\View;

use Commercetools\Symfony\ExampleBundle\Model\ViewData;

class Url extends ViewData
{
public $text;
public $url;

public function __construct($text, $url)
{
$this->text = $text;
$this->url = $url;
}
}
12 changes: 8 additions & 4 deletions src/ExampleBundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<p class="filter-products"><b>{{ 'filters.title'|trans([], 'catalog') }}</b></p>
</div>
</div>
{% include '@Example/partials/catalog/pop/facets.html.twig' with {'facets':content.facets} %}
{% include '@Example/partials/catalog/pop/facets.html.twig' with {'facets':facets} %}
</div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@

{% for category in navMenu.categories %}
<li class="dropdown menu-large">
{# TODO
add
data-toggle="dropdown"
to the following <a> to support parent/children categories
#}
<a href="{{ path('_ctp_example_products_of_category', {'categoryId': category.id}) }}" class="dropdown-toggle {% if category.sale %}sale icon-ribbon{% endif %}">
{{ category.name }}
{# <a href="{{ path('_ctp_example_products_of_category', {'categoryId': category.id}) }}" class="dropdown-toggle {% if category.sale %}sale icon-ribbon{% endif %}" data-toggle="dropdown">#}
<a href="{{ category.url }}" class="dropdown-toggle {% if category.sale %}sale icon-ribbon{% endif %}" data-toggle="dropdown">
{{ category.text }}
<img class="mobile-plus-content visible-xs" src="{{ asset('bundles/example/img/plus79.png') }}" alt="{{ 'more'|trans([], 'main') }}">
</a>
{% include '@Example/partials/common/nav-menu-category.html.twig' with {'category':category} %}
Expand Down
7 changes: 6 additions & 1 deletion src/ExampleBundle/Tests/Controller/SunriseControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Commercetools\Symfony\CartBundle\Manager\MeCartManager;
use Commercetools\Symfony\CatalogBundle\Manager\CatalogManager;
use Commercetools\Symfony\ExampleBundle\Controller\SunriseController;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Twig\Environment;

Expand All @@ -15,11 +17,14 @@ class SunriseControllerTest extends WebTestCase
private $catalogManager;
/** @var MeCartManager */
private $meCartManager;
/** @var CacheItemPoolInterface */
private $cache;

public function setUp(): void
{
$this->catalogManager = $this->prophesize(CatalogManager::class);
$this->meCartManager = $this->prophesize(MeCartManager::class);
$this->cache = $this->prophesize(CacheItemPoolInterface::class);
}

public function testHome()
Expand All @@ -33,7 +38,7 @@ public function testHome()

$twigMock->render('@Example/home.html.twig', [])->shouldBeCalledOnce();

$controller = new SunriseController($this->catalogManager->reveal(), $this->meCartManager->reveal());
$controller = new SunriseController($this->cache->reveal(), $this->catalogManager->reveal(), $this->meCartManager->reveal());
$controller->setContainer($container->reveal());
$controller->homeAction();
}
Expand Down

0 comments on commit 450b3b0

Please sign in to comment.