GrupoUsuarioController.php
4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace Cacic\CommonBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Cacic\CommonBundle\Entity\GrupoUsuario;
use Cacic\CommonBundle\Form\Type\GrupoUsuarioType;
use Doctrine\Common\Util\Debug;
/**
*
* CRUD da Entidade Grupo de Usuario
* @author lightbase
*
*/
class GrupoUsuarioController extends Controller
{
/**
*
* Tela de listagem
* @param $page
*/
public function indexAction( $page )
{
return $this->render(
'CacicCommonBundle:GrupoUsuario:index.html.twig',
array( 'GrupoUsuario' => $this->getDoctrine()->getRepository( 'CacicCommonBundle:GrupoUsuario' )->paginar( $this->get( 'knp_paginator' ), $page ))
);
}
public function cadastrarAction(Request $request)
{
$GrupoUsuario = new GrupoUsuario();
$form = $this->createForm( new GrupoUsuarioType(), $GrupoUsuario );
if ( $request->isMethod('POST') )
{
$form->bind( $request );
if ( $form->isValid() )
{
$this->getDoctrine()->getManager()->persist( $GrupoUsuario );
$this->getDoctrine()->getManager()->flush();
$this->get('session')->getFlashBag()->add('success', 'Dados salvos com sucesso!');
return $this->redirect( $this->generateUrl( 'cacic_grupo_usuario_index') );
}
}
return $this->render( 'CacicCommonBundle:GrupoUsuario:cadastrar.html.twig', array( 'form' => $form->createView() ) );
}
/**
* Página de editar dados do Grupo de Usuarios
* @param int $idGrupoUsuario
*/
public function editarAction( $idGrupoUsuario, Request $request )
{
$GrupoUsuario = $this->getDoctrine()->getRepository('CacicCommonBundle:GrupoUsuario')->find( $idGrupoUsuario );
if ( ! $GrupoUsuario )
throw $this->createNotFoundException( 'Grupo de Usuario não encontrado' );
$form = $this->createForm( new GrupoUsuarioType(), $GrupoUsuario );
if ( $request->isMethod('POST') )
{
$form->bind( $request );
if ( $form->isValid() )
{
$csNivel = $this->getDoctrine()->getRepository('CacicCommonBundle:GrupoUsuario')->nivel($GrupoUsuario);
if($csNivel[0]['teGrupoUsuarios'] != "Administração")
{
$this->getDoctrine()->getManager()->persist( $GrupoUsuario );
$this->getDoctrine()->getManager()->flush();
$this->get('session')->getFlashBag()->add('success', 'Dados salvos com sucesso!');
return $this->redirect($this->generateUrl('cacic_grupo_usuario_editar', array( 'idGrupoUsuario'=>$GrupoUsuario->getIdGrupoUsuario() ) ) );
}else{
$this->get('session')->getFlashBag()->add('error', 'O Grupo de Administradores, não pode ser editado!!!');
}
}
}
return $this->render( 'CacicCommonBundle:GrupoUsuario:cadastrar.html.twig', array( 'form' => $form->createView() ) );
}
/**
*
* [AJAX] Exclusão de Grupo de Usuario já cadastrado
* @param integer $idGrupoUsuario
*/
public function excluirAction( Request $request )
{
if ( ! $request->isXmlHttpRequest() ) // Verifica se se trata de uma requisição AJAX
throw $this->createNotFoundException( 'Página não encontrada' );
$GrupoUsuario = $this->getDoctrine()->getRepository('CacicCommonBundle:GrupoUsuario')->find( $request->get('id') );
if ( ! $GrupoUsuario )
throw $this->createNotFoundException( 'Grupo de Usuario não encontrado' );
$csNivel = $this->getDoctrine()->getRepository('CacicCommonBundle:GrupoUsuario')->nivel($request->get('id'));
if($csNivel[0]['teGrupoUsuarios'] != "Administração")
{
$em = $this->getDoctrine()->getManager();
$em->remove( $GrupoUsuario );
$em->flush();
$response = new Response( json_encode( array('status' => 'ok') ) );
$response->headers->set('Content-Type', 'application/json');
return $response;
}else{
$this->get('session')->getFlashBag()->add('error', 'O Grupo de Administradores, não pode ser Excluido!!!');
}
}
}