Browse Source

Filter text

feature/filters
Fab 3 years ago
parent
commit
aa7fe64722
4 changed files with 323 additions and 25 deletions
  1. +56
    -25
      Controller/Admin/AbstractCrudController.php
  2. +188
    -0
      Form/Type/Crud/IndexFilterType.php
  3. +61
    -0
      Resources/views/adminlte/block/table_fitlers.html.twig
  4. +18
    -0
      Resources/views/adminlte/crud/index.html.twig

+ 56
- 25
Controller/Admin/AbstractCrudController.php View File

use Lc\SovBundle\Doctrine\Extension\DevAliasInterface; use Lc\SovBundle\Doctrine\Extension\DevAliasInterface;
use Lc\SovBundle\Doctrine\Extension\SeoInterface; use Lc\SovBundle\Doctrine\Extension\SeoInterface;
use Lc\SovBundle\Doctrine\Extension\SortableInterface; use Lc\SovBundle\Doctrine\Extension\SortableInterface;
use Lc\SovBundle\Doctrine\Extension\StatusInterface;
use Lc\SovBundle\Doctrine\Extension\TranslatableInterface; use Lc\SovBundle\Doctrine\Extension\TranslatableInterface;
use Lc\SovBundle\Doctrine\Extension\TreeInterface; use Lc\SovBundle\Doctrine\Extension\TreeInterface;
use Lc\SovBundle\Field\CollectionField; use Lc\SovBundle\Field\CollectionField;
use Lc\SovBundle\Field\GalleryManagerField; use Lc\SovBundle\Field\GalleryManagerField;
use Lc\SovBundle\Form\Type\Crud\IndexFilterType;
use Lc\SovBundle\Form\Type\Crud\PositionType; use Lc\SovBundle\Form\Type\Crud\PositionType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType; use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\TextType;
{ {
protected $session; protected $session;
protected $request; protected $request;
protected $filtersForm;


public function __construct(SessionInterface $session, RequestStack $request, EntityManager $em, Environment $twig) public function __construct(SessionInterface $session, RequestStack $request, EntityManager $em, Environment $twig)
{ {
Action::NEW => [ Action::NEW => [
'icon' => 'plus', 'icon' => 'plus',
'label' => 'Créer', 'label' => 'Créer',
'add_class'=>'btn-sm'
'add_class' => 'btn-sm',
], ],
Action::EDIT => [ Action::EDIT => [
'class' => 'btn btn-sm btn-primary', 'class' => 'btn btn-sm btn-primary',
'icon' => 'edit', 'icon' => 'edit',
'label' => false, 'label' => false,
'html_attributes'=> array('data-toggle'=> 'tooltip', 'title'=> 'Éditer')
'html_attributes' => array('data-toggle' => 'tooltip', 'title' => 'Éditer'),
], ],
Action::DELETE => [ Action::DELETE => [
'icon' => 'trash', 'icon' => 'trash',
$indexChildAction = Action::new('index_children', 'Afficher les enfants', 'fa fa-list') $indexChildAction = Action::new('index_children', 'Afficher les enfants', 'fa fa-list')
->linkToCrudAction(Action::INDEX) ->linkToCrudAction(Action::INDEX)
->setLabel('') ->setLabel('')
->setHtmlAttributes(array('data-toggle'=> 'tooltip', 'title'=> 'Afficher les enfants'))
->setHtmlAttributes(array('data-toggle' => 'tooltip', 'title' => 'Afficher les enfants'))
->setTemplatePath('@LcSov/adminlte/crud/action/index_children.html.twig') ->setTemplatePath('@LcSov/adminlte/crud/action/index_children.html.twig')
->setCssClass('btn btn-sm btn-success'); ->setCssClass('btn btn-sm btn-success');


} }


if (isset($button['html_attributes']) && $button['html_attributes']) { if (isset($button['html_attributes']) && $button['html_attributes']) {
$action->setHtmlAttributes( $button['html_attributes']);
$action->setHtmlAttributes($button['html_attributes']);
} }


return $action; return $action;
{ {
if (Crud::PAGE_INDEX === $responseParameters->get('pageName')) { if (Crud::PAGE_INDEX === $responseParameters->get('pageName')) {
$responseParameters->set('fields', $this->configureFields('index')); $responseParameters->set('fields', $this->configureFields('index'));

if ($this->filtersForm === null) {
$options['fields'] = $responseParameters->get('fields');
$this->filtersForm = $this->createForm(IndexFilterType::class, null, $options);
}
$responseParameters->set('filters_form', $this->filtersForm);
} }


return $responseParameters; return $responseParameters;
$crud = parent::configureCrud($crud); $crud = parent::configureCrud($crud);


$this->setMaxResults($crud); $this->setMaxResults($crud);
if($this->isInstanceOf(SortableInterface::class)){
if ($this->isInstanceOf(SortableInterface::class)) {
$crud->setDefaultSort(['position' => 'ASC']); $crud->setDefaultSort(['position' => 'ASC']);
} }

return $crud; return $crud;
} }


->getForm(); ->getForm();


$entityManager = $this->getDoctrine()->getManagerForClass($this->getEntityFqcn()); $entityManager = $this->getDoctrine()->getManagerForClass($this->getEntityFqcn());
dump($entityManager);
$repository = $entityManager->getRepository($this->getEntityFqcn()); $repository = $entityManager->getRepository($this->getEntityFqcn());


$sortableForm->handleRequest($context->getRequest()); $sortableForm->handleRequest($context->getRequest());
$fields, $fields,
$filters $filters
); );

dump(get_defined_vars());
if ($this->isInstanceOf(TreeInterface::class)) { if ($this->isInstanceOf(TreeInterface::class)) {
$entityId = $searchDto->getRequest()->get('entityId'); $entityId = $searchDto->getRequest()->get('entityId');
if ($entityId !== null) { if ($entityId !== null) {
$queryBuilder->andWhere('entity.parent IS NULL'); $queryBuilder->andWhere('entity.parent IS NULL');
} }
} }
if ($this->isInstanceOf(StatusInterface::class)) {
$queryBuilder->andWhere('entity.status >= 0');
}


$fields = FieldCollection::new($this->configureFields(Crud::PAGE_INDEX));
$this->filtersForm = $this->createForm(IndexFilterType::class, null, array('fields' => $fields));


$this->filtersForm->handleRequest($searchDto->getRequest());
if (($this->filtersForm->isSubmitted() && $this->filtersForm->isValid())) {
foreach ($fields as $field) {

if ($field->isDisplayedOn(Crud::PAGE_INDEX)) {
if ($this->filtersForm->has($field->getProperty())) {
switch ($field->getFormType()) {
case TextType::class:
$filter = $this->filtersForm->get($field->getProperty())->getData();
//$filter = $this->filtersForm->get($field['property'])->getData();
if ($filter !== null) {
$queryBuilder->andWhere(
'entity.'.$field->getProperty().' LIKE :'.$field->getProperty().''
);
$queryBuilder->setParameter($field->getProperty(), '%'.$filter.'%');
}
}
break;
}
}
}
}


return $queryBuilder; return $queryBuilder;
} }
FieldCollection $fields, FieldCollection $fields,
FilterCollection $filters FilterCollection $filters
): QueryBuilder { ): QueryBuilder {
$queryBuilder = parent::createIndexQueryBuilder(
$searchDto,
$entityDto,
$fields,
$filters
);

if ($this->isInstanceOf(TreeInterface::class)) {
$entityId = $searchDto->getRequest()->get('entityId');
if ($entityId !== null) {
$queryBuilder->andWhere('entity.parent = :entityId');
$queryBuilder->setParameter('entityId', $searchDto->getRequest()->get('entityId'));
} else {
$queryBuilder->andWhere('entity.parent IS NULL');
}
}

$queryBuilder = $this->createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters);


