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
306
307
308
309
310
311
312
313
314
|
<?php
/**
* TScaffoldListView class file.
*
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
* @link http://www.pradosoft.com/
* @copyright Copyright © 2005-2014 PradoSoft
* @license http://www.pradosoft.com/license/
* @package Prado\Data\ActiveRecord\Scaffold
*/
namespace Prado\Data\ActiveRecord\Scaffold;
/**
* Load the scaffold base class.
*/
use Prado\Data\ActiveRecord\TActiveRecordCriteria;
use Prado\Exceptions\TConfigurationException;
use Prado\Prado;
use Prado\TPropertyValue;
use Prado\Web\UI\WebControls\IItemDataRenderer;
use Prado\Web\UI\WebControls\TListItemType;
use Prado\Web\UI\WebControls\TRepeaterCommandEventParameter;
Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldBase');
/**
* TScaffoldListView displays a list of Active Records.
*
* The {@link getHeader Header} property is a TRepeater displaying the
* Active Record property/field names. The {@link getSort Sort} property
* is a drop down list displaying the combination of properties and its possible
* ordering. The {@link getPager Pager} property is a TPager control displaying
* the links and/or buttons that navigate to different pages in the Active Record data.
* The {@link getList List} property is a TRepeater that renders a row of
* Active Record data.
*
* Custom rendering of the each Active Record can be achieved by specifying
* the ItemTemplate or AlternatingItemTemplate property of the main {@linnk getList List}
* repeater.
*
* The TScaffoldListView will listen for two command events named "delete" and
* "edit". A "delete" command will delete a the record for the row where the
* "delete" command is originates. An "edit" command will push
* the record data to be edited by a TScaffoldEditView with ID specified by the
* {@link setEditViewID EditViewID}.
*
* Additional {@link setSearchCondition SearchCondition} and
* {@link setSearchParameters SearchParameters} (takes array values) can be
* specified to customize the records to be shown. The {@link setSearchCondition SearchCondition}
* will be used as the Condition property of TActiveRecordCriteria, and similarly
* the {@link setSearchParameters SearchParameters} will be the corresponding
* Parameters property of TActiveRecordCriteria.
*
* @author Wei Zhuo <weizho[at]gmail[dot]com>
* @package Prado\Data\ActiveRecord\Scaffold
* @since 3.1
*/
class TScaffoldListView extends TScaffoldBase
{
/**
* Initialize the sort drop down list and the column names repeater.
*/
protected function initializeSort()
{
$table = $this->getTableInfo();
$sorts = array('Sort By', str_repeat('-',15));
$headers = array();
foreach($table->getColumns() as $name=>$colum)
{
$fname = ucwords(str_replace('_', ' ', $name));
$sorts[$name.' ASC'] = $fname .' Ascending';
$sorts[$name.' DESC'] = $fname .' Descending';
$headers[] = $fname ;
}
$this->_sort->setDataSource($sorts);
$this->_sort->dataBind();
$this->_header->setDataSource($headers);
$this->_header->dataBind();
}
/**
* Loads and display the data.
*/
public function onPreRender($param)
{
parent::onPreRender($param);
if(!$this->getPage()->getIsPostBack() || $this->getViewState('CurrentClass')!=$this->getRecordClass())
{
$this->initializeSort();
$this->setViewState('CurrentClass', $this->getRecordClass());
}
$this->loadRecordData();
}
/**
* Fetch the records and data bind it to the list.
*/
protected function loadRecordData()
{
$search = new TActiveRecordCriteria($this->getSearchCondition(), $this->getSearchParameters());
$this->_list->setVirtualItemCount($this->getRecordFinder()->count($search));
$finder = $this->getRecordFinder();
$criteria = $this->getRecordCriteria();
$this->_list->setDataSource($finder->findAll($criteria));
$this->_list->dataBind();
}
/**
* @return TActiveRecordCriteria sort/search/paging criteria
*/
protected function getRecordCriteria()
{
$total = $this->_list->getVirtualItemCount();
$limit = $this->_list->getPageSize();
$offset = $this->_list->getCurrentPageIndex()*$limit;
if($offset + $limit > $total)
$limit = $total - $offset;
$criteria = new TActiveRecordCriteria($this->getSearchCondition(), $this->getSearchParameters());
if($limit > 0)
{
$criteria->setLimit($limit);
if($offset <= $total)
$criteria->setOffset($offset);
}
$order = explode(' ',$this->_sort->getSelectedValue(), 2);
if(is_array($order) && count($order) === 2)
$criteria->OrdersBy[$order[0]] = $order[1];
return $criteria;
}
/**
* @param string search condition, the SQL string after the WHERE clause.
*/
public function setSearchCondition($value)
{
$this->setViewState('SearchCondition', $value);
}
/**
* @param string SQL search condition for list display.
*/
public function getSearchCondition()
{
return $this->getViewState('SearchCondition');
}
/**
* @param array search parameters
*/
public function setSearchParameters($value)
{
$this->setViewState('SearchParameters', TPropertyValue::ensureArray($value),array());
}
/**
* @return array search parameters
*/
public function getSearchParameters()
{
return $this->getViewState('SearchParameters', array());
}
/**
* Continue bubbling the "edit" command, "delete" command is handled in this class.
*/
public function bubbleEvent($sender, $param)
{
switch(strtolower($param->getCommandName()))
{
case 'delete':
return $this->deleteRecord($sender, $param);
case 'edit':
$this->initializeEdit($sender, $param);
}
$this->raiseBubbleEvent($this, $param);
return true;
}
/**
* Initialize the edit view control form when EditViewID is set.
*/
protected function initializeEdit($sender, $param)
{
if(($ctrl=$this->getEditViewControl())!==null)
{
if($param instanceof TRepeaterCommandEventParameter)
{
$pk = $param->getItem()->getCustomData();
$ctrl->setRecordPk($pk);
$ctrl->initializeEditForm();
}
}
}
/**
* Deletes an Active Record.
*/
protected function deleteRecord($sender, $param)
{
if($param instanceof TRepeaterCommandEventParameter)
{
$pk = $param->getItem()->getCustomData();
$this->getRecordFinder()->deleteByPk($pk);
}
}
/**
* Initialize the default display for each Active Record item.
*/
protected function listItemCreated($sender, $param)
{
$item = $param->getItem();
if($item instanceof IItemDataRenderer)
{
$type = $item->getItemType();
if($type==TListItemType::Item || $type==TListItemType::AlternatingItem)
$this->populateField($sender, $param);
}
}
/**
* Sets the Record primary key to the current repeater item's CustomData.
* Binds the inner repeater with properties of the current Active Record.
*/
protected function populateField($sender, $param)
{
$item = $param->getItem();
if(($data = $item->getData()) !== null)
{
$item->setCustomData($this->getRecordPkValues($data));
if(($prop = $item->findControl('_properties'))!==null)
{
$item->_properties->setDataSource($this->getRecordPropertyValues($data));
$item->_properties->dataBind();
}
}
}
/**
* Updates repeater page index with the pager new index value.
*/
protected function pageChanged($sender, $param)
{
$this->_list->setCurrentPageIndex($param->getNewPageIndex());
}
/**
* @return TRepeater Repeater control for Active Record instances.
*/
public function getList()
{
$this->ensureChildControls();
return $this->getRegisteredObject('_list');
}
/**
* @return TPager List pager control.
*/
public function getPager()
{
$this->ensureChildControls();
return $this->getRegisteredObject('_pager');
}
/**
* @return TDropDownList Control that displays and controls the record ordering.
*/
public function getSort()
{
$this->ensureChildControls();
return $this->getRegisteredObject('_sort');
}
/**
* @return TRepeater Repeater control for record property names.
*/
public function getHeader()
{
$this->ensureChildControls();
return $this->getRegisteredObject('_header');
}
/**
* @return string TScaffoldEditView control ID for editing selected Active Record.
*/
public function getEditViewID()
{
return $this->getViewState('EditViewID');
}
/**
* @param string TScaffoldEditView control ID for editing selected Active Record.
*/
public function setEditViewID($value)
{
$this->setViewState('EditViewID', $value);
}
/**
* @return TScaffoldEditView control for editing selected Active Record, null if EditViewID is not set.
*/
protected function getEditViewControl()
{
if(($id=$this->getEditViewID())!==null)
{
$ctrl = $this->getParent()->findControl($id);
if($ctrl===null)
throw new TConfigurationException('scaffold_unable_to_find_edit_view', $id);
return $ctrl;
}
}
}
|