|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Monsieur Biz's Blog plugin for Sylius. |
| 5 | + * (c) Monsieur Biz <[email protected]> |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace MonsieurBiz\SyliusBlogPlugin\Menu; |
| 13 | + |
| 14 | +use MonsieurBiz\SyliusBlogPlugin\Entity\ArticleInterface; |
| 15 | +use MonsieurBiz\SyliusBlogPlugin\Entity\TagInterface; |
| 16 | +use MonsieurBiz\SyliusBlogPlugin\Repository\TagRepositoryInterface; |
| 17 | +use MonsieurBiz\SyliusMenuPlugin\Provider\AbstractUrlProvider; |
| 18 | +use Symfony\Component\Routing\RouterInterface; |
| 19 | +use Symfony\Contracts\Translation\TranslatorInterface; |
| 20 | +use Webmozart\Assert\Assert; |
| 21 | + |
| 22 | +class CaseStudyListUrlProvider extends AbstractUrlProvider |
| 23 | +{ |
| 24 | + public const PROVIDER_CODE = 'case_study_list'; |
| 25 | + |
| 26 | + protected string $code = self::PROVIDER_CODE; |
| 27 | + |
| 28 | + protected string $icon = 'crosshairs'; |
| 29 | + |
| 30 | + protected int $priority = 30; |
| 31 | + |
| 32 | + public function __construct( |
| 33 | + RouterInterface $router, |
| 34 | + private TagRepositoryInterface $tagRepository, |
| 35 | + private TranslatorInterface $translator |
| 36 | + ) { |
| 37 | + parent::__construct($router); |
| 38 | + } |
| 39 | + |
| 40 | + protected function getResults(string $locale, string $search = ''): iterable |
| 41 | + { |
| 42 | + $queryBuilder = $this->tagRepository->createEnabledListQueryBuilderByType($locale, ArticleInterface::CASE_STUDY_TYPE); |
| 43 | + |
| 44 | + if (!empty($search)) { |
| 45 | + $queryBuilder |
| 46 | + ->andWhere('translation.name LIKE :search OR translation.slug LIKE :search') |
| 47 | + ->setParameter('search', '%' . $search . '%') |
| 48 | + ; |
| 49 | + } |
| 50 | + |
| 51 | + $queryBuilder->setMaxResults($this->getMaxResults()); |
| 52 | + |
| 53 | + /** @phpstan-ignore-next-line */ |
| 54 | + return $queryBuilder->getQuery()->getResult(); |
| 55 | + } |
| 56 | + |
| 57 | + protected function addItemFromResult(object $result, string $locale): void |
| 58 | + { |
| 59 | + Assert::isInstanceOf($result, TagInterface::class); |
| 60 | + /** @var TagInterface $result */ |
| 61 | + $result->setCurrentLocale($locale); |
| 62 | + $this->addItem( |
| 63 | + (string) $result->getName(), |
| 64 | + $this->router->generate('monsieurbiz_case_study_tag_show', ['slug' => $result->getSlug(), '_locale' => $locale]) |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + public function getItems(string $locale, string $search = ''): array |
| 69 | + { |
| 70 | + parent::getItems($locale, $search); |
| 71 | + |
| 72 | + // Add item to link to all articles |
| 73 | + $firstItemLabel = $this->translator->trans('sylius.ui.all', [], null, $locale); |
| 74 | + if (empty($search) || false !== strpos($search, $firstItemLabel)) { |
| 75 | + $this->addItem($firstItemLabel, $this->router->generate('monsieurbiz_case_study_index', ['_locale' => $locale])); |
| 76 | + // Add this last element to the beginning of the array |
| 77 | + $lastElement = array_pop($this->items); |
| 78 | + array_unshift($this->items, $lastElement); |
| 79 | + } |
| 80 | + |
| 81 | + return $this->items; |
| 82 | + } |
| 83 | +} |
0 commit comments