Skip to content

Commit

Permalink
[shopsys] Change blog categories for real root category (#3595)
Browse files Browse the repository at this point in the history
Co-authored-by: Michal Vanek <michal.vanek@shopsys.com>
  • Loading branch information
AmpMVn and Michal Vanek authored Nov 19, 2024
1 parent 804ed76 commit b64f6f6
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 19 deletions.
1 change: 1 addition & 0 deletions assets/js/admin/components/CategoryTreeSorting.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default class CategoryTreeSorting {
helper: 'clone',
opacity: 0.6,
revert: 100,
protectRoot: this.$rootTree.hasClass('js-protect-root'),
change: () => _this.onChange()
});

Expand Down
2 changes: 1 addition & 1 deletion src/Form/Admin/Blog/BlogCategoryFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private function createSettingsGroup(FormBuilderInterface $builder, array $optio
'required' => true,
'choices' => $parentChoices,
'choice_label' => function (BlogCategory $blogCategory) {
$padding = str_repeat("\u{00a0}", ($blogCategory->getLevel() - 1) * 2);
$padding = str_repeat("\u{00a0}", $blogCategory->getLevel() * 2);

return $padding . $blogCategory->getName();
},
Expand Down
53 changes: 53 additions & 0 deletions src/Migrations/Version20241112100245.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Exception;
use Gedmo\Tree\Entity\Repository\NestedTreeRepository;
use Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategory;
use Shopsys\MigrationBundle\Component\Doctrine\Migrations\AbstractMigration;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;

class Version20241112100245 extends AbstractMigration implements ContainerAwareInterface
{
use MultidomainMigrationTrait;

/**
* @param \Doctrine\DBAL\Schema\Schema $schema
*/
public function up(Schema $schema): void
{
$rootBlogCategories = $this->sql('SELECT id FROM blog_categories WHERE parent_id IS NULL AND level = 0')->fetchAllAssociative();

if (count($rootBlogCategories) === 1) {
$blogCategoryId = reset($rootBlogCategories)['id'];
$translation = $this->sql('SELECT 1 FROM blog_category_translations WHERE translatable_id = ?', [$blogCategoryId])->fetchOne();

if ($translation === false) {
$this->sql('UPDATE blog_categories SET parent_id = NULL WHERE parent_id = ?', [$blogCategoryId]);
$this->sql('DELETE FROM blog_category_domains WHERE blog_category_id = ?', [$blogCategoryId]);
$this->sql('DELETE FROM blog_categories WHERE id = ?', [$blogCategoryId]);
$this->sql('UPDATE blog_categories SET level = level -1');
}

$entityManager = $this->container->get('doctrine.orm.default_entity_manager');
$repository = $entityManager->getRepository(BlogCategory::class);

if ($repository instanceof NestedTreeRepository) {
$repository->recover();
}
} elseif (count($rootBlogCategories) > 1) {
throw new Exception('There is more than one root blog category. Please see upgrade notes.');
}
}

/**
* @param \Doctrine\DBAL\Schema\Schema $schema
*/
public function down(Schema $schema): void
{
}
}
4 changes: 1 addition & 3 deletions src/Model/Blog/Category/BlogCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
*/
class BlogCategory extends AbstractTranslatableEntity
{
public const BLOG_MAIN_PAGE_CATEGORY_ID = 2;

/**
* @var int
* @ORM\Column(type="integer")
Expand Down Expand Up @@ -336,7 +334,7 @@ public function createDomains(BlogCategoryData $blogCategoryData): void
*/
public function isMainPage()
{
return $this->id === self::BLOG_MAIN_PAGE_CATEGORY_ID;
return $this->parent === null;
}

/**
Expand Down
26 changes: 19 additions & 7 deletions src/Model/Blog/Category/BlogCategoryFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
use Shopsys\FrameworkBundle\Model\Blog\Article\Elasticsearch\BlogArticleExportQueueFacade;
use Shopsys\FrameworkBundle\Model\Blog\BlogVisibilityRecalculationScheduler;
use Shopsys\FrameworkBundle\Model\Blog\Category\Exception\BlogCategoryNotFoundException;
use Shopsys\FrameworkBundle\Model\Blog\Category\Exception\RootLevelBlogCategoryAlreadyExistsException;

class BlogCategoryFacade
{
protected const INCREMENT_DUE_TO_MISSING_ROOT_CATEGORY = 1;

/**
* @param \Doctrine\ORM\EntityManagerInterface $em
* @param \Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategoryRepository $blogCategoryRepository
Expand Down Expand Up @@ -65,6 +64,10 @@ public function create(BlogCategoryData $blogCategoryData): BlogCategory
$rootCategory = $this->getRootBlogCategory();
$blogCategory = $this->blogCategoryFactory->create($blogCategoryData, $rootCategory);

if ($blogCategory->getParent() === null) {
throw new RootLevelBlogCategoryAlreadyExistsException($rootCategory->getId());
}

$this->em->persist($blogCategory);
$this->em->flush();

Expand Down Expand Up @@ -93,7 +96,7 @@ public function edit(int $blogCategoryId, BlogCategoryData $blogCategoryData): B
$blogCategory = $this->blogCategoryRepository->getById($blogCategoryId);
$blogCategory->edit($blogCategoryData);

if ($blogCategory->getParent() === null) {
if ($blogCategory->getParent() === null && $rootCategory !== $blogCategory) {
$blogCategory->setParent($rootCategory);
}

Expand Down Expand Up @@ -187,6 +190,15 @@ public function getVisibleBlogCategoriesInPathFromRootOnDomain(BlogCategory $blo
return $this->blogCategoryRepository->getVisibleBlogCategoriesInPathFromRootOnDomain($blogCategory, $domainId);
}

/**
* @param int $domainId
* @return \Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategory[]
*/
public function getAllVisibleChildrenWithRootByDomainId(int $domainId): array
{
return $this->blogCategoryRepository->getAllVisibleChildrenWithRootByDomainId($domainId);
}

/**
* @param \Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategory $category
* @param int $domainId
Expand Down Expand Up @@ -307,10 +319,10 @@ public function reorderByNestedSetValues(array $blogCategoriesOrderingData): voi
foreach ($blogCategoriesOrderingData as $categoryOrderingData) {
$query->execute([
'id' => (int)$categoryOrderingData['id'],
'parent' => $categoryOrderingData['parent_id'] ? (int)$categoryOrderingData['parent_id'] : $rootCategoryId,
'level' => $categoryOrderingData['depth'] + static::INCREMENT_DUE_TO_MISSING_ROOT_CATEGORY,
'lft' => $categoryOrderingData['left'] + static::INCREMENT_DUE_TO_MISSING_ROOT_CATEGORY,
'rgt' => $categoryOrderingData['right'] + static::INCREMENT_DUE_TO_MISSING_ROOT_CATEGORY,
'parent' => (int)$categoryOrderingData['id'] === $rootCategoryId ? null : ($categoryOrderingData['parent_id'] ? (int)$categoryOrderingData['parent_id'] : $rootCategoryId),
'level' => $categoryOrderingData['depth'],
'lft' => $categoryOrderingData['left'],
'rgt' => $categoryOrderingData['right'],
]);
}

Expand Down
21 changes: 15 additions & 6 deletions src/Model/Blog/Category/BlogCategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ protected function getAllQueryBuilder(): QueryBuilder
{
return $this->getBlogCategoryRepository()
->createQueryBuilder('bc')
->where('bc.parent IS NOT NULL')
->orderBy('bc.lft');
}

Expand Down Expand Up @@ -111,10 +110,6 @@ public function findById(int $blogCategoryId): ?BlogCategory
/** @var \Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategory $blogCategory */
$blogCategory = $this->getBlogCategoryRepository()->find($blogCategoryId);

if ($blogCategory !== null && $blogCategory->getParent() === null) {
return null;
}

return $blogCategory;
}

Expand Down Expand Up @@ -173,7 +168,6 @@ protected function getPreOrderTreeTraversalForAllBlogCategoriesQueryBuilder(stri
$this->addTranslation($queryBuilder, $locale);

$queryBuilder
->andWhere('bc.level >= 1')
->orderBy('bc.lft');

return $queryBuilder;
Expand Down Expand Up @@ -207,6 +201,21 @@ public function getAllVisibleByDomainIdQueryBuilder(int $domainId): QueryBuilder
return $queryBuilder;
}

/**
* @param int $domainId
* @return \Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategory[]
*/
public function getAllVisibleChildrenWithRootByDomainId(int $domainId): array
{
$queryBuilder = $this->getAllVisibleByDomainIdQueryBuilder($domainId)
->andWhere('bc.parent IS NULL');

$locale = $this->domain->getDomainConfigById($domainId)->getLocale();
$this->addTranslation($queryBuilder, $locale);

return $queryBuilder->getQuery()->execute();
}

/**
* @param \Shopsys\FrameworkBundle\Model\Blog\Category\BlogCategory $blogCategory
* @param int $domainId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected function getFirstLevelBlogCategories(array $blogCategories): array
$firstLevelBlogCategories = [];

foreach ($blogCategories as $blogCategory) {
if ($blogCategory->getLevel() === 1) {
if ($blogCategory->getLevel() === 0) {
$firstLevelBlogCategories[] = $blogCategory;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Model\Blog\Category\Exception;

use Exception;

class RootLevelBlogCategoryAlreadyExistsException extends Exception
{
/**
* @param int $blogCategoryId
* @param \Exception|null $previous
*/
public function __construct(int $blogCategoryId, ?Exception $previous = null)
{
$message = sprintf('There can be only one blog category with root level. The current root level blog category ID is "%d".', $blogCategoryId);

parent::__construct($message, 0, $previous);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
id: blogCategoryWithPreloadedChildren.blogCategory.id,
(constant('Shopsys\\FrameworkBundle\\Component\\Router\\Security\\RouteCsrfProtector::CSRF_TOKEN_REQUEST_PARAMETER')): csrf_token(csrfTokenId)
}) %}
{% if blogCategoryWithPreloadedChildren.blogCategory.level is not same as(1) %}
{% if blogCategoryWithPreloadedChildren.blogCategory.level is not same as(0) %}
<a
data-delete-url="{{ blogCategoryDeleteUrl }}"
class="form-tree__item__control js-category-delete-confirm"
Expand Down

0 comments on commit b64f6f6

Please sign in to comment.