src/Controller/Customer/AlertController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Customer;
  3. use App\Entity\Customer\Alert;
  4. use App\Entity\SMS\SMS;
  5. use App\Entity\VignetteAlertTemplate\VignetteAlertTemplate;
  6. use App\Service\SMSService;
  7. use App\Validator\AlertFormValidator;
  8. use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use function Sentry\captureException;
  12. class AlertController extends ResourceController
  13. {
  14.     protected $container;
  15.     public function getContainer()
  16.     {
  17.         return $this->container;
  18.     }
  19.     public function getManager()
  20.     {
  21.         return $this->getDoctrine()->getManager();
  22.     }
  23.     public function indexAction(Request $request): Response
  24.     {
  25.         $customer null;
  26.         if ($user $this->getUser()) {
  27.             $customer $user->getCustomer();
  28.         }
  29.         return $this->render('@templates/Account/Alert/show.html.twig', ['customer' => $customer]);
  30.     }
  31.     public function sendSmsManually(Request $request)
  32.     {
  33.         $smsId $request->get('sms_id');
  34.         if ($smsId) {
  35.             $sms $this->getDoctrine()->getRepository(SMS::class)->find($smsId);
  36.             if ($sms) {
  37.                 if (!$sms->getIsSent()) {
  38.                     if ($sms->hasVignette()) {
  39.                         $vignette $sms->getVignette();
  40.                         if (SMSService::sendVignetteSMS($vignette)) {
  41.                             $sms->setIsSent(1);
  42.                         }
  43.                     }
  44.                     $this->manager->persist($sms);
  45.                     $this->manager->flush();
  46.                 }
  47.             }
  48.         }
  49.         $referer $request->headers->get('referer');
  50.         return $this->redirect($referer);
  51.     }
  52.     public function alertDetailsAction(Request $request): Response
  53.     {
  54.         $templates $this->getDoctrine()->getRepository(VignetteAlertTemplate::class)->findAll();
  55.         $alert null;
  56.         if ( $alertId =$request->get('id')) {
  57.             if ($user $this->getUser()) {
  58.                 $alertEntity $this->getDoctrine()->getRepository(Alert::class)->find($alertId);
  59.                 if ($alertEntity->getCustomer()->getId() == $user->getCustomer()->getId()) {
  60.                     $alert $alertEntity;
  61.                 }
  62.             }
  63.         }
  64.         return $this->render('@templates/Account/Alert/newAlert.html.twig', ['templates' => $templates'alert' => $alert]);
  65.     }
  66.     public function addAction(Request $request): Response
  67.     {
  68.         $errors null;
  69.         $session $request->getSession();
  70.         $alertValidator = new AlertFormValidator($this->getManager(), $this->getContainer());
  71.         if ($alertValidator->checkForm($request)) {
  72.             $this->setAlert($request);
  73.             $errors json_encode(array('success' => true));
  74.         } else {
  75.             if ($session->has('alertErrors')) {
  76.                 $errors $session->get('alertErrors');
  77.             }
  78.         }
  79.         return new Response($errors);
  80.     }
  81.     public function setAlert(Request $request)
  82.     {
  83.         try {
  84.             $authUser $this->getUser();
  85.             $customerEntity $authUser->getCustomer();
  86.             if ($customerEntity) {
  87.                 $alertDate $request->get('alertDate');
  88.                 $alertTitle $request->get('alertTitle');
  89.                 $alertMessage $request->get('alertMessage');
  90.                 $alertId $request->get('alertId');
  91.                 if ($alertId) {
  92.                     $alert $this->getDoctrine()->getRepository(Alert::class)->find($alertId);
  93.                 } else {
  94.                     $alert = new Alert();
  95.                     $alert->setCustomer($customerEntity);
  96.                 }
  97.                 $alert->setAlertDate(new \DateTime($alertDate));
  98.                 $alert->setTitle($alertTitle);
  99.                 $alert->setIsFirstSent(false);
  100.                 $alert->setIsSecondSent(false);
  101.                 if ($alertMessage != '') {
  102.                     $alert->setMessage($alertMessage);
  103.                 }
  104.                 $this->getDoctrine()->getManager()->persist($alert);
  105.                 $this->getDoctrine()->getManager()->flush();
  106.             }
  107.         } catch(\Exception $exception) {
  108.         }
  109.     }
  110.     public function deleteAlert(Request $request)
  111.     {
  112.         try {
  113.             if ($user $this->getUser()) {
  114.                 $customer $user->getCustomer();
  115.                 $alertId $request->get('id');
  116.                 $alertEntity $this->getDoctrine()->getRepository(Alert::class)->find($alertId);
  117.                 if ($customer->getId() == $alertEntity->getCustomer()->getId()) {
  118.                     $this->getDoctrine()->getRepository(Alert::class)->deleteAlert($alertId);
  119.                 }
  120.             }
  121.         } catch(\Exception $exception) {
  122.             captureException($exception);
  123.         }
  124.         return $this->redirectToRoute('app_view_alerts');
  125.     }
  126. }