src/Controller/ClubController.php line 85

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Psr\Log\LoggerInterface;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use App\Util\DateIntervalUtils;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use App\Entity\ClubPrice;
  11. use App\Entity\EntityFinder;
  12. use App\Entity\Club;
  13. use App\Security\ClubAccess;
  14. use App\Service\ClubService;
  15. use Hateoas\HateoasBuilder;
  16. use App\Security\Roles;
  17. use App\Entity\ClubLesson;
  18. use App\Model\ClubLessonView;
  19. class ClubController extends AbstractController
  20. {
  21.     private $logger;
  22.     
  23.     public function __construct(LoggerInterface $logger)
  24.     {
  25.         $this->logger $logger;
  26.     }
  27.     
  28.     
  29.     /**
  30.      * @Route("/club", name="web_club_list-active", methods={"GET"})
  31.      */
  32.     public function listActive(Request $requestSessionInterface $session)
  33.     {
  34.         if($request->query->get('select') === 'clear') {
  35.             $session->remove('club-selected');
  36.             $session->remove('lessons-selected');
  37.         }
  38.         $response $this->forward('App\Controller\Api\ClubSearchController::search', ["request" => $request]);
  39.         $json json_decode($response->getContent());
  40.         return $this->render('club/club-list.html.twig', [
  41.             'clubs' => $json
  42.         ]);
  43.     }
  44.     /**
  45.      * @Route("/clubs", name="web_clubs_list", methods={"GET"})
  46.      */
  47.     public function getAllClubs(Request $requestSessionInterface $session)
  48.     {
  49.         if(! $this->isGranted(Roles::ROLE_ADMIN)
  50.             && ! $this->isGranted(Roles::ROLE_SUPER_ADMIN)
  51.             && ! $this->isGranted(Roles::ROLE_CLUB_MANAGER)
  52.             && ! $this->isGranted(Roles::ROLE_TEACHER)) {
  53.                 throw $this->createAccessDeniedException();    
  54.         }
  55.         
  56.         $clubAccess = new ClubAccess($this->container$this->logger);
  57.         $clubs $clubAccess->getClubsForAccount($this->getUser());
  58.         
  59.         $clubService = new ClubService($this->container->get('doctrine'));
  60.         $clubViews $clubService->convertToView($clubs);
  61.        
  62.         $hateoas HateoasBuilder::create()->build();
  63.         
  64.         return $this->render('club/clubs.html.twig', [
  65.             'clubs' => json_decode($hateoas->serialize($clubViews'json'))
  66.         ]);
  67.     }
  68.     
  69.     /**
  70.      * @Route("/club/{uuid}", name="web_club_one", methods={"GET"}, requirements={"uuid"="[a-z0-9_]{2,64}"})
  71.      */
  72.     public function viewOne($uuidLoggerInterface $loggerSessionInterface $session)
  73.     {
  74.         $response $this->forward('App\Controller\Api\ClubController::one', ['uuid' => $uuid]);
  75.         if($response->getStatusCode() != 200) {
  76.             throw $this->createNotFoundException();
  77.         }
  78.         $club json_decode($response->getContent());
  79.         $session->set('club-selected'$club);
  80.         $response $this->forward('App\Controller\Api\ClubLessonsController::getLessons', ['club_uuid' => $uuid]);
  81.         $lessons json_decode($response->getContent());
  82.         $session->set('lessons-selected'$lessons);
  83.         return $this->render('club/club.html.twig', [
  84.             'club' => $club,
  85.             'lessons' => $lessons,
  86.             'canConfigure' => $this->canConfigure($uuid)
  87.         ]);
  88.     }
  89.     
  90.     /**
  91.      * @Route("/club/{uuid}/infos", name="web_club_infos", methods={"GET"}, requirements={"uuid"="[a-z0-9_]{2,64}"})
  92.      */
  93.     public function viewInfos($uuidLoggerInterface $loggerSessionInterface $session)
  94.     {
  95.         $response $this->forward('App\Controller\Api\ClubController::one', ['uuid' => $uuid]);
  96.         if($response->getStatusCode() != 200) {
  97.             throw $this->createNotFoundException();
  98.         }
  99.         $club json_decode($response->getContent());
  100.         $session->set('club-selected'$club);
  101.         $response $this->forward('App\Controller\Api\ClubLessonsController::getLessons', ['club_uuid' => $uuid]);
  102.         $lessons json_decode($response->getContent());
  103.         $session->set('lessons-selected'$lessons);
  104.         
  105.         return $this->render('club/club-infos.html.twig', [
  106.             'club' => $club,
  107.             'lessons' => $lessons,
  108.             'startTimeByDays' => $this->determineStartsOffsetByQuarter($lessons),
  109.             'canConfigure' => $this->canConfigure($uuid)
  110.         ]);
  111.     }
  112.     
  113.     /**
  114.      * @Route("/club/{uuid}/sc/{code}", name="web_club_static_custom", methods={"GET"}, requirements={"uuid"="[a-z0-9_]{2,64}","code"="[a-z0-9_]{2,64}"})
  115.      */
  116.     public function viewStaticCustom($uuid$codeSessionInterface $session)
  117.     {
  118.         $response $this->forward('App\Controller\Api\ClubController::one', ['uuid' => $uuid]);
  119.         if($response->getStatusCode() != 200) {
  120.             throw $this->createNotFoundException();
  121.         }
  122.         $club json_decode($response->getContent());
  123.         $session->set('club-selected'$club);
  124.         
  125.         $response $this->forward('App\Controller\Api\ClubLessonsController::getLessons', ['club_uuid' => $uuid]);
  126.         $lessons json_decode($response->getContent());
  127.         $session->set('lessons-selected'$lessons);
  128.         
  129.         return $this->render('club/'.$uuid.'/'.$code.'.html.twig', [
  130.             'club' => $club
  131.         ]);
  132.     }
  133.     
  134.     
  135.     /**
  136.      * @Route("/club-new", name="web_new_club", methods={"GET"})
  137.      */
  138.     public function create()
  139.     {
  140.         $this->denyAccessUnlessGranted(Roles::ROLE_ADMIN);
  141.         return $this->render('club/club-new.html.twig', []);
  142.     }
  143.     
  144.     
  145.     /**
  146.      * @Route("/club/{uuid}/modify", name="web_modify_club", methods={"GET"}, requirements={"uuid"="[a-z0-9_]{2,64}"})
  147.      */
  148.     public function modifyOne($uuidLoggerInterface $loggerSessionInterface $session)
  149.     {
  150.         if(! $this->isGranted(Roles::ROLE_ADMIN)
  151.             && ! $this->isGranted(Roles::ROLE_SUPER_ADMIN)
  152.             && ! $this->isGranted(Roles::ROLE_CLUB_MANAGER)
  153.             && ! $this->isGranted(Roles::ROLE_TEACHER)) {
  154.             throw $this->createAccessDeniedException();
  155.         }
  156.          
  157.         $doctrine $this->container->get('doctrine');
  158.         
  159.         $entityFinder = new EntityFinder($doctrine);
  160.         $club $entityFinder->findOneByOrThrow(Club::class, ['uuid' => $uuid]); // 404
  161.         
  162.         $clubAccess = new ClubAccess($this->container$this->logger);
  163.         $clubAccess->checkAccessForUser($club$this->getUser()); // 403
  164.         
  165.         $response $this->forward('App\Controller\Api\ClubController::one', ['uuid' => $uuid]);
  166.         if($response->getStatusCode() != 200) {
  167.             throw $this->createNotFoundException();
  168.         }
  169.         $club json_decode($response->getContent());
  170.         $session->set('club-selected'$club);
  171.         
  172.         return $this->render('club/club-modify.html.twig', [
  173.             'club' => $club
  174.         ]);
  175.     }
  176.     
  177.     /**
  178.      * @Route("/club/{uuid}/logo/modify", name="web_modify_club_logo", methods={"GET"}, requirements={"uuid"="[a-z0-9_]{2,64}"})
  179.      */
  180.     public function modifyLogo($uuidLoggerInterface $loggerSessionInterface $session)
  181.     {
  182.         if(! $this->isGranted(Roles::ROLE_ADMIN)
  183.             && ! $this->isGranted(Roles::ROLE_SUPER_ADMIN)
  184.             && ! $this->isGranted(Roles::ROLE_CLUB_MANAGER)
  185.             && ! $this->isGranted(Roles::ROLE_TEACHER)) {
  186.             throw $this->createAccessDeniedException();
  187.         }
  188.             
  189.         $doctrine $this->container->get('doctrine');
  190.         
  191.         $entityFinder = new EntityFinder($doctrine);
  192.         $club $entityFinder->findOneByOrThrow(Club::class, ['uuid' => $uuid]); // 404
  193.         
  194.         $clubAccess = new ClubAccess($this->container$this->logger);
  195.         $clubAccess->checkAccessForUser($club$this->getUser()); // 403
  196.         
  197.         $response $this->forward('App\Controller\Api\ClubController::one', ['uuid' => $uuid]);
  198.         if($response->getStatusCode() != 200) {
  199.             throw $this->createNotFoundException();
  200.         }
  201.         $club json_decode($response->getContent());
  202.         $session->set('club-selected'$club);
  203.         
  204.         return $this->render('club/club-logo-modify.html.twig', [
  205.             'club' => $club
  206.         ]);
  207.     }
  208.     
  209.     
  210.     //************************************************
  211.     
  212.     
  213.     private function determineStartsOffsetByQuarter($lessons)
  214.     {
  215.         if($lessons === null) {
  216.             return array();
  217.         }
  218.         /** @var ClubLessonView[] $lessons */
  219.         $startTimeMinuteByDays = array();
  220.         foreach($lessons as &$lesson) {
  221.             $startMinutes DateIntervalUtils::getTotalMinutes(DateIntervalUtils::parseHourDoubleDotsMinute($lesson->start_time));
  222.             $this->logger->debug('lesson '.$lesson->day_of_week.' '.$lesson->start_time.' : '.$startMinutes);
  223.             if(array_key_exists($lesson->day_of_week$startTimeMinuteByDays)) {
  224.                 $startTimeMinuteByDays[$lesson->day_of_week] = min($startMinutes$startTimeMinuteByDays[$lesson->day_of_week]);
  225.                 $this->logger->debug('exists '.$startTimeMinuteByDays[$lesson->day_of_week].' with '.$startMinutes);
  226.             } else {
  227.                 $startTimeMinuteByDays[$lesson->day_of_week] = $startMinutes;
  228.                 $this->logger->debug('not exists '.$startTimeMinuteByDays[$lesson->day_of_week]);
  229.             }
  230.         }
  231.         $minStartMinutes min($startTimeMinuteByDays);
  232.         $this->logger->debug('minStartMinutes: '.$minStartMinutes);
  233.         
  234.         $startOffsetByDays = array();
  235.         foreach ($startTimeMinuteByDays as $day => $minutes) {
  236.             $startOffsetByDays[$day] = ceil(($minutes $minStartMinutes) / 15);
  237.             $this->logger->debug($day.' => : ('.$minutes.' - '.$minStartMinutes.')/15 = '.$startOffsetByDays[$day]);
  238.         }
  239.         asort($startOffsetByDays);
  240.         $previous 0;
  241.         $diff 0;
  242.         foreach ($startOffsetByDays as $day => $minutes) {
  243.             if($minutes != 0) {
  244.                 if($previous == 0) {
  245.                    $d 0;
  246.                 } else {
  247.                    $d $minutes $previous;
  248.                 }
  249.                 if($d 1) {
  250.                     $diff $d 1;
  251.                 }
  252.                 $previous $startOffsetByDays[$day];
  253.                 $startOffsetByDays[$day] = $startOffsetByDays[$day] - $diff;
  254.                 $this->logger->debug($day.' => '.$minutes.' - '.$previous.' = '.$d.' >> '.$startOffsetByDays[$day]);
  255.             } else {
  256.                 $this->logger->debug($day.' OFF');
  257.             }
  258.         }
  259.         return $startOffsetByDays;
  260.     }
  261.     
  262.     
  263.     private function canConfigure($club_uuid): bool
  264.     {
  265.         $doctrine $this->container->get('doctrine');
  266.         $entityFinder = new EntityFinder($doctrine);
  267.         $clubObj $entityFinder->findOneByOrThrow(Club::class, ['uuid' => $club_uuid]); // 404, never happen !
  268.         
  269.         $clubAccess = new ClubAccess($this->container$this->logger);
  270.        return $clubAccess->hasAccessForUser($clubObj$this->getUser());
  271.         
  272.     }
  273. }