forked from koala-framework/koala-framework
/
Form.php
246 lines (209 loc) · 8.34 KB
/
Form.php
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
<?php
abstract class Kwf_Controller_Action_Auto_Form extends Kwf_Controller_Action_Auto_Abstract
{
/**
* @var Kwf_Form
*/
protected $_form;
protected $_fields = array(); //deprecated
protected $_buttons = array();
protected $_formName;
public function indexAction()
{
if ($this->_form->getProperties()) {
$this->view->assign($this->_form->getProperties());
}
$this->view->controllerUrl = $this->getRequest()->getBaseUrl().'/'.ltrim($this->getRequest()->getPathInfo(), '/');
$this->view->xtype = 'kwf.autoform';
}
protected function _initFields()
{
}
public function preDispatch()
{
parent::preDispatch();
$t = microtime(true);
if (!isset($this->_form)) {
if (isset($this->_formName)) {
$this->_form = new $this->_formName();
} else {
$this->_form = new Kwf_Form();
}
}
foreach ($this->_fields as $k=>$field) {
if (!isset($field['type'])) throw new Kwf_Exception("no type for field no $k specified");
$cls = 'Kwf_Form_Field_'.$field['type'];
if (!class_exists($cls)) throw new Kwf_Exception("Invalid type: Form-Field-Class $cls does not exist.");
$fieldObject = new $cls();
unset($field['type']);
foreach ($field as $propName => $propValue) {
$fieldObject->setProperty($propName, $propValue);
}
$this->_form->fields[] = $fieldObject;
}
if (!$this->_form->getModel()) {
if (isset($this->_table)) {
$this->_form->setTable($this->_table);
} else if (isset($this->_tableName)) {
$this->_form->setTable(new $this->_tableName);
} else if (isset($this->_modelName)) {
$this->_form->setModel(Kwf_Model_Abstract::getInstance($this->_modelName));
} else if (isset($this->_model)) {
if (is_string($this->_model)) {
$this->_form->setModel(Kwf_Model_Abstract::getInstance($this->_model));
} else {
$this->_form->setModel($this->_model);
}
}
}
$this->_initFields();
$this->_form->initFields();
$this->_form->trlStaticExecute();
if (!$this->_form->fields->first() instanceof Kwf_Form_Container_Tabs) {
$this->_form->setBodyStyle('padding: 10px;');
}
if (!$this->_form->getId()) {
if (is_array($this->_form->getPrimaryKey())) {
foreach ($this->_form->getPrimaryKey() as $key) {
$id[$key] = $this->_getParam($key);
}
$this->_form->setId($id);
} else {
$this->_form->setId($this->_getParam($this->_form->getPrimaryKey()));
}
}
Kwf_Benchmark::subCheckpoint('init form', microtime(true)-$t);
}
public function jsonLoadAction()
{
if ($this->_form->getId()) { //nur laden wennn einen id über get daherkommt
$row = $this->_form->getRow();
if (!$this->_hasPermissions($row, 'load')) {
throw new Kwf_Exception('You don\'t have the permission for this entry.');
}
$this->_beforeLoad($row);
$this->view->data = $this->_form->load(null);
}
if ($this->getRequest()->getParam('meta')) {
$this->_appendMetaData();
}
}
protected function _appendMetaData()
{
$this->view->meta = array();
$this->view->meta['helpText'] = $this->getHelpText();
$this->view->meta['form'] = $this->_form->getMetaData();
$this->view->meta['buttons'] = (object)$this->_buttons; //in objekt casten damit json kein [] sondern {} ausgibt
$this->view->meta['permissions'] = (object)$this->_permissions; //in objekt casten damit json kein [] sondern {} ausgibt
}
public function jsonSaveAction()
{
ignore_user_abort(true);
$db = Zend_Registry::get('db');
if ($db) $db->beginTransaction();
// zuvor war statt diesem kommentar das $row = $this->_form->getRow();
// drin und wurde bei processInput und validate übergeben, aber die form
// weiß selbst das model, deshalb passt NULL
// Runtergeschoben wurde das $this->_form->getRow() weil bei der Kwf_User_Form
// die row im processInput gefaket wird, da hier ->createUserRow() aufgerufen
// wird anstatt ->createRow() und diese dann im _form->getRow() zurück kommt
$postData = $this->_form->processInput(null, $this->getRequest()->getParams());
$this->_beforeValidate($postData);
$invalid = $this->_form->validate(null, $postData);
if ($invalid) {
$invalid = Kwf_Form::formatValidationErrors($invalid);
throw new Kwf_ClientException(implode("<br />", $invalid));
}
$data = $this->_form->prepareSave(null, $postData);
$row = $this->_form->getRow();
$insert = false;
$primaryKey = $this->_form->getPrimaryKey();
$skip = false;
if ($row && $primaryKey) {
if (is_array($primaryKey)) $primaryKey = $primaryKey[1];
if (!$row->$primaryKey){
$insert = true;
}
if ($insert) {
$sessionFormId = new Kwf_Session_Namespace('avoid_reinsert_id');
if ($this->_getParam('avoid_reinsert_id') &&
isset($sessionFormId->avoid[$this->_getParam('avoid_reinsert_id')])
) {
$skip = true;
}
if (!isset($this->_permissions['add']) || !$this->_permissions['add']) {
throw new Kwf_Exception('Add is not allowed.');
}
if (!$skip) $this->_beforeInsert($row);
} else {
if (!isset($this->_permissions['save']) || !$this->_permissions['save']) {
throw new Kwf_Exception('Save is not allowed.');
}
}
if (!$skip) $this->_beforeSave($row);
}
if (!$skip) {
//erst hier unten Berechtigungen überprüfen, damit beforeInsert usw vorher noch ausgeführt
//wird und eventuelle Daten gesetzt werden
if (!$this->_hasPermissions($row, 'save')) {
throw new Kwf_Exception("Save is not allowed for this row.");
}
$data = $this->_form->save(null, $postData);
$this->_form->afterSave(null, $postData);
if ($row) {
if ($insert) {
$this->_afterInsert($row);
}
$this->_afterSave($row);
}
if ($db) $db->commit();
$this->view->data = $data;
$sessionFormId = new Kwf_Session_Namespace('avoid_reinsert_id');
if (!isset($sessionFormId->avoid)) {
$avoid = array();
} else {
$avoid = $sessionFormId->avoid;
}
$avoid[$this->_getParam('avoid_reinsert_id')] = $data;
$sessionFormId->avoid = $avoid;
} else {
$this->view->data = $sessionFormId->avoid[$this->_getParam('avoid_reinsert_id')];
}
}
public function jsonDeleteAction()
{
if (!isset($this->_permissions['delete']) || !$this->_permissions['delete']) {
throw new Kwf_Exception('Delete is not allowed.');
}
$row = $this->_form->getRow();
if (!$this->_hasPermissions($row, 'delete')) {
throw new Kwf_Exception("Delete is not allowed for this row.");
}
$db = Zend_Registry::get('db');
if ($db) $db->beginTransaction();
$this->_form->delete(null);
if ($db) $db->commit();
}
protected function _beforeSave(Kwf_Model_Row_Interface $row)
{
}
protected function _afterSave(Kwf_Model_Row_Interface $row)
{
}
protected function _beforeInsert(Kwf_Model_Row_Interface $row)
{
}
protected function _afterInsert(Kwf_Model_Row_Interface $row)
{
}
protected function _beforeLoad(Kwf_Model_Row_Interface $row)
{
}
protected function _beforeValidate(array $postData)
{
}
protected function _hasPermissions($row, $action)
{
return true;
}
}