<?php
namespace HomeBundle\Controller;
use CoreBundle\Entity\Branche;
use CoreBundle\Entity\NoteTest;
use CoreBundle\Entity\Periode;
use CoreBundle\Entity\Post;
use CoreBundle\Entity\Secteur;
use CoreBundle\Entity\Tutor;
use CoreBundle\Provider\DefaultValuesProvider;
use Doctrine\ORM\EntityManagerInterface;
use HomeBundle\Service\BoxImageByUser;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use CoreBundle\Entity\BoxText;
use Symfony\Component\HttpFoundation\JsonResponse;
use CoreBundle\Entity\BoxImage;
use CoreBundle\Entity\Student;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use CoreBundle\S3;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController
{
/**
* @Route("/", name="app_home")
*/
public function indexAction(
EntityManagerInterface $em,
DefaultValuesProvider $defaultValuesProvider,
BoxImageByUser $boxImageByUser,
) {
/*if ($this->getUser() instanceof Student) {
return $this->redirectToRoute('documents');
}*/
$periode = $defaultValuesProvider->getPeriode();
$indexedBoximages = $boxImageByUser->get();
if ($this->getUser()->getAdmin()) {
$branches = $em->getRepository(Branche::class)
->findBy([], ['name' => 'asc']);
} elseif ($this->getUser() instanceof Student) {
$branches = [$this->getUser()->getBranche()];
} else {
$branches = $em->getRepository(Branche::class)
->getByTutor($this->getUser());
}
$lastPosts = $em->getRepository(Post::class)
->getUserLastPosts($periode, $this->getUser());
if ($this->getUser() instanceof Tutor) {
$openedTests = $em->getRepository(NoteTest::class)
->getOpensByTutor($this->getUser(), $periode);
} else {
$openedTests = [];
}
$openedQcms = [];
return $this->render('@HomeBundle/Default/index.html.twig', [
'branches' => $branches,
'boxImages' => $indexedBoximages,
'lastPosts' => $lastPosts,
'openedTests' => $openedTests,
'openedQcms' => $openedQcms
]);
}
/**
* @Route("/upload", name="app_home_upload", options={"expose" : true})
* @IsGranted("ROLE_ADMIN")
*/
public function upload(
Request $request,
EntityManagerInterface $em,
S3\Uploader $uploader
) {
$id = $request->request->get('id');
$secteur = $em->getRepository(Secteur::class)
->find($request->request->get('secteur_id'));
if (null === $id) {
$boxImage = new BoxImage();
$boxImage->setSecteur($secteur);
$boxImage->addSecteur($secteur);
} else {
$boxImage = $em->getRepository(BoxImage::class)
->find($id);
}
/** @var UploadedFile $upload */
$upload = $request->files->get('upload');
$mimetype = $upload->getMimeType();
if ($upload->getError() !== UPLOAD_ERR_OK) {
return new JsonResponse(['error' => $upload->getErrorMessage()]);
} elseif (! in_array($mimetype, ['image/jpeg', 'image/png', 'image/jpg', 'image/gif', 'application/pdf'])) {
return new JsonResponse(['error' => 'Seules les images et pdf sont autorisées !']);
}
$file = $uploader->run(
$upload,
S3\Uploader::BUCKET_FILES,
sprintf(
'home/branche_%d/secteur_%d',
$secteur->getBranche()->getId(),
$secteur->getId()
)
);
$boxImage->setFile($file);
$em->persist($boxImage);
$em->flush();
$formatter = new \IntlDateFormatter('fr', \IntlDateFormatter::FULL, \IntlDateFormatter::SHORT);
$url = $uploader->getLink($file);
return new JsonResponse([
'file' => $url,
'tn_file' => $url,
'id' => $boxImage->getId(),
'date' => $formatter->format(new \DateTime())
]);
}
/**
* @Route("/delete-image-box/{id}", name="app_home_delete_image_box", options={"expose" : true})
* @IsGranted("ROLE_ADMIN")
*/
public function delImageBox(EntityManagerInterface $em, BoxImage $boxImage)
{
$em->remove($boxImage);
$em->flush();
return new JsonResponse(['code' => "success"]);
}
/**
* @Route("/text/{box_image_id}/{id}",
* name="app_home_text",
* requirements={"box_image_id": "\d+", "id": "\d+"},
* defaults={"id": null},
* options={"expose" : true})
* @ParamConverter("boxImage", options={"mapping"={"box_image_id": "id"}})
* @IsGranted("ROLE_ADMIN")
*/
public function text(
EntityManagerInterface $em,
Request $request,
BoxImage $boxImage,
BoxText $boxText = null
) {
if (null === $boxText) {
$boxText = new BoxText();
$boxText->setBoxImage($boxImage);
}
$boxText->setContent($request->request->get('content'));
$em->persist($boxText);
$em->flush();
$formatter = new \IntlDateFormatter('fr', \IntlDateFormatter::FULL, \IntlDateFormatter::SHORT);
return new JsonResponse([
'code' => 'success',
'id' => $boxText->getId(),
'date' => $formatter->format(new \DateTime())
]);
}
/**
* @Route("/delete-text-box/{id}", name="app_home_delete_text_box", options={"expose" : true})
* @IsGranted("ROLE_ADMIN")
*/
public function delTextBox(EntityManagerInterface $em, BoxText $boxText)
{
$em->remove($boxText);
$em->flush();
return new JsonResponse(['code' => "success"]);
}
}