return $queryBuilder; return $queryBuilder;
} }
$entityManager->flush(); $entityManager->flush();
} }


public function deleteEntity(EntityManagerInterface $entityManager, $entityInstance): void
{
if ($entityInstance instanceof StatusInterface) {
$entityInstance->setStatus(-1);
$entityManager->update($entityInstance);
} else {
$entityManager->remove($entityInstance);
}
$entityManager->flush();
}
} }



+ 188
- 0
Form/Type/Crud/IndexFilterType.php View File

<?php

namespace Lc\SovBundle\Form\Type\Crud;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;


class IndexFilterType extends AbstractType
{
protected $em;
protected $translator;
protected $utils;

public function __construct(EntityManagerInterface $entityManager, TranslatorInterface $translator)
{
$this->em = $entityManager;
$this->translator = $translator;
}


public function buildForm(FormBuilderInterface $builder, array $options)
{


foreach ($options['fields'] as $field) {
if($field instanceof FieldInterface) {
$fieldDto = $field->getAsDto();
}else{
$fieldDto = $field;
}
if ($fieldDto->isDisplayedOn(Crud::PAGE_INDEX)) {

switch ($fieldDto->getFormType()) {
case 'option':

$refl = new \ReflectionClass($options['entity_class']);

$choiceOptions = array();
foreach ($refl->getConstants() as $key => $value) {
if (stripos($key, $this->utils->snakeCase($field['property'])) !== false) {
$choiceOptions[$this->translator->trans(
'field.'.$options['entity_name'].'.'.$field['property'].'Options.'.$value
)] = $value;
}
}

$builder->add(
$field['property'],
ChoiceType::class,
array(
'required' => false,
'choices' => $choiceOptions,
'attr' => array(
'class' => 'select2 input-sm',
'form' => 'filters-form',
),
)
);
break;
case 'integer':
$builder->add(
$field['property'],
TextType::class,
array(
'required' => false,
'attr' => array(
'class' => ' input-sm',
'form' => 'filters-form',
),
)
);
break;
case TextType::class:
$builder->add(
str_replace('.', '_', $fieldDto->getProperty()),
TextType::class,
array(
'required' => false,
'attr' => array(
'class' => ' input-sm',
'form' => 'filters-form',
),
)
);
break;
case 'toggle':
$builder->add(
$field['property'],
ChoiceType::class,
array(
'choices' => array(
'Oui' => 1,
'Non' => 0,
),
'placeholder' => '--',
'required' => false,
'attr' => array(
'class' => 'select2 input-sm',
'form' => 'filters-form',
),
)
);
break;
case 'datetime':
case 'date':
$builder->add(
$builder->create($field['property'], FormType::class, array('inherit_data' => true))
->add(
'dateStart',
DateType::class,
array(
'widget' => 'single_text',
'required' => false,
)
)
->add(
'dateEnd',
DateType::class,
array(
'widget' => 'single_text',
'required' => false,

)
)
);
break;
case 'association':
$classImplements = class_implements($field['targetEntity']);
$builder->add(
$field['property'],
EntityType::class,
array(
'class' => $field['targetEntity'],
'placeholder' => '--',
'query_builder' => function (EntityRepository $repo) use ($classImplements) {
return $repo->createQueryBuilder('entity');
},
'required' => false,
'attr' => array(
'class' => 'select2 input-sm',
'form' => 'filters-form',
),


)
);
break;
case 'dateinterval':

break;
case 'float':

break;
}
}
}
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
[
'label' => false,
'csrf_protection' => false,
//'translation_domain' => 'lcshop',
'fields' => false,
'entity_name' => false,
'entity_class' => false,
//'entityClass' => false
]
);
}
}

