From a664e9a9bd4978cd20e8ba876de448e392ae3118 Mon Sep 17 00:00:00 2001 From: Sarahshr <51380592+Sarahshr@users.noreply.github.com> Date: Thu, 21 Dec 2023 14:21:58 +0100 Subject: [PATCH] Feat/disable services 662 (#669) * disable button for services * add button to disable services --------- Co-authored-by: Sarahshr --- fixtures/test/configuration.yaml | 2 + src/Controller/Product/ProductController.php | 31 ++- src/Controller/User/Account/ProfileAction.php | 3 + src/Controller/User/MyAccountAction.php | 4 +- .../User/Product/ServiceController.php | 51 ++-- .../User/Product/UserProductsController.php | 9 +- src/Entity/Configuration.php | 18 ++ src/Form/Type/Admin/ParametersFormType.php | 8 + .../Command/Admin/ParametersFormCommand.php | 6 + src/Repository/ConfigurationRepository.php | 12 + templates/admin/parameters.html.twig | 5 + .../components/product/_lender.html.twig | 2 +- .../components/product/_section.html.twig | 22 +- .../components/product/_tab_content.html.twig | 2 +- templates/pages/account/index.html.twig | 227 +++++++++--------- templates/pages/product/list.html.twig | 4 +- .../pages/user/account/profile.html.twig | 2 +- .../Admin/ParametersControllerTest.php | 4 +- translations/admin.fr.xlf | 5 + translations/parameters/admin.fr.xlf | 6 + 20 files changed, 266 insertions(+), 157 deletions(-) diff --git a/fixtures/test/configuration.yaml b/fixtures/test/configuration.yaml index 0fbb4b4..74fe9ee 100644 --- a/fixtures/test/configuration.yaml +++ b/fixtures/test/configuration.yaml @@ -4,6 +4,8 @@ App\Entity\Configuration: features (extends configuration_template): configuration: + services: + servicesEnabled: true notificationsSender: notificationsSenderEmail: info@example.com notificationsSenderName: Contact diff --git a/src/Controller/Product/ProductController.php b/src/Controller/Product/ProductController.php index fdd5f30..ea2b278 100644 --- a/src/Controller/Product/ProductController.php +++ b/src/Controller/Product/ProductController.php @@ -10,14 +10,19 @@ use App\Controller\RequestTrait; use App\Dto\Product\Search; use App\Entity\Product; use App\Entity\User; +use App\Enum\Product\ProductType; use App\Form\Type\Product\SearchFormType; use App\Message\Query\Product\GetProductByIdQuery; use App\MessageBus\QueryBus; +use App\Repository\ConfigurationRepository; +use App\Repository\ProductRepository; use App\Search\Meilisearch; use Knp\Component\Pager\PaginatorInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\GoneHttpException; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Messenger\Exception\HandlerFailedException; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Requirement\Requirement; @@ -42,6 +47,8 @@ final class ProductController extends AbstractController private readonly QueryBus $queryBus, private readonly PaginatorInterface $paginator, private readonly Meilisearch $meilisearch, + private readonly ConfigurationRepository $configurationRepository, + private readonly ProductRepository $productRepository, ) { } @@ -64,6 +71,7 @@ final class ProductController extends AbstractController 'objects_pagination' => $this->paginate($this->meilisearch->searchObjects($searchDto)), 'services_pagination' => $this->paginate($this->meilisearch->searchServices($searchDto)), 'search_form' => $searchForm, + 'services_enabled' => $this->configurationRepository->getServicesParameter(), ]); } @@ -79,13 +87,24 @@ final class ProductController extends AbstractController )] public function show(string $slug, string $id): Response { - try { - /** @var Product $product */ - $product = $this->queryBus->query(new GetProductByIdQuery(Uuid::fromString($id))); - } catch (HandlerFailedException $e) { - throw $this->createNotFoundException($e->getMessage()); + /** @var ?Product $product */ + $product = $this->productRepository->find(['id' => $id]); + + if ($product === null) { + throw new NotFoundHttpException(); } - return $this->render('pages/product/show.html.twig', compact('slug', 'id', 'product')); + if (($product->getType() === ProductType::SERVICE && $this->configurationRepository->getServicesParameter()) || $product->getType() === ProductType::OBJECT) { + try { + /** @var Product $product */ + $product = $this->queryBus->query(new GetProductByIdQuery(Uuid::fromString($id))); + } catch (HandlerFailedException $e) { + throw $this->createNotFoundException($e->getMessage()); + } + + return $this->render('pages/product/show.html.twig', compact('slug', 'id', 'product')); + } else { + throw new GoneHttpException(); + } } } diff --git a/src/Controller/User/Account/ProfileAction.php b/src/Controller/User/Account/ProfileAction.php index 9e9b6c5..e2672ff 100644 --- a/src/Controller/User/Account/ProfileAction.php +++ b/src/Controller/User/Account/ProfileAction.php @@ -10,6 +10,7 @@ use App\Dto\Product\Search; use App\Entity\User; use App\Message\Query\User\Account\GetUserQuery; use App\MessageBus\QueryBus; +use App\Repository\ConfigurationRepository; use App\Search\Meilisearch; use Knp\Component\Pager\PaginatorInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -33,6 +34,7 @@ final class ProfileAction extends AbstractController private readonly QueryBus $queryBus, private readonly PaginatorInterface $paginator, private readonly Meilisearch $meilisearch, + private readonly ConfigurationRepository $configurationRepository, ) { } @@ -59,6 +61,7 @@ final class ProfileAction extends AbstractController 'user' => $user, 'objects_pagination' => $this->paginate($this->meilisearch->searchObjects($searchDto)), 'services_pagination' => $this->paginate($this->meilisearch->searchServices($searchDto)), + 'services_enabled' => $this->configurationRepository->getServicesParameter(), ]); } } diff --git a/src/Controller/User/MyAccountAction.php b/src/Controller/User/MyAccountAction.php index 8808169..69c51e8 100644 --- a/src/Controller/User/MyAccountAction.php +++ b/src/Controller/User/MyAccountAction.php @@ -47,6 +47,8 @@ final class MyAccountAction extends AbstractController $canCreateGroup = $configuration->isGroupsCreationForAll() || $user->isAdmin(); $contactEmail = $configuration->getContactEmail(); - return $this->render('pages/account/index.html.twig', compact('userHasNewLoanMessage', 'userHasNewLendingMessage', 'canCreateGroup', 'contactEmail')); + $servicesConfig = $this->configurationRepository->getServicesParameter(); + + return $this->render('pages/account/index.html.twig', compact('userHasNewLoanMessage', 'userHasNewLendingMessage', 'canCreateGroup', 'contactEmail', 'servicesConfig')); } } diff --git a/src/Controller/User/Product/ServiceController.php b/src/Controller/User/Product/ServiceController.php index ba51c18..f5fbe9b 100644 --- a/src/Controller/User/Product/ServiceController.php +++ b/src/Controller/User/Product/ServiceController.php @@ -11,12 +11,14 @@ use App\Entity\Product; use App\Entity\User; use App\Form\Type\Product\ServiceFormType; use App\MessageBus\QueryBus; +use App\Repository\ConfigurationRepository; use App\Tests\Functional\Controller\Product\ServiceControllerTest; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\GoneHttpException; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Requirement\Requirement; use Symfony\Component\Security\Http\Attribute\CurrentUser; @@ -37,6 +39,7 @@ final class ServiceController extends AbstractController public function __construct( private readonly QueryBus $queryBus, private readonly ProductManager $productManager, + private readonly ConfigurationRepository $configurationRepository, ) { } @@ -51,18 +54,22 @@ final class ServiceController extends AbstractController ], name: 'new')] public function new(Request $request, #[CurrentUser] User $user): Response { - $product = $this->productManager->initService($user); - $form = $this->getForm($product, $request); - if ($form->isSubmitted() && $form->isValid()) { - /** @var array|null $images */ - $images = $form->get('images')->getData(); - $this->productManager->multipleUpload($images, $product); - $this->productManager->save($product, true); + if ($this->configurationRepository->getServicesParameter()) { + $product = $this->productManager->initService($user); + $form = $this->getForm($product, $request); + if ($form->isSubmitted() && $form->isValid()) { + /** @var array|null $images */ + $images = $form->get('images')->getData(); + $this->productManager->multipleUpload($images, $product); + $this->productManager->save($product, true); - return $this->redirectToRoute('app_product_show', $product->getRoutingParameters()); + return $this->redirectToRoute('app_product_show', $product->getRoutingParameters()); + } + + return $this->render('pages/product/new_service.html.twig', compact('form')); + } else { + throw new GoneHttpException(); } - - return $this->render('pages/product/new_service.html.twig', compact('form')); } #[Route([ @@ -74,17 +81,21 @@ final class ServiceController extends AbstractController )] public function edit(string $id, Request $request): Response { - $product = $this->getProductForEdit($id); - $form = $this->getForm($product, $request); - if ($form->isSubmitted() && $form->isValid()) { - /** @var array|null $images */ - $images = $form->get('images')->getData(); - $this->productManager->multipleUpload($images, $product); - $this->productManager->save($product, true); + if ($this->configurationRepository->getServicesParameter()) { + $product = $this->getProductForEdit($id); + $form = $this->getForm($product, $request); + if ($form->isSubmitted() && $form->isValid()) { + /** @var array|null $images */ + $images = $form->get('images')->getData(); + $this->productManager->multipleUpload($images, $product); + $this->productManager->save($product, true); - return $this->redirectToRoute('app_product_show', $product->getRoutingParameters()); + return $this->redirectToRoute('app_product_show', $product->getRoutingParameters()); + } + + return $this->render('pages/product/edit_service.html.twig', compact('form', 'product')); + } else { + throw new GoneHttpException(); } - - return $this->render('pages/product/edit_service.html.twig', compact('form', 'product')); } } diff --git a/src/Controller/User/Product/UserProductsController.php b/src/Controller/User/Product/UserProductsController.php index 50d9a44..54e7eb5 100644 --- a/src/Controller/User/Product/UserProductsController.php +++ b/src/Controller/User/Product/UserProductsController.php @@ -15,12 +15,14 @@ use App\Message\Query\User\GetUserObjectsQuery; use App\Message\Query\User\GetUserServicesQuery; use App\MessageBus\QueryBus; use App\Repository\CategoryRepository; +use App\Repository\ConfigurationRepository; use Doctrine\ORM\Query; use Knp\Component\Pager\Pagination\PaginationInterface; use Knp\Component\Pager\PaginatorInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\GoneHttpException; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Security\Http\Attribute\CurrentUser; use Symfony\Component\Security\Http\Attribute\IsGranted; @@ -40,6 +42,7 @@ final class UserProductsController extends AbstractController private readonly QueryBus $queryBus, private readonly PaginatorInterface $paginator, public readonly CategoryRepository $categoryRepository, + private readonly ConfigurationRepository $configurationRepository, ) { } @@ -89,6 +92,10 @@ final class UserProductsController extends AbstractController $query = $this->queryBus->query(new GetUserServicesQuery($user->getId(), $category?->getId())); $pagination = $this->paginate($query, $this->getPage($request)); - return $this->render('pages/account/product/list.html.twig', compact('pagination', 'form')); + if ($this->configurationRepository->getServicesParameter()) { + return $this->render('pages/account/product/list.html.twig', compact('pagination', 'form')); + } else { + throw new GoneHttpException('there is no services'); + } } } diff --git a/src/Entity/Configuration.php b/src/Entity/Configuration.php index 89415b4..86984f6 100644 --- a/src/Entity/Configuration.php +++ b/src/Entity/Configuration.php @@ -75,6 +75,24 @@ class Configuration /** end of basic getters and setters ------------------------------------------------ */ + /** + * @return bool[] + */ + public function getServices(): array + { + /** @var array $services */ + $services = $this->configuration['services'] ?? []; + + return $services; + } + + public function getServicesEnabled(): bool + { + $services = $this->getServices(); + + return $services['servicesEnabled']; + } + /** * @return array */ diff --git a/src/Form/Type/Admin/ParametersFormType.php b/src/Form/Type/Admin/ParametersFormType.php index 91ae35a..c4e7735 100755 --- a/src/Form/Type/Admin/ParametersFormType.php +++ b/src/Form/Type/Admin/ParametersFormType.php @@ -25,10 +25,18 @@ final class ParametersFormType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options): void { $builder + ->add('servicesEnabled', CheckboxType::class, [ + 'label' => 'parameter.services', + 'label_attr' => [ + 'class' => 'checkbox-inline checkbox-switch', + ], + ]) + ->add('notificationsSenderEmail', EmailType::class, [ 'label' => 'parameter.mail', 'label_attr' => ['class' => 'col-sm-2 col-form-label'], ]) + ->add('notificationsSenderName', TextType::class, [ 'label' => 'parameter.name', ]) diff --git a/src/Message/Command/Admin/ParametersFormCommand.php b/src/Message/Command/Admin/ParametersFormCommand.php index 7e836af..c719a45 100755 --- a/src/Message/Command/Admin/ParametersFormCommand.php +++ b/src/Message/Command/Admin/ParametersFormCommand.php @@ -17,6 +17,10 @@ final class ParametersFormCommand extends AbstractFormCommand final public const ONLY_ADMIN = 'only_admin'; final public const ALL = 'all'; + // services section ————————————————————————————————————————————— + #[Assert\Type('bool')] + public bool $servicesEnabled = true; + // notificationsSender section ————————————————————————————————————————————— #[Assert\Email()] #[Assert\NotBlank()] @@ -55,6 +59,7 @@ final class ParametersFormCommand extends AbstractFormCommand protected function getSections(): array { return [ + 'services', 'notificationsSender', 'contact', 'groups', @@ -71,6 +76,7 @@ final class ParametersFormCommand extends AbstractFormCommand public function hydrate(Configuration $configuration): self { $instanceConfiguration = $configuration->getConfiguration(); +// dd($instanceConfiguration); foreach (array_keys(get_class_vars($this::class)) as $classVar) { $this->{$classVar} = $instanceConfiguration[$this->getSection($classVar)][$classVar]; // @phpstan-ignore-line } diff --git a/src/Repository/ConfigurationRepository.php b/src/Repository/ConfigurationRepository.php index 3826420..9601154 100644 --- a/src/Repository/ConfigurationRepository.php +++ b/src/Repository/ConfigurationRepository.php @@ -58,4 +58,16 @@ final class ConfigurationRepository extends ServiceEntityRepository return $cfg; } + + public function getServicesParameter(): bool + { + /** @var array{configuration: array{ services: array{ servicesEnabled: bool }}} $config */ + $config = $this + ->createQueryBuilder('c') + ->select('c.configuration') + ->setMaxResults(1) + ->getQuery()->getOneOrNullResult(); + + return $config['configuration']['services']['servicesEnabled']; + } } diff --git a/templates/admin/parameters.html.twig b/templates/admin/parameters.html.twig index 2f7f82b..435fdda 100644 --- a/templates/admin/parameters.html.twig +++ b/templates/admin/parameters.html.twig @@ -14,6 +14,11 @@ {% block main %} {{ form_start(form) }}
+

{{ 'parameters.services.h2'|trans }}

+
+ + {{ form_widget(form.servicesEnabled) }} +

{{ 'parameters.senders.h2'|trans }}


diff --git a/templates/components/product/_lender.html.twig b/templates/components/product/_lender.html.twig index 1ab743f..08883c0 100644 --- a/templates/components/product/_lender.html.twig +++ b/templates/components/product/_lender.html.twig @@ -15,7 +15,7 @@

{{ name }}

- {% if isPlace %} + {% if isPlace and address is not null %}

{{ 'templates.components.product.lender.address'|trans }}

diff --git a/templates/components/product/_section.html.twig b/templates/components/product/_section.html.twig index a06a0a0..4747b1d 100644 --- a/templates/components/product/_section.html.twig +++ b/templates/components/product/_section.html.twig @@ -11,16 +11,18 @@ {{ 'product.list_type_objects'|trans }} ({{ objects_pagination.totalItemCount }}) - + {% if services_enabled %} + + {% endif %}
diff --git a/templates/components/product/_tab_content.html.twig b/templates/components/product/_tab_content.html.twig index 95a3585..123fb34 100644 --- a/templates/components/product/_tab_content.html.twig +++ b/templates/components/product/_tab_content.html.twig @@ -15,7 +15,7 @@ pagination: objects_pagination, } %} - {% if services_pagination is defined and services_pagination is not null %} + {% if services_pagination is defined and services_pagination is not null and services_enabled %}
{% for my_account_link in my_account_links %} -
- -
+ {% endfor %} +
+ + + {% endif %} {% endfor %}
diff --git a/templates/pages/product/list.html.twig b/templates/pages/product/list.html.twig index 44971f4..49be74e 100644 --- a/templates/pages/product/list.html.twig +++ b/templates/pages/product/list.html.twig @@ -4,8 +4,8 @@
{% include 'components/layout/_title_3.html.twig' with {name: 'product.list_name'|trans} %} {% include 'components/product/_search.html.twig' with {form: search_form} %} - {% include 'components/product/_section.html.twig'with {objects_pagination, services_pagination} %} - {% include 'components/product/_tab_content.html.twig' with {objects_pagination, services_pagination} %} + {% include 'components/product/_section.html.twig'with {objects_pagination, services_pagination, services_enabled} %} + {% include 'components/product/_tab_content.html.twig' with {objects_pagination, services_pagination, services_enabled} %}
{% endblock %} diff --git a/templates/pages/user/account/profile.html.twig b/templates/pages/user/account/profile.html.twig index b814169..e0e7a31 100644 --- a/templates/pages/user/account/profile.html.twig +++ b/templates/pages/user/account/profile.html.twig @@ -56,7 +56,7 @@ {{ (i18n_prefix ~ '.no_result')|trans }}
{% else %} - {% include 'components/product/_section.html.twig' %} + {% include 'components/product/_section.html.twig' with {services_enabled} %} {% include 'components/product/_tab_content.html.twig' with {objects_pagination, services_pagination} %} {% endif %} diff --git a/tests/Functional/Controller/Admin/ParametersControllerTest.php b/tests/Functional/Controller/Admin/ParametersControllerTest.php index e4f6ba9..0eca960 100644 --- a/tests/Functional/Controller/Admin/ParametersControllerTest.php +++ b/tests/Functional/Controller/Admin/ParametersControllerTest.php @@ -34,7 +34,7 @@ final class ParametersControllerTest extends WebTestCase $form = $crawler->selectButton('parameters_form_submit')->form(); - self::assertSame(5, $crawler->filter('input:checked')->count()); + self::assertSame(6, $crawler->filter('input:checked')->count()); /** @var FormField $notificationsSenderEmailField */ $notificationsSenderEmailField = $form->get('parameters_form[notificationsSenderEmail]'); @@ -69,7 +69,7 @@ final class ParametersControllerTest extends WebTestCase $crawler = $client->followRedirect(); self::assertResponseIsSuccessful(); - self::assertSame(1, $crawler->filter('input:checked')->count()); + self::assertSame(2, $crawler->filter('input:checked')->count()); $form = $crawler->selectButton('parameters_form_submit')->form(); /** @var FormField $groupsCreationModeField */ diff --git a/translations/admin.fr.xlf b/translations/admin.fr.xlf index ef10811..7e4880a 100644 --- a/translations/admin.fr.xlf +++ b/translations/admin.fr.xlf @@ -260,6 +260,11 @@ Paramètres de l'instance + + parameters.services.h2 + Services + + parameters.senders.h2 Expéditeur·rice des notifications diff --git a/translations/parameters/admin.fr.xlf b/translations/parameters/admin.fr.xlf index e232273..87cfc04 100644 --- a/translations/parameters/admin.fr.xlf +++ b/translations/parameters/admin.fr.xlf @@ -6,6 +6,12 @@ + + + parameter.services + Services activés + + parameter.mail