From fd4fc745dee4c48dbe1263fd35c6781f73f90648 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kr=C3=B3l?= Date: Mon, 13 Nov 2017 22:53:57 +0100 Subject: [PATCH 1/3] [General] Present admin data in the store --- .../FrequentlyAskedQuestionFixtureFactory.php | 10 +-- src/Repository/BlockRepository.php | 31 +++++++- src/Repository/BlockRepositoryInterface.php | 19 +++-- src/Repository/PageRepository.php | 17 ++-- src/Repository/PageRepositoryInterface.php | 7 +- src/Repository/SectionRepositoryInterface.php | 4 +- src/Resources/config/routing/shop/page.yml | 15 +++- src/Resources/config/routing/shop/section.yml | 10 ++- src/Resources/config/services/twig.yml | 9 --- .../Index/_show.html.twig | 6 ++ .../FrequentlyAskedQuestion/index.html.twig | 6 +- .../views/Shop/Page/Index/_section.html.twig | 2 +- .../views/Shop/Page/Show/_link.html.twig | 1 + src/Resources/views/Shop/Page/_link.html.twig | 1 - src/Resources/views/Shop/Page/index.html.twig | 4 +- .../RenderPageLinkByCodeExtension.php | 79 ------------------- .../SyliusShopBundle/views/layout.html.twig | 18 ++++- tests/Application/app/config/fixtures.yml | 13 +-- 18 files changed, 120 insertions(+), 132 deletions(-) create mode 100644 src/Resources/views/Shop/Page/Show/_link.html.twig delete mode 100644 src/Resources/views/Shop/Page/_link.html.twig delete mode 100644 src/Twig/Extension/RenderPageLinkByCodeExtension.php diff --git a/src/Fixture/Factory/FrequentlyAskedQuestionFixtureFactory.php b/src/Fixture/Factory/FrequentlyAskedQuestionFixtureFactory.php index 6664a9df..93b78587 100644 --- a/src/Fixture/Factory/FrequentlyAskedQuestionFixtureFactory.php +++ b/src/Fixture/Factory/FrequentlyAskedQuestionFixtureFactory.php @@ -68,10 +68,10 @@ public function load(array $data): void if (null !== $fields['number']) { for ($i = 0; $i < $fields['number']; $i++) { - $this->createFrequentlyAskedQuestion(md5(uniqid()), $fields); + $this->createFrequentlyAskedQuestion(md5(uniqid()), $fields, ++$i); } } else { - $this->createFrequentlyAskedQuestion($code, $fields); + $this->createFrequentlyAskedQuestion($code, $fields, $fields['position']); } } } @@ -80,18 +80,18 @@ public function load(array $data): void * @param string $code * @param array $frequentlyAskedQuestionData */ - private function createFrequentlyAskedQuestion(string $code, array $frequentlyAskedQuestionData): void + private function createFrequentlyAskedQuestion(string $code, array $frequentlyAskedQuestionData, int $position): void { /** @var FrequentlyAskedQuestionInterface $frequentlyAskedQuestion */ $frequentlyAskedQuestion = $this->frequentlyAskedQuestionFactory->createNew(); $frequentlyAskedQuestion->setCode($code); $frequentlyAskedQuestion->setEnabled($frequentlyAskedQuestionData['enabled']); - $frequentlyAskedQuestion->setPosition($frequentlyAskedQuestionData['position']); + $frequentlyAskedQuestion->setPosition($position); foreach ($frequentlyAskedQuestionData['translations'] as $localeCode => $translation) { /** @var FrequentlyAskedQuestionTranslationInterface $frequentlyAskedQuestionTranslation */ - $frequentlyAskedQuestionTranslation = $this->frequentlyAskedQuestionFactory->createNew(); + $frequentlyAskedQuestionTranslation = $this->frequentlyAskedQuestionTranslationFactory->createNew(); $frequentlyAskedQuestionTranslation->setLocale($localeCode); $frequentlyAskedQuestionTranslation->setQuestion($translation['question']); diff --git a/src/Repository/BlockRepository.php b/src/Repository/BlockRepository.php index 8aa96b6f..9456bff5 100644 --- a/src/Repository/BlockRepository.php +++ b/src/Repository/BlockRepository.php @@ -25,12 +25,12 @@ class BlockRepository extends EntityRepository implements BlockRepositoryInterfa /** * {@inheritdoc} */ - public function createListQueryBuilder(string $locale): QueryBuilder + public function createListQueryBuilder(string $localeCode): QueryBuilder { return $this->createQueryBuilder('o') ->innerJoin('o.translations', 'translation') - ->where('translation.locale = :locale') - ->setParameter('locale', $locale) + ->where('translation.locale = :localeCode') + ->setParameter('localeCode', $localeCode) ; } @@ -85,13 +85,36 @@ public function findOneByTypeAndContent(string $type, string $content): ?BlockIn /** * {@inheritdoc} */ - public function createShopListQueryBuilder(string $sectionCode): QueryBuilder + public function findBySectionCode(string $sectionCode, string $localeCode): ?array { return $this->createQueryBuilder('o') + ->leftJoin('o.translations', 'translation') ->innerJoin('o.sections', 'section') + ->andWhere('translation.locale = :localeCode') ->andWhere('section.code = :sectionCode') ->andWhere('o.enabled = true') + ->setParameter('localeCode', $localeCode) ->setParameter('sectionCode', $sectionCode) + ->getQuery() + ->getResult() + ; + } + + /** + * {@inheritdoc} + */ + public function findByProductCode(string $productCode, string $localeCode): ?array + { + return $this->createQueryBuilder('o') + ->leftJoin('o.translations', 'translation') + ->innerJoin('o.products', 'products') + ->andWhere('translation.locale = :localeCode') + ->andWhere('product.code = :productCode') + ->andWhere('o.enabled = true') + ->setParameter('localeCode', $localeCode) + ->setParameter('productCode', $productCode) + ->getQuery() + ->getResult() ; } } \ No newline at end of file diff --git a/src/Repository/BlockRepositoryInterface.php b/src/Repository/BlockRepositoryInterface.php index 90065c31..6d998969 100644 --- a/src/Repository/BlockRepositoryInterface.php +++ b/src/Repository/BlockRepositoryInterface.php @@ -22,11 +22,11 @@ interface BlockRepositoryInterface extends RepositoryInterface { /** - * @param string $locale + * @param string $localeCode * * @return QueryBuilder */ - public function createListQueryBuilder(string $locale): QueryBuilder; + public function createListQueryBuilder(string $localeCode): QueryBuilder; /** * @param string $code @@ -53,8 +53,17 @@ public function findOneByTypeAndContent(string $type, string $content): ?BlockIn /** * @param string $sectionCode + * @param string $localeCode * - * @return QueryBuilder + * @return null|BlockInterface[] + */ + public function findBySectionCode(string $sectionCode, string $localeCode): ?array; + + /** + * @param string $productCode + * @param string $localeCode + * + * @return null|BlockInterface[] */ - public function createShopListQueryBuilder(string $sectionCode): QueryBuilder; -} \ No newline at end of file + public function findByProductCode(string $productCode, string $localeCode): ?array; +} diff --git a/src/Repository/PageRepository.php b/src/Repository/PageRepository.php index 3de76c95..a8e70411 100644 --- a/src/Repository/PageRepository.php +++ b/src/Repository/PageRepository.php @@ -36,12 +36,15 @@ public function createListQueryBuilder(string $locale): QueryBuilder /** * {@inheritdoc} */ - public function findEnabledByCode(string $code): ?PageInterface + public function findOneEnabledByCode(string $code, ?string $localeCode): ?PageInterface { return $this->createQueryBuilder('o') - ->where('o.code = :code') + ->leftJoin('o.translations', 'translation') + ->where('translation.locale = :localeCode') + ->andWhere('o.code = :code') ->andWhere('o.enabled = true') ->setParameter('code', $code) + ->setParameter('localeCode', $localeCode) ->getQuery() ->getOneOrNullResult() ; @@ -50,14 +53,14 @@ public function findEnabledByCode(string $code): ?PageInterface /** * {@inheritdoc} */ - public function findEnabledBySlug(string $slug, string $localeCode): ?PageInterface + public function findOneEnabledBySlug(string $slug, ?string $localeCode): ?PageInterface { return $this->createQueryBuilder('o') - ->addSelect('translation') - ->innerJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale') + ->leftJoin('o.translations', 'translation') + ->where('translation.locale = :localeCode') ->andWhere('translation.slug = :slug') ->andWhere('o.enabled = true') - ->setParameter('locale', $localeCode) + ->setParameter('localeCode', $localeCode) ->setParameter('slug', $slug) ->getQuery() ->getOneOrNullResult() @@ -71,7 +74,7 @@ public function createShopListQueryBuilder(string $sectionCode): QueryBuilder { return $this->createQueryBuilder('o') ->innerJoin('o.sections', 'section') - ->andWhere('section.code = :sectionCode') + ->where('section.code = :sectionCode') ->andWhere('o.enabled = true') ->setParameter('sectionCode', $sectionCode) ; diff --git a/src/Repository/PageRepositoryInterface.php b/src/Repository/PageRepositoryInterface.php index 15594777..3e8657df 100644 --- a/src/Repository/PageRepositoryInterface.php +++ b/src/Repository/PageRepositoryInterface.php @@ -30,10 +30,11 @@ public function createListQueryBuilder(string $locale): QueryBuilder; /** * @param string $code + * @param null|string $localeCode * - * @return null|PageInterface + * @return PageInterface|null */ - public function findEnabledByCode(string $code): ?PageInterface; + public function findOneEnabledByCode(string $code, ?string $localeCode): ?PageInterface; /** * @param string $slug @@ -41,7 +42,7 @@ public function findEnabledByCode(string $code): ?PageInterface; * * @return null|PageInterface */ - public function findEnabledBySlug(string $slug, string $localeCode): ?PageInterface; + public function findOneEnabledBySlug(string $slug, ?string $localeCode): ?PageInterface; /** * @param string $sectionCode diff --git a/src/Repository/SectionRepositoryInterface.php b/src/Repository/SectionRepositoryInterface.php index ef87ca8a..5ba548b8 100644 --- a/src/Repository/SectionRepositoryInterface.php +++ b/src/Repository/SectionRepositoryInterface.php @@ -30,7 +30,7 @@ public function createListQueryBuilder(): QueryBuilder; * @param string $phrase * @param string|null $locale * - * @return SectionInterface[] + * @return null|SectionInterface[] */ - public function findByNamePart(string $phrase, ?string $locale = null): array; + public function findByNamePart(string $phrase, ?string $locale = null): ?array; } diff --git a/src/Resources/config/routing/shop/page.yml b/src/Resources/config/routing/shop/page.yml index e7f12678..4d6bd056 100644 --- a/src/Resources/config/routing/shop/page.yml +++ b/src/Resources/config/routing/shop/page.yml @@ -6,11 +6,24 @@ bitbag_shop_page_show: _sylius: template: "@BitBagCmsPlugin/Shop/Page/show.html.twig" repository: - method: findEnabledBySlug + method: findOneEnabledBySlug arguments: - $slug - "expr:service('sylius.context.locale').getLocaleCode()" +bitbag_shop_page_link_by_code: + path: /page/link/{code} + methods: [GET] + defaults: + _controller: bitbag.controller.page:showAction + _sylius: + template: $template + repository: + method: findOneEnabledByCode + arguments: + - $code + - "expr:service('sylius.context.locale').getLocaleCode()" + bitbag_shop_page_index_by_section_code: path: /pages/{sectionCode} methods: [GET] diff --git a/src/Resources/config/routing/shop/section.yml b/src/Resources/config/routing/shop/section.yml index 29123c37..869172c6 100644 --- a/src/Resources/config/routing/shop/section.yml +++ b/src/Resources/config/routing/shop/section.yml @@ -1,9 +1,11 @@ -bitbag_shop_section_frequently_asked_question_index: - path: /section/faq +bitbag_shop_section_show: + path: /section/{code} methods: [GET] defaults: - _controller: bitbag.controller.section:indexAction + _controller: bitbag.controller.section:showAction _sylius: template: $template repository: - method: findAllWithFrequentlyAskedQuestions + method: findOneByCode + arguments: + - $code diff --git a/src/Resources/config/services/twig.yml b/src/Resources/config/services/twig.yml index 97398372..8376c8d9 100644 --- a/src/Resources/config/services/twig.yml +++ b/src/Resources/config/services/twig.yml @@ -6,14 +6,5 @@ services: - "@bitbag.repository.block" - "@bitbag.resolver.block_template" - "@bitbag.resolver.block_resource" - tags: - - { name: twig.extension } - - bitbag.twig.extension.page_link_by_code: - class: BitBag\CmsPlugin\Twig\Extension\RenderPageLinkByCodeExtension - public: false - arguments: - - "@bitbag.repository.page" - - "@logger" tags: - { name: twig.extension } \ No newline at end of file diff --git a/src/Resources/views/Shop/FrequentlyAskedQuestion/Index/_show.html.twig b/src/Resources/views/Shop/FrequentlyAskedQuestion/Index/_show.html.twig index e69de29b..d392637f 100644 --- a/src/Resources/views/Shop/FrequentlyAskedQuestion/Index/_show.html.twig +++ b/src/Resources/views/Shop/FrequentlyAskedQuestion/Index/_show.html.twig @@ -0,0 +1,6 @@ +

+ {{ frequentlyAskedQuestion.question }} +

+

+ {{ frequentlyAskedQuestion.answer }} +

\ No newline at end of file diff --git a/src/Resources/views/Shop/FrequentlyAskedQuestion/index.html.twig b/src/Resources/views/Shop/FrequentlyAskedQuestion/index.html.twig index 49a9b8ce..1a2f48c6 100644 --- a/src/Resources/views/Shop/FrequentlyAskedQuestion/index.html.twig +++ b/src/Resources/views/Shop/FrequentlyAskedQuestion/index.html.twig @@ -6,9 +6,9 @@
{% if resources|length > 0 %} -
- {% for page in resources.data %} -
+
+ {% for frequentlyAskedQuestion in resources %} +
{% include '@BitBagCmsPlugin/Shop/FrequentlyAskedQuestion/Index/_show.html.twig' %}
{% endfor %} diff --git a/src/Resources/views/Shop/Page/Index/_section.html.twig b/src/Resources/views/Shop/Page/Index/_section.html.twig index 508ad94a..cc29a5b3 100644 --- a/src/Resources/views/Shop/Page/Index/_section.html.twig +++ b/src/Resources/views/Shop/Page/Index/_section.html.twig @@ -1,3 +1,3 @@ -

+

{{ section.name }}

\ No newline at end of file diff --git a/src/Resources/views/Shop/Page/Show/_link.html.twig b/src/Resources/views/Shop/Page/Show/_link.html.twig new file mode 100644 index 00000000..72979c91 --- /dev/null +++ b/src/Resources/views/Shop/Page/Show/_link.html.twig @@ -0,0 +1 @@ +{{ page.name }} \ No newline at end of file diff --git a/src/Resources/views/Shop/Page/_link.html.twig b/src/Resources/views/Shop/Page/_link.html.twig deleted file mode 100644 index 7049dc69..00000000 --- a/src/Resources/views/Shop/Page/_link.html.twig +++ /dev/null @@ -1 +0,0 @@ -{{ page.name }} \ No newline at end of file diff --git a/src/Resources/views/Shop/Page/index.html.twig b/src/Resources/views/Shop/Page/index.html.twig index 08dacb57..a8490e5d 100644 --- a/src/Resources/views/Shop/Page/index.html.twig +++ b/src/Resources/views/Shop/Page/index.html.twig @@ -3,12 +3,12 @@ {% extends '@SyliusShop/layout.html.twig' %} {% block content %} + {{ render(path('bitbag_shop_section_show', {'code' : app.request.get('sectionCode'), 'template' : '@BitBagCmsPlugin/Shop/Page/Index/_section.html.twig'})) }}
- {{ render(path('bitbag_shop_section_show', {'code' : app.request.get('sectionCode'), 'template' : '@BitBagCmsPlugin/Shop/Page/Index/_section.html.twig'})) }} {% if resources.data|length > 0 %} -
+
{% for page in resources.data %}
diff --git a/src/Twig/Extension/RenderPageLinkByCodeExtension.php b/src/Twig/Extension/RenderPageLinkByCodeExtension.php deleted file mode 100644 index 9d19e1d6..00000000 --- a/src/Twig/Extension/RenderPageLinkByCodeExtension.php +++ /dev/null @@ -1,79 +0,0 @@ - - */ -final class RenderPageLinkByCodeExtension extends \Twig_Extension -{ - const PAGE_LINK_TEMPLATE = 'BitBagCmsPlugin:Shop:Page:_link.html.twig'; - - /** - * @var PageRepositoryInterface - */ - private $pageRepository; - - /** - * @var LoggerInterface - */ - private $logger; - - /** - * @param PageRepositoryInterface $pageRepository - * @param LoggerInterface $logger - */ - public function __construct( - PageRepositoryInterface $pageRepository, - LoggerInterface $logger - ) - { - $this->pageRepository = $pageRepository; - $this->logger = $logger; - } - - /** - * @return \Twig_SimpleFunction[] - */ - public function getFunctions() - { - return [ - new \Twig_SimpleFunction('bitbag_render_page_link_by_code', [$this, 'renderPageLinkByCode'], ['needs_environment' => true, 'is_safe' => ['html'],]), - ]; - } - - /** - * @param \Twig_Environment $twigEnvironment - * @param string $code - * - * @return null|string - */ - public function renderPageLinkByCode(\Twig_Environment $twigEnvironment, $code) - { - $page = $this->pageRepository->findEnabledByCode($code); - - if (false === $page instanceof PageInterface) { - - $this->logger->warning(sprintf( - 'Page with "%s" code was not found in the database.', - $code - )); - - return null; - } - - return $twigEnvironment->render(self::PAGE_LINK_TEMPLATE, ['page' => $page]); - } -} diff --git a/tests/Application/app/Resources/SyliusShopBundle/views/layout.html.twig b/tests/Application/app/Resources/SyliusShopBundle/views/layout.html.twig index 91ede2dd..eb63ce0e 100644 --- a/tests/Application/app/Resources/SyliusShopBundle/views/layout.html.twig +++ b/tests/Application/app/Resources/SyliusShopBundle/views/layout.html.twig @@ -43,6 +43,20 @@ width: 100%; color: #ffffff; } + + .bitbag-page-item, .bitbag-frequently-asked-question-item { + padding-top: 1rem; + padding-bottom: 1rem; + border-bottom: 1px solid rgba(34, 36, 38, .15); + } + + .bitbag-page-item:hover { + background: rgba(0,0,0,.03); + } + + .italic { + font-style: italic; + } {% include '@SyliusUi/_stylesheets.html.twig' with {'path': 'assets/shop/css/style.css'} %} @@ -80,7 +94,9 @@