src/Controller/Backend/BrandController.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Backend;
  3. use App\Entity\Brand;
  4. use App\Services\Helper;
  5. use Exception;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class BrandController extends AbstractController
  12. {
  13.     /**
  14.      * @Route("/{_locale}/dashboard/brand/list")
  15.      */
  16.     public function listBrandAction(Request $request){
  17.         $em $this->getDoctrine()->getManager();
  18.         $brands $em->getRepository(Brand::class)->findBy(['deleted' => 0]);
  19.         return $this->render('/backend/brands/list.html.twig',[
  20.             'brands' => $brands
  21.         ]);
  22.     }
  23.     /**
  24.      * @Route("/{_locale}/dashboard/brand/edit/{idBrand}")
  25.      * @Route("/{_locale}/dashboard/brand/add", defaults={"idBrand":null})
  26.      */
  27.     public function editBrandAction(Request $requestHelper $helper){
  28.         $em $this->getDoctrine()->getManager();
  29.         $idBrand $helper->sanitize($request->get('idBrand'));
  30.         $brand $em->getRepository(Brand::class)->findOneBy(['idBrand' => $idBrand]);
  31.         if(!$brand){
  32.             $brand = new Brand();
  33.         }
  34.         return $this->render('backend/brands/edit.html.twig',[
  35.             'brand' => $brand
  36.         ]);
  37.     }
  38.     /**
  39.      * @Route("/{_locale}/dashboard/brand/save")
  40.      * @Security("has_role('ROLE_ADMINISTRADOR')")
  41.      */
  42.     public function brandSaveAction(Request $requestHelper $helper){
  43.         $em $this->getDoctrine()->getManager();
  44.         $response = new JsonResponse();
  45.         try{
  46.             $idBrand $helper->sanitize($request->request->get('idBrand'));
  47.             $name $helper->sanitize($request->request->get('name'));
  48.             if (!$name)
  49.                 throw new Exception('Name not valid'); 
  50.             $brand $em->getRepository(Brand::class)->findOneBy(['idBrand' => $idBrand]);
  51.             if(!$brand){
  52.                 $brand = new Brand();
  53.             }
  54.             $brand->setName($name);
  55.             $brand->setDeleted(0);
  56.             $em->persist($brand);
  57.             $em->flush();
  58.             $response->setStatusCode(200);
  59.             $response->setData(array(
  60.                 'response' => 'success',
  61.                 'msg' => 'Action performed successfully',
  62.                 'idBrand' => $brand->getIdBrand()
  63.             ));
  64.             return $response;
  65.         } catch(Exception $e){
  66.             $response->setStatusCode(500);
  67.             $response->setData(array(
  68.                 'response' => 'error',
  69.                 'msg' => 'Something has gone wrong'
  70.             ));
  71.             return $response;
  72.         }
  73.     }
  74.     /**
  75.      * @Route("/{_locale}/dashboard/brand/delete")
  76.      * @Security("has_role('ROLE_ADMINISTRADOR')")
  77.      */
  78.     public function brandDeleteAction(Request $requestHelper $helper){
  79.         $em $this->getDoctrine()->getManager();
  80.         $response = new JsonResponse();
  81.         try{
  82.             $idBrand $helper->sanitize($request->request->get('idBrand'));
  83.             $brand $em->getRepository(Brand::class)->findOneBy(['idBrand' => $idBrand]);
  84.             $brand->setDeleted(1);
  85.             $em->persist($brand);
  86.             $em->flush();
  87.             $response->setStatusCode(200);
  88.             $response->setData(array(
  89.                 'response' => 'success'
  90.             ));
  91.             return $response;
  92.         } catch(Exception $e){
  93.             $response->setStatusCode(500);
  94.             $response->setData(array(
  95.                 'response' => 'error',
  96.                 'msg' => 'Something has gone wrong'
  97.             ));
  98.             return $response;
  99.         }
  100.     }
  101. }