src/Controller/Client/v2/DashController.php line 79

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Client\v2;
  3. use App\Entity\Client;
  4. use App\Entity\Company;
  5. use App\Entity\Segmentation;
  6. use App\Entity\User;
  7. use App\Form\Client\SegmentationAxesType;
  8. use App\Form\Client\SegmentationType;
  9. use App\Repository\ArticleRepository;
  10. use App\Repository\ChannelRepository;
  11. use App\Repository\ProgramRepository;
  12. use App\Repository\RegistrationRepository;
  13. use App\Repository\SlideRepository;
  14. use App\Repository\TeamplayRepository;
  15. use App\Repository\TeamRepository;
  16. use App\Repository\VideoRepository;
  17. use App\Service\LogService;
  18. use App\Service\SegmentationService;
  19. use Doctrine\ORM\EntityManagerInterface;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. /**
  26.  * @isGranted("ROLE_CLIENT")
  27.  */
  28. class DashController extends AbstractController
  29. {
  30.     private $em;
  31.     private $teamRepository;
  32.     private $teamplayRepository;
  33.     private $registrationRepository;
  34.     private $slideRepository;
  35.     private $programRepository;
  36.     private $channelRepository;
  37.     private $videoRepository;
  38.     private $articleRepository;
  39.     private $segmentationService;
  40.     /**
  41.      * @var LogService
  42.      */
  43.     private $logService;
  44.     
  45.     public function __construct(
  46.         EntityManagerInterface $em,
  47.         TeamRepository $teamRepository,
  48.         TeamplayRepository $teamplayRepository,
  49.         RegistrationRepository $registrationRepository,
  50.         SlideRepository $slideRepository,
  51.         ProgramRepository $programRepository,
  52.         ChannelRepository $channelRepository,
  53.         VideoRepository $videoRepository,
  54.         ArticleRepository $articleRepository,
  55.         SegmentationService $segmentationService,
  56.         LogService $logService
  57.     )
  58.     {
  59.         $this->em = $em;
  60.         $this->teamRepository = $teamRepository;
  61.         $this->teamplayRepository = $teamplayRepository;
  62.         $this->registrationRepository = $registrationRepository;
  63.         $this->slideRepository = $slideRepository;
  64.         $this->programRepository = $programRepository;
  65.         $this->channelRepository = $channelRepository;
  66.         $this->videoRepository = $videoRepository;
  67.         $this->articleRepository = $articleRepository;
  68.         $this->segmentationService = $segmentationService;
  69.         $this->logService = $logService;
  70.     }
  71.     /**
  72.      * @Route("/", name="dashboard")
  73.      */
  74.     public function index(Request $request): Response
  75.     {   
  76.         $client = $allowedSegmentations = null;
  77.         $formView  = null;
  78.         $segmentationAxesFormView = null;
  79.         $showSegmentationForm = false;
  80.         /**
  81.          *  @var Client 
  82.          */
  83.         $client = $this->em->getRepository(Client::class)->find($this->getUser()->getClient()->getId());
  84.         // Ajout log d'utilisation de la plateforme
  85.         $this->logService->postLogForUser($this->getUser()->getId(), $client->getCompany()->getId(), "NULL");
  86.         if($client instanceof Client) {
  87.             // Redirection vers "Informations" si pas de "Nom" et "Prénom ou si pas de cgu"
  88.             if(empty($client->getFirstName()) || empty($client->getLastName())) {
  89.                 return $this->redirectToRoute('client_myaccount', ["error" => "no_name"]);
  90.             }
  91.             if(empty($client->getIsCgu())) {
  92.                 return $this->redirectToRoute('client_myaccount', ["error" => "no_cgu"]);
  93.             }
  94.             if($client->getSegmentation() instanceof Segmentation) {
  95.                 if($client->getSegmentation()->getParent() instanceof Segmentation && !$client->getSegmentation()->isAlone()) {
  96.                     $allowedSegmentations = $client->getSegmentation()->getParent()->getChildren()->toArray();
  97.                 } else {
  98.                     $allowedSegmentations = [$client->getSegmentation()];
  99.                 }
  100.             }
  101.             // Rattrapage multi-axes : demande les valeurs manquantes sur les axes de classement.
  102.             // Remplace le modal legacy pour les entreprises ayant des axes classés.
  103.             // NB : le user de session n'est jamais rafraîchi par les providers (refreshUser
  104.             // renvoie l'objet détaché tel quel), il faut re-fetcher le user managé.
  105.             /**
  106.              *  @var User
  107.              */
  108.             $managedUser = $this->em->getRepository(User::class)->find($this->getUser()->getId());
  109.             if ($this->segmentationService->hasRankedAxes($managedUser)) {
  110.                 $missingRankedAxes = $this->segmentationService->getMissingRankedAxesForUser($managedUser);
  111.                 if (!empty($missingRankedAxes)) {
  112.                     $showSegmentationForm = true;
  113.                     $form = $this->createForm(SegmentationAxesType::class, null, ['axes' => $missingRankedAxes]);
  114.                     $form->handleRequest($request);
  115.                     if ($form->isSubmitted() && $form->isValid()) {
  116.                         foreach ($missingRankedAxes as $axis) {
  117.                             $value = $form->get('axis_'.$axis->getId())->getData();
  118.                             if ($value instanceof Segmentation) {
  119.                                 $managedUser->assignSegmentationValue($value);
  120.                             }
  121.                         }
  122.                         $this->em->persist($managedUser);
  123.                         $this->em->flush();
  124.                         return $this->redirectToRoute('client_dashboard');
  125.                     }
  126.                     $segmentationAxesFormView = $form->createView();
  127.                 }
  128.             } elseif (!$client->getSegmentation() instanceof Segmentation) {
  129.                 if ($client->getCompany()->getForceSegmentation() == true) {
  130.                     $showSegmentationForm = true;
  131.                     /**
  132.                      *  @var User 
  133.                      */
  134.                     $user = $this->em->getRepository(User::class)->find($this->getUser());
  135.     
  136.                     $company = $this->em->getRepository(Company::class)->find($client->getCompany()->getId());
  137.                     $segmentationChoices = [];
  138.                     if (!empty($company->getSegmentations()->toArray())) {
  139.                         foreach ($company->getSegmentations()->toArray() as $key => $segmentation) {
  140.                             if (empty($segmentation->getChildren()->toArray())) {
  141.     
  142.                                 $segmentationKey = ($segmentation->getParent() != null) ? ($segmentation->getParent()->getName()) : "Autre" ;
  143.     
  144.                                 $segmentationChoices[$segmentationKey][$this->segmentationService->getSegmentationName($segmentation->getName())] = $segmentation;
  145.                             }
  146.                         }
  147.                         $form = $this->createForm(SegmentationType::class, $client, ['segmentationChoices' => $segmentationChoices]);
  148.                         $form->handleRequest($request);
  149.                         if ($form->isSubmitted() && $form->isValid()) {
  150.                             $user->setSegmentation($client->getSegmentation());
  151.                             $this->em->persist($client);
  152.                             $this->em->persist($user);
  153.                             $this->em->flush();
  154.                             return $this->redirectToRoute('client_dashboard');
  155.                         }
  156.                     }
  157.                     $formView = $form->createView();
  158.                 }
  159.             }
  160.         } 
  161.         $teamplay = $this->teamplayRepository->findTeamplayInProgressForDesktop($client->getCompany());
  162.         $team = null;
  163.         if ($teamplay) {
  164.             $team = $this->teamRepository->getTeamByUserAndTeamplay($this->getUser(), $teamplay);
  165.         }
  166.         $registrations = $this->registrationRepository->getRegistrationsForClient($client, 'start', true);
  167.         $hpSlides = $this->slideRepository->getHpSlide($client->getCompany(), 'homepage_slide');
  168.         $programs = $this->programRepository->getRandomProgramsDql(3);
  169.         $expressPrograms = $this->channelRepository->findRandomExpressProgramsBy([], 3 );
  170.         $dailyVideo = $this->videoRepository->getDailyVideo();
  171.         $randomVideos = $this->videoRepository->customGetVideosRandom(['is_recipe' => 0, 'forthcoming' => 0], 10);
  172.         $dailySlideArray  = array_merge($programs, $expressPrograms);
  173.         $articles = $this->articleRepository->findBy([
  174.             'homepage' => 1,
  175.             'active' => 1
  176.         ]);
  177.         return $this->render('client/v2/dashboard.html.twig', [
  178.             'registrations'         => $registrations,
  179.             'showSegmentationForm'  => $showSegmentationForm,
  180.             'hpSlides'              => $hpSlides,
  181.             'dailySlideContent'     => $dailySlideArray,
  182.             "dailyVideo"            => $dailyVideo,
  183.             'randomVideos'          => $randomVideos,
  184.             'articles'              => $articles,
  185.             'teamplay'              => $teamplay,
  186.             'team'                  => $team,
  187.             'allowedSegmentations'  => $allowedSegmentations,
  188.             'formView'              => $formView,
  189.             'segmentationAxesFormView' => $segmentationAxesFormView,
  190.         ]);
  191.     }
  192.     
  193. }