diff --git a/src/Controller/CapsuleController.php b/src/Controller/CapsuleController.php index d713918e9c1983168fbbf57ab6d552cfa4831159..fa48abb70049e09252bf1897822f1be21ab0baeb 100755 --- a/src/Controller/CapsuleController.php +++ b/src/Controller/CapsuleController.php @@ -12,7 +12,6 @@ use App\Helper\StringHelper; use App\Repository\CapsuleRepository; use App\Builder\CapsuleBuilder; use App\Form\CreateCapsuleFormType; -use App\Repository\GroupRepository; use Doctrine\ORM\EntityManagerInterface; use Knp\Component\Pager\PaginatorInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -29,18 +28,15 @@ class CapsuleController extends AbstractController private CapsuleRepository $capsule_repository; private TranslatorInterface $translator; private EntityManagerInterface $entity_manager; - private GroupRepository $group_repository; public function __construct( CapsuleRepository $capsule_repository, TranslatorInterface $translator, - EntityManagerInterface $entity_manager, - GroupRepository $group_repository + EntityManagerInterface $entity_manager ) { $this->capsule_repository = $capsule_repository; $this->translator = $translator; $this->entity_manager = $entity_manager; - $this->group_repository = $group_repository; } /** @@ -59,7 +55,9 @@ class CapsuleController extends AbstractController // get all capsules without any filter $all_capsules = $current_user->getCapsules()->toArray(); - $form = $this->createGroupFilterForm($current_user); + $groups = $this->prependGroupAllToUserGroups($current_user); + + $form = $this->createForm(FilterByGroupFormType::class, ['groups' => $groups]); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { @@ -69,8 +67,8 @@ class CapsuleController extends AbstractController throw new \Exception('Group not found'); } - if ($group->getName() !== $this->translator->trans('groups.filter.no_filter')) { - $all_capsules = $group->getCapsulesIntersection($all_capsules); + if ($group->getId() !== Group::$GROUP_ALL_ID) { + $all_capsules = $group->getCapsules()->toArray(); } } @@ -325,33 +323,27 @@ class CapsuleController extends AbstractController return $capsule; } - private function createGroupFilterForm(User $current_user): FormInterface - { - $this->createGroupAll($current_user); - $groups = $current_user->getGroups()->toArray(); - return $this->createForm(FilterByGroupFormType::class, ['groups' => $groups]); - } - private function createGroupAll(User $current_user): Group { - $group_all = $this->group_repository->findOneBy( - [ - 'name' => $this->translator->trans('groups.filter.no_filter'), - 'author' => $current_user->getId() - ] - ); - - if ($group_all instanceof Group) { - return $group_all; - } - $group_all = new Group(); - $group_all->setId($group_all::$GROUP_ALL_ID); - $group_all->setAuthor($current_user); $group_all->setName($this->translator->trans('groups.filter.no_filter')); - $this->entity_manager->persist($group_all); - $this->entity_manager->flush(); + $group_all->setId(Group::$GROUP_ALL_ID); + $group_all->setAuthor($current_user); return $group_all; } + + /** + * @return array<Group> + */ + private function prependGroupAllToUserGroups(User $current_user): array + { + $group_all = $this->createGroupAll($current_user); + + $author_groups_without_group_all = $current_user->getGroups()->toArray(); + $groups[] = $group_all; + $groups = array_merge($groups, $author_groups_without_group_all); + + return $groups; + } } diff --git a/src/Controller/CapsuleGroupController.php b/src/Controller/CapsuleGroupController.php index 798e9055a53080efca59bd544fa9366e671c2c87..d488c5d7d14f049be8cc9da2ee2a5c5b2501bf15 100755 --- a/src/Controller/CapsuleGroupController.php +++ b/src/Controller/CapsuleGroupController.php @@ -54,15 +54,6 @@ class CapsuleGroupController extends AbstractController throw new \Exception("Capsule does not exist"); } - $group_all = $this->group_repository->findOneBy( - [ - 'name' => $this->translator->trans('groups.filter.no_filter'), - 'author' => $current_user->getId() - ] - ); - - $current_user->getGroups()->removeElement($group_all); - $capsule_groups_by_author = $current_user->getGroupsByCapsule($capsule); $author_capsule_groups_not_added = $capsule->getNotAddedGroupsByAuthor($current_user); diff --git a/src/DataFixtures/GroupFixtures.php b/src/DataFixtures/GroupFixtures.php index 11ef8ba22994396f56587442b7c96cd72198a062..129f6bad594691f87496dce50214689cf6ba4866 100644 --- a/src/DataFixtures/GroupFixtures.php +++ b/src/DataFixtures/GroupFixtures.php @@ -26,6 +26,7 @@ class GroupFixtures extends Fixture implements DependentFixtureInterface throw new \Exception("User does not exist."); } + //TODO: refacto with real group $group_all = new Group(); $group_all->setName($this->translator->trans('groups.filter.no_filter')); $group_all->setAuthor($group_author); diff --git a/src/Entity/Group.php b/src/Entity/Group.php index 50bde465c451432e02af10df612a2448b3338953..64b3e07e5b33131d1623d4ff6a0186a3c8629da6 100755 --- a/src/Entity/Group.php +++ b/src/Entity/Group.php @@ -122,19 +122,4 @@ class Group return $this; } - - /** - * @param array<Capsule> $capsules - * @return array<Capsule> - */ - public function getCapsulesIntersection(array $capsules): array - { - return array_uintersect( - $capsules, - $this->getCapsules()->toArray(), - function ($obj_a, $obj_b) { - return $obj_a->getId() - $obj_b->getId(); - } - ); - } }