EditController.php
6.27 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
<?php
#error_reporting(E_ALL);
#ini_set("display_errors", 1);
/**
* 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 Lucas D'Avila <lucasdavila@portabilis.com.br>
* @category i-Educar
* @license @@license@@
* @package Portabilis
* @subpackage lib
* @since Arquivo disponível desde a versão ?
* @version $Id$
*/
require_once 'Core/Controller/Page/EditController.php';
require_once 'lib/Portabilis/Messenger.php';
require_once 'lib/Portabilis/Validator.php';
require_once 'lib/Portabilis/Array/Utils.php';
require_once 'lib/Portabilis/Utils/Database.php';
require_once 'lib/Portabilis/DataMapper/Utils.php';
require_once 'lib/Portabilis/View/Helper/Application.php';
// Resource controller
class Portabilis_Controller_Page_EditController extends Core_Controller_Page_EditController
{
protected $_dataMapper = null;
# vars that must be overwritten in subclasses
# protected $_processoAp = 0;
# protected $_nivelAcessoOption = App_Model_NivelAcesso::SOMENTE_ESCOLA;
# vars that can be overwritten
# protected $_dataMapper = 'Avaliacao_Model_NotaComponenteDataMapper';
# protected $_saveOption = FALSE;
# protected $_deleteOption = FALSE;
# protected $_titulo = 'Cadastro de aluno';
protected $_nivelAcessoInsuficiente = "/module/Error/unauthorized";
protected $_titulo = '';
protected $backwardCompatibility = false;
public function __construct(){
parent::__construct();
$this->loadAssets();
}
// methods that can be overwritten
protected function canSave()
{
return true;
}
// methods that must be overwritten
function Gerar()
{
throw new Exception("The method 'Gerar' must be overwritten!");
}
protected function save()
{
throw new Exception("The method 'save' must be overwritten!");
}
// methods that cannot be overwritten
protected function _save()
{
$result = false;
// try set or load entity before validation or save
if (! $this->_initNovo())
$this->_initEditar();
if (! $this->messenger()->hasMsgWithType('error') && $this->canSave()) {
try {
$result = $this->save();
if (is_null($result))
$result = ! $this->messenger()->hasMsgWithType('error');
elseif(! is_bool($result))
throw new Exception("Invalid value returned from '_save' method: '$result', please return null, true or false!");
}
catch (Exception $e) {
$this->messenger()->append('Erro ao gravar alterações, por favor, tente novamente.', 'error');
error_log("Erro ao gravar alteracoes: " . $e->getMessage());
$result = false;
}
$result = $result && ! $this->messenger()->hasMsgWithType('error');
if ($result)
$this->messenger()->append('Alterações gravadas com sucesso.', 'success', false, 'success');
}
return $result;
}
protected function flashMessage()
{
if (! $this->hasErrors())
return $this->messenger()->toHtml();
return '';
}
// helpers
protected function validator() {
if (! isset($this->_validator))
$this->_validator = new Portabilis_Validator();
return $this->_validator;
}
protected function messenger() {
if (! isset($this->_messenger))
$this->_messenger = new Portabilis_Messenger();
return $this->_messenger;
}
protected function mailer() {
if (! isset($this->_mailer))
$this->_mailer = new Portabilis_Mailer();
return $this->_mailer;
}
protected function loadResourceAssets($dispatcher){
$rootPath = $_SERVER['DOCUMENT_ROOT'];
$controllerName = ucwords($dispatcher->getControllerName());
$actionName = ucwords($dispatcher->getActionName());
$style = "/modules/$controllerName/Assets/Stylesheets/$actionName.css";
$script = "/modules/$controllerName/Assets/Javascripts/$actionName.js";
if (file_exists($rootPath . $style))
Portabilis_View_Helper_Application::loadStylesheet($this, $style);
if (file_exists($rootPath . $script))
Portabilis_View_Helper_Application::loadJavascript($this, $script);
}
protected function loadAssets(){
Portabilis_View_Helper_Application::loadJQueryLib($this);
Portabilis_View_Helper_Application::loadJQueryFormLib($this);
$styles = array('/modules/Portabilis/Assets/Stylesheets/Frontend.css',
'/modules/Portabilis/Assets/Stylesheets/Frontend/Resource.css',
'styles/localizacaoSistema.css');
Portabilis_View_Helper_Application::loadStylesheet($this, $styles);
$scripts = array('/modules/Portabilis/Assets/Javascripts/ClientApi.js',
'/modules/Portabilis/Assets/Javascripts/Validator.js',
'/modules/Portabilis/Assets/Javascripts/Utils.js');
if (! $this->backwardCompatibility)
$scripts[] = '/modules/Portabilis/Assets/Javascripts/Frontend/Resource.js';
Portabilis_View_Helper_Application::loadJavascript($this, $scripts);
}
// wrappers for Portabilis_*Utils*
protected static function mergeOptions($options, $defaultOptions) {
return Portabilis_Array_Utils::merge($options, $defaultOptions);
}
protected function fetchPreparedQuery($sql, $options = array()) {
return Portabilis_Utils_Database::fetchPreparedQuery($sql, $options);
}
protected function getDataMapperFor($packageName, $modelName){
return Portabilis_DataMapper_Utils::getDataMapperFor($packageName, $modelName);
}
}