+ 61
- 0
Resources/views/adminlte/block/table_fitlers.html.twig View File

{% set filters_form_are_not_empty = false %}
{% if filters_form is defined %}
<tr class="table-filters-line">
{% if has_batch_actions %}
<th></th>
{% endif %}
{% for field in fields ?? [] %}
{% set field = field.getAsDto() %}
{% if field.isDisplayedOn('index') %}
<th>

{% if filters_form[field.property] is defined %}
{% set form_field = filters_form[field.property] %}
{{ dump(field.property) }}
{{ dump(form_field) }}

{% if form_field.vars.value is not null and form_field.vars.value is not empty %}{% set filters_form_are_not_empty = true %}{% endif %}
{# {% if metadata['dataType'] == 'datetime' or metadata['dataType'] == 'date' %}
<div class="input-group input-group-sm">
<input type="text"
class="form-control input-sm float-right date-range">
<div class="hidden date-time-range-fields"
style="display: none;">
{{ form_widget(filters_form[field]['dateStart'], {"attr" : {'class' : 'date-start', 'form': 'filters-form'}}) }}
{{ form_widget(filters_form[field]['dateEnd'], {"attr" : {'class' : 'date-end', 'form': 'filters-form'}}) }}
</div>
</div>
{% else %} #}
<div class="form-widget input-group-sm">
{# {% if metadata.dataType == 'integer' or metadata.dataType== 'string' or metadata.dataType== 'text' %} #}
{# {{ form_widget(filters_form[field], {'attr': {'autocomplete': 'off', 'data-lc-autocomplete-url' : path('easyadmin', {
action: 'autocomplete',
field: field,
entity: _entity_config.name,
})|raw }}) }} #}
{# {% else %} #}
{{ form_widget(form_field) }}
{# {% endif %} #}
</div>
{# {% endif %} #}{#
#}
{% endif %}
{% endif %}


</th>
{% endfor %}
{# {% if _list_item_actions|length > 0 %} #}
<th class="actions">
<button type="submit" form="filters-form" class="btn btn-sm btn-info"
data-toggle="tooltip"
title="{{ "action.apply"|trans({}, 'lcshop') }}"
aria-label="{{ "action.apply"|trans({}, 'lcshop') }}">
<i class="fa fa-search"></i>
</button>

</th>
{# {% endif %} #}

</tr>
{% endif %}

+ 18
- 0
Resources/views/adminlte/crud/index.html.twig View File

</th> </th>
</tr> </tr>
{% endblock table_head %} {% endblock table_head %}
{% block table_filters %}
{{ include('@LcSov/adminlte/block/table_fitlers.html.twig') }}
{% endblock table_filters %}
</thead> </thead>


<tbody> <tbody>
{% endblock table_body %} {% endblock table_body %}
</tbody> </tbody>
</table> </table>

{% block filters_form %}
{% if filters_form is defined %}
{{ form_start(filters_form, {'attr' :{'id' : 'filters-form'}}) }}

{#<input type="hidden" name="entity" value="{{ _entity_config.name }}">
<input type="hidden" name="menuIndex" value="{{ app.request.get('menuIndex') }}">
<input type="hidden" name="submenuIndex" value="{{ app.request.get('submenuIndex') }}">
<input type="hidden" name="sortField" value="{{ app.request.get('sortField', '') }}">
<input type="hidden" name="sortDirection"
value="{{ app.request.get('sortDirection', 'DESC') }}">
<input type="hidden" name="action" value="{{ app.request.get('action') }}">#}
{{ form_end(filters_form) }}
{% endif %}
{% endblock filters_form %}
</div> </div>
{% endblock %} {% endblock %}
{% block footer %} {% block footer %}

Loading…
Cancel
Save