Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TScaffoldListView class file.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TScaffoldListView.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Data.ActiveRecord.Scaffold
11
 */
12
 
13
/**
14
 * Load the scaffold base class.
15
 */
16
Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldBase');
17
 
18
/**
19
 * TScaffoldListView displays a list of Active Records.
20
 *
21
 * The {@link getHeader Header} property is a TRepeater displaying the
22
 * Active Record property/field names. The {@link getSort Sort} property
23
 * is a drop down list displaying the combination of properties and its possible
24
 * ordering. The {@link getPager Pager} property is a TPager control displaying
25
 * the links and/or buttons that navigate to different pages in the Active Record data.
26
 * The {@link getList List} property is a TRepeater that renders a row of
27
 * Active Record data.
28
 *
29
 * Custom rendering of the each Active Record can be achieved by specifying
30
 * the ItemTemplate or AlternatingItemTemplate property of the main {@linnk getList List}
31
 * repeater.
32
 *
33
 * The TScaffoldListView will listen for two command events named "delete" and
34
 * "edit". A "delete" command will delete a the record for the row where the
35
 * "delete" command is originates. An "edit" command will push
36
 * the record data to be edited by a TScaffoldEditView with ID specified by the
37
 * {@link setEditViewID EditViewID}.
38
 *
39
 * Additional {@link setSearchCondition SearchCondition} and
40
 * {@link setSearchParameters SearchParameters} (takes array values) can be
41
 * specified to customize the records to be shown. The {@link setSearchCondition SearchCondition}
42
 * will be used as the Condition property of TActiveRecordCriteria, and similarly
43
 * the {@link setSearchParameters SearchParameters} will be the corresponding
44
 * Parameters property of TActiveRecordCriteria.
45
 *
46
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
47
 * @version $Id: TScaffoldListView.php 2541 2008-10-21 15:05:13Z qiang.xue $
48
 * @package System.Data.ActiveRecord.Scaffold
49
 * @since 3.1
50
 */
