AnoController.php
7.46 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
266
267
268
269
270
<?php
/**
* i-Educar - Sistema de gestão escolar
*
* Copyright (C) 2006 Prefeitura Municipal de Itajaí
* <ctima@itajai.sc.gov.br>
*
* Este programa é software livre; você pode redistribuí-lo e/ou modificá-lo
* sob os termos da Licença Pública Geral GNU conforme publicada pela Free
* Software Foundation; tanto a versão 2 da Licença, como (a seu critério)
* qualquer versão posterior.
*
* Este programa é distribuído na expectativa de que seja útil, porém, SEM
* NENHUMA GARANTIA; nem mesmo a garantia implícita de COMERCIABILIDADE OU
* ADEQUAÇÃO A UMA FINALIDADE ESPECÍFICA. Consulte a Licença Pública Geral
* do GNU para mais detalhes.
*
* Você deve ter recebido uma cópia da Licença Pública Geral do GNU junto
* com este programa; se não, escreva para a Free Software Foundation, Inc., no
* endereço 59 Temple Street, Suite 330, Boston, MA 02111-1307 USA.
*
* @author Eriksen Costa Paixão <eriksen.paixao_bs@cobra.com.br>
* @category i-Educar
* @license @@license@@
* @package ComponenteCurricular
* @subpackage Modules
* @since Arquivo disponível desde a versão 1.1.0
* @version $Id$
*/
require_once 'Core/Controller/Page/EditController.php';
require_once 'ComponenteCurricular/Model/Componente.php';
require_once 'ComponenteCurricular/Model/AnoEscolarDataMapper.php';
/**
* AnoController class.
*
* @author Eriksen Costa Paixão <eriksen.paixao_bs@cobra.com.br>
* @category i-Educar
* @license @@license@@
* @package ComponenteCurricular
* @subpackage Modules
* @since Classe disponível desde a versão 1.1.0
* @version @@package_version@@
*/
class AnoController extends Core_Controller_Page_EditController
{
protected $_dataMapper = 'ComponenteCurricular_Model_AnoEscolarDataMapper';
protected $_titulo = 'Configuração de ano escolar';
protected $_processoAp = 946;
protected $_formMap = array();
/**
* Array de instâncias ComponenteCurricular_Model_AnoEscolar.
* @var array
*/
protected $_entries = array();
/**
* Setter.
* @param array $entries
* @return Core_Controller_Page Provê interface fluída
*/
public function setEntries(array $entries = array())
{
foreach ($entries as $entry) {
$this->_entries[$entry->anoEscolar] = $entry;
}
return $this;
}
/**
* Getter.
* @return array
*/
public function getEntries()
{
return $this->_entries;
}
/**
* Getter.
* @param int $id
* @return ComponenteCurricular_Model_AnoEscolar
*/
public function getEntry($id)
{
return $this->_entries[$id];
}
/**
* Verifica se uma instância ComponenteCurricular_Model_AnoEscolar identificada
* por $id existe.
* @param int $id
* @return bool
*/
public function hasEntry($id)
{
if (isset($this->_entries[$id])) {
return TRUE;
}
return FALSE;
}
/**
* Retorna um array associativo de séries com código de curso como chave.
* @return array
*/
protected function _getSeriesAgrupadasPorCurso()
{
$series = App_Model_IedFinder::getSeries($this->getEntity()->instituicao);
$cursos = array();
foreach ($series as $id => $nome) {
$serie = App_Model_IedFinder::getSerie($id);
$codCurso = $serie['ref_cod_curso'];
$cursos[$codCurso][$id] = $nome;
}
return $cursos;
}
/**
* Retorna o nome de um curso.
* @param int $id
* @return string
*/
protected function _getCursoNome($id)
{
return App_Model_IedFinder::getCurso($id);
}
/**
* @see Core_Controller_Page_EditController#_preConstruct()
*/
public function _preConstruct()
{
// Popula array de disciplinas selecionadas
$this->setOptions(array('edit_success_params' => array('id' => $this->getRequest()->cid)));
$this->setEntries($this->getDataMapper()->findAll(array(),
array('componenteCurricular' => $this->getRequest()->cid)));
// Configura ação cancelar
$this->setOptions(array('url_cancelar' => array(
'path' => 'view', 'options' => array(
'query' => array('id' => $this->getRequest()->cid)
)
)));
}
/**
* @see Core_Controller_Page_EditController#_initNovo()
*/
protected function _initNovo()
{
if (!isset($this->getRequest()->cid)) {
$this->setEntity($this->getDataMapper()->createNewEntityInstance());
return TRUE;
}
return FALSE;
}
/**
* @see Core_Controller_Page_EditController#_initEditar()
*/
protected function _initEditar()
{
try {
$this->setEntity($this->getDataMapper()->createNewEntityInstance(array('componenteCurricular' => $this->getRequest()->cid)));
} catch(Exception $e) {
$this->mensagem = $e;
return FALSE;
}
return TRUE;
}
protected function _preRender(){
parent::_preRender();
Portabilis_View_Helper_Application::loadStylesheet($this, 'intranet/styles/localizacaoSistema.css');
$localizacao = new LocalizacaoSistema();
$localizacao->entradaCaminhos( array(
$_SERVER['SERVER_NAME']."/intranet" => "Início",
"educar_index.php" => "i-Educar - Escola",
"" => "Editando anos escolares"
));
$this->enviaLocalizacao($localizacao->montar());
}
/**
* @see clsCadastro#Gerar()
*/
public function Gerar()
{
$this->campoOculto('cid', $this->getEntity()->get('componenteCurricular'));
// Cursos
$cursos = $this->_getSeriesAgrupadasPorCurso();
// Cria a matriz de checkboxes
foreach ($cursos as $key => $curso) {
$this->campoRotulo($key, $this->_getCursoNome($key), '', FALSE, '', '');
foreach ($curso as $c => $serie) {
$this->campoCheck('ano_escolar['.$c.']', '', $this->hasEntry($c), $serie, FALSE);
$valor = $this->hasEntry($c) ? $this->getEntry($c)->cargaHoraria : NULL;
$this->campoTexto('carga_horaria['.$c.']', 'Carga horária',
$valor, 5, 5, FALSE, FALSE,
FALSE);
}
$this->campoQuebra();
}
}
/**
* @see Core_Controller_Page_EditController#_save()
*/
protected function _save()
{
$data = $insert = $delete = $intersect = array();
// O id de componente_curricular será igual ao id da request
if ($cid = $this->getRequest()->cid) {
$data['componenteCurricular'] = $cid;
}
// Cria um array de Entity geradas pela requisição
foreach ($this->getRequest()->ano_escolar as $key => $val) {
$data['anoEscolar'] = $key;
$data['cargaHoraria'] = $this->getRequest()->carga_horaria[$key];
$insert[$key] = $this->getDataMapper()->createNewEntityInstance($data);
}
// Cria um array de chaves da Entity AnoEscolar para remover
$entries = $this->getEntries();
$delete = array_diff(array_keys($entries), array_keys($insert));
// Cria um array de chaves da Entity AnoEscolar para evitar inserir novamente
$intersect = array_intersect(array_keys($entries), array_keys($insert));
// Registros a apagar
foreach ($delete as $id)
{
$this->getDataMapper()->delete($entries[$id]);
}
// Registros a inserir
foreach ($insert as $key => $entity) {
// Se o registro já existe, passa para o próximo
if (FALSE !== array_search($key, $intersect)) {
$entity->markOld();
}
try {
$this->getDataMapper()->save($entity);
}
catch (Exception $e) {
$this->mensagem = 'Erro no preenchimento do formulário.';
return FALSE;
}
}
return TRUE;
}
}