backports murph-skeleton

This commit is contained in:
Simon Vieille 2021-10-11 14:10:38 +02:00
parent ebbe787bac
commit d5d7ef730b
10 changed files with 85 additions and 8 deletions

View File

@ -31,7 +31,7 @@ abstract class CrudController extends AdminController
$configuration = $this->getConfiguration();
$this->applySort('index', $query, $request);
$this->updatefilters($request, $session);
$this->updateFilters($request, $session);
$pager = $query
->usefilters($this->filters)

View File

@ -143,9 +143,19 @@ class NodeAdminController extends AdminController
]);
}
$page = $entity->getPage();
if ($page !== null) {
$pageConfiguration = $pageLocator->getPages()[get_class($page)] ?? null;
} else {
$pageConfiguration = null;
}
return $this->render('@Core/site/node_admin/edit.html.twig', [
'form' => $form->createView(),
'entity' => $entity,
'page' => $page,
'pageConfiguration' => $pageConfiguration,
'tab' => $tab,
]);
}

View File

@ -17,6 +17,7 @@ use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route;
use App\Core\Event\Page\PageEditEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use App\Core\Entity\EntityInterface;
class PageAdminController extends CrudController
{
@ -76,6 +77,14 @@ class PageAdminController extends CrudController
return $this->doDelete($entity, $entityManager, $request);
}
/**
* @Route("/admin/site/page/batch/{page}", name="admin_site_page_batch", methods={"POST"}, requirements={"page":"\d+"})
*/
public function batch(int $page = 1, RepositoryQuery $query, EntityManager $entityManager, Request $request, Session $session): Response
{
return $this->doBatch($page, $query, $entityManager, $request, $session);
}
protected function getConfiguration(): CrudConfiguration
{
return CrudConfiguration::create()
@ -87,6 +96,7 @@ class PageAdminController extends CrudController
->setPageRoute('edit', 'admin_site_page_edit')
->setPageRoute('delete', 'admin_site_page_delete')
->setPageRoute('filter', 'admin_site_page_filter')
->setPageRoute('batch', 'admin_site_page_batch')
->setForm('edit', Type::class, [])
->setForm('filter', FilterType::class)
@ -99,6 +109,7 @@ class PageAdminController extends CrudController
->setField('index', 'Name', Field\TextField::class, [
'property' => 'name',
'sort' => ['name', '.name'],
'attr' => ['class' => 'col-4'],
])
->setField('index', 'Elements', Field\TextField::class, [
'view' => '@Core/site/page_admin/fields/nodes.html.twig',
@ -110,7 +121,11 @@ class PageAdminController extends CrudController
->orderBy('navigation.label', $direction)
;
}],
'attr' => ['class' => 'col-6'],
])
->setBatchAction('index', 'delete', 'Delete', function(EntityInterface $entity, EntityManager $manager) {
$manager->delete($entity);
})
;
}

View File

@ -9,6 +9,7 @@ use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
class PageFilterType extends AbstractType
{
@ -47,6 +48,19 @@ class PageFilterType extends AbstractType
],
]
);
$builder->add(
'isAssociated',
ChoiceType::class,
[
'label' => 'Associated',
'choices' => [
'Anyway' => -1,
'No' => 0,
'Yes' => 1,
],
]
);
}
public function configureOptions(OptionsResolver $resolver)

View File

@ -17,6 +17,7 @@ abstract class RepositoryQuery
protected QueryBuilder $query;
protected PaginatorInterface $paginator;
protected string $id;
protected array $forcedFilterHandlers;
public function __construct(ServiceEntityRepository $repository, string $id, PaginatorInterface $paginator = null)
{
@ -24,6 +25,7 @@ abstract class RepositoryQuery
$this->query = $repository->createQueryBuilder($id);
$this->paginator = $paginator;
$this->id = $id;
$this->forcedFilterHandlers = [];
}
public function __call(string $name, $params): self
@ -81,7 +83,9 @@ abstract class RepositoryQuery
continue;
}
if (is_int($value) || is_bool($value)) {
if (in_array($name, $this->forcedFilterHandlers)) {
$this->filterHandler($name, $value);
} elseif (is_int($value) || is_bool($value)) {
$this->andWhere('.'.$name.' = :'.$name);
$this->setParameter(':'.$name, $value);
} elseif (is_string($value)) {

View File

@ -13,11 +13,13 @@ class NodeRepository extends NestedTreeRepository
parent::__construct($manager, $manager->getClassMetadata(Node::class));
}
public function urlExists($url, Node $node)
public function urlExists($url, Node $node): bool
{
$query = $this->createQueryBuilder('n')
->join('n.menu', 'm')
->where('n.url = :url')
->andWhere('n.disableUrl = 0')
->andWhere('n.aliasNode is null')
->andWhere('m.navigation = :navigation')
->setParameter(':url', $url)
->setParameter(':navigation', $node->getMenu()->getNavigation())
@ -32,7 +34,7 @@ class NodeRepository extends NestedTreeRepository
return $query->getQuery()
->setMaxResults(1)
->getOneOrNullResult()
->getOneOrNullResult() !== null
;
}
}

View File

@ -16,9 +16,11 @@ class PageRepositoryQuery extends RepositoryQuery
public function __construct(PageRepository $repository, PaginatorInterface $paginator)
{
parent::__construct($repository, 'p', $paginator);
$this->forcedFilterHandlers[] = 'isAssociated';
}
public function filterByNavigation(Navigation $navigation)
public function filterByNavigation(Navigation $navigation): self
{
return $this
->leftJoin('.nodes', 'node')
@ -29,7 +31,7 @@ class PageRepositoryQuery extends RepositoryQuery
;
}
public function filterById($id)
public function filterById($id): self
{
$this
->where('.id = :id')
@ -39,12 +41,37 @@ class PageRepositoryQuery extends RepositoryQuery
return $this;
}
protected function withAssociation(bool $isAssociated): self
{
$entities = $this->create()->find();
$ids = [];
foreach ($entities as $entity) {
if ($isAssociated && !$entity->getNodes()->isEmpty()) {
$ids[] = $entity->getId();
} elseif (!$isAssociated && $entity->getNodes()->isEmpty()) {
$ids[] = $entity->getId();
}
}
$this
->andWhere('.id IN (:ids)')
->setParameter(':ids', $ids)
;
return $this;
}
protected function filterHandler(string $name, $value)
{
if ('navigation' === $name) {
return $this->filterByNavigation($value);
}
if ('isAssociated' === $name && $value > -1) {
$this->withAssociation((bool) $value);
}
return parent::filterHandler($name, $value);
}
}

View File

@ -191,3 +191,4 @@
"Insert": "Insérer"
"Attributes": "Attributs"
"Choose": "Choisir"
"Associated": "Associé(e)"

View File

@ -147,6 +147,10 @@
<div id="form-node-page-action-keep" class="collapse show" data-parent="#node-page-action">
<div class="card-body">
{{ 'No action'|trans }}
{% if page and pageConfiguration %}
({{ pageConfiguration.name }})
{% endif %}
</div>
</div>
</div>

View File

@ -25,8 +25,8 @@ class StringExtension extends AbstractExtension
];
}
public function buildString(string $format, $object): string
public function buildString(?string $format, $object): string
{
return $this->stringBuilder->build($format, $object);
return $this->stringBuilder->build((string) $format, $object);
}
}