51
class TScaffoldListView extends TScaffoldBase
52
{
53
	/**
54
	 * Initialize the sort drop down list and the column names repeater.
55
	 */
56
	protected function initializeSort()
57
	{
58
		$table = $this->getTableInfo();
59
		$sorts = array('Sort By', str_repeat('-',15));
60
		$headers = array();
61
		foreach($table->getColumns() as $name=>$colum)
62
		{
63
			$fname = ucwords(str_replace('_', ' ', $name));
64
			$sorts[$name.' ASC'] = $fname .' Ascending';
65
			$sorts[$name.' DESC'] = $fname .' Descending';
66
			$headers[] = $fname ;
67
		}
68
		$this->_sort->setDataSource($sorts);
69
		$this->_sort->dataBind();
70
		$this->_header->setDataSource($headers);
71
		$this->_header->dataBind();
72
	}
73
 
74
	/**
75
	 * Loads and display the data.
76
	 */
77
	public function onPreRender($param)
78
	{
79
		parent::onPreRender($param);
80
		if(!$this->getPage()->getIsPostBack() || $this->getViewState('CurrentClass')!=$this->getRecordClass())
81
		{
82
			$this->initializeSort();
83
			$this->setViewState('CurrentClass', $this->getRecordClass());
84
		}
85
		$this->loadRecordData();
86
	}
87
 
88
	/**
89
	 * Fetch the records and data bind it to the list.
90
	 */
91
	protected function loadRecordData()
92
	{
93
		$search = new TActiveRecordCriteria($this->getSearchCondition(), $this->getSearchParameters());
94
		$this->_list->setVirtualItemCount($this->getRecordFinder()->count($search));
95
		$finder = $this->getRecordFinder();
96
		$criteria = $this->getRecordCriteria();
97
		$this->_list->setDataSource($finder->findAll($criteria));
98
		$this->_list->dataBind();
99
	}
100
 
101
	/**
102
	 * @return TActiveRecordCriteria sort/search/paging criteria
103
	 */
104
	protected function getRecordCriteria()
105
	{
106
		$total = $this->_list->getVirtualItemCount();
107
		$limit = $this->_list->getPageSize();
108
		$offset = $this->_list->getCurrentPageIndex()*$limit;
109
		if($offset + $limit > $total)
110
			$limit = $total - $offset;
111
		$criteria = new TActiveRecordCriteria($this->getSearchCondition(), $this->getSearchParameters());
112
		if($limit > 0)
113
		{
114
			$criteria->setLimit($limit);
115
			if($offset <= $total)
116
				$criteria->setOffset($offset);
117
		}
118
		$order = explode(' ',$this->_sort->getSelectedValue(), 2);
119
		if(is_array($order) && count($order) === 2)
120
			$criteria->OrdersBy[$order[0]] = $order[1];
121
		return $criteria;
122
	}
123
 
124
	/**
125
	 * @param string search condition, the SQL string after the WHERE clause.
126
	 */
127
	public function setSearchCondition($value)
128
	{
129
		$this->setViewState('SearchCondition', $value);
130
	}
131
 
132
	/**
133
	 * @param string SQL search condition for list display.
134
	 */
135
	public function getSearchCondition()
136
	{
137
		return $this->getViewState('SearchCondition');
138
	}
139
 
140
	/**
141
	 * @param array search parameters
142
	 */
143
	public function setSearchParameters($value)
144
	{
145
		$this->setViewState('SearchParameters', TPropertyValue::ensureArray($value),array());
146
	}
147
 
148
	/**
149
	 * @return array search parameters
150
	 */
151
	public function getSearchParameters()
152
	{
153
		return $this->getViewState('SearchParameters', array());
154
	}
155
 
156
	/**
157
	 * Continue bubbling the "edit" command, "delete" command is handled in this class.
158
	 */
159
	public function bubbleEvent($sender, $param)
160
	{
161
		switch(strtolower($param->getCommandName()))
162
		{
163
			case 'delete':
164
				return $this->deleteRecord($sender, $param);
165
			case 'edit':
166
				$this->initializeEdit($sender, $param);
167
		}
168
		$this->raiseBubbleEvent($this, $param);
169
		return true;
170
	}
171
 
172
	/**
173
	 * Initialize the edit view control form when EditViewID is set.
174
	 */
175
	protected function initializeEdit($sender, $param)
176
	{
177
		if(($ctrl=$this->getEditViewControl())!==null)
178
		{
179
			if($param instanceof TRepeaterCommandEventParameter)
180
			{
181
				$pk = $param->getItem()->getCustomData();
182
				$ctrl->setRecordPk($pk);
183
				$ctrl->initializeEditForm();
184
			}
185
		}
186
	}
187
 
188
	/**
189
	 * Deletes an Active Record.
190
	 */
191
	protected function deleteRecord($sender, $param)
192
	{
193
		if($param instanceof TRepeaterCommandEventParameter)
194
		{
195
			$pk = $param->getItem()->getCustomData();
196
			$this->getRecordFinder()->deleteByPk($pk);
197
		}
198
	}
199
 
200
	/**
201
	 * Initialize the default display for each Active Record item.
202
	 */
203
	protected function listItemCreated($sender, $param)
204
	{
205
		$item = $param->getItem();
206
		if($item instanceof IItemDataRenderer)
207
		{
208
			$type = $item->getItemType();
209
			if($type==TListItemType::Item || $type==TListItemType::AlternatingItem)
210
				$this->populateField($sender, $param);
211
		}
212
	}
213
 
214
	/**
215
	 * Sets the Record primary key to the current repeater item's CustomData.
216
	 * Binds the inner repeater with properties of the current Active Record.
217
	 */
218
	protected function populateField($sender, $param)
219
	{
220
		$item = $param->getItem();
221
		if(($data = $item->getData()) !== null)
222
		{
223
			$item->setCustomData($this->getRecordPkValues($data));
224
			if(($prop = $item->findControl('_properties'))!==null)
225
			{
226
				$item->_properties->setDataSource($this->getRecordPropertyValues($data));
227
				$item->_properties->dataBind();
228
			}
229
		}
230
	}
231
 
232
	/**
233
	 * Updates repeater page index with the pager new index value.
234
	 */
235
	protected function pageChanged($sender, $param)
236
	{
237
		$this->_list->setCurrentPageIndex($param->getNewPageIndex());
238
	}
239
 
240
	/**
241
	 * @return TRepeater Repeater control for Active Record instances.
242
	 */
243
	public function getList()
244
	{
245
		$this->ensureChildControls();
246
		return $this->getRegisteredObject('_list');
247
	}
248
 
249
	/**
250
	 * @return TPager List pager control.
251
	 */
252
	public function getPager()
253
	{
254
		$this->ensureChildControls();
255
		return $this->getRegisteredObject('_pager');
256
	}
257
 
258
	/**
259
	 * @return TDropDownList Control that displays and controls the record ordering.
260
	 */
261
	public function getSort()
262
	{
263
		$this->ensureChildControls();
264
		return $this->getRegisteredObject('_sort');
265
	}
266
 
267
	/**
268
	 * @return TRepeater Repeater control for record property names.
269
	 */
270
	public function getHeader()
271
	{
272
		$this->ensureChildControls();
273
		return $this->getRegisteredObject('_header');
274
	}
275
 
276
	/**
277
	 * @return string TScaffoldEditView control ID for editing selected Active Record.
278
	 */
279
	public function getEditViewID()
280
	{
281
		return $this->getViewState('EditViewID');
282
	}
283
 
284
	/**
285
	 * @param string TScaffoldEditView control ID for editing selected Active Record.
286
	 */
287
	public function setEditViewID($value)
288
	{
289
		$this->setViewState('EditViewID', $value);
290
	}
291
 
292
	/**
293
	 * @return TScaffoldEditView control for editing selected Active Record, null if EditViewID is not set.
294
	 */
295
	protected function getEditViewControl()
296
	{
297
		if(($id=$this->getEditViewID())!==null)
298
		{
299
			$ctrl = $this->getParent()->findControl($id);
300
			if($ctrl===null)
301
				throw new TConfigurationException('scaffold_unable_to_find_edit_view', $id);
302
			return $ctrl;
303
		}
304
	}
305
}
306