vendor/roothirsch/core-bundle/Behaviors/DragDropSortable/DragDropSortableSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\CoreBundle\Behaviors\DragDropSortable;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use Roothirsch\CoreBundle\Admin\Field\DragDropPositionField;
  5. use Doctrine\Common\Annotations\Reader;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use EasyCorp\Bundle\EasyAdminBundle\Collection\EntityCollection;
  8. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  9. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  10. use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
  11. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterCrudActionEvent;
  12. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;
  13. use Gedmo\Mapping\Annotation\SortableGroup;
  14. use Gedmo\Mapping\Annotation\SortablePosition;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. use Symfony\Component\PropertyAccess\PropertyAccess;
  18. class DragDropSortableSubscriber implements EventSubscriberInterface
  19. {
  20. /**
  21. * @var EntityManagerInterface
  22. */
  23. protected $entityManager;
  24. /**
  25. * @var Reader
  26. */
  27. protected $reader;
  28. public function __construct(EntityManagerInterface $entityManager, Reader $reader)
  29. {
  30. $this->entityManager = $entityManager;
  31. $this->reader = $reader;
  32. }
  33. public static function getSubscribedEvents()
  34. {
  35. return [
  36. AfterCrudActionEvent::class => ['processGroups']
  37. ];
  38. }
  39. public function processGroups(AfterCrudActionEvent $event)
  40. {
  41. $context = $event->getAdminContext();
  42. $responseParameters = $event->getResponseParameters();
  43. $metadata = $this->entityManager->getMetadataFactory()->getMetadataFor($context->getEntity()->getFqcn());
  44. $positionProperty = null;
  45. $groupProperty = null;
  46. foreach($metadata->getReflectionClass()->getProperties() as $property) {
  47. if ($this->reader->getPropertyAnnotation($property, SortablePosition::class) instanceof SortablePosition) {
  48. $positionProperty = $property;
  49. }
  50. if ($this->reader->getPropertyAnnotation($property, SortableGroup::class) instanceof SortableGroup) {
  51. $groupProperty = $property;
  52. }
  53. }
  54. if ($groupProperty !== null && $responseParameters->has('entities')) {
  55. $propertyAccessor = PropertyAccess::createPropertyAccessor();
  56. $groups = [];
  57. /** @var EntityDto $entityDto */
  58. foreach($responseParameters->get('entities') as $entityDto) {
  59. $group = $propertyAccessor->getValue($entityDto->getInstance(), $groupProperty->getName());
  60. if (!isset($groups[$group->getId()])) {
  61. $groups[$group->getId()] = [
  62. 'group' => $group,
  63. 'entites' => []
  64. ];
  65. }
  66. $groups[$group->getId()]['entities'][] = $entityDto;
  67. }
  68. usort($groups, function($a, $b) {
  69. return $a['group']->getPosition() > $b['group']->getPosition();
  70. });
  71. $responseParameters->set('groups', $groups);
  72. }
  73. }
  74. }