Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sylius 20 adaptation #46

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"license": "MIT",
"require": {
"php": "^8.0",
"sylius/sylius": ">=1.11 <1.14"
"sylius/sylius": "^2.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.16",
Expand Down
106 changes: 0 additions & 106 deletions src/Controller/MenuItemResourceController.php

This file was deleted.

92 changes: 92 additions & 0 deletions src/DataProvider/Tree/MenuItemTreeProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/**
* @copyright C UAB NFQ Technologies
*
* This Software is the property of NFQ Technologies
* and is protected by copyright law – it is NOT Freeware.
*
* Any unauthorized use of this software without a valid license key
* is a violation of the license agreement and will be prosecuted by
* civil and criminal law.
*
* Contact UAB NFQ Technologies:
* E-mail: [email protected]
* http://www.nfq.lt
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusMenuPlugin\DataProvider\Tree;

use Doctrine\Common\Collections\Criteria;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManagerInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Sylius\Component\Locale\Context\LocaleNotFoundException;
use Sylius\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
use MonsieurBiz\SyliusMenuPlugin\Entity\Menu;

class MenuItemTreeProvider
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly LocaleContextInterface $localeContext,
private readonly TranslationLocaleProviderInterface $translationLocaleProvider,
) {
}

public function getArrayResult(Menu $menu): array
{
$fallbackLocale = $this->translationLocaleProvider->getDefaultLocaleCode();

try {
$currentLocale = $this->localeContext->getLocaleCode();
} catch (LocaleNotFoundException) {
$currentLocale = $fallbackLocale;
}

$queryBuilder = $this->entityManager->getConnection()->createQueryBuilder();

$queryBuilder
->select([
'mni.id as id',
'mni.parent_id as parent_id',
'mm.id as menu_id',
'COALESCE(current_translation.label, fallback_translation.label) as name',
])
->from('monsieurbiz_menu_item', 'mni')
->join(
'mni',
'monsieurbiz_menu',
'mm',
'mni.menu_id = mm.id'
)
->leftJoin(
'mni',
'monsieurbiz_menu_item_translation',
'current_translation',
(string) $queryBuilder->expr()->and(
$queryBuilder->expr()->eq('current_translation.translatable_id', 'mni.id'),
$queryBuilder->expr()->eq('current_translation.locale', ':currentLocale'),
),
)
->leftJoin(
'mni',
'monsieurbiz_menu_item_translation',
'fallback_translation',
(string) $queryBuilder->expr()->and(
$queryBuilder->expr()->eq('fallback_translation.translatable_id', 'mni.id'),
$queryBuilder->expr()->eq('fallback_translation.locale', ':fallbackLocale'),
),
)
->where('mni.menu_id = :menu')
->orderBy('mni.parent_id', Criteria::DESC)
->addOrderBy('mni.position', Criteria::ASC)
->setParameter('menu', $menu->getId(), Types::INTEGER)
->setParameter('currentLocale', $currentLocale, Types::STRING)
->setParameter('fallbackLocale', $fallbackLocale, Types::STRING);

return $queryBuilder->executeQuery()->fetchAllAssociative();
}
}
2 changes: 1 addition & 1 deletion src/EventListener/AdminMenuListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function addAdminMenuItem(MenuBuilderEvent $event): void
{
$menu = $event->getMenu();
if (null !== $content = $menu->getChild('configuration')) {
$content->addChild('app-menu', ['route' => 'monsieurbiz_menu_admin_menu_index'])
$content->addChild('menu_type', ['route' => 'monsieurbiz_menu_admin_menu_index'])
->setLabel('monsieurbiz_menu.ui.menus')
->setLabelAttribute('icon', 'sitemap')
;
Expand Down
25 changes: 25 additions & 0 deletions src/Exception/IndexExceededException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* @copyright C UAB NFQ Technologies
*
* This Software is the property of NFQ Technologies
* and is protected by copyright law – it is NOT Freeware.
*
* Any unauthorized use of this software without a valid license key
* is a violation of the license agreement and will be prosecuted by
* civil and criminal law.
*
* Contact UAB NFQ Technologies:
* E-mail: [email protected]
* http://www.nfq.lt
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusMenuPlugin\Exception;

class IndexExceededException extends \Exception
{

}
107 changes: 107 additions & 0 deletions src/Manager/MenuPositionHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

/**
* @copyright C UAB NFQ Technologies
*
* This Software is the property of NFQ Technologies
* and is protected by copyright law – it is NOT Freeware.
*
* Any unauthorized use of this software without a valid license key
* is a violation of the license agreement and will be prosecuted by
* civil and criminal law.
*
* Contact UAB NFQ Technologies:
* E-mail: [email protected]
* http://www.nfq.lt
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusMenuPlugin\Manager;

use Doctrine\ORM\EntityManagerInterface;
use MonsieurBiz\SyliusMenuPlugin\Entity\MenuItemInterface;
use MonsieurBiz\SyliusMenuPlugin\Entity\MenuItem;
use MonsieurBiz\SyliusMenuPlugin\Exception\IndexExceededException;

class MenuPositionHandler
{
public const MOVE_UP = 'up';

public const MOVE_DOWN = 'down';

public function __construct(
private readonly EntityManagerInterface $entityManager,
) {
}

public function moveUp(MenuItem $menuItem): void
{
try {
$this->move($menuItem, self::MOVE_UP);
} catch (IndexExceededException) {
}
}

public function moveDown(MenuItem $menuItem)
{
try {
$this->move($menuItem, self::MOVE_DOWN);
} catch (IndexExceededException) {
}
}

private function move(MenuItem $menuItem, string $direction): void
{
$items = $this->getItems($menuItem);

$index = array_search($menuItem, $items, true);
if (\is_int($index)) {
$indexToGo = $this->getNewIndex($index, \count($items) - 1, $direction);

array_splice($items, $index, 1);
array_splice($items, $indexToGo, 0, [$menuItem]);

$position = 1;
foreach ($items as $item) {
$item->setPosition($position++);
}
}
}

private function getNewIndex(int $index, int $max, string $direction): int
{
$indexToGo = $index + (self::MOVE_UP === $direction ? -1 : 1);

if ($indexToGo < 0 || $indexToGo > $max) {
throw new IndexExceededException();
}

return $indexToGo;
}

/**
* @return MenuItemInterface[]
*/
private function getItems(MenuItemInterface $resource): array
{
$items = [];
$parent = $resource->getParent();

if (null !== $parent) {
$resourceItems = $parent->getItems();
if (null !== $resourceItems) {
$items = $resourceItems->toArray();
}

return $items;
}

$menu = $resource->getMenu();
if (null !== $menu) {
$items = $menu->getFirstLevelItems();
}

return $items;
}
}
2 changes: 1 addition & 1 deletion src/Provider/ProductUrlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ProductUrlProvider extends AbstractUrlProvider

