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
|
<?php
Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldBase');
Prado::using('System.Data.ActiveRecord.Scaffold.InputBuilder.TScaffoldInputBase');
class TScaffoldEditView extends TScaffoldBase
{
private static $_builders=array();
private $_editRenderer;
public function onLoad($param)
{
if($this->getVisible())
$this->initializeEditForm();
}
public function getEditRenderer()
{
return $this->getViewState('EditRenderer', '');
}
public function setEditRenderer($value)
{
$this->setViewState('EditRenderer', $value, '');
}
public function setRecordPk($value)
{
$this->clearRecordObject();
$val = TPropertyValue::ensureArray($value);
$this->setViewState('PK', count($val) > 0 ? $val : null);
}
public function getRecordPk()
{
return $this->getViewState('PK');
}
protected function getCurrentRecord()
{
return $this->getRecordObject($this->getRecordPk());
}
public function initializeEditForm()
{
$record = $this->getCurrentRecord();
$classPath = $this->getEditRenderer();
if($classPath === '')
{
$columns = $this->getTableMetaData()->getColumns();
$this->getInputRepeater()->setDataSource($columns);
$this->getInputRepeater()->dataBind();
}
else
{
if($this->_editRenderer===null)
$this->createEditRenderer($record, $classPath);
else
$this->_editRenderer->setData($record);
}
}
protected function createEditRenderer($record, $classPath)
{
$this->_editRenderer = Prado::createComponent($classPath);
if($this->_editRenderer instanceof IScaffoldEditRenderer)
{
$index = $this->getControls()->remove($this->getInputRepeater());
$this->getControls()->insertAt($index,$this->_editRenderer);
$this->_editRenderer->setData($record);
}
else
{
throw new TConfigurationException(
'scaffold_invalid_edit_renderer', $this->getID(), get_class($record));
}
}
protected function repeaterItemCreated($sender, $param)
{
$type = $param->getItem()->getItemType();
if($type==TListItemType::Item || $type==TListItemType::AlternatingItem)
{
$item = $param->getItem();
$column = $item->getDataItem();
if($column===null)
return;
$record = $this->getCurrentRecord();
$builder = $this->getScaffoldInputBuilder($record);
$builder->createScaffoldInput($this, $item, $column, $record);
}
}
public function bubbleEvent($sender, $param)
{
switch(strtolower($param->getCommandName()))
{
case 'save':
if($this->getPage()->getIsValid())
return $this->doSave() === true ? false : true;
return true;
case 'clear':
$this->setRecordPk(null);
$this->initializeEditForm();
return false;
default:
return false;
}
}
protected function doSave()
{
$record = $this->getCurrentRecord();
if($this->_editRenderer===null)
{
$table = $this->getTableMetaData();
$builder = $this->getScaffoldInputBuilder($record);
foreach($this->getInputRepeater()->getItems() as $item)
{
$column = $table->getColumn($item->getCustomData());
$builder->loadScaffoldInput($this, $item, $column, $record);
}
}
else
{
$this->_editRenderer->updateRecord($record);
}
$record->save();
return true;
}
protected function getInputRepeater()
{
$this->ensureChildControls();
return $this->getRegisteredObject('_repeater');
}
public function getSaveButton()
{
$this->ensureChildControls();
return $this->getRegisteredObject('_save');
}
public function getClearButton()
{
$this->ensureChildControls();
return $this->getRegisteredObject('_clear');
}
public function getCancelButton()
{
$this->ensureChildControls();
return $this->getRegisteredObject('_cancel');
}
protected function getScaffoldInputBuilder($record)
{
$class = get_class($record);
if(!isset(self::$_builders[$class]))
self::$_builders[$class] = TScaffoldInputBase::createInputBuilder($record);
return self::$_builders[$class];
}
public function getValidationGroup()
{
return 'group_'.$this->getUniqueID();
}
}
interface IScaffoldEditRenderer extends IDataRenderer
{
public function updateRecord($record);
}
?>
|