src/Controller/AppealController.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Document\Appeal;
  4. use App\Document\AppealExecution;
  5. use App\Document\Location;
  6. use App\Finder\Appeals\AppealsFinder;
  7. use App\Form\Type\AppealsExecutionType;
  8. use App\Form\Type\AppealsType;
  9. use App\Service\AttributeResolver;
  10. use App\Service\AttributesUpdater;
  11. use App\Service\RatingUpdater;
  12. use App\Service\ResourceFinders\CarriageResourcesFinders\PartiallyBedPlacesResourcesFinder;
  13. use App\Service\ResourceFinders\ResourceFinderFactory\ResourceFinderFactory;
  14. use App\Service\Statuses;
  15. use App\Service\ResourceFinders\CarriageResourcesFinders\StrictlyCarriageHouseResourcesFinder;
  16. use App\Service\Workflow;
  17. use Doctrine\ODM\MongoDB\DocumentManager;
  18. use Exception;
  19. use Knp\Component\Pager\PaginatorInterface;
  20. use Psr\Log\LoggerInterface;
  21. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  22. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  23. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  24. use Symfony\Component\HttpFoundation\RedirectResponse;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\HttpFoundation\Response;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  29. class AppealController extends AbstractController
  30. {
  31.     public function __construct(
  32.         private DocumentManager $dm,
  33.         private LoggerInterface $logger,
  34.         private AttributeResolver $resolver,
  35.         private Workflow $workflow,
  36.         private AppealsFinder $finder,
  37.         private Statuses $statuses,
  38.         private RatingUpdater $rating,
  39.         private AttributesUpdater $updater,
  40.         private PaginatorInterface $paginator,
  41.         private ResourceFinderFactory $finderFactory
  42.     ) {
  43.     }
  44.     #[Route('/appeals'name'appeals'methods: ['GET',])]
  45.     #[IsGranted('IS_AUTHENTICATED_FULLY')]
  46.     public function index(Request $request): Response
  47.     {
  48.         $appealsExecute $this->finder->doFind(
  49.             $this->dm->createQueryBuilder(Appeal::class),
  50.             $request->query->all()
  51.         );
  52.         $appealsCount count($appealsExecute->getQuery()->execute()->toArray());
  53.         $appealsPaginator $this->paginator->paginate(
  54.             $appealsExecute->getQuery()->toArray(),
  55.             $request->query->getInt('page'1),
  56.             $request->query->get('pageSize',Appeal::NUMBERS_ON_PAGE),
  57.         );
  58.         return $this->render('appeals/index.html.twig', [
  59.             'appeals' => $appealsPaginator,
  60.             'appealsCount' => $appealsCount,
  61.             'types' => $this->resolver->getTypes(),
  62.             'locations' => $this->dm->getRepository(Location::class)->findAll(),
  63.             'statuses' => $this->statuses->get(new Appeal()),
  64.             'title' => 'Заявки',
  65.             'pageSize' => Appeal::pageSize(),
  66.         ]);
  67.     }
  68.     #[Route('/my-appeals'name'my-appeals'methods: ['GET',])]
  69.     #[IsGranted('IS_AUTHENTICATED_FULLY')]
  70.     public function myAppeals(Request $request): Response
  71.     {
  72.         $appealsExecute $this->finder->doFind(
  73.             $this->dm->createQueryBuilder(Appeal::class)
  74.                 ->field('author.id')->equals($this->getUser()->getId()),
  75.             $request->query->all()
  76.         );
  77.         $appealsCount count($appealsExecute->getQuery()->execute()->toArray());
  78.         $appealsPaginator $this->paginator->paginate(
  79.             $appealsExecute->getQuery()->toArray(),
  80.             $request->query->getInt('page'1),
  81.             $request->query->get('pageSize',Appeal::NUMBERS_ON_PAGE),
  82.         );
  83.         return $this->render('appeals/index.html.twig', [
  84.             'appeals' => $appealsPaginator,
  85.             'appealsCount' => $appealsCount,
  86.             'types' => $this->resolver->getTypes(),
  87.             'locations' => $this->dm->getRepository(Location::class)->findAll(),
  88.             'statuses' => $this->statuses->get(new Appeal()),
  89.             'title' => 'Мои заявки',
  90.             'pageSize' => Appeal::pageSize(),
  91.         ]);
  92.     }
  93.     #[Route('/appeals/new'name'new_appeal'methods: ['POST''GET'])]
  94.     #[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_APPLICANT')")]
  95.     public function create(Request $request): Response
  96.     {
  97.         $appeal = new Appeal();
  98.         $appeal->setAuthor($this->getUser());
  99.         $appeal->setType($request->query->get('type'));
  100.         $form $this->createForm(AppealsType::class, $appeal);
  101.         $form->add('attributes'$this->resolver->resolve($request->query->get('type')));
  102.         $form->handleRequest($request);
  103.         if ($form->isSubmitted() && $form->isValid()) {
  104.             if($request->query->get('type') == 'special-equipment'){
  105.                 if(empty($form->get('attributes')->get('auctionType')->getData())) {
  106.                     $this->addFlash('warning''Выберите тип аукциона');
  107.                     return $this->redirect($request->headers->get('referer'));
  108.                 }
  109.                 if($size $form->get('attributes')->get('additionalFeature')->get('dimension')->getData()) {
  110.                     foreach ($size as $value) {
  111.                         if (empty($value)){
  112.                             $attr $appeal->getAttributes()->getAdditionalFeature();
  113.                             $attr->setDimension([]);
  114.                             break;
  115.                         }
  116.                     }
  117.                 }
  118.             }
  119.             $appeal->setStatus(Appeal::STATUS_SEARCH);
  120.             $this->dm->persist($appeal);
  121.             $this->dm->flush();
  122.             return $this->redirectToRoute('show_appeal', ['id' => $appeal->getId()]);
  123.         }
  124.         return $this->render('appeals/create.html.twig', [
  125.             'form' => $form->createView(),
  126.             'type' => $request->query->get('type'),
  127.         ]);
  128.     }
  129.     #[Route('/appeals/{id}'name'show_appeal'methods: ['GET',])]
  130.     #[ParamConverter('appeal', class: Appeal::class)]
  131.     #[IsGranted('IS_AUTHENTICATED_FULLY')]
  132.     public function show(Appeal $appealRequest $request): Response
  133.     {
  134.         $this->finderFactory->setAppealType($appeal->getType());
  135.         $strictlyFinder $this->finderFactory->createStrictlyFinder();
  136.         $partiallyFinder $this->finderFactory->createPartiallyResourceFinder();
  137.         return $this->render('appeals/show.html.twig', [
  138.             'appeal' => $appeal,
  139.             'strictlyResources' => $strictlyFinder->find($appeal),
  140.             'partiallyResourcesWithComments' => $partiallyFinder->find($appeal),
  141.             'bookedToResource' => $this->dm->getRepository(AppealExecution::class)
  142.                 ->getBookedToResource($appeal),
  143.             'bookingCancelled' => $this->dm->getRepository(AppealExecution::class)
  144.                 ->getBookingCancelled($appeal),
  145.         ]);
  146.     }
  147.     #[Route('/appeals/{id}/edit'name'edit_appeal'methods: ['GET''POST'])]
  148.     #[ParamConverter('appeal', class: Appeal::class)]
  149.     #[Security("is_granted('ROLE_ADMIN') or appeal.isAuthor(user)")]
  150.     public function edit(Appeal $appealRequest $request): Response
  151.     {
  152.         $form $this->createForm(AppealsType::class, $appeal);
  153.         $form->add('attributes'$this->resolver->resolve($appeal->getType()));
  154.         $form->handleRequest($request);
  155.         if ($form->isSubmitted() && $form->isValid()) {
  156.             $appeal->setStatus(Appeal::STATUS_NEW);
  157.             $this->updater->update($appeal$form->get('attributes')->getData());
  158.             $this->dm->persist($appeal);
  159.             $this->dm->flush();
  160.             return $this->redirectToRoute('show_appeal', ['id' => $appeal->getId()]);
  161.         }
  162.         return $this->render('appeals/edit.html.twig', [
  163.             'form' => $form->createView()
  164.         ]);
  165.     }
  166.     #[Route('appeals/{id}/remove'name'remove_appeal'methods: ['GET''POST'])]
  167.     #[ParamConverter('appeal', class: Appeal::class)]
  168.     #[Security("is_granted('ROLE_ADMIN') or appeal.isAuthor(user)")]
  169.     public function remove(Appeal $appealRequest $request): RedirectResponse|Response
  170.     {
  171.         try {
  172.             $executionsForAppeal $this->dm->createQueryBuilder(AppealExecution::class)
  173.                 ->field('appeal.id')->equals($appeal->getId())
  174.                 ->field('status')->in(['booked''in_work''in_confirmation'])
  175.                 ->getQuery()->execute();
  176.             if (count($executionsForAppeal->toArray()) > 0) {
  177.                 $this->addFlash('warning''Не получилось удалить заявку: '
  178.                     'она забронирована или на согласовании');
  179.             } else {
  180.                 $this->dm->remove($appeal);
  181.                 $this->dm->flush();
  182.             }
  183.         } catch (Exception $e) {
  184.             $this->logger->error('Appeal remove error', [$e->getCode(), $e->getMessage(),]);
  185.         }
  186.         return $this->redirect($request->headers->get('referer'));
  187.     }
  188.     #[Route('appeals/{id}/done'name'rating_appeal'methods: ['GET''POST',])]
  189.     #[ParamConverter('execution', class: AppealExecution::class)]
  190.     #[Security("is_granted('ROLE_ADMIN')")]
  191.     public function done(AppealExecution $executionRequest $request): Response
  192.     {
  193.         $form $this->createForm(AppealsExecutionType::class, $execution);
  194.         $form->handleRequest($request);
  195.         if ($form->isSubmitted() && $form->isValid()) {
  196.             try {
  197.                 $this->workflow->doneByApplicant($execution);
  198.                 $this->dm->flush();
  199.                 $this->rating->updateResourceRating($execution);
  200.                 $this->rating->updateAuthorRating($execution);
  201.             } catch (Exception $e) {
  202.                 $this->logger->error('Can not done appeal by applicant', [$e->getMessage(), $e->getCode()]);
  203.             }
  204.             return $this->redirectToRoute('desktop');
  205.         }
  206.         return $this->render('appeals/rating.html.twig', [
  207.             'execution' => $execution,
  208.             'form' => $form->createView(),
  209.         ]);
  210.     }
  211. }