protected string $code = self::PROVIDER_CODE;

protected string $icon = 'cube';
protected string $icon = 'tabler:brand-producthunt';

protected int $priority = 50;

Expand Down
2 changes: 1 addition & 1 deletion src/Provider/TaxonUrlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TaxonUrlProvider extends AbstractUrlProvider

protected string $code = self::PROVIDER_CODE;

protected string $icon = 'folder';
protected string $icon = 'tabler:letter-t';

protected int $priority = 100;

Expand Down
2 changes: 2 additions & 0 deletions src/Repository/MenuItemRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

namespace MonsieurBiz\SyliusMenuPlugin\Repository;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\Query\Expr;
use MonsieurBiz\SyliusMenuPlugin\Entity\MenuInterface;
use MonsieurBiz\SyliusMenuPlugin\Entity\MenuItem;
use MonsieurBiz\SyliusMenuPlugin\Entity\MenuItemInterface;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;

Expand Down
2 changes: 2 additions & 0 deletions src/Repository/MenuRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

namespace MonsieurBiz\SyliusMenuPlugin\Repository;

use Doctrine\Common\Persistence\ManagerRegistry;
use MonsieurBiz\SyliusMenuPlugin\Entity\MenuInterface;
use MonsieurBiz\SyliusMenuPlugin\Entity\Menu;
use MonsieurBiz\SyliusMenuPlugin\Hydrator\MenuTreeHydrator;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;

Expand Down
1 change: 0 additions & 1 deletion src/Resources/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ imports:
- { resource: "services.yaml" }
- { resource: "sylius/resources.yaml" }
- { resource: "sylius/grids.yaml" }
- { resource: "sylius/ui.yaml" }
- { resource: "sylius/fixtures.yaml"}

sylius_labs_doctrine_migrations_extra:
Expand Down
Loading