SoftwareController.php
6.89 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
<?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\Software;
use Cacic\CommonBundle\Form\Type\SoftwareType;
class SoftwareController extends Controller
{
/**
*
* Tela de listagem de Softwares cadastrados
* @param int $page
*/
public function indexAction( $page )
{
return $this->render(
'CacicCommonBundle:Software:index.html.twig',
array( 'softwares' => $this->getDoctrine()->getRepository( 'CacicCommonBundle:Software' )
->paginar( $this->get( 'knp_paginator' ), $page )
)
);
}
/**
*
* Tela de Cadastro de novo Software
* @param Symfony\Component\HttpFoundation\Request $request
*/
public function cadastrarAction(Request $request)
{
$Software = new Software();
$form = $this->createForm( new SoftwareType(), $Software );
if ( $request->isMethod('POST') )
{
$form->bind( $request );
if ( $form->isValid() )
{
$this->getDoctrine()->getManager()->persist( $Software );
$this->getDoctrine()->getManager()->flush(); //Persiste os dados do Software
$this->get('session')->getFlashBag()->add('success', 'Dados salvos com sucesso!');
return $this->redirect( $this->generateUrl( 'cacic_software_index') );
}
}
return $this->render( 'CacicCommonBundle:Software:cadastrar.html.twig', array( 'form' => $form->createView() ) );
}
/**
* Tela de editar dados do Software
* @param int $idSoftware
* @param Symfony\Component\HttpFoundation\Request $request
*/
public function editarAction( $idSoftware, Request $request )
{
$Software = $this->getDoctrine()->getRepository('CacicCommonBundle:Software')->find( $idSoftware );
if ( ! $Software )
throw $this->createNotFoundException( 'Software não encontrado' );
$form = $this->createForm( new SoftwareType(), $Software );
if ( $request->isMethod('POST') )
{
$form->bind( $request );
if ( $form->isValid() )
{
$this->getDoctrine()->getManager()->persist( $Software );
$this->getDoctrine()->getManager()->flush();// Efetuar a edição do Software
$this->get('session')->getFlashBag()->add('success', 'Dados salvos com sucesso!');
return $this->redirect($this->generateUrl('cacic_software_editar', array( 'idSoftware'=>$Software->getIdSoftware() ) ) );
}
}
return $this->render( 'CacicCommonBundle:Software:cadastrar.html.twig', array( 'form' => $form->createView() ) );
}
/**
*
* [AJAX] Exclusão de Software já cadastrado
* @param Symfony\Component\HttpFoundation\Request $request
*/
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' );
$tipoSoftware = $this->getDoctrine()->getRepository('CacicCommonBundle:Software')->find( $request->get('id') );
if ( ! $tipoSoftware )
throw $this->createNotFoundException( 'Software não encontrado' );
$em = $this->getDoctrine()->getManager();
$em->remove( $tipoSoftware );
$em->flush();
$response = new Response( json_encode( array('status' => 'ok') ) );
$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
*
* Tela de classificação EM LOTE de Softwares
* @param Symfony\Component\HttpFoundation\Request $request
*/
public function naoClassificadosAction($page, Request $request )
{
if ( $request->isMethod('POST') )
{
if ( count( $request->get('software') ) )
{
foreach ( $request->get('software') as $idSoftware => $idTipo )
{
$software = $this->getDoctrine()->getRepository('CacicCommonBundle:Software')->find( (int) $idSoftware );
$tipoSoftware = $this->getDoctrine()->getRepository('CacicCommonBundle:TipoSoftware')->find( (int) $idTipo );
if ( ! $software || ! $tipoSoftware )
{ // Impede injection verificando a existência tanto do Software quanto do Tipo de Software
$this->get('session')->getFlashBag()->add('error', 'Dados inválidos');
break;
}
$software->setIdTipoSoftware( $tipoSoftware ); // Associa o Tipo de Software ao Software
$this->getDoctrine()->getManager()->persist( $software );
}
$this->getDoctrine()->getManager()->flush(); // Efetiva a edição dos dados na base
$this->get('session')->getFlashBag()->add('success', 'Dados salvos com sucesso!');
}
else
$this->get('session')->getFlashBag()->add('error', 'Nenhum software informado!');
return $this->redirect( $this->generateUrl( 'cacic_software_naoclassificados') );
}
return $this->render(
'CacicCommonBundle:Software:naoclassificados.html.twig',
array(
'softwares' => $this->getDoctrine()->getRepository( 'CacicCommonBundle:Software' )->listarNaoClassificados( $this->get( 'knp_paginator' ), $page ),
'tipos' => $this->getDoctrine()->getRepository( 'CacicCommonBundle:TipoSoftware' )->findAll()
)
);
}
/**
*
* Tela de exclusão de Softwares não associados a nenhuma máquina
* @param Symfony\Component\HttpFoundation\Request $request
*/
public function naoUsadosAction( Request $request )
{
if ( $request->isMethod('POST') )
{
if ( count( $request->get('software') ) )
{
foreach ( $request->get('software') as $idSoftware )
{
$software = $this->getDoctrine()->getRepository('CacicCommonBundle:Software')->find( (int) $idSoftware );
if ( ! $software )
{ // Impede injection verificando a existência do Software
$this->get('session')->getFlashBag()->add('error', 'Dados inválidos');
break;
}
$this->getDoctrine()->getManager()->remove( $software );
}
$this->getDoctrine()->getManager()->flush(); // Efetiva a exclusão dos dados na base
$this->get('session')->getFlashBag()->add('success', 'Softwares excluídos com sucesso!');
}
else
$this->get('session')->getFlashBag()->add('error', 'Nenhum software informado!');
return $this->redirect( $this->generateUrl( 'cacic_software_naousados') );
}
return $this->render(
'CacicCommonBundle:Software:naousados.html.twig',
array(
'softwares' => $this->getDoctrine()->getRepository( 'CacicCommonBundle:Software' )->listarNaoUsados()
)
);
}
}