src/HomeBundle/Controller/DefaultController.php line 31

Open in your IDE?
  1. <?php
  2. namespace HomeBundle\Controller;
  3. use CoreBundle\Entity\Branche;
  4. use CoreBundle\Entity\NoteTest;
  5. use CoreBundle\Entity\Periode;
  6. use CoreBundle\Entity\Post;
  7. use CoreBundle\Entity\Secteur;
  8. use CoreBundle\Entity\Tutor;
  9. use CoreBundle\Provider\DefaultValuesProvider;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use HomeBundle\Service\BoxImageByUser;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use CoreBundle\Entity\BoxText;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use CoreBundle\Entity\BoxImage;
  19. use CoreBundle\Entity\Student;
  20. use Symfony\Component\HttpFoundation\File\UploadedFile;
  21. use CoreBundle\S3;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. class DefaultController extends AbstractController
  24. {
  25.     /**
  26.      * @Route("/", name="app_home")
  27.      */
  28.     public function indexAction(
  29.         EntityManagerInterface $em,
  30.         DefaultValuesProvider $defaultValuesProvider,
  31.         BoxImageByUser $boxImageByUser,
  32.     ) {
  33.         /*if ($this->getUser() instanceof Student) {
  34.             return $this->redirectToRoute('documents');
  35.         }*/
  36.         $periode $defaultValuesProvider->getPeriode();
  37.         $indexedBoximages $boxImageByUser->get();
  38.         if ($this->getUser()->getAdmin()) {
  39.             $branches $em->getRepository(Branche::class)
  40.                 ->findBy([], ['name' => 'asc']);
  41.         } elseif ($this->getUser() instanceof Student) {
  42.             $branches = [$this->getUser()->getBranche()];
  43.         } else {
  44.             $branches $em->getRepository(Branche::class)
  45.                 ->getByTutor($this->getUser());
  46.         }
  47.         $lastPosts $em->getRepository(Post::class)
  48.             ->getUserLastPosts($periode$this->getUser());
  49.         if ($this->getUser() instanceof Tutor) {
  50.             $openedTests $em->getRepository(NoteTest::class)
  51.                 ->getOpensByTutor($this->getUser(), $periode);
  52.         } else {
  53.             $openedTests = [];
  54.         }
  55.         $openedQcms = [];
  56.         return $this->render('@HomeBundle/Default/index.html.twig', [
  57.             'branches' => $branches,
  58.             'boxImages' => $indexedBoximages,
  59.             'lastPosts' => $lastPosts,
  60.             'openedTests' => $openedTests,
  61.             'openedQcms' => $openedQcms
  62.         ]);
  63.     }
  64.     /**
  65.      * @Route("/upload", name="app_home_upload", options={"expose" : true})
  66.      * @IsGranted("ROLE_ADMIN")
  67.      */
  68.     public function upload(
  69.         Request $request,
  70.         EntityManagerInterface $em,
  71.         S3\Uploader $uploader
  72.     ) {
  73.         $id $request->request->get('id');
  74.         $secteur $em->getRepository(Secteur::class)
  75.             ->find($request->request->get('secteur_id'));
  76.         if (null === $id) {
  77.             $boxImage = new BoxImage();
  78.             $boxImage->setSecteur($secteur);
  79.             $boxImage->addSecteur($secteur);
  80.         } else {
  81.             $boxImage $em->getRepository(BoxImage::class)
  82.                 ->find($id);
  83.         }
  84.         /** @var UploadedFile $upload */
  85.         $upload $request->files->get('upload');
  86.         $mimetype $upload->getMimeType();
  87.         if ($upload->getError() !== UPLOAD_ERR_OK) {
  88.             return new JsonResponse(['error' => $upload->getErrorMessage()]);
  89.         } elseif (! in_array($mimetype, ['image/jpeg''image/png''image/jpg''image/gif''application/pdf'])) {
  90.             return new JsonResponse(['error' => 'Seules les images et pdf sont autorisées !']);
  91.         }
  92.         $file $uploader->run(
  93.             $upload,
  94.             S3\Uploader::BUCKET_FILES,
  95.             sprintf(
  96.                 'home/branche_%d/secteur_%d',
  97.                 $secteur->getBranche()->getId(),
  98.                 $secteur->getId()
  99.             )
  100.         );
  101.         $boxImage->setFile($file);
  102.         $em->persist($boxImage);
  103.         $em->flush();
  104.         $formatter = new \IntlDateFormatter('fr'\IntlDateFormatter::FULL\IntlDateFormatter::SHORT);
  105.         $url $uploader->getLink($file);
  106.         return new JsonResponse([
  107.             'file' => $url,
  108.             'tn_file' => $url,
  109.             'id' => $boxImage->getId(),
  110.             'date' => $formatter->format(new \DateTime())
  111.         ]);
  112.     }
  113.     /**
  114.      * @Route("/delete-image-box/{id}", name="app_home_delete_image_box", options={"expose" : true})
  115.      * @IsGranted("ROLE_ADMIN")
  116.      */
  117.     public function delImageBox(EntityManagerInterface $emBoxImage $boxImage)
  118.     {
  119.         $em->remove($boxImage);
  120.         $em->flush();
  121.         return new JsonResponse(['code' => "success"]);
  122.     }
  123.     /**
  124.      * @Route("/text/{box_image_id}/{id}",
  125.      *      name="app_home_text",
  126.      *      requirements={"box_image_id": "\d+", "id": "\d+"},
  127.      *      defaults={"id": null},
  128.      *      options={"expose" : true})
  129.      * @ParamConverter("boxImage", options={"mapping"={"box_image_id": "id"}})
  130.      * @IsGranted("ROLE_ADMIN")
  131.      */
  132.     public function text(
  133.         EntityManagerInterface $em,
  134.         Request $request,
  135.         BoxImage $boxImage,
  136.         BoxText $boxText null
  137.     ) {
  138.         if (null === $boxText) {
  139.             $boxText = new BoxText();
  140.             $boxText->setBoxImage($boxImage);
  141.         }
  142.         $boxText->setContent($request->request->get('content'));
  143.         $em->persist($boxText);
  144.         $em->flush();
  145.         $formatter = new \IntlDateFormatter('fr'\IntlDateFormatter::FULL\IntlDateFormatter::SHORT);
  146.         return new JsonResponse([
  147.             'code' => 'success',
  148.             'id' => $boxText->getId(),
  149.             'date' => $formatter->format(new \DateTime())
  150.         ]);
  151.     }
  152.     /**
  153.      * @Route("/delete-text-box/{id}", name="app_home_delete_text_box", options={"expose" : true})
  154.      * @IsGranted("ROLE_ADMIN")
  155.      */
  156.     public function delTextBox(EntityManagerInterface $emBoxText $boxText)
  157.     {
  158.         $em->remove($boxText);
  159.         $em->flush();
  160.         return new JsonResponse(['code' => "success"]);
  161.     }
  162. }