Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

64 lines
2.2KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Field\Filter;
  3. use Doctrine\ORM\EntityRepository;
  4. use Doctrine\ORM\QueryBuilder;
  5. use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
  6. use Lc\ShopBundle\Context\TreeInterface;
  7. use Lc\SovBundle\Field\Filter\AssociationFilter;
  8. use Lc\SovBundle\Field\Filter\FilterTrait;
  9. use Lc\SovBundle\Repository\RepositoryQueryInterface;
  10. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  11. use Symfony\Component\Form\FormBuilderInterface;
  12. /**
  13. * @author La clic ! <contact@laclic.fr>
  14. */
  15. class ProductCategoriesFilter extends AssociationFilter
  16. {
  17. public function buildProperty(FormBuilderInterface $builder, FieldDto $fieldDto, $options = array())
  18. {
  19. $targetEntity = $options['entity_dto']->getPropertyMetadata($fieldDto->getProperty())->get('targetEntity');
  20. $builder->add(
  21. $fieldDto->getProperty(),
  22. EntityType::class,
  23. array(
  24. 'class' => $targetEntity,
  25. 'placeholder' => '--',
  26. 'choices' => $fieldDto->getFormTypeOption('choices'),
  27. 'required' => false,
  28. 'attr' => array(
  29. 'class' => 'select2 input-sm',
  30. 'form' => 'filters-form',
  31. ),
  32. 'choice_label' => function ($category) {
  33. $isOffline = '';
  34. if ($category->getStatus() != 1) {
  35. $isOffline = " [Hors ligne]";
  36. }
  37. $section = ' ['.$category->getSection()->getTitle().']' ;;
  38. return $category . $section. $isOffline;
  39. }
  40. )
  41. );
  42. }
  43. public function applyFilter(RepositoryQueryInterface $repositoryQuery, FieldDto $fieldDto, $filteredValue = null)
  44. {
  45. $fieldProperty = $this->getFieldProperty($fieldDto);
  46. if ($filteredValue !== null) {
  47. if ($filteredValue->getParent() == null) {
  48. $param = array_merge(array($filteredValue), $filteredValue->getChildrens()->toArray());
  49. } else {
  50. $param = array($filteredValue);
  51. }
  52. $repositoryQuery->filterByProductCategoryArray($param);
  53. }
  54. }
  55. }