src/Controller/ColorController.php line 96

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\ColorService;
  4. use App\Service\FormulaService;
  5. use FOS\RestBundle\Controller\AbstractFOSRestController;
  6. use FOS\RestBundle\Controller\Annotations as Rest;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use JMS\Serializer\SerializationContext;
  11. use Symfony\Component\Security\Core\Security;
  12. use JMS\Serializer\SerializerInterface;
  13. use Psr\Log\LoggerInterface;
  14. class ColorController extends  AbstractController
  15. {
  16.     private  $security;
  17.     private  $serializer;
  18.     private  $serviceFormula;
  19.     private $log;
  20.     public function __construct(ColorService $colorServiceFormulaService $serviceFormulaSecurity $security,  SerializerInterface $serializerLoggerInterface $logger)
  21.     {
  22.         $this->serializer $serializer;
  23.         $this->service $colorService;
  24.         $this->serviceFormula $serviceFormula;
  25.         $this->security $security;
  26.         $this->log $logger;
  27.     }
  28.     // TODO::Refactorizar este método (borrar)
  29.     /*
  30.      * @Rest\Get("api/colors/{id}",name="obtener_color")
  31.     public function obtenerColor($id)
  32.     {
  33.         try {
  34.             $response = $this->service->findById($id);
  35.             $response =  new Response($this->serializer->serialize($response, 'json',SerializationContext::create()->setGroups('listAdmin')),Response::HTTP_OK);
  36.             return $response;
  37.         } catch (Exception $e) {
  38.             error_log($e);
  39.             return new Response(Response::HTTP_BAD_REQUEST);
  40.         }
  41.     }
  42.     */
  43.     // TODO::Refactorizar este metodo (borrar)
  44.     /*
  45.      * @Rest\Options("api/colors",name="obtener_colores")
  46.     public function obtenerColores(Request $request)
  47.     {   
  48.        //$data = $this->service->getAll();
  49.        $data = $this->service->getExample();
  50.         //$data = $this->service->get(1);
  51.        $code = 200;
  52.        $error = false;
  53.        $message = '';
  54.         $response = [
  55.             'code' => $code,
  56.             'error' => $error,
  57.             'data' => $code == 200 ? $data : $message,
  58.         ];
  59.         $response =  new Response($this->serializer->serialize($data, "json"),Response::HTTP_OK);
  60.           $response->headers->set('Content-Type', 'json'); //xml
  61.         $response->prepare($request);
  62.        // var_dump($data);
  63.         //return $this->render('default/index.html.twig', [
  64.         //    'controller_name' => 'DefaultController',
  65.         //    'colors' => $data
  66.         //]);
  67.         $response->send();
  68.     }
  69. */
  70.     /*
  71.      * @Rest\Get("api_inicio/load_inicial/obtenerColores")
  72.     public function obtenerColoresIniciales(Request $request)
  73.     {
  74.         return $this->obtenerColoresPorDisponibilidadPastas($request);
  75.     }
  76. */
  77.     /**
  78.      *  
  79.      * @Rest\Get("api/obtenerColores", name="obtener_colores_disponibilidad")
  80.      *
  81.      */
  82.     public function obtenerColoresPorDisponibilidadPastas(Request $request)
  83.     {
  84.         $cuenta $this->get('security.token_storage')->getToken()->getUser();
  85.         $data $this->service->getAllOrderNombre($cuenta);
  86.         $response =  new Response($this->serializer->serialize($data"json"), Response::HTTP_OK);
  87.         $response->headers->set('Content-Type''json'); //xml
  88.         $response->prepare($request);
  89.         return $response;
  90.     }
  91.     /**
  92.      * @Rest\Post("/api/colorPersonalDesactivar", name="colorPersonalDesactivar")
  93.      */
  94.     public function desactivarColorPersonal(Request $requestSerializerInterface $serializer)
  95.     {
  96.         $colorPersonal '';
  97.         $usr $this->get('security.token_storage')->getToken()->getUser();
  98.         if (=== strpos($request->headers->get('Content-Type'), 'application/json')) {
  99.             $requestObject json_decode($request->getContent(), true);
  100.             if (isset($requestObject['id'])) {
  101.                 //desactivamos FP
  102.                 try {
  103.                     $colorPersonal $this->service->desactivaColorPersonal($requestObject['id']);
  104.                 } catch (\Exception $e) {
  105.                     $response =  new Response('KO'Response::HTTP_CONFLICT);
  106.                     return $response;
  107.                 }
  108.                 $response =  new Response(Response::HTTP_OK);
  109.                 return $response;
  110.             } else {
  111.                 $response =  new Response(Response::HTTP_BAD_REQUEST);
  112.                 return $response;
  113.             }
  114.         } else {
  115.             $response =  new Response(Response::HTTP_BAD_REQUEST);
  116.             return $response;
  117.         }
  118.     }
  119.     /**
  120.      * @Rest\Post("/api/coloresPersonalesDesactivar", name="coloresPersonalesDesactivar")
  121.      */
  122.     public function desactivarColoresPersonales(Request $requestSerializerInterface $serializer)
  123.     {
  124.         $cuenta $this->get('security.token_storage')->getToken()->getUser();
  125.         //desactivamos FP
  126.         try {
  127.             $correcto $this->service->desactivarColoresPersonales($cuenta->getId());
  128.         } catch (\Exception $e) {
  129.             $response =  new Response('KO'Response::HTTP_CONFLICT);
  130.             return $response;
  131.         }
  132.         if ($correcto != 0) {
  133.             $response =  new Response(Response::HTTP_OK);
  134.         } else {
  135.             $response =  new Response(Response::HTTP_BAD_REQUEST);
  136.         }
  137.         $response->prepare($request);
  138.         return $response;
  139.     }
  140.     /**
  141.      * @Rest\Post("/api/coloresPersonalesShare", name="coloresPersonalesShare")
  142.      */
  143.     public function compartirColoresPersonales(Request $requestSerializerInterface $serializer)
  144.     {
  145.         $cuenta $this->get('security.token_storage')->getToken()->getUser();
  146.         $requestObject json_decode($request->getContent(), true);
  147.         //desactivamos FP
  148.         try {
  149.             $correcto $this->service->compartirColoresPersonales($cuenta$requestObject['colorId']);
  150.         } catch (\Exception $e) {
  151.             $response =  new Response('KO'Response::HTTP_CONFLICT);
  152.             return $response;
  153.         }
  154.         if ($correcto != 0) {
  155.             $response =  new Response(Response::HTTP_OK);
  156.         } else {
  157.             $response =  new Response(Response::HTTP_BAD_REQUEST);
  158.         }
  159.         $response->prepare($request);
  160.         return $response;
  161.     }
  162.     /**
  163.      * @Rest\Post("/api/coloresPersonalesNoShare", name="coloresPersonalesNoShare")
  164.      */
  165.     public function noCompartirColoresPersonales(Request $requestSerializerInterface $serializer)
  166.     {
  167.         $cuenta $this->get('security.token_storage')->getToken()->getUser();
  168.         $requestObject json_decode($request->getContent(), true);
  169.         //desactivamos FP
  170.         try {
  171.             $correcto $this->service->noCompartirColoresPersonales($cuenta$requestObject['colorId']);
  172.         } catch (\Exception $e) {
  173.             $response =  new Response('KO'Response::HTTP_CONFLICT);
  174.             return $response;
  175.         }
  176.         if ($correcto != 0) {
  177.             $response =  new Response(Response::HTTP_OK);
  178.         } else {
  179.             $response =  new Response(Response::HTTP_BAD_REQUEST);
  180.         }
  181.         $response->prepare($request);
  182.         return $response;
  183.     }
  184.     /**
  185.      * @Rest\Post("/api/colorPersonalVisto", name="colorPersonalVisto")
  186.      */
  187.     public function marcarColorPersonalVisto(Request $requestSerializerInterface $serializer)
  188.     {
  189.         $cuenta $this->get('security.token_storage')->getToken()->getUser();
  190.         $requestObject json_decode($request->getContent(), true);
  191.         //desactivamos FP
  192.         try {
  193.             $correcto $this->service->marcarColorPersonalVisto($cuenta$requestObject['colorId']);
  194.         } catch (\Exception $e) {
  195.             error_log($e);
  196.             $this->log->info('ERROR: ' $e);
  197.             $response =  new Response('KO'Response::HTTP_CONFLICT);
  198.             return $response;
  199.         }
  200.         if ($correcto != 0) {
  201.             $response =  new Response(Response::HTTP_OK);
  202.         } else {
  203.             $response =  new Response(Response::HTTP_BAD_REQUEST);
  204.         }
  205.         $response->prepare($request);
  206.         return $response;
  207.     }
  208.     /**
  209.      * @Rest\Get("/api/numeroColoresShared", name="numeroColoresShared")
  210.      */
  211.     public function getNumeroColoresShared(Request $request)
  212.     {
  213.         $cuenta $this->get('security.token_storage')->getToken()->getUser();
  214.         $result 0;
  215.         $result $this->service->getNumeroColoresShared($cuenta);
  216.         $response =  new Response(
  217.             $this->serializer->serialize(
  218.                 $result,
  219.                 'json'
  220.             ),
  221.             Response::HTTP_OK,
  222.             ['Content-Type' => 'json']
  223.         );
  224.         return $response;
  225.     }
  226.     /**
  227.      * @Rest\Get("api/paginarestringida", name="prueba_tes")
  228.      */
  229.     public function funcion_test()
  230.     {
  231.         $response =  new Response("acceso correcto"Response::HTTP_OK);
  232.         return $response;
  233.     }
  234.     /**
  235.      * @Rest\Get("api/admin/colores", name="obtieneColorTipo")
  236.      */
  237.     public function obtienecolores(Request $request)
  238.     {
  239.         $data $this->service->getAllNormal();
  240.         if (!$data) {
  241.             $data = [];
  242.         }
  243.         $response =  new Response($this->serializer->serialize($data'json'), Response::HTTP_OK);
  244.         $response->headers->set('Content-Type''json'); //xml
  245.         $response->prepare($request);
  246.         return $response;
  247.     }
  248.     /**
  249.      * @Rest\Post("api/admin/formulaColores", name="obtieneFormulaColores")
  250.      */
  251.     public function obtieneFormulaColores(Request $request)
  252.     {
  253.         $formulaID $request->getContent();
  254.         $data $this->service->getFormulaColores($formulaID);
  255.         if (!$data) {
  256.             $data = [];
  257.         }
  258.         $response =  new Response($this->serializer->serialize($data'json'), Response::HTTP_OK);
  259.         $response->headers->set('Content-Type''json'); //xml
  260.         $response->prepare($request);
  261.         return $response;
  262.     }
  263.     /**
  264.      * @Rest\Get("api/admin/coloresDetalles", name="obtieneColoresDetalles")
  265.      */
  266.     public function obtienecoloresDetalles(Request $request)
  267.     {
  268.         $data $this->service->getAllDetalles();
  269.         if (!$data) {
  270.             $data = [];
  271.         }
  272.         $response =  new Response($this->serializer->serialize($data'json'), Response::HTTP_OK);
  273.         $response->headers->set('Content-Type''json'); //xml
  274.         $response->prepare($request);
  275.         return $response;
  276.     }
  277.     /**
  278.      * @Rest\Get("api/admin/coloresPersonales", name="obtieneColorPersonal")
  279.      */
  280.     public function obtienecoloresPersonales(Request $request)
  281.     {
  282.         //$serializer = $this->get('jms_serializer');
  283.         $data $this->service->getAllPersonal('');
  284.         if (!$data) {
  285.             $data = [];
  286.         }
  287.         $response =  new Response($this->serializer->serialize($data'json'SerializationContext::create()->setGroups('listAdmin')), Response::HTTP_OK);
  288.         $response->headers->set('Content-Type''json'); //xml
  289.         $response->prepare($request);
  290.         return $response;
  291.     }
  292.     /**
  293.      * @Rest\Get("api/admin/coloresPersonalesBesa", name="obtieneColorPersonalBesa")
  294.      */
  295.     public function obtienecoloresPersonalesBesa(Request $request)
  296.     {
  297.         //$serializer = $this->get('jms_serializer');
  298.         $data $this->service->getAllPersonal('BESA');
  299.         if (!$data) {
  300.             $data = [];
  301.         }
  302.         $response =  new Response($this->serializer->serialize($data'json'SerializationContext::create()->setGroups('listAdmin')), Response::HTTP_OK);
  303.         $response->headers->set('Content-Type''json'); //xml
  304.         $response->prepare($request);
  305.         return $response;
  306.     }
  307.     /**
  308.      * @Rest\Get("api/admin/coloresPersonalesCliente", name="obtieneColorPersonalCliente")
  309.      */
  310.     public function obtienecoloresPersonalesCliente(Request $request)
  311.     {
  312.         //$serializer = $this->get('jms_serializer');
  313.         $customerId null;
  314.         if ($request->query->get('customerId') != null && $request->query->get('customerId') != "") {
  315.             $customerId $request->query->get('customerId');
  316.         }
  317.         $data $this->service->getAllPersonal('CLIENTE'$customerId);
  318.         if (!$data) {
  319.             $data = [];
  320.         }
  321.         $response =  new Response($this->serializer->serialize($data'json'SerializationContext::create()->setGroups('listAdmin')), Response::HTTP_OK);
  322.         $response->headers->set('Content-Type''json'); //xml
  323.         $response->prepare($request);
  324.         return $response;
  325.     }
  326.     /**
  327.      * @Rest\Get("api/admin/numeroColoresPersonales", name="obtieneNumeroColoresPersonales")
  328.      */
  329.     public function numeroColoresPersonales(Request $request)
  330.     {
  331.         //$serializer = $this->get('jms_serializer');
  332.         $data $this->service->getAllPersonal('ALL');
  333.         // if(!$data)
  334.         // {
  335.         //     $data = 0;
  336.         // }
  337.         $response =  new Response($this->serializer->serialize($data'json'SerializationContext::create()->setGroups('listAdmin')), Response::HTTP_OK);
  338.         $response->headers->set('Content-Type''json'); //xml
  339.         $response->prepare($request);
  340.         return $response;
  341.     }
  342.     /**
  343.      * @Rest\Post("api/admin/colorUpdate", name="colorCreateOrReplace")
  344.      */
  345.     public function colorNewOrUpdate(Request $request)
  346.     {
  347.         $correcto null;
  348.         $response =  new Response(Response::HTTP_BAD_REQUEST);
  349.         if (=== strpos($request->headers->get('Content-Type'), 'application/json')) {
  350.             $color json_decode($request->getContent(), true);
  351.             try {
  352.                 if ($color['codigo'] == "" || $color['nombre'] == "") {
  353.                     throw new \Exception();
  354.                 }
  355.                 if (isset($color['id']) && $color['id'] != "") {
  356.                     if (isset($color['copiar'])) {
  357.                         // Copiaremos el color con sus formulas
  358.                         $correcto $this->serviceFormula->copiaColor($color);
  359.                     } else {
  360.                         //Tenemos Id por lo que es una actulizacion
  361.                         $correcto $this->service->modificaColor($color);
  362.                     }
  363.                 } else {
  364.                     $correcto $this->service->nuevoColor($color);
  365.                 }
  366.             } catch (\Exception $e) {
  367.                 error_log($e);
  368.                 $this->log->error('ERROR EXCEPTION: ' $e);
  369.                 $response =  new Response(Response::HTTP_BAD_REQUESTResponse::HTTP_BAD_REQUEST);
  370.                 return $response;
  371.             }
  372.         }
  373.         if ($correcto != null) {
  374.             $response =  new Response($this->serializer->serialize($correcto->getId(), 'json'SerializationContext::create()->setGroups('listAdmin')), Response::HTTP_OK);
  375.             // $response =  new Response(Response::HTTP_OK,Response::HTTP_OK);
  376.         } else {
  377.             $response =  new Response(Response::HTTP_BAD_REQUESTResponse::HTTP_BAD_REQUEST);
  378.         }
  379.         $response->prepare($request);
  380.         return $response;
  381.     }
  382. }