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
|
<?php
abstract class TDataBoundControl extends TWebControl
{
private $_initialized=false;
private $_dataSource=null;
private $_requiresBindToNull=false;
private $_requiresDataBinding=false;
private $_throwOnDataPropertyChange=false;
private $_prerendered=false;
/**
* @return Traversable data source object, defaults to null.
*/
public function getDataSource()
{
return $this->_dataSource;
}
/**
* @param Traversable|array|string data source object
*/
public function setDataSource($value)
{
if($value!==null)
$this->validateDataSource($value);
$this->_dataSource=$value;
$this->onDataPropertyChanged();
}
/**
* @return string ID path to the data source control. Defaults to empty.
*/
public function getDataSourceID()
{
return $this->getViewState('DataSourceID','');
}
/**
* @param string ID path to the data source control. The data source
* control must be locatable via {@link TControl::findControl} call.
*/
public function setDataSourceID($value)
{
$dsid=$this->getViewState('DataSourceID','');
if($dsid!=='' && $value==='')
$this->_requiresBindToNull=true;
$this->setViewState('DataSourceID',$value,'');
$this->onDataPropertyChanged();
}
/**
* This method is invoked when either {@link setDataSource} or {@link setDataSourceID} is changed.
*/
protected function onDataPropertyChanged()
{
if($this->_throwOnDataPropertyChanged)
throw new TInvalidOperationException('databoundcontrol_dataproperty_unchangeable');
if($this->getInitialized())
$this->setRequiresDataBinding(true);
}
/**
* @return boolean whether the databound control has been initialized.
*/
protected function getInitialized()
{
return $this->_initialized;
}
/**
* @param boolean a value indicating whether the databound control is initialized.
*/
protected function setInitialized($value)
{
$this->_initialized=TPropertyValue::ensureBoolean($value);
}
/**
* @return boolean if the databound control uses the data source control specified
* by {@link setDataSourceID}, or it uses the data source object specified
* by {@link setDataSource}.
*/
protected function getUsingDataSourceID()
{
return $this->getDataSourceID()!=='';
}
/**
* @return boolean whether a databind call is required (by the data bound control)
*/
protected function getRequiresDataBinding()
{
return $this->_requiresDataBinding;
}
/**
* Sets a value indicating whether a databind call is required by the data bound control.
* If true and the control has been prerendered while it uses the data source
* specified by {@link setDataSourceID}, a databind call will be called by this method.
* @param boolean whether a databind call is required.
*/
protected function setRequiresDataBinding($value)
{
$value=TPropertyValue::ensureBoolean($value);
if($value && $this->_prerendered && $this->getUsingDataSourceID())
{
$this->_requiresDataBinding=true;
$this->ensureDataBound();
}
else
$this->_requiresDataBinding=$value;
}
/**
* Performs databinding.
* This method overrides the parent implementation by calling
* {@link performSelect} which fetches data from data source and does
* the actual binding work.
* @param boolean whether to raise DataBind event. This parameter is ignored.
*/
public function dataBind($raiseDataBindingEvent=true)
{
$this->performSelect();
}
/**
* Ensures any pending {@link dataBind} is called.
* This method calls {@link dataBind} if the data source is specified
* by {@link setDataSourceID} or if {@link getRequiresDataBinding RequiresDataBinding}
* is true.
*/
protected function ensureDataBound()
{
try
{
$this->_throwOnDataPropertyChange=true;
if($this->_requiresDataBinding && ($this->getUsingDataSourceID() || $this->_requiresBindToNull))
{
$this->dataBind();
$this->_requiresBindToNull=false;
}
}
catch(Exception $e)
{
$this->_throwOnDataPropertyChange=false;
throw $e;
}
}
/**
* Raises <b>DataBound</b> event.
* This method should be invoked after a databind is performed.
* It is mainly used by framework and component developers.
*/
public function onDataBound($param)
{
$this->raiseEvent('DataBound',$this,$param);
}
/**
* Sets page's <b>PreLoad</b> event handler as {@link onPagePreLoad}.
* If viewstate is disabled and the current request is a postback,
* {@link setRequiresDataBinding RequiresDataBinding} will be set true.
* This method overrides the parent implementation.
* @param TEventParameter event parameter
*/
protected function onInit($param)
{
parent::onInit($param);
$page=$this->getPage();
$page->attachEventHandler('PreLoad',array($this,'onPagePreLoad'));
if(!$this->getEnableViewState(true) && $page->getIsPostBack())
$this->setRequiresDataBinding(true);
}
/**
* Sets {@link getInitialized} as true.
* This method is invoked when page raises <b>PreLoad</b> event.
* @param mixed event sender
* @param TEventParameter event parameter
*/
protected function onPagePreLoad($sender,$param)
{
$this->_initialized=true;
}
/**
* Ensures any pending databind is performed.
* This method overrides the parent implementation.
* @param TEventParameter event parameter
*/
protected function onPreRender($param)
{
$this->_prerendered=true;
$this->ensureDataBound();
parent::onPreRender($param);
}
/**
* Validates if the parameter is a valid data source.
* @return boolean if the parameter is a valid data source
*/
protected function validateDataSource($value)
{
if(!is_array($value) && !($value instanceof Traversable))
throw new TInvalidDataTypeException('databoundcontrol_datasource_invalid');
}
/**
* @return ???
*/
protected function performSelect()
{
if(!$this->getUsingDataSourceID())
$this->onDataBinding(null);
$view=$this->getDataSourceView();
$this->setRequiresDataBinding(false);
$this->setDataBound(true);
$data=$view->select($this->getSelectParameters());
if($this->getUsingDataSourceID())
$this->onDataBinding(null);
$this->performDataBinding($data);
$this->onDataBound(null);
}
protected function getDataSourceView()
{
$source=$this->getDataSourceByID();
return $source->getView($this->getDataMember());
}
protected function performDataBinding($data)
{
}
public function getDataMember()
{
return $this->getViewState('DataMember','');
}
public function setDataMember($value)
{
$this->setViewState('DataMember',$value,'');
}
public function getSelectParameters()
{
if(!$this->_parameters)
$this->_parameters=$this->createSelectParameters();
return $this->_parameters;
}
protected function createSelectParameters()
{
return new TDataSourceSelectParameters;
}
}
?>
|