<?php
namespace App\Controller;
use App\Entity\Quotation\Quotation;
use App\Form\ContactType;
use App\Form\Quotation\QuotationType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController
{
public function __construct(EntityManagerInterface $entityManager, KernelInterface $kernel)
{
$this->em = $entityManager;
$this->kernel = $kernel;
}
/**
* @Route("/", name="app_index")
*/
public function index(Request $request)
{
return $this->redirectToRoute('app_home', ['_locale' => 'fr']);
}
/**
* @Route({
* "fr": "/fr/",
* "en": "/en/"
* }, name="app_home")
*/
public function home(Request $request)
{
$locale = $request->getLocale();
$page = $this->em->getRepository('App:Page\Page')->findHomeLeftJoined($locale);
if (!$page) {
throw $this->createNotFoundException('La page n\'a pas été trouvé.');
}
$contents = $this->getContents($page, $locale);
return $this->render('theme/default/home.html.twig', [
'page' => $page,
'contents' => $contents,
]);
}
/**
* @Route({
* "fr": "/fr/demande-de-devis",
* "en": "/en/request-a-quotation"
* }, name="app_quotation_create")
*/
public function quotation(Request $request)
{
$quotation = new Quotation();
$form = $this->createForm(QuotationType::class, $quotation);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$quotation->setReference(uniqid());
$this->em->persist($quotation);
$this->em->flush();
$firstname = $form->get('firstname')->getData();
return $this->render('theme/default/quotation_end.html.twig', [
'firstname' => $firstname
]);
}
}
return $this->render('theme/default/quotation.html.twig', [
'form' => $form->createView()
]);
}
/**
* @Route({
* "fr": "/fr/contact",
* "en": "/en/contact"
* }, name="app_contact")
*/
public function contact(Request $request)
{
$locale = $request->getLocale();
$page = $this->em->getRepository('App:Page\Page')->findContactPageLeftJoined($locale);
if (!$page) {
throw $this->createNotFoundException('La page n\'a pas été trouvé.');
}
$contents = $this->getContents($page, $locale);
$form = $this->createForm(ContactType::class);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$this->addFlash('success', 'Le formulaire de contact a bien été envoyé.');
return $this->redirectToRoute('app_contact');
}
}
return $this->render('theme/default/contact.html.twig', [
'page' => $page,
'contents' => $contents,
'form' => $form->createView()
]);
}
/**
* @Route({
* "fr": "/fr/mon-compte",
* "en": "/en/my-account",
* }, name="app_user_folders")
*/
public function folders()
{
$user = $this->getUser();
if ($this->isGranted('ROLE_ADMIN')) {
$folders = $this->em->getRepository('App:Folder\Folder')->findRootFoldersForAdmin();
} else {
$folders = $this->em->getRepository('App:Folder\Folder')->findRootFoldersForUser($user);
}
return $this->render('theme/default/folders.html.twig', [
'user' => $user,
'folders' => $folders
]);
}
/**
* @Route({
* "fr": "/fr/dossier/{id}",
* "en": "/en/folder/{id}",
* }, name="app_user_folder")
*/
public function folder($id)
{
$user = $this->getUser();
$folder = $this->em->getRepository('App:Folder\Folder')->find($id);
if (!$folder) {
throw $this->createNotFoundException('Resource not found.');
}
$parent = $folder;
// Security
if (!$this->isGranted('ROLE_ADMIN')) {
$acccessGranted = false;
if ($folder->getParent() != null) {
while ($folder->getParent() != null) {
$folder = $folder->getParent();
if ($folder->getUsers()->contains($user)) {
$acccessGranted = true;
}
foreach($user->getGroups() as $group) {
if ($folder->getGroups()->contains($group)) {
$acccessGranted = true;
}
}
}
} else {
if ($folder->getUsers()->contains($user)) {
$acccessGranted = true;
}
foreach($user->getGroups() as $group) {
if ($folder->getGroups()->contains($group)) {
$acccessGranted = true;
}
}
}
if (!$acccessGranted) {
throw $this->createAccessDeniedException('Vous n\'avez pas les droits pour voir ce contenu');
}
}
return $this->render('theme/default/folder.html.twig', [
'user' => $user,
'parent' => $parent
]);
}
/**
* @Route({
* "fr": "/fr/{slug}",
* "en": "/en/{slug}"
* }, name="app_page")
*/
public function page(Request $request, $slug)
{
$locale = $request->getLocale();
$page = $this->em->getRepository('App:Page\Page')->findPage($slug, $locale);
if (!$page) {
throw $this->createNotFoundException('La page n\'a pas été trouvé.');
}
// Check if the user has authorization to access the page
if ($page->getPrivacy() != "public") {
$authorized = false;
$user = $this->getUser();
if (in_array($user, $page->getUsers())) {
$authorized = true;
}
foreach ($user->getGroups() as $group) {
if (in_array($group, $page->getGroups())) {
$authorized = true;
}
}
if (!$authorized) {
throw $this->createAccessDeniedException('Vous n\'avez pas accès à cette page.');
}
}
$contents = $this->getContents($page, $locale);
$theme = 'theme/default/';
$template = $theme . $page->getTemplate()->getFilename();
return $this->render($template , [
'page' => $page,
'contents' => $contents
]);
}
/**
* This function help to retrieve the contents of the page
*/
private function getContents($page, $locale)
{
$contents = [];
$filePath = "";
$entityNamespaced = "";
foreach($page->getContents() as $content) {
$field = $content->getField();
if ($content->getLocale() == $locale) {
if ($field) {
$type = $field->getType();
$parent = $field->getParent();
// If field is type of entity, get the values for that entity
if ($type->getSlug() == "entity") {
$entity = $field->getOptions()[0]->getValue();
// Find namespace
$finder = new Finder();
$finder->files()->in($this->kernel->getProjectDir().'/src/Entity')->name($entity.'.php');
foreach ($finder as $file) {
$filePath = $file->getRealPath();
}
$explodedFilePath = explode('/', $filePath);
// Append namespace if exists to entity name
if ($explodedFilePath[count($explodedFilePath) - 2] != 'Entity') {
$entityNamespaced = $explodedFilePath[count($explodedFilePath) - 2].'\\'.$entity;
} else {
$entityNamespaced = $entity;
}
}
if ($parent) {
$block = $parent->getBlock();
$parent = $parent->getIdentifier();
$collection = $content->getCollectionId();
$field = $field->getIdentifier();
if ($block && $parent && $field) {
$block = $block->getIdentifier();
if ($type->getSlug() == 'entity') {
if ($content->getValue()) {
$contents[$block][$parent][$collection][$field] = $this->em->getRepository('App:'.$entityNamespaced)->find($content->getValue());
}
} else {
$contents[$block][$parent][$collection][$field] = $content->getValue();
}
}
} else {
$block = $field->getBlock();
$field = $field->getIdentifier();
if ($block && $field) {
$block = $block->getIdentifier();
if ($type->getSlug() == 'entity') {
$contents[$block][$field] = $this->em->getRepository('App:'.$entityNamespaced)->find($content->getValue());
} else {
$contents[$block][$field] = $content->getValue();
}
}
}
}
}
}
return $contents;
}
public function renderWorksList($identifier)
{
$works = $this->em->getRepository('App:Work\Work')->findAllLeftJoined();
return $this->render('theme/default/blocks/' . $identifier . '.html.twig', [
'works' => $works
]);
}
public function renderOfficesList($identifier)
{
$stores = $this->em->getRepository('App:Store\Store')->findAll();
return $this->render('theme/default/blocks/' . $identifier . '.html.twig', [
'stores' => $stores
]);
}
}