add parent category in category, fix pager

This commit is contained in:
Simon Vieille 2022-02-19 12:13:30 +01:00
parent 655f9c9690
commit b839f4df0b
8 changed files with 114 additions and 11 deletions

View file

@ -61,6 +61,11 @@ class CategoryAdminController extends CrudController
'button_attr' => ['class' => 'btn btn-sm btn-light'],
'raw' => true,
])
->setField('index', 'Catégorie', TextField::class, [
'property_builder' => function (EntityInterface $entity) {
return $entity->getParentCategory()?->getTitle() ?? '';
},
])
->setField('index', 'Status', ButtonField::class, [
'sort' => ['isActive', '.isActive'],
'attr' => ['class' => 'miw-100'],
@ -106,8 +111,8 @@ class CategoryAdminController extends CrudController
public function show(Entity $entity, PostRepositoryQuery $postQuery): Response
{
$posts = $postQuery->create()
->orderBy('.publishedAt', 'DESC')
->orderBy('.createdAt', 'DESC')
->addOrderBy('.publishedAt', 'DESC')
->addOrderBy('.createdAt', 'DESC')
->inCategory($entity)
->paginate(1, 10)
;

View file

@ -81,6 +81,9 @@ class PostAdminController extends CrudController
'sort' => ['status', '.status'],
'attr' => ['class' => 'miw-100'],
])
->setBatchAction('index', 'delete', 'Delete', function(EntityInterface $entity, EntityManager $manager) {
$manager->delete($entity);
})
;
}

View file

@ -125,7 +125,7 @@ class PostController extends PageController
public function category(Category $category, string $slug, int $page = 1): Response
{
$entities = $this->createQuery()
->inCategory($category)
->inCategory($category, false)
->paginate($page, 5)
;

View file

@ -54,9 +54,20 @@ class Category implements EntityInterface
*/
private $isActive;
/**
* @ORM\ManyToOne(targetEntity=Category::class, inversedBy="categories")
*/
private $parentCategory;
/**
* @ORM\OneToMany(targetEntity=Category::class, mappedBy="parentCategory")
*/
private $categories;
public function __construct()
{
$this->posts = new ArrayCollection();
$this->categories = new ArrayCollection();
}
public function getId(): ?int
@ -150,4 +161,46 @@ class Category implements EntityInterface
return $this;
}
public function getParentCategory(): ?self
{
return $this->parentCategory;
}
public function setParentCategory(?self $parentCategory): self
{
$this->parentCategory = $parentCategory;
return $this;
}
/**
* @return Collection|self[]
*/
public function getCategories(): Collection
{
return $this->categories;
}
public function addCategory(self $category): self
{
if (!$this->categories->contains($category)) {
$this->categories[] = $category;
$category->setParentCategory($this);
}
return $this;
}
public function removeCategory(self $category): self
{
if ($this->categories->removeElement($category)) {
// set the owning side to null (unless already changed)
if ($category->getParentCategory() === $this) {
$category->setParentCategory(null);
}
}
return $this;
}
}

View file

@ -10,6 +10,8 @@ use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
class CategoryType extends AbstractType
{
@ -29,6 +31,28 @@ class CategoryType extends AbstractType
]
);
$builder->add(
'parentCategory',
EntityType::class,
[
'label' => 'Catégorie parente',
'class' => Category::class,
'choice_label' => 'title',
'required' => false,
'multiple' => false,
'attr' => [
'data-jschoice' => '',
],
'query_builder' => function (EntityRepository $repo) {
return $repo->createQueryBuilder('a')
->orderBy('a.title', 'ASC')
;
},
'constraints' => [
],
]
);
$builder->add(
'subTitle',
TextareaType::class,

View file

@ -18,15 +18,29 @@ class PostRepositoryQuery extends RepositoryQuery
parent::__construct($repository, 'p', $paginator);
}
public function inCategory(Category $category)
public function inCategory(Category $category, bool $strict = true)
{
$c = 'c'.mt_rand();
$this
->innerJoin('p.categories', $c)
->andWhere($c.'.id = :category')
->setParameter(':category', $category->getId())
;
if ($strict) {
$this
->innerJoin('p.categories', $c)
->andWhere($c.'.id = :category')
->setParameter(':category', $category->getId())
;
} else {
$ids = [$category->getId()];
foreach ($category->getCategories() as $childCategory) {
$ids[] = $childCategory->getId();
}
$this
->innerJoin('p.categories', $c)
->andWhere($c.'.id IN (:categories)')
->setParameter(':categories', $ids)
;
}
return $this;
}

View file

@ -1,7 +1,7 @@
<div class="row">
<div class="col-md-4 p-3">
<div class="row">
{% for item in ['title', 'subTitle', 'slug', 'isActive'] %}
{% for item in ['title', 'subTitle', 'slug', 'parentCategory', 'isActive'] %}
<div class="col-md-12">
{{ form_row(form[item]) }}
</div>

View file

@ -34,7 +34,11 @@
{% block pager %}
{{ include('module/_pager.html.twig', {
route: _node.routeName,
routeParams: {_domain: _domain},
routeParams: {
_domain: _domain,
category: category.id,
slug: category.slug
},
pages: pager.paginationData.endPage,
currentPage: pager.paginationData.current
}) }}