<?php
namespace Noahtech\Sistemas\InterjamaBundle\Controller;
use Noahtech\Sistemas\InterjamaBundle\Utils\Codes;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Noahtech\Sistemas\InterjamaBundle\Controller\BaseController;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Noahtech\Sistemas\InterjamaBundle\Handler\McVepHandler;
class VepController extends BaseController {
private function validarAdmin () {
$usuario = $this->getUser();
$admin = false;
foreach ($usuario->getRoles() as $rol) {
if ($rol == 'ROLE_ADMIN'){
$admin = true;
}
}
if (!$admin){
throw new AccessDeniedException("No tiene permiso para acceder a esta pagina.");
}
}
/**
* @Route("/admin/veps", name="admin_vep_listado")
* @return type
*/
public function vepListadoAction() {
$this->validarAdmin();
$usuario = $this->getUser();
$this->setTitle("Listado de VEPS | InterJama");
$this->addBreadCrumb("Inicio - Admin", false, "admin_home");
$this->addBreadCrumb("Listado de VEPS", true);
$this->data['data'] = null;
return $this->render(
'@NoahtechSistemasInterjama/admin/veps/listado.html.twig', $this->data
);
}
/**
* consulta de veps
*
* @Route("/admin/ajax/veps/search", name="admin_ajax_veps_listado", methods={"POST"}, condition="request.isXmlHttpRequest()")
*/
public function searchAction(Request $request) {
$this->validarAdmin();
$searchParam = $request->request->all();
$currentPage = $request->query->get('page');
$sortField = $request->query->get('sort');
$sortDirection = $request->query->get('direction');
$currentPage = null == $currentPage ? 1 : $currentPage;
$offset = ($currentPage - 1) * $this->getTamanioPagina();
$limit = $this->getTamanioPagina();
try {
/** @var McVepHandler $handler */
$handler = $this->get(McVepHandler::class);
$lp = $handler->search($offset, $limit, $sortField, $sortDirection, $searchParam);
$lp->setCurrentPage($currentPage);
$lp->setPageSize($this->getTamanioPagina());
$this->response->setData($lp);
$this->response->setCode(Codes::OK);
} catch (Exception $e) {
$this->response->setCode(Codes::ERROR);
$this->response->setMessage($e->getMessage());
}
$serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json');
return new Response($serializedEntity);
}
/**
* @Route("/admin/vep/nuevo", name="admin_vep_nuevo")
* @return type
*/
public function vepNuevoAction() {
$this->validarAdmin();
$this->setTitle("Nuevo VEP");
$this->addBreadCrumb("Inicio - Admin", false, "admin_home");
$this->addBreadCrumb("Nuevo VEP", true);
$this->data['data'] = null;
return $this->render(
'@NoahtechSistemasInterjama/admin/veps/nuevo.html.twig', $this->data
);
}
/**
* Guarda una VEP
*
* @Route("/admin/ajax/vep", name="admin_vep_save", methods={"POST"}, condition="request.isXmlHttpRequest()")
*/
public function postSaveAction(Request $request) {
$this->validarAdmin();
$comprobante = $request->files->get('comprobante');
try {
$handler = $this->get(McVepHandler::class);
$vep = $handler->getVepFromRequest($request, $comprobante);
$result = $handler->save($vep);
$this->response->setData($result);
$this->response->setCode(Codes::OK);
} catch (Exception $e) {
$this->response->setCode(Codes::ERROR);
$this->response->setMessage($e->getMessage());
}
$serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json');
return new Response($serializedEntity);
}
/**
* @Route("/admin/vep/{id}", name="admin_vep_modificacion")
* @return type
*/
public function vepModificacionAction($id) {
$this->validarAdmin();
$this->setTitle("Modificar VEP");
$this->addBreadCrumb("Inicio - Admin", false, "admin_home");
$this->addBreadCrumb("Listado de VEPS", false, "admin_vep_listado");
$this->addBreadCrumb("Modificar VEP", true);
$this->data['data'] = $id;
return $this->render(
'@NoahtechSistemasInterjama/admin/veps/modificacion.html.twig', $this->data
);
}
/**
* Devuelve un VEP
*
* @Route("/admin/ajax/vep/{id}", name="admin_ajax_vep_get_by_id", methods={"GET"}, condition="request.isXmlHttpRequest()")
*/
public function getVepByIdAction($id) {
$this->validarAdmin();
try {
$handler = $this->get(McVepHandler::class);
$vep = $handler->getById($id);
$this->response->setData($vep);
$this->response->setCode(Codes::OK);
} catch (Exception $e) {
$this->response->setCode(Codes::ERROR);
$this->response->setMessage($e->getMessage());
}
$serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json');
return new Response($serializedEntity);
}
/**
* Modifica un VEP
*
* @Route("/admin/ajax/vep/{id}", name="admin_ajax_vep_update", methods={"POST"}, condition="request.isXmlHttpRequest()")
*/
public function putUpdateAction(Request $request, $id) {
$this->validarAdmin();
$data = $request->request->all();
$comprobante = $request->files->get('comprobante');
try {
$handler = $this->get(McVepHandler::class);
$vep = $handler->getVepFromRequest($request, $comprobante, $id);
$result = $handler->update($vep, $id);
$this->response->setData($result);
$this->response->setCode(Codes::OK);
} catch (Exception $e) {
$this->response->setCode(Codes::ERROR);
$this->response->setMessage($e->getMessage());
}
$serializedEntity = $this->container->get('serializer')->serialize($this->response, 'json');
return new Response($serializedEntity);
}
}