Skip to content

Commit

Permalink
[shopsys] move ready category seo mix from project-base to packages (…
Browse files Browse the repository at this point in the history
…#3494)
  • Loading branch information
grossmannmartin authored Nov 4, 2024
2 parents 83c7dc6 + 01d348e commit 80a2050
Show file tree
Hide file tree
Showing 68 changed files with 3,724 additions and 240 deletions.
20 changes: 20 additions & 0 deletions assets/js/admin/components/CategoryWithSeoMixDeleteConfirm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Register from '../../common/utils/Register';
import Window from '../utils/Window';
import Translator from 'bazinga-translator';

export default class CategoryWithSeoMixDeleteConfirm {
static init ($container) {
$container.filterAllNodes('.js-category-with-seomix-delete-confirm').click((event) => {
// eslint-disable-next-line no-new
new Window({
content: Translator.trans('Do you really want to remove this category even with their SEO mixes?'),
buttonCancel: true,
buttonContinue: true,
textContinue: Translator.trans('Delete category with SEO mix'),
urlContinue: $(event.currentTarget).data('delete-url')
});
});
}
}

(new Register()).registerCallback(CategoryWithSeoMixDeleteConfirm.init, 'CategoryWithSeoMixDeleteConfirm.init');
1 change: 1 addition & 0 deletions assets/js/admin/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import './AdvancedSearch';
import './AjaxConfirm';
import './Article';
import './CategoryDeleteConfirm';
import './CategoryWithSeoMixDeleteConfirm';
import './CategoryTreeForm';
import './CategoryTreeSorting';
import './CharactersCounter';
Expand Down
81 changes: 77 additions & 4 deletions src/Component/Router/FriendlyUrl/FriendlyUrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@
namespace Shopsys\FrameworkBundle\Component\Router\FriendlyUrl;

use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Model\CategorySeo\Exception\ReadyCategorySeoMixNotFoundException;
use Shopsys\FrameworkBundle\Model\CategorySeo\ReadyCategorySeoMixRepository;
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\RouteCollection;

class FriendlyUrlMatcher
{
/**
* @param \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlRepository $friendlyUrlRepository
* @param \Shopsys\FrameworkBundle\Model\CategorySeo\ReadyCategorySeoMixRepository $readyCategorySeoMixRepository
*/
public function __construct(protected readonly FriendlyUrlRepository $friendlyUrlRepository)
{
public function __construct(
protected readonly FriendlyUrlRepository $friendlyUrlRepository,
protected readonly ReadyCategorySeoMixRepository $readyCategorySeoMixRepository,
) {
}

/**
Expand All @@ -23,7 +29,7 @@ public function __construct(protected readonly FriendlyUrlRepository $friendlyUr
* @param \Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig $domainConfig
* @return array
*/
public function match($pathinfo, RouteCollection $routeCollection, DomainConfig $domainConfig)
public function match(string $pathinfo, RouteCollection $routeCollection, DomainConfig $domainConfig): array
{
$pathWithoutSlash = substr($pathinfo, 1);
$friendlyUrl = $this->friendlyUrlRepository->findByDomainIdAndSlug($domainConfig->getId(), $pathWithoutSlash);
Expand All @@ -32,22 +38,89 @@ public function match($pathinfo, RouteCollection $routeCollection, DomainConfig
throw new ResourceNotFoundException();
}

$matchedParameters = [];

if ($friendlyUrl->getRedirectTo() !== null) {
$matchedParameters['_route'] = $friendlyUrl->getRouteName();
$matchedParameters['_controller'] = RedirectController::class . '::urlRedirectAction';
$matchedParameters['path'] = $friendlyUrl->getRedirectTo();
$matchedParameters['permanent'] = $friendlyUrl->getRedirectCode() !== 302;
$matchedParameters['id'] = $friendlyUrl->getEntityId();

return $matchedParameters;
}

$route = $routeCollection->get($friendlyUrl->getRouteName());

if ($route === null) {
throw new ResourceNotFoundException();
}

$matchedParameters = $route->getDefaults();

if ($friendlyUrl->getRouteName() === 'front_category_seo' && $friendlyUrl->isMain() === false) {
return $this->getMatchedParametersForNonMainFrontCategorySeoFriendlyUrl($friendlyUrl, $matchedParameters);
}

if ($friendlyUrl->getRouteName() === 'front_category_seo') {
return $this->getMatchedParametersForMainFrontCategorySeoFriendlyUrl($friendlyUrl, $matchedParameters);
}

$matchedParameters['_route'] = $friendlyUrl->getRouteName();
$matchedParameters['id'] = $friendlyUrl->getEntityId();

if (!$friendlyUrl->isMain()) {
$matchedParameters['_controller'] = 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::redirectAction';
$matchedParameters['_controller'] = RedirectController::class . '::urlRedirectAction';
$matchedParameters['route'] = $friendlyUrl->getRouteName();
$matchedParameters['permanent'] = true;
}

return $matchedParameters;
}

/**
* @param \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrl $friendlyUrl
* @param array $matchedParameters
* @return array
*/
protected function getMatchedParametersForMainFrontCategorySeoFriendlyUrl(
FriendlyUrl $friendlyUrl,
array $matchedParameters,
): array {
$readyCategorySeoMixId = $friendlyUrl->getEntityId();
$readyCategorySeoMix = $this->readyCategorySeoMixRepository->findById($readyCategorySeoMixId);

if ($readyCategorySeoMix === null) {
throw new ReadyCategorySeoMixNotFoundException(sprintf('ReadyCategorySeoMix with ID %s not found', $readyCategorySeoMixId));
}

$matchedParameters['_route'] = 'front_product_list';
$matchedParameters['id'] = $readyCategorySeoMix->getCategory()->getId();
$matchedParameters['readyCategorySeoMixId'] = $readyCategorySeoMixId;

return $matchedParameters;
}

/**
* @param \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrl $friendlyUrl
* @param array $matchedParameters
* @return array
*/
protected function getMatchedParametersForNonMainFrontCategorySeoFriendlyUrl(
FriendlyUrl $friendlyUrl,
array $matchedParameters,
): array {
$readyCategorySeoMixId = $friendlyUrl->getEntityId();

$matchedParameters['_controller'] = 'FrameworkBundle:Redirect:redirect';

// Both are necessary
$matchedParameters['route'] = $friendlyUrl->getRouteName();
$matchedParameters['_route'] = $friendlyUrl->getRouteName();

$matchedParameters['id'] = $readyCategorySeoMixId;
$matchedParameters['permanent'] = true;

return $matchedParameters;
}
}
2 changes: 2 additions & 0 deletions src/Component/Router/FriendlyUrl/FriendlyUrlRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Shopsys\FrameworkBundle\Model\Blog\Article\BlogArticle;
use Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategory;
use Shopsys\FrameworkBundle\Model\Category\Category;
use Shopsys\FrameworkBundle\Model\CategorySeo\ReadyCategorySeoMix;
use Shopsys\FrameworkBundle\Model\Product\Brand\Brand;
use Shopsys\FrameworkBundle\Model\Product\Flag\Flag;
use Shopsys\FrameworkBundle\Model\Product\Product;
Expand Down Expand Up @@ -281,6 +282,7 @@ public function getRouteNameToEntityMap(): array
'front_stores_detail' => $this->entityNameResolver->resolve(Store::class),
'front_flag_detail' => $this->entityNameResolver->resolve(Flag::class),
'front_page_seo' => $this->entityNameResolver->resolve(SeoPage::class),
'front_category_seo' => $this->entityNameResolver->resolve(ReadyCategorySeoMix::class),
];
}
}
14 changes: 7 additions & 7 deletions src/Component/Router/FriendlyUrl/FriendlyUrlRouterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@
namespace Shopsys\FrameworkBundle\Component\Router\FriendlyUrl;

use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Model\CategorySeo\ReadyCategorySeoMixRepository;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Contracts\Cache\CacheInterface;

class FriendlyUrlRouterFactory
{
protected string $friendlyUrlRouterResourceFilepath;

/**
* @param mixed $friendlyUrlRouterResourceFilepath
* @param string $friendlyUrlRouterResourceFilepath
* @param \Symfony\Component\Config\Loader\LoaderInterface $configLoader
* @param \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlRepository $friendlyUrlRepository
* @param \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlCacheKeyProvider $friendlyUrlCacheKeyProvider
* @param \Symfony\Contracts\Cache\CacheInterface $mainFriendlyUrlSlugCache
* @param \Shopsys\FrameworkBundle\Model\CategorySeo\ReadyCategorySeoMixRepository $readyCategorySeoMixRepository
*/
public function __construct(
$friendlyUrlRouterResourceFilepath,
protected string $friendlyUrlRouterResourceFilepath,
protected readonly LoaderInterface $configLoader,
protected readonly FriendlyUrlRepository $friendlyUrlRepository,
protected readonly FriendlyUrlCacheKeyProvider $friendlyUrlCacheKeyProvider,
protected readonly CacheInterface $mainFriendlyUrlSlugCache,
protected readonly ReadyCategorySeoMixRepository $readyCategorySeoMixRepository,
) {
$this->friendlyUrlRouterResourceFilepath = $friendlyUrlRouterResourceFilepath;
}

/**
* @param \Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig $domainConfig
* @param \Symfony\Component\Routing\RequestContext $context
* @return \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlRouter
*/
public function createRouter(DomainConfig $domainConfig, RequestContext $context)
public function createRouter(DomainConfig $domainConfig, RequestContext $context): FriendlyUrlRouter
{
return new FriendlyUrlRouter(
$context,
Expand All @@ -46,7 +46,7 @@ public function createRouter(DomainConfig $domainConfig, RequestContext $context
$this->friendlyUrlCacheKeyProvider,
$this->mainFriendlyUrlSlugCache,
),
new FriendlyUrlMatcher($this->friendlyUrlRepository),
new FriendlyUrlMatcher($this->friendlyUrlRepository, $this->readyCategorySeoMixRepository),
$domainConfig,
$this->friendlyUrlRouterResourceFilepath,
);
Expand Down
12 changes: 10 additions & 2 deletions src/Controller/Admin/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
use Nette\Utils\Json;
use Shopsys\FrameworkBundle\Component\Domain\AdminDomainFilterTabsFacade;
use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Component\Form\FormBuilderHelper;
use Shopsys\FrameworkBundle\Component\Router\Security\Annotation\CsrfProtection;
use Shopsys\FrameworkBundle\Form\Admin\Category\CategoryFormType;
use Shopsys\FrameworkBundle\Model\AdminNavigation\BreadcrumbOverrider;
use Shopsys\FrameworkBundle\Model\Category\CategoryDataFactoryInterface;
use Shopsys\FrameworkBundle\Model\Category\CategoryFacade;
use Shopsys\FrameworkBundle\Model\Category\Exception\CategoryNotFoundException;
use Shopsys\FrameworkBundle\Model\CategorySeo\ReadyCategorySeoMixFacade;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
Expand All @@ -25,13 +27,17 @@ class CategoryController extends AdminBaseController
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
* @param \Shopsys\FrameworkBundle\Model\AdminNavigation\BreadcrumbOverrider $breadcrumbOverrider
* @param \Shopsys\FrameworkBundle\Component\Domain\AdminDomainFilterTabsFacade $adminDomainFilterTabsFacade
* @param \Shopsys\FrameworkBundle\Component\Form\FormBuilderHelper $formBuilderHelper
* @param \Shopsys\FrameworkBundle\Model\CategorySeo\ReadyCategorySeoMixFacade $categorySeoMixFacade
*/
public function __construct(
protected readonly CategoryFacade $categoryFacade,
protected readonly CategoryDataFactoryInterface $categoryDataFactory,
protected readonly Domain $domain,
protected readonly BreadcrumbOverrider $breadcrumbOverrider,
protected readonly AdminDomainFilterTabsFacade $adminDomainFilterTabsFacade,
protected readonly FormBuilderHelper $formBuilderHelper,
protected readonly ReadyCategorySeoMixFacade $categorySeoMixFacade,
) {
}

Expand Down Expand Up @@ -141,15 +147,17 @@ public function listAction(Request $request): Response

return $this->render('@ShopsysFramework/Admin/Content/Category/list.html.twig', [
'categoriesWithPreloadedChildren' => $categoriesWithPreloadedChildren,
'domainFilterNamespace' => $domainFilterNamespace,
'isForAllDomains' => ($selectedDomainId === null),
'domainFilterNamespace' => $domainFilterNamespace,
'disabledFormFields' => $this->formBuilderHelper->hasFormDisabledFields(),
'allCategoryIdsInSeoMixes' => $this->categorySeoMixFacade->getAllCategoryIdsInSeoMixes(),
]);
}

/**
* @see node_modules/@shopsys/framework/js/admin/components/CategoryTreeSorting.js
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Response
* @see node_modules/@shopsys/framework/js/admin/components/CategoryTreeSorting.js
*/
#[Route(path: '/category/apply-sorting/', methods: ['post'])]
public function applySortingAction(Request $request): Response
Expand Down
Loading

0 comments on commit 80a2050

Please sign in to comment.