EditController.php
10.1 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?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 TabelaArredondamento
* @subpackage Modules
* @since Arquivo disponível desde a versão 1.1.0
* @version $Id$
*/
require_once 'Core/Controller/Page/EditController.php';
require_once 'TabelaArredondamento/Model/TabelaDataMapper.php';
require_once 'TabelaArredondamento/Model/TabelaValor.php';
/**
* EditController class.
*
* @author Eriksen Costa Paixão <eriksen.paixao_bs@cobra.com.br>
* @category i-Educar
* @license @@license@@
* @package TabelaArredondamento
* @subpackage Modules
* @since Classe disponível desde a versão 1.1.0
* @version @@package_version@@
*/
class EditController extends Core_Controller_Page_EditController
{
protected $_dataMapper = 'TabelaArredondamento_Model_TabelaDataMapper';
protected $_titulo = 'Cadastro de tabela de arredondamento de notas';
protected $_processoAp = 949;
protected $_nivelAcessoOption = App_Model_NivelAcesso::INSTITUCIONAL;
protected $_saveOption = TRUE;
protected $_deleteOption = FALSE;
protected $_formMap = array(
'instituicao' => array(
'label' => 'Instituição',
'help' => ''
),
'nome' => array(
'label' => 'Nome',
'help' => 'Um nome para a tabela. Exemplo: "<em>Tabela genérica de conceitos</em>".'
),
'tipoNota' => array(
'label' => 'Tipo de nota',
'help' => ''
),
'valor_nome' => array(
'label' => 'Rótulo da nota:',
'help' => 'Exemplos: A, B, C (conceituais)<br />
<b>6,5<b>, <b>7,5<b> (numéricas)'
),
'valor_descricao' => array(
'label' => '<span style="padding-left: 10px"></span>Descrição:',
'help' => 'Exemplos: Bom, Regular, Em Processo.'
),
'valor_valor_minimo' => array(
'label' => '<span style="padding-left: 10px"></span>Valor mínimo:',
'help' => 'O valor numérico mínimo da nota.'
),
'valor_valor_maximo' => array(
'label' => '<span style="padding-left: 10px"></span>Valor máximo:',
'help' => 'O valor numérico máximo da nota.'
)
);
/**
* Array de instâncias TabelaArredondamento_Model_TabelaValor.
* @var array
*/
protected $_valores = array();
/**
* Setter.
* @param array $valores
* @return Core_Controller_Page_Abstract Provê interface fluída
*/
protected function _setValores(array $valores = array())
{
foreach ($valores as $key => $valor) {
$this->_valores[$valor->id] = $valor;
}
return $this;
}
/**
* Getter.
* @return array
*/
protected function _getValores()
{
return $this->_valores;
}
/**
* Getter
* @param int $id
* @return TabelaArredondamento_Model_TabelaValor
*/
protected function _getValor($id)
{
return isset($this->_valores[$id]) ? $this->_valores[$id] : NULL;
}
/**
* @see Core_Controller_Page_EditController#_preConstruct()
* @todo Interação com a API está errada. Isso já é feito em _initNovo()
* na superclasse. VER.
*/
protected function _preConstruct()
{
if (isset($this->getRequest()->id) && 0 < $this->getRequest()->id) {
$this->setEntity($this->getDataMapper()->find($this->getRequest()->id));
$this->_setValores($this->getDataMapper()->findTabelaValor($this->getEntity()));
}
}
function _preRender(){
parent::_preRender();
Portabilis_View_Helper_Application::loadStylesheet($this, 'intranet/styles/localizacaoSistema.css');
$nomeMenu = $this->getRequest()->id == null ? "Cadastrar" : "Editar";
$localizacao = new LocalizacaoSistema();
$localizacao->entradaCaminhos( array(
$_SERVER['SERVER_NAME']."/intranet" => "Início",
"educar_index.php" => "Módulo Escola",
"" => "$nomeMenu tabela de arredondamento"
));
$this->enviaLocalizacao($localizacao->montar());
}
/**
* @see clsCadastro#Gerar()
*/
public function Gerar()
{
$this->campoOculto('id', $this->getEntity()->id);
// Instituição
$instituicoes = App_Model_IedFinder::getInstituicoes();
$this->campoLista('instituicao', $this->_getLabel('instituicao'),
$instituicoes, $this->getEntity()->instituicao);
// Nome
$this->campoTexto('nome', $this->_getLabel('nome'), $this->getEntity()->nome,
40, 50, TRUE, FALSE, FALSE, $this->_getHelp('nome'));
// Tipo de nota
$notaTipoValor = RegraAvaliacao_Model_Nota_TipoValor::getInstance();
$notaTipos = $notaTipoValor->getEnums();
unset($notaTipos[RegraAvaliacao_Model_Nota_TipoValor::NENHUM]);
if ($this->getEntity()->id!='')
$this->campoTexto('tipNota',$this->_getLabel('tipoNota'),$notaTipos[$this->getEntity()->get('tipoNota')],
40,40,false,false,false,'','','','',true);
else
$this->campoRadio('tipoNota', $this->_getLabel('tipoNota'), $notaTipos,
$this->getEntity()->get('tipoNota'), '', $this->_getHelp('tipoNota'));
// Parte condicional
if (!$this->getEntity()->isNew()) {
// Quebra
$this->campoQuebra();
// Ajuda
$help = 'Caso seja necessário adicionar mais notas, '
. 'salve o formulário. Automaticamente 3 campos '
. 'novos ficarão disponíveis.<br /><br />';
$this->campoRotulo('__help1', '<strong>Notas para arredondamento</strong><br />', $help, FALSE, '', '');
// Cria campos para a postagem de notas
$valores = $this->getDataMapper()->findTabelaValor($this->getEntity());
for ($i = 0, $loop = count($valores); $i < ($loop == 0 ? 5 : $loop + 3); $i++) {
$valorNota = $valores[$i];
$valor_label = sprintf("valor[label][%d]", $i);
$valor_id = sprintf("valor[id][%d]", $i);
$valor_nome = sprintf("valor[nome][%d]", $i);
$valor_descricao = sprintf("valor[descricao][%d]", $i);
$valor_valor_minimo = sprintf("valor[valor_minimo][%d]", $i);
$valor_valor_maximo = sprintf("valor[valor_maximo][%d]", $i);
$this->campoRotulo($valor_label, 'Arredondamento ' . ($i + 1),
$this->_getLabel(''), TRUE);
// Id
$this->campoOculto($valor_id, $valorNota->id);
// Nome
$this->campoTexto($valor_nome, $this->_getLabel('valor_nome'),
$valorNota->nome, 5, 5, FALSE, FALSE, TRUE, $this->_getHelp('valor_nome'));
// Descrição (se conceitual)
if (RegraAvaliacao_Model_Nota_TipoValor::CONCEITUAL == $this->getEntity()->get('tipoNota')) {
$this->campoTexto($valor_descricao, $this->_getLabel('valor_descricao'),
$valorNota->descricao, 15, 25, FALSE, FALSE, TRUE,
$this->_getHelp('valor_descricao'));
}
// Valor mínimo
$this->campoTexto($valor_valor_minimo, $this->_getLabel('valor_valor_minimo'),
$valorNota->valorMinimo, 6, 6, FALSE, FALSE, TRUE,
$this->_getHelp('valor_valor_minimo'));
// Valor máximo
$this->campoTexto($valor_valor_maximo, $this->_getLabel('valor_valor_maximo'),
$valorNota->valorMaximo, 6, 6, FALSE, FALSE, FALSE,
$this->_getHelp('valor_valor_maximo'));
}
// Quebra
$this->campoQuebra();
}
}
protected function _save()
{
// Verifica pela existência do field identity
if (isset($this->getRequest()->id) && 0 < $this->getRequest()->id) {
$this->setEntity($this->getDataMapper()->find($this->getRequest()->id));
$entity = $this->getEntity();
}
// Se existir, chama _save() do parent
if (!isset($entity)) {
return parent::_save();
}
// Processa os dados da requisição, apenas os valores para a tabela de valores.
$valores = $this->getRequest()->valor;
// A contagem usa um dos índices do formulário, senão ia contar sempre 4.
$loop = count($valores['id']);
// Array de objetos a persistir
$insert = array();
// Cria um array de objetos a persistir
for ($i = 0; $i < $loop; $i++) {
$id = $valores['id'][$i];
// Não atribui a instância de $entity senão não teria sucesso em verificar
// se a instância é isNull().
$data = array(
'id' => $id,
'nome' => $valores['nome'][$i],
'descricao' => $valores['descricao'][$i],
'valorMinimo' => $valores['valor_minimo'][$i],
'valorMaximo' => $valores['valor_maximo'][$i]
);
// Se a instância já existir, use-a para garantir UPDATE
if (NULL != ($instance = $this->_getValor($id))) {
$insert[$id] = $instance->setOptions($data);
}
else {
$instance = new TabelaArredondamento_Model_TabelaValor($data);
if (!$instance->isNull()) {
$insert['new_' . $i] = $instance;
}
}
}
// Persiste
foreach ($insert as $tabelaValor) {
// Atribui uma tabela de arredondamento a instância de tabela valor
$tabelaValor->tabelaArredondamento = $entity;
// Se não tiver nome, passa para o próximo
if ($tabelaValor->isValid()) {
$this->getDataMapper()->getTabelaValorDataMapper()->save($tabelaValor);
}
else {
$this->mensagem = 'Erro no formulário';
return FALSE;
}
}
return TRUE;
}
}