UorgController.php
5.63 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
namespace Cacic\CommonBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Cacic\CommonBundle\Entity\Uorg;
use Cacic\CommonBundle\Form\Type\UorgType;
use Doctrine\Common\Util\Debug;
/**
*
* UNIDADES ORGANIZACIONAIS
* @author LightBase
*
*/
class UorgController extends Controller
{
/**
*
* Tela de listagem das Unidades Organizacionais cadastradas
*/
public function indexAction()
{
$treesUorg = $this->getDoctrine()->getRepository( 'CacicCommonBundle:Uorg' )->getPrimeiroNivel(); // Carrega o primeiro nível de UOrgs
return $this->render( 'CacicCommonBundle:Uorg:index.html.twig', array( 'uorgs' => $treesUorg ) );
}
/**
*
* [AJAX][jqTree] Carrega as Unidades Organizacionais filhas da Unidade Organizacional informada
* @param Symfony\Component\HttpFoundation\Request $request
*/
public function loadnodesAction( Request $request )
{
if ( ! $request->isXmlHttpRequest() )
throw $this->createNotFoundException( 'Página não encontrada' );
$uorgs = $this->getDoctrine()->getRepository( 'CacicCommonBundle:Uorg' )->getFolhasDoNo( $request->get( 'idUorgPai' ) );
# Monta um array no formato suportado pelo plugin-in jqTree (JQuery)
$_tree = array();
foreach ( $uorgs as $uorg )
{
$_tree[] = array(
'label' => $uorg['nmUorg'],
'filho' => $uorg['numFilhas'],
'rede' => $uorg['idRede'],
'id' => $uorg['idUorg'],
'load_on_demand' => (bool) $uorg['numFilhas']
);
}
$response = new Response( json_encode( $_tree ) );
$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
*
* Tela de cadastro de Unidade Organizacional
*
* @param int $idUorgPai
* @param Symfony\Component\HttpFoundation\Request $request
*/
public function cadastrarAction( $idUorgPai, Request $request )
{
$uorgPai = null; // Inicializa o UOrgPai
if ( $idUorgPai !== null )
{ // Caso o idUorgPai seja informado
$uorgPai = $this->getDoctrine()->getRepository( 'CacicCommonBundle:Uorg' )->find( $idUorgPai );
if ( ! $uorgPai ) // UOrgPai não é válida
throw $this->createNotFoundException( 'Unidade Organizacional não encontrada' );
}
$uorg = new Uorg();
$uorg->setUorgPai( $uorgPai ); // Relaciona a nova UOrg à UOrgPai
$form = $this->createForm( new UorgType(), $uorg );
if ( $request->isMethod('POST') )
{
$form->bind( $request );
if ( $form->isValid() )
{
$this->getDoctrine()->getManager()->persist( $uorg );
$this->getDoctrine()->getManager()->flush(); // Efetua o cadastro da Unidade
$this->get('session')->getFlashBag()->add('success', 'Dados salvos com sucesso!');
return $this->redirect($this->generateUrl('cacic_uorg_index') );
}
}
return $this->render(
'CacicCommonBundle:Uorg:cadastrar.html.twig',
array( 'form' => $form->createView(), 'uorgPai' => $uorgPai )
);
}
/**
*
* Tela de edição de Unidade Organizacional
* @param int $idUorg
* @param Symfony\Component\HttpFoundation\Request $request
*/
public function editarAction( $idUorg, Request $request )
{
$uorg = $this->getDoctrine()->getRepository( 'CacicCommonBundle:Uorg' )->find( $idUorg );
if ( ! $uorg ) // UOrg não é válida
throw $this->createNotFoundException( 'Unidade Organizacional não encontrada' );
$form = $this->createForm( new UorgType(), $uorg );
if ( $request->isMethod('POST') )
{
$form->bind( $request );
if ( $form->isValid() )
{
$this->getDoctrine()->getManager()->persist( $uorg );
$this->getDoctrine()->getManager()->flush(); // Efetua o cadastro da Unidade
$this->get('session')->getFlashBag()->add('success', 'Dados salvos com sucesso!');
return $this->redirect($this->generateUrl('cacic_uorg_editar', array( 'idUorg'=>$uorg->getIdUorg() ) ) );
}
}
return $this->render(
'CacicCommonBundle:Uorg:cadastrar.html.twig',
array( 'form' => $form->createView(), 'uorgPai' => $uorg->getUorgPai() )
);
}
/**
*
* [AJAX][MODAL] Tela de visualização dos dados da UNIDADE parametrizada
* @param int $idUorg
* @param Symfony\Component\HttpFoundation\Request $request
*/
public function visualizarAction( $idUorg, Request $request )
{
/*if ( ! $request->isXmlHttpRequest() ) // Verifica se é uma requisição AJAX
throw $this->createNotFoundException( 'Página não encontrada' );
*/
$uorg = $this->getDoctrine()->getRepository( 'CacicCommonBundle:Uorg' )->find( $idUorg );
if ( ! $uorg ) // UOrg não é válida
throw $this->createNotFoundException( 'Unidade Organizacional não encontrada' );
return $this->render(
'CacicCommonBundle:Uorg:visualizar.html.twig',
array( 'uorg' => $uorg )
);
}
/**
*
* [AJAX] Remove a UNIDADE ORGANIZACIONAL INFORMADA E TODAS AS UNIDADES A ELA RELACIONADAS
* @param int $idUorg
* @param Symfony\Component\HttpFoundation\Request $request
*/
public function excluirAction( $idUorg, Request $request )
{
if ( ! $request->isXmlHttpRequest() ) // Verifica se se trata de uma requisição AJAX
throw $this->createNotFoundException( 'Página não encontrada' );
$uorg = $this->getDoctrine()->getRepository('CacicCommonBundle:Uorg')->find( $request->get('id') );
if ( ! $uorg )
throw $this->createNotFoundException( 'Unidade Organizacional não encontrada' );
$em = $this->getDoctrine()->getManager();
$em->remove( $uorg );
$em->flush();
$response = new Response( json_encode( array('status' => 'ok') ) );
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}