From 9369dbed72c95b992f49178323aa5096c1587aab Mon Sep 17 00:00:00 2001 From: Sebastien Curt <sebastien.curt@tetras-libre.fr> Date: Fri, 11 Feb 2022 13:16:47 +0100 Subject: [PATCH] Remove all capsule within the database --- src/Controller/CapsuleController.php | 42 +++++++++++++++------------- src/Entity/Group.php | 21 ++++++++++++++ src/Entity/User.php | 14 ---------- translations/messages.en.yaml | 3 +- translations/messages.fr.yaml | 3 +- 5 files changed, 48 insertions(+), 35 deletions(-) diff --git a/src/Controller/CapsuleController.php b/src/Controller/CapsuleController.php index a1a8543..15fa5a4 100755 --- a/src/Controller/CapsuleController.php +++ b/src/Controller/CapsuleController.php @@ -13,6 +13,7 @@ use App\Repository\CapsuleRepository; use App\Builder\CapsuleBuilder; use App\Form\CreateCapsuleFormType; use App\Repository\GroupRepository; +use ArrayObject; use Doctrine\ORM\EntityManagerInterface; use Knp\Component\Pager\PaginatorInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -28,18 +29,15 @@ class CapsuleController extends AbstractController { private CapsuleRepository $capsule_repository; private TranslatorInterface $translator; - private GroupRepository $group_repository; private EntityManagerInterface $entity_manager; public function __construct( CapsuleRepository $capsule_repository, TranslatorInterface $translator, - GroupRepository $group_repository, EntityManagerInterface $entity_manager ) { $this->capsule_repository = $capsule_repository; $this->translator = $translator; - $this->group_repository = $group_repository; $this->entity_manager = $entity_manager; } @@ -57,14 +55,9 @@ class CapsuleController extends AbstractController return $this->redirectToRoute('app_logout'); } - $all_capsules = $current_user->getCapsules(); - - $group_all = $this->getGroupAll(); - $group_all->addCapsules($all_capsules->toArray()); - $this->entity_manager->persist($group_all); - $this->entity_manager->flush(); - - $form = $this->createForm(FilterByGroupFormType::class, ['groups' => $current_user->getGroups()]); + // get all capsules without any filter + $all_capsules = $current_user->getCapsules()->toArray(); + $form = $this->createGroupFilterForm($current_user); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { @@ -74,7 +67,9 @@ class CapsuleController extends AbstractController throw new \Exception('Group not found'); } - $all_capsules = $current_user->getCapsulesFilteredByGroup($group); + if ($group->getId() !== Group::$ALL_GROUP_ID) { + $all_capsules = $group->getCapsulesIntersection($all_capsules); + } } $capsules = $paginator->paginate( @@ -320,7 +315,6 @@ class CapsuleController extends AbstractController ->withLinkPath($preview_link) ->withUpdateDate($new_date_time) ->withPassword($password) - ->withGroup($this->getGroupAll()) ->createCapsule(); $this->entity_manager->persist($capsule); @@ -329,14 +323,24 @@ class CapsuleController extends AbstractController return $capsule; } - private function getGroupAll(): Group + private function getDefaultGroup(): Group { - $group_all = $this->group_repository->findOneBy(['name' => "All"]); + $noGroup = new Group(); + + // TODO : to be translated + $noGroup->setName($this->translator->trans('groups.filter.no_filter')); + $noGroup->setId(Group::$ALL_GROUP_ID); + + return $noGroup; + } + + private function createGroupFilterForm(User $current_user): FormInterface + { + $groups = new ArrayObject($current_user->getGroups()); + $groups = $groups->getArrayCopy(); + array_unshift($groups, self::getDefaultGroup()); - if (! $group_all instanceof Group) { - throw new \Exception("Group not found"); - } - return $group_all; + return $this->createForm(FilterByGroupFormType::class, ['groups' => $groups]); } } diff --git a/src/Entity/Group.php b/src/Entity/Group.php index a8c2fd3..af8ad78 100755 --- a/src/Entity/Group.php +++ b/src/Entity/Group.php @@ -15,6 +15,7 @@ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; */ class Group { + public static int $ALL_GROUP_ID = -1; /** * @ORM\Id * @ORM\GeneratedValue @@ -43,6 +44,11 @@ class Group return $this->id; } + public function setId(int $id): void + { + $this->id = $id; + } + public function getName(): string { return $this->name; @@ -91,4 +97,19 @@ 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(); + } + ); + } } diff --git a/src/Entity/User.php b/src/Entity/User.php index 4d527cf..21bcad2 100755 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -316,18 +316,4 @@ class User implements UserInterface, LegacyPasswordAuthenticatedUserInterface return $existing_groups_for_current_editor; } - - /** - * @return array<Capsule> - */ - public function getCapsulesFilteredByGroup(Group $group): array - { - return array_uintersect( - $this->getCapsules()->toArray(), - $group->getCapsules()->toArray(), - function ($obj_a, $obj_b) { - return $obj_a->getId() - $obj_b->getId(); - } - ); - } } diff --git a/translations/messages.en.yaml b/translations/messages.en.yaml index c41fd1e..0ca87ae 100644 --- a/translations/messages.en.yaml +++ b/translations/messages.en.yaml @@ -181,4 +181,5 @@ groups: title: Remove group success: Group group_name removed successfully filter: - button: Apply group filter \ No newline at end of file + button: Apply group filter + no_filter: Show all \ No newline at end of file diff --git a/translations/messages.fr.yaml b/translations/messages.fr.yaml index 63d83b0..258d7ce 100644 --- a/translations/messages.fr.yaml +++ b/translations/messages.fr.yaml @@ -180,4 +180,5 @@ groups: title: Supprimer le groupe success: Le groupe group_name a bien été supprimé filter: - button: Filtrer par groupe \ No newline at end of file + button: Filtrer par groupe + no_filter: Tout afficher \ No newline at end of file -- GitLab