<?php
namespace App\Controller\Customer;
use App\Entity\Customer\Alert;
use App\Entity\SMS\SMS;
use App\Entity\VignetteAlertTemplate\VignetteAlertTemplate;
use App\Service\SMSService;
use App\Validator\AlertFormValidator;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use function Sentry\captureException;
class AlertController extends ResourceController
{
protected $container;
public function getContainer()
{
return $this->container;
}
public function getManager()
{
return $this->getDoctrine()->getManager();
}
public function indexAction(Request $request): Response
{
$customer = null;
if ($user = $this->getUser()) {
$customer = $user->getCustomer();
}
return $this->render('@templates/Account/Alert/show.html.twig', ['customer' => $customer]);
}
public function sendSmsManually(Request $request)
{
$smsId = $request->get('sms_id');
if ($smsId) {
$sms = $this->getDoctrine()->getRepository(SMS::class)->find($smsId);
if ($sms) {
if (!$sms->getIsSent()) {
if ($sms->hasVignette()) {
$vignette = $sms->getVignette();
if (SMSService::sendVignetteSMS($vignette)) {
$sms->setIsSent(1);
}
}
$this->manager->persist($sms);
$this->manager->flush();
}
}
}
$referer = $request->headers->get('referer');
return $this->redirect($referer);
}
public function alertDetailsAction(Request $request): Response
{
$templates = $this->getDoctrine()->getRepository(VignetteAlertTemplate::class)->findAll();
$alert = null;
if ( $alertId =$request->get('id')) {
if ($user = $this->getUser()) {
$alertEntity = $this->getDoctrine()->getRepository(Alert::class)->find($alertId);
if ($alertEntity->getCustomer()->getId() == $user->getCustomer()->getId()) {
$alert = $alertEntity;
}
}
}
return $this->render('@templates/Account/Alert/newAlert.html.twig', ['templates' => $templates, 'alert' => $alert]);
}
public function addAction(Request $request): Response
{
$errors = null;
$session = $request->getSession();
$alertValidator = new AlertFormValidator($this->getManager(), $this->getContainer());
if ($alertValidator->checkForm($request)) {
$this->setAlert($request);
$errors = json_encode(array('success' => true));
} else {
if ($session->has('alertErrors')) {
$errors = $session->get('alertErrors');
}
}
return new Response($errors);
}
public function setAlert(Request $request)
{
try {
$authUser = $this->getUser();
$customerEntity = $authUser->getCustomer();
if ($customerEntity) {
$alertDate = $request->get('alertDate');
$alertTitle = $request->get('alertTitle');
$alertMessage = $request->get('alertMessage');
$alertId = $request->get('alertId');
if ($alertId) {
$alert = $this->getDoctrine()->getRepository(Alert::class)->find($alertId);
} else {
$alert = new Alert();
$alert->setCustomer($customerEntity);
}
$alert->setAlertDate(new \DateTime($alertDate));
$alert->setTitle($alertTitle);
$alert->setIsFirstSent(false);
$alert->setIsSecondSent(false);
if ($alertMessage != '') {
$alert->setMessage($alertMessage);
}
$this->getDoctrine()->getManager()->persist($alert);
$this->getDoctrine()->getManager()->flush();
}
} catch(\Exception $exception) {
}
}
public function deleteAlert(Request $request)
{
try {
if ($user = $this->getUser()) {
$customer = $user->getCustomer();
$alertId = $request->get('id');
$alertEntity = $this->getDoctrine()->getRepository(Alert::class)->find($alertId);
if ($customer->getId() == $alertEntity->getCustomer()->getId()) {
$this->getDoctrine()->getRepository(Alert::class)->deleteAlert($alertId);
}
}
} catch(\Exception $exception) {
captureException($exception);
}
return $this->redirectToRoute('app_view_alerts');
}
}