SoController.php
5.35 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
<?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\So;
use Cacic\CommonBundle\Form\Type\SoType;
class SoController extends Controller
{
public function indexAction( $page )
{
$arrso = $this->getDoctrine()->getRepository( 'CacicCommonBundle:So' )->paginar( $this->get( 'knp_paginator' ), $page );
return $this->render( 'CacicCommonBundle:So:index.html.twig', array( 'So' => $arrso ) );
}
public function cadastrarAction(Request $request)
{
$so = new So();
$form = $this->createForm(new SoType(), $so);
if ( $request->isMethod('POST') )
{
$form->bind( $request );
if ( $form->isValid() )
{
$this->getDoctrine()->getManager()->persist( $so );
$this->getDoctrine()->getManager()->flush(); //Persiste os dados do sistema operacional
$this->get('session')->getFlashBag()->add('success', 'Dados salvos com sucesso!');
return $this->redirect( $this->generateUrl( 'cacic_sistemaoperacional_index') );
}
}
return $this->render( 'CacicCommonBundle:So:cadastrar.html.twig', array( 'form' => $form->createView() ) );
}
/**
* Página de editar dados do sistema operacional
* @param int $idSo
*/
public function editarAction( $idSo, Request $request )
{
$so = $this->getDoctrine()->getRepository('CacicCommonBundle:So')->find( $idSo );
if ( ! $so )
throw $this->createNotFoundException( 'sistema operacional não encontrado' );
$form = $this->createForm( new SoType(), $so);
if ( $request->isMethod('POST') )
{
$form->bind( $request );
if ( $form->isValid() )
{
$this->getDoctrine()->getManager()->persist( $so );
$this->getDoctrine()->getManager()->flush();// Efetuar a edição do sistema operacional
$this->get('session')->getFlashBag()->add('success', 'Dados salvos com sucesso!');
return $this->redirect($this->generateUrl('cacic_sistemaoperacional_editar', array( 'idSo'=>$so->getIdSo() ) ) );
}
}
return $this->render( 'CacicCommonBundle:So:cadastrar.html.twig', array( 'form' => $form->createView() ) );
}
/**
*
* [AJAX] Exclusão de sistema operacional já cadastrado
* @param integer $idSo
*/
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' );
$So = $this->getDoctrine()->getRepository('CacicCommonBundle:So')->find( $request->get('id') );
if ( ! $So )
throw $this->createNotFoundException( ' sistema operacional não encontrado' );
$em = $this->getDoctrine()->getManager();
$em->remove( $So );
$em->flush();
$response = new Response( json_encode( array('status' => 'ok') ) );
$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
*
* Tela de importação de arquivo CSV com registros de Sistemas Operacionais
*/
public function importarcsvAction( Request $request )
{
$form = $this->createFormBuilder()
->add('arquivocsv', 'file', array('label' => 'Arquivo', 'attr' => array( 'accept' => '.csv' )))
->getForm();
if ( $request->isMethod('POST') )
{
$form->bindRequest( $request );
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 = 'SO_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 ) != 5 )
continue;
$so = new So();
// desabilita a geracao automatica do id
$metadata = $em->getClassMetaData(get_class($so));
$metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);
$so->setIdSo( (int) $v[0] );
$so->setTeDescSo( $v[1] );
$so->setSgSo( $v[2] );
$so->setTeSo( $v[3] );
$so->setInMswindows( $v[4] );
$em->persist( $so );
}
$em->flush(); // Persiste os dados dos Sistemas Operacionais
$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_so') );
}
return $this->render(
'CacicCommonBundle:So:importarcsv.html.twig',
array( 'form' => $form->createView() )
);
}
}