<?php
namespace App\Controller;
use App\Service\ColorService;
use App\Service\FormulaService;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use JMS\Serializer\SerializationContext;
use Symfony\Component\Security\Core\Security;
use JMS\Serializer\SerializerInterface;
use Psr\Log\LoggerInterface;
class ColorController extends AbstractController
{
private $security;
private $serializer;
private $serviceFormula;
private $log;
public function __construct(ColorService $colorService, FormulaService $serviceFormula, Security $security, SerializerInterface $serializer, LoggerInterface $logger)
{
$this->serializer = $serializer;
$this->service = $colorService;
$this->serviceFormula = $serviceFormula;
$this->security = $security;
$this->log = $logger;
}
// TODO::Refactorizar este método (borrar)
/*
* @Rest\Get("api/colors/{id}",name="obtener_color")
public function obtenerColor($id)
{
try {
$response = $this->service->findById($id);
$response = new Response($this->serializer->serialize($response, 'json',SerializationContext::create()->setGroups('listAdmin')),Response::HTTP_OK);
return $response;
} catch (Exception $e) {
error_log($e);
return new Response(Response::HTTP_BAD_REQUEST);
}
}
*/
// TODO::Refactorizar este metodo (borrar)
/*
* @Rest\Options("api/colors",name="obtener_colores")
public function obtenerColores(Request $request)
{
//$data = $this->service->getAll();
$data = $this->service->getExample();
//$data = $this->service->get(1);
$code = 200;
$error = false;
$message = '';
$response = [
'code' => $code,
'error' => $error,
'data' => $code == 200 ? $data : $message,
];
$response = new Response($this->serializer->serialize($data, "json"),Response::HTTP_OK);
$response->headers->set('Content-Type', 'json'); //xml
$response->prepare($request);
// var_dump($data);
//return $this->render('default/index.html.twig', [
// 'controller_name' => 'DefaultController',
// 'colors' => $data
//]);
$response->send();
}
*/
/*
* @Rest\Get("api_inicio/load_inicial/obtenerColores")
public function obtenerColoresIniciales(Request $request)
{
return $this->obtenerColoresPorDisponibilidadPastas($request);
}
*/
/**
*
* @Rest\Get("api/obtenerColores", name="obtener_colores_disponibilidad")
*
*/
public function obtenerColoresPorDisponibilidadPastas(Request $request)
{
$cuenta = $this->get('security.token_storage')->getToken()->getUser();
$data = $this->service->getAllOrderNombre($cuenta);
$response = new Response($this->serializer->serialize($data, "json"), Response::HTTP_OK);
$response->headers->set('Content-Type', 'json'); //xml
$response->prepare($request);
return $response;
}
/**
* @Rest\Post("/api/colorPersonalDesactivar", name="colorPersonalDesactivar")
*/
public function desactivarColorPersonal(Request $request, SerializerInterface $serializer)
{
$colorPersonal = '';
$usr = $this->get('security.token_storage')->getToken()->getUser();
if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
$requestObject = json_decode($request->getContent(), true);
if (isset($requestObject['id'])) {
//desactivamos FP
try {
$colorPersonal = $this->service->desactivaColorPersonal($requestObject['id']);
} catch (\Exception $e) {
$response = new Response('KO', Response::HTTP_CONFLICT);
return $response;
}
$response = new Response(Response::HTTP_OK);
return $response;
} else {
$response = new Response(Response::HTTP_BAD_REQUEST);
return $response;
}
} else {
$response = new Response(Response::HTTP_BAD_REQUEST);
return $response;
}
}
/**
* @Rest\Post("/api/coloresPersonalesDesactivar", name="coloresPersonalesDesactivar")
*/
public function desactivarColoresPersonales(Request $request, SerializerInterface $serializer)
{
$cuenta = $this->get('security.token_storage')->getToken()->getUser();
//desactivamos FP
try {
$correcto = $this->service->desactivarColoresPersonales($cuenta->getId());
} catch (\Exception $e) {
$response = new Response('KO', Response::HTTP_CONFLICT);
return $response;
}
if ($correcto != 0) {
$response = new Response(Response::HTTP_OK);
} else {
$response = new Response(Response::HTTP_BAD_REQUEST);
}
$response->prepare($request);
return $response;
}
/**
* @Rest\Post("/api/coloresPersonalesShare", name="coloresPersonalesShare")
*/
public function compartirColoresPersonales(Request $request, SerializerInterface $serializer)
{
$cuenta = $this->get('security.token_storage')->getToken()->getUser();
$requestObject = json_decode($request->getContent(), true);
//desactivamos FP
try {
$correcto = $this->service->compartirColoresPersonales($cuenta, $requestObject['colorId']);
} catch (\Exception $e) {
$response = new Response('KO', Response::HTTP_CONFLICT);
return $response;
}
if ($correcto != 0) {
$response = new Response(Response::HTTP_OK);
} else {
$response = new Response(Response::HTTP_BAD_REQUEST);
}
$response->prepare($request);
return $response;
}
/**
* @Rest\Post("/api/coloresPersonalesNoShare", name="coloresPersonalesNoShare")
*/
public function noCompartirColoresPersonales(Request $request, SerializerInterface $serializer)
{
$cuenta = $this->get('security.token_storage')->getToken()->getUser();
$requestObject = json_decode($request->getContent(), true);
//desactivamos FP
try {
$correcto = $this->service->noCompartirColoresPersonales($cuenta, $requestObject['colorId']);
} catch (\Exception $e) {
$response = new Response('KO', Response::HTTP_CONFLICT);
return $response;
}
if ($correcto != 0) {
$response = new Response(Response::HTTP_OK);
} else {
$response = new Response(Response::HTTP_BAD_REQUEST);
}
$response->prepare($request);
return $response;
}
/**
* @Rest\Post("/api/colorPersonalVisto", name="colorPersonalVisto")
*/
public function marcarColorPersonalVisto(Request $request, SerializerInterface $serializer)
{
$cuenta = $this->get('security.token_storage')->getToken()->getUser();
$requestObject = json_decode($request->getContent(), true);
//desactivamos FP
try {
$correcto = $this->service->marcarColorPersonalVisto($cuenta, $requestObject['colorId']);
} catch (\Exception $e) {
error_log($e);
$this->log->info('ERROR: ' . $e);
$response = new Response('KO', Response::HTTP_CONFLICT);
return $response;
}
if ($correcto != 0) {
$response = new Response(Response::HTTP_OK);
} else {
$response = new Response(Response::HTTP_BAD_REQUEST);
}
$response->prepare($request);
return $response;
}
/**
* @Rest\Get("/api/numeroColoresShared", name="numeroColoresShared")
*/
public function getNumeroColoresShared(Request $request)
{
$cuenta = $this->get('security.token_storage')->getToken()->getUser();
$result = 0;
$result = $this->service->getNumeroColoresShared($cuenta);
$response = new Response(
$this->serializer->serialize(
$result,
'json'
),
Response::HTTP_OK,
['Content-Type' => 'json']
);
return $response;
}
/**
* @Rest\Get("api/paginarestringida", name="prueba_tes")
*/
public function funcion_test()
{
$response = new Response("acceso correcto", Response::HTTP_OK);
return $response;
}
/**
* @Rest\Get("api/admin/colores", name="obtieneColorTipo")
*/
public function obtienecolores(Request $request)
{
$data = $this->service->getAllNormal();
if (!$data) {
$data = [];
}
$response = new Response($this->serializer->serialize($data, 'json'), Response::HTTP_OK);
$response->headers->set('Content-Type', 'json'); //xml
$response->prepare($request);
return $response;
}
/**
* @Rest\Post("api/admin/formulaColores", name="obtieneFormulaColores")
*/
public function obtieneFormulaColores(Request $request)
{
$formulaID = $request->getContent();
$data = $this->service->getFormulaColores($formulaID);
if (!$data) {
$data = [];
}
$response = new Response($this->serializer->serialize($data, 'json'), Response::HTTP_OK);
$response->headers->set('Content-Type', 'json'); //xml
$response->prepare($request);
return $response;
}
/**
* @Rest\Get("api/admin/coloresDetalles", name="obtieneColoresDetalles")
*/
public function obtienecoloresDetalles(Request $request)
{
$data = $this->service->getAllDetalles();
if (!$data) {
$data = [];
}
$response = new Response($this->serializer->serialize($data, 'json'), Response::HTTP_OK);
$response->headers->set('Content-Type', 'json'); //xml
$response->prepare($request);
return $response;
}
/**
* @Rest\Get("api/admin/coloresPersonales", name="obtieneColorPersonal")
*/
public function obtienecoloresPersonales(Request $request)
{
//$serializer = $this->get('jms_serializer');
$data = $this->service->getAllPersonal('');
if (!$data) {
$data = [];
}
$response = new Response($this->serializer->serialize($data, 'json', SerializationContext::create()->setGroups('listAdmin')), Response::HTTP_OK);
$response->headers->set('Content-Type', 'json'); //xml
$response->prepare($request);
return $response;
}
/**
* @Rest\Get("api/admin/coloresPersonalesBesa", name="obtieneColorPersonalBesa")
*/
public function obtienecoloresPersonalesBesa(Request $request)
{
//$serializer = $this->get('jms_serializer');
$data = $this->service->getAllPersonal('BESA');
if (!$data) {
$data = [];
}
$response = new Response($this->serializer->serialize($data, 'json', SerializationContext::create()->setGroups('listAdmin')), Response::HTTP_OK);
$response->headers->set('Content-Type', 'json'); //xml
$response->prepare($request);
return $response;
}
/**
* @Rest\Get("api/admin/coloresPersonalesCliente", name="obtieneColorPersonalCliente")
*/
public function obtienecoloresPersonalesCliente(Request $request)
{
//$serializer = $this->get('jms_serializer');
$customerId = null;
if ($request->query->get('customerId') != null && $request->query->get('customerId') != "") {
$customerId = $request->query->get('customerId');
}
$data = $this->service->getAllPersonal('CLIENTE', $customerId);
if (!$data) {
$data = [];
}
$response = new Response($this->serializer->serialize($data, 'json', SerializationContext::create()->setGroups('listAdmin')), Response::HTTP_OK);
$response->headers->set('Content-Type', 'json'); //xml
$response->prepare($request);
return $response;
}
/**
* @Rest\Get("api/admin/numeroColoresPersonales", name="obtieneNumeroColoresPersonales")
*/
public function numeroColoresPersonales(Request $request)
{
//$serializer = $this->get('jms_serializer');
$data = $this->service->getAllPersonal('ALL');
// if(!$data)
// {
// $data = 0;
// }
$response = new Response($this->serializer->serialize($data, 'json', SerializationContext::create()->setGroups('listAdmin')), Response::HTTP_OK);
$response->headers->set('Content-Type', 'json'); //xml
$response->prepare($request);
return $response;
}
/**
* @Rest\Post("api/admin/colorUpdate", name="colorCreateOrReplace")
*/
public function colorNewOrUpdate(Request $request)
{
$correcto = null;
$response = new Response(Response::HTTP_BAD_REQUEST);
if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
$color = json_decode($request->getContent(), true);
try {
if ($color['codigo'] == "" || $color['nombre'] == "") {
throw new \Exception();
}
if (isset($color['id']) && $color['id'] != "") {
if (isset($color['copiar'])) {
// Copiaremos el color con sus formulas
$correcto = $this->serviceFormula->copiaColor($color);
} else {
//Tenemos Id por lo que es una actulizacion
$correcto = $this->service->modificaColor($color);
}
} else {
$correcto = $this->service->nuevoColor($color);
}
} catch (\Exception $e) {
error_log($e);
$this->log->error('ERROR EXCEPTION: ' . $e);
$response = new Response(Response::HTTP_BAD_REQUEST, Response::HTTP_BAD_REQUEST);
return $response;
}
}
if ($correcto != null) {
$response = new Response($this->serializer->serialize($correcto->getId(), 'json', SerializationContext::create()->setGroups('listAdmin')), Response::HTTP_OK);
// $response = new Response(Response::HTTP_OK,Response::HTTP_OK);
} else {
$response = new Response(Response::HTTP_BAD_REQUEST, Response::HTTP_BAD_REQUEST);
}
$response->prepare($request);
return $response;
}
}