<?php
namespace App\Controller\Front;
use App\Entity\User\User;
use App\Service\LanguagesManager;
use App\Entity\Request\RequestForm;
use App\Service\Manager\AdminManager;
use App\Entity\Request\RequestComment;
use App\Controller\Admin\CoreController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Repository\Configuration\SurveyRepository;
use App\Repository\Request\RequestCommentRepository;
use App\Service\Manager\RequestForm\RequestFormManager;
use App\Service\Manager\TemplateEmail\TagsManager;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use App\Service\Manager\TemplateMailerManager;
use App\Repository\Request\RequestFormRepository;
use App\Entity\TemplateEmail\Template;
use App\Service\Mailer\Mailer;
#[Route('/request')]
class RequestController extends CoreController
{
#[Route('/index/{refSuffix}', name: 'front_request_index')]
public function index($refSuffix,
SurveyRepository $surveyRepository,
Request $request,
LanguagesManager $languagesManager,
RequestFormManager $requestFormManager
): Response
{
$language = $languagesManager->getLanguageUser($request);
$survey = "";
$user = $this->getUser();
$parametres = [
'refSuffix' => $refSuffix,
'deletedAt' => null,
'isEnable' => 1,
'isDisplay' => 1
];
if ($user && (!in_array($user->getRoles(), [User::ROLE_ADMIN]))) {
unset($parametres['isEnable']);
unset($parametres['isDisplay']);
}
$survey = $surveyRepository->getAllQb([
'refSuffix' => $refSuffix,
'fk' => ['survey.label'],
'id_language' => $language->getId()
])->getOneOrNullResult();
$response = $requestFormManager->getForm($survey);
if($response instanceof Response) return $response;
return $this->render('front/request/index.html.twig', (!empty($response['paramsTemplate'])) ? $response['paramsTemplate'] : [] );
}
#[Route('/save_comment/{id}', name: 'front_request_form_save_comment')]
public function saveComment(Request $request,RequestForm $requestForm,RequestCommentRepository $requestCommentRepository, AdminManager $adminManager, TemplateMailerManager $templateMailer, UrlGeneratorInterface $urlGenerator): Response
{
$requestComment = new RequestComment();
$requestComment->setComment($request->request->get('comment'));
$requestComment->setCreatedDate(new \DateTime());
$requestComment->setSupportUser($requestForm->getSupportUser());
$requestComment->setShare(1);
$requestComment->setRequestForm($requestForm);
$adminManager->saveEntity($requestComment);
$comments = $requestCommentRepository->getCommentByRequest($requestForm);
$this->sendEmail($requestComment, $templateMailer, $urlGenerator);
return $this->render('front/request/requestComment.html.twig', [
'comments' => $comments,
'modeFront' => true
]);
}
/**
* sendEmail
*
* @param TagsManager $tagsManager
* @param Mailer $mailer
* @return bool
*/
private function sendEmail(
$requestComment,
TemplateMailerManager $templateMailer,
UrlGeneratorInterface $urlGenerator,
$data = []
)
{
$recipients = [];
$requestForm = $requestComment->getRequestForm();
if($requestForm->getUser()){
$recipients[] = $requestForm->getUser()->getEmail();
}
$token = $requestComment->getSupportUser()->getClient() ? $requestComment->getSupportUser()->getClient()->getToken() : $requestComment->getSupportUser()->getToken();
$send = $templateMailer->selectTemplateAndSendEmail(
Template::TYPE_NEW_COMMENT,
null,
[
'name' => $requestForm ? $requestForm->getName() : '--',
'email' => $requestForm->getUser() ? $requestForm->getUser()->getEmail() : '--',
'company' => $requestForm ? $requestForm->getCompany() : '--',
'phone' => $requestForm ? $requestForm->getTelephone() : '--',
'idRequest' => $requestForm ? $requestForm->getRequestNum() : '--',
'user_not_connected' => true,
'url' => $urlGenerator->generate('admin_request_form_show_details', array('id' => $requestForm->getId()), UrlGeneratorInterface::ABSOLUTE_URL )
], [],
$recipients
);
return $send;
}
}