diff --git a/assets/styles/app.scss b/assets/styles/app.scss index 4cb6082d77715d3584394c8b10648ae2f40f4776..cf6890562c4369911c7a20c0dba3c2043128204c 100644 --- a/assets/styles/app.scss +++ b/assets/styles/app.scss @@ -123,11 +123,16 @@ button[type=submit]{ float: left; color: rgba(255,255,255,.75) !important; line-height: 60px; + margin-bottom: 0; background: -webkit-linear-gradient(top left, #FA772E, #FC4326); -webkit-background-clip: text; -webkit-text-fill-color: transparent; } +.h2 { + margin-bottom: 0; +} + .button-cancel { border: none; border-radius: 3px; diff --git a/src/Controller/CapsuleController.php b/src/Controller/CapsuleController.php index dbf0c440a63e0778bbef6ccd949dd8609eb4f226..0bf9c029daa2fb2e7a0d1e3c6bc165d2e000d0a0 100755 --- a/src/Controller/CapsuleController.php +++ b/src/Controller/CapsuleController.php @@ -3,9 +3,11 @@ namespace App\Controller; use App\Entity\Capsule; +use App\Entity\Group; use App\Entity\User; use App\Form\DeleteCapsuleFormType; use App\Form\DuplicateCapsuleFormType; +use App\Form\FilterByGroupFormType; use App\Helper\StringHelper; use App\Repository\CapsuleRepository; use App\Builder\CapsuleBuilder; @@ -49,6 +51,19 @@ class CapsuleController extends AbstractController $all_capsules = $current_user->getCapsules(); + $form = $this->createForm(FilterByGroupFormType::class, ['groups' => $current_user->getGroups()]); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $group = $form->getData()['name']; + + if (! $group instanceof Group) { + throw new \Exception('Group not found'); + } + + $all_capsules = $current_user->getCapsulesFilteredByGroup($group); + } + $capsules = $paginator->paginate( $all_capsules, $request->query->getInt('page', 1), @@ -56,6 +71,7 @@ class CapsuleController extends AbstractController ); return $this->render('capsule/index.html.twig', [ + 'filterByGroupForm' => $form->createView(), 'capsules' => $capsules, 'current_user' => $current_user, 'legacy_url' => $this->getParameter('app.legacy_external_prefix') diff --git a/src/Controller/CapsuleGroupController.php b/src/Controller/CapsuleGroupController.php index 17a92c6d56d080375cf43a84b8595a2e14c8d1d1..6374a92b387d9c23b0eaa63a94429ea36627a617 100755 --- a/src/Controller/CapsuleGroupController.php +++ b/src/Controller/CapsuleGroupController.php @@ -233,33 +233,14 @@ class CapsuleGroupController extends AbstractController return $group; } - /** - * @return array<Group> - */ - private function getExistingGroupsForCurrentEditor(User $current_user): array - { - $editor_capsules = $current_user->getCapsules(); - $existing_groups_for_current_editor = []; - foreach ($editor_capsules as $editor_capsule) { - $existing_groups_for_current_editor = array_merge( - $existing_groups_for_current_editor, - $editor_capsule->getGroups()->toArray() - ); - } - - return $existing_groups_for_current_editor; - } - /** * @param Collection<Group> $groups * @return array<Group> */ private function getEditorGroupsNotAdded(User $current_user, Collection $groups): array { - $editor_existing_groups = $this->getExistingGroupsForCurrentEditor($current_user); - return array_udiff( - $editor_existing_groups, + $current_user->getGroups(), $groups->toArray(), function ($obj_a, $obj_b) { return $obj_a->getId() - $obj_b->getId(); diff --git a/src/DataFixtures/CapsuleFixtures.php b/src/DataFixtures/CapsuleFixtures.php index c95b19e2c784c20fc50b2a0ee689f7d4dba9dc05..3383bf14cd736af0f233736f1c979b2278de283d 100755 --- a/src/DataFixtures/CapsuleFixtures.php +++ b/src/DataFixtures/CapsuleFixtures.php @@ -12,7 +12,6 @@ use Symfony\Component\Uid\Uuid; class CapsuleFixtures extends Fixture implements DependentFixtureInterface { - public function load(ObjectManager $manager): void { $user_repository = $manager->getRepository(User::class); diff --git a/src/Entity/User.php b/src/Entity/User.php index fcde46a8ed06432d816aa83069a1036af58d30b0..4d527cfd46202d01562cb7a7974a20fe87b6c1a4 100755 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -299,4 +299,35 @@ class User implements UserInterface, LegacyPasswordAuthenticatedUserInterface { $this->is_subscribed_news_letter = $is_subscribed_news_letter; } + + /** + * @return array<Group> + */ + public function getGroups(): array + { + $editor_capsules = $this->getCapsules(); + $existing_groups_for_current_editor = []; + foreach ($editor_capsules as $editor_capsule) { + $existing_groups_for_current_editor = array_merge( + $existing_groups_for_current_editor, + $editor_capsule->getGroups()->toArray() + ); + } + + 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/src/Exception/CapsuleNotFoundException.php b/src/Exception/CapsuleNotFoundException.php index 5f33efe481c08690ba06ef5f9b797e6135a23335..60db948770eda7d83ef7a47abe9c17b98fba2659 100755 --- a/src/Exception/CapsuleNotFoundException.php +++ b/src/Exception/CapsuleNotFoundException.php @@ -4,5 +4,4 @@ namespace App\Exception; class CapsuleNotFoundException extends \Exception { - } diff --git a/src/Form/FilterByGroupFormType.php b/src/Form/FilterByGroupFormType.php new file mode 100644 index 0000000000000000000000000000000000000000..7e17f84042e952c6ca6b2c021fb9d3bf2ec0fb1e --- /dev/null +++ b/src/Form/FilterByGroupFormType.php @@ -0,0 +1,42 @@ +<?php + +namespace App\Form; + +use App\Entity\Group; +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\SubmitType; +use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\OptionsResolver\OptionsResolver; + +class FilterByGroupFormType extends AbstractType +{ + public function buildForm(FormBuilderInterface $builder, array $options): void + { + $groups = $options['data']['groups']; + + $builder + ->add('name', ChoiceType::class, [ + 'choices' => $groups, + 'choice_translation_domain' => false, + 'label' => false, + 'choice_label' => function (?Group $entity) { + return $entity ? $entity->getName() : ''; + }, + 'choice_value' => 'name', + 'choice_name' => 'name', + 'expanded' => false + ]) + ->add( + 'filter', + SubmitType::class, + ['label' => 'groups.filter.button'] + ); + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([]); + } +} diff --git a/templates/capsule/index.html.twig b/templates/capsule/index.html.twig index 48b0b61155c14a5198f403ade3930cb9c8d268b4..f0bf7f272038c1c49ecc4b1d4e01ae16f591f38e 100644 --- a/templates/capsule/index.html.twig +++ b/templates/capsule/index.html.twig @@ -8,11 +8,20 @@ {% block body %} <div class="row gx-0"> - <div class="row-title-box d-flex justify-content-between align-items-center"> + <div class="row-title-box d-flex flex-column flex-lg-row justify-content-between align-items-center"> <h2 class="row-title"> {{ 'capsule.title'|trans }} </h2> - <form class="d-none d-md-flex"> + + <div class="mb-3 mb-sm-0"> + {{ form_start(filterByGroupForm, {'attr': {novalidate: 'novalidate', 'class': 'd-flex flex-column flex-sm-row mb-0 align-items-center pt-3'}}) }} + {{ form_row(filterByGroupForm.name, {'attr': {'class': ''}}) }} + {{ form_row(filterByGroupForm.filter, {'attr': {'class': 'ms-2'}}) }} + {{ form_end(filterByGroupForm) }} + </div> + + + <form class="d-flex mb-4 mb-lg-0"> <button id="btn-orange" formaction="/create"> + {{ 'capsule.create_capsule'|trans }} </button> @@ -20,12 +29,6 @@ </div> </div> - <form class="d-md-none d-flex justify-content-center align-items-center"> - <button id="btn-orange" formaction="/create"> - + {{ 'capsule.create_capsule'|trans }} - </button> - </form> - {% for flashWarning in app.flashes('warning') %} <div class="text-center alert alert-warning col-5 mx-auto my-5" role="alert"> {{ flashWarning }} @@ -41,7 +44,7 @@ <div class="d-flex flex-column align-items-center mt-4 mb-4"> {% for capsule in capsules %} <div class="capsule-item pb-4 col-12 col-sm-11 col-md-10 col-xxl-8"> - <div class="d-flex flex-column justify-content-center align-items-center mt-sm-4"> + <div class="d-flex flex-column flex-md-row justify-content-center align-items-center mt-sm-4"> <div class="list-item"> <a href="/capsule/preview/{{ capsule.getLinkPath() }}" class="capsule-title"> {{ capsule.getName() }} diff --git a/translations/messages.en.yaml b/translations/messages.en.yaml index a6d1235d8b0411704a0bbf75c2d865e8db040e07..58e65a0609fdf62242e707e7d53bb60e3f66e9c6 100644 --- a/translations/messages.en.yaml +++ b/translations/messages.en.yaml @@ -179,4 +179,6 @@ groups: link: Remove group text: Do you really want to remove group %group_name% ? title: Remove group - success: Group group_name removed successfully \ No newline at end of file + success: Group group_name removed successfully + filter: + button: Filter by group \ No newline at end of file diff --git a/translations/messages.fr.yaml b/translations/messages.fr.yaml index 81fa761aef9b7f120627c6ddf343dd1038212eab..63d83b0278e2fc16a01bd02a31089ba9ed6ee1aa 100644 --- a/translations/messages.fr.yaml +++ b/translations/messages.fr.yaml @@ -178,4 +178,6 @@ groups: link: Supprimer le groupe text: Souhaitez-vous vraiment supprimer le groupe %group_name% ? title: Supprimer le groupe - success: Le groupe group_name a bien été supprimé \ No newline at end of file + success: Le groupe group_name a bien été supprimé + filter: + button: Filtrer par groupe \ No newline at end of file