LocalController.php
7.8 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
namespace Cacic\CommonBundle\Controller;
use Doctrine\Common\Util\Debug;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Cacic\CommonBundle\Entity\Local;
use Cacic\CommonBundle\Form\Type\LocalType;
/**
*
* CRUD da Entidade Locais
* @author lightbase
*
*/
class LocalController extends Controller
{
/**
*
* Tela de listagem
* @param $page
*/
public function indexAction()
{
return $this->render(
'CacicCommonBundle:Local:index.html.twig',
array(
'locais' => $this->getDoctrine()->getRepository( 'CacicCommonBundle:Local' )->listar()
)
);
}
/**
*
* Tela de cadastro de novo Local
*/
public function cadastrarAction( Request $request )
{
$local = new Local();
$form = $this->createForm( new LocalType(), $local );
if ( $request->isMethod('POST') )
{
$form->bind( $request );
if ( $form->isValid() )
{
$em = $this->getDoctrine()->getManager();
$em->getConnection()->beginTransaction(); // Inicia a transação
try
{
$em->persist( $local );
$em->flush(); // Persiste os dados do Local
$this->getDoctrine()->getRepository( 'CacicCommonBundle:ConfiguracaoLocal' )->configurarLocalFromConfigPadrao( $local );
$em->getConnection()->commit(); // Encerra a transação e confirma as alterações na base de dados
$this->get('session')->getFlashBag()->add('success', 'Dados salvos com sucesso!');
}
catch ( \Exception $e )
{
$em->getConnection()->rollback(); // Desfaz as alterações feitas na base de dados até o momento em que a exceção foi lançada
$em->close();
/**
* @todo verificar maneiras para tratar a mensagem antes de envia-la ao usuário
*/
throw $e; // Envia a mensagem de erro para a tela
}
return $this->redirect( $this->generateUrl( 'cacic_local_index' ) );
}
}
return $this->render(
'CacicCommonBundle:Local:cadastrar.html.twig',
array( 'form' => $form->createView() )
);
}
/**
*
* Tela de edição de Local já cadastrado
* @param integer $idLocal
*/
public function editarAction( $idLocal, Request $request )
{
$local = $this->getDoctrine()->getRepository( 'CacicCommonBundle:Local' )->find( $idLocal );
if ( ! $local )
throw $this->createNotFoundException( 'Local não encontrado' );
$form = $this->createForm( new LocalType(), $local );
if ( $request->isMethod('POST') )
{
$form->bind( $request );
if ( $form->isValid() )
{
$this->getDoctrine()->getManager()->persist( $local );
$this->getDoctrine()->getManager()->flush(); // Efetua a edição do Local
$this->get('session')->getFlashBag()->add('success', 'Dados salvos com sucesso!');
return $this->redirect($this->generateUrl('cacic_local_editar', array( 'idLocal'=>$local->getIdLocal() ) ) );
}
}
return $this->render(
'CacicCommonBundle:Local:editar.html.twig',
array( 'form' => $form->createView() )
);
}
/**
*
* [AJAX] Exclusão de Local já cadastrado
* @param integer $idLocal
*/
public function excluirAction( Request $request )
{
$local = $this->getDoctrine()->getRepository('CacicCommonBundle:Local')->find( $request->get('id') );
if ( ! $local )
throw $this->createNotFoundException( 'Local não encontrado' );
$em = $this->getDoctrine()->getManager();
$em->getConnection()->beginTransaction(); // Inicia a transação
try
{
/**
* Primeiramente, remove as configurações aplicadas ao Local
*/
$this->getDoctrine()->getManager()->getRepository('CacicCommonBundle:Local')->excluirLocal( $local );
$em->remove( $local ); // Tenta excluir o registro da base de dados
$em->flush();
$em->getConnection()->commit(); // Aplica as alterações na base de dados
$out = array('status' => 'ok');
}
catch ( \Doctrine\DBAL\DBALException $e )
{
$this->get('logger')->error("Erro na exclusão do local\n".$e->getMessage());
$out = array('status' => 'error', 'code' => false);
if ( preg_match('#SQLSTATE\[(\d+)\]#', $e->getMessage(), $tmp) )
$out['code'] = $tmp[1];
$em->getConnection()->rollback(); // Desfaz as alterações feitas na base de dados até o momento em que a exceção foi lançada
$em->close();
}
$response = new Response( json_encode( $out ) );
$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
*
* [GRID] Redes associadas ao Local
*/
public function redesAction( $idLocal )
{
return $this->render(
'CacicCommonBundle:Local:redes.html.twig',
array( 'redes' => $this->getDoctrine()->getRepository( 'CacicCommonBundle:Rede' )->listarPorLocal( $idLocal ) )
);
}
/**
*
* [GRID] Usuários associados ao Local
*/
public function usuariosAction( $idLocal )
{
return $this->render(
'CacicCommonBundle:Local:usuarios.html.twig',
array(
'usuarios' => $this->getDoctrine()->getRepository( 'CacicCommonBundle:Usuario' )->listarPorLocal( $idLocal ),
'idLocal' => $idLocal
)
);
}
/**
*
* [FORM] Configurações associadas ao Local
*/
public function configuracoesAction( $idLocal )
{
return $this->render(
'CacicCommonBundle:Local:configuracoes.html.twig',
array(
'configuracoes' => $this->getDoctrine()->getRepository( 'CacicCommonBundle:ConfiguracaoLocal' )->listarPorLocal( $idLocal ),
'idLocal' => $idLocal
)
);
}
/**
*
* Tela de importação de arquivo CSV com registros de Locais
*/
public function importarcsvAction( Request $request )
{
$form = $this->createFormBuilder()
->add('arquivocsv', 'file', array('label' => 'Arquivo', 'attr' => array( 'accept' => '.csv' )))
->getForm();
$form->handleRequest( $request );
if ( $form->isValid() )
{
//if ( $form['arquivocsv']->getData() instanceof \Symfony\Component\HttpFoundation\File\UploadedFile )
{
// Executa a importação do arquivo - grava no diretório web/upload/migracao
$dirMigracao = realpath( dirname(__FILE__) .'/../../../../web/upload/migracao/' );
$fileName = 'Locais_U'.$this->getUser()->getIdUsuario().'T'.time().'.csv';
$form['arquivocsv']->getData()->move( $dirMigracao, $fileName );
$em = $this->getDoctrine()->getManager();
// Abre o arquivo salvo e começa a rotina de importação dos dados do CSV
$csv = file( $dirMigracao.'/'.$fileName );
foreach( $csv as $k => $v )
{
// Valida a linha
$v = explode( ';', trim( str_replace( '"', '', $v ) ) );
if ( count( $v ) != 4 )
continue;
$local = new Local();
// desabilita a geracao automatica do id
$metadata = $em->getClassMetaData(get_class($local));
$metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);
$local->setIdLocal( (int) $v[0] );
$local->setNmLocal( $v[1] );
$local->setSgLocal( $v[2] );
$local->setTeObservacao( $v[3] );
$em->persist( $local );
}
$em->flush(); // Persiste os dados dos Locais
$this->get('session')->getFlashBag()->add('success', 'Importação realizada com sucesso!');
}
//else $this->get('session')->getFlashBag()->add('error', 'Arquivo CSV inválido!');
return $this->redirect( $this->generateUrl( 'cacic_migracao_local') );
}
return $this->render(
'CacicCommonBundle:Local:importarcsv.html.twig',
array( 'form' => $form->createView() )
);
}
}