summaryrefslogtreecommitdiff
path: root/framework/Data/ActiveRecord/Scaffold/TScaffoldListView.php
blob: 98c0aab8d58a60ca251823febd4c8fff71bb9dd8 (plain)
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
<?php

Prado::using('System.Data.ActiveRecord.Scaffold.TScaffoldBase');

class TScaffoldListView extends TScaffoldBase
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		if(!$this->getPage()->getIsPostBack())
			$this->initializeSort();
	}

	protected function initializeSort()
	{
		$table = $this->getTableMetaData();
		$sorts = array('Sorty 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();
	}

	public function onPreRender($param)
	{
		parent::onPreRender($param);
		$this->initializeItemCount();
		$this->loadRecordData();
	}

	protected function initializeItemCount()
	{
		$this->_list->setVirtualItemCount($this->getRecordFinder()->count());
	}

	protected function loadRecordData()
	{
		$finder = $this->getRecordFinder();
		$criteria = $this->getPagingCriteria();
		$this->_list->setDataSource($finder->findAll($criteria));
		$this->_list->dataBind();
	}

	protected function getPagingCriteria()
	{
		$total = $this->_list->getVirtualItemCount();
		$limit = $this->_list->getPageSize();
		$offset = $this->_list->getCurrentPageIndex()*$limit;
		if($offset + $limit > $total)
			$limit = $total - $offset;
		$criteria = new TActiveRecordCriteria;
		$criteria->setLimit($limit);
		$criteria->setOffset($offset);
		$order = explode(' ',$this->_sort->getSelectedValue(), 2);
		if(is_array($order) && count($order) === 2)
			$criteria->OrdersBy[$order[0]] = $order[1];
		return $criteria;
	}

	public function bubbleEvent($sender, $param)
	{
		switch(strtolower($param->getCommandName()))
		{
			case 'delete':
				return $this->deleteRecord($sender, $param);
			case 'edit':
				if(($ctrl=$this->getEditViewControl())!==null)
				{
					if($param instanceof TRepeaterCommandEventParameter)
					{
						$pk = $param->getItem()->getCustomData();
						$ctrl->setRecordPk($pk);
						$ctrl->initializeEditForm();
					}
				}
		}
		$this->raiseBubbleEvent($this, $param);
		return true;
	}

	public function deleteRecord($sender, $param)
	{
		if($param instanceof TRepeaterCommandEventParameter)
		{
			$pk = $param->getItem()->getCustomData();
			$this->getRecordFinder()->deleteByPk($pk);
		}
	}

	protected function listItemCreated($sender, $param)
	{
		$type = $param->getItem()->getItemType();
		if($type==TListItemType::Item || $type==TListItemType::AlternatingItem)
			$this->populateField($sender, $param);
	}

	protected function populateField($sender, $param)
	{
		$item = $param->getItem();
		$data = $item->getDataItem();
		if($data !== null)
		{
			$item->setCustomData($this->getRecordObjectPk($data));

			if(($prop = $item->findControl('_properties'))!==null)
			{
				$item->_properties->setDataSource($this->getRecordProperties($data));
				$item->_properties->dataBind();
			}
		}
	}

	protected function pageChanged($sender, $param)
	{
		$this->_list->setCurrentPageIndex($param->getNewPageIndex());
	}

	public function getList()
	{
		$this->ensureChildControls();
		return $this->getRegisteredObject('_list');
	}

	public function getPager()
	{
		$this->ensureChildControls();
		return $this->getRegisteredObject('_pager');
	}

	public function getSort()
	{
		$this->ensureChildControls();
		return $this->getRegisteredObject('_sort');
	}

	public function getHeader()
	{
		$this->ensureChildControls();
		return $this->getRegisteredObject('_header');
	}

	public function getEditViewID()
	{
		return $this->getViewState('EditViewID');
	}

	public function setEditViewID($value)
	{
		$this->setViewState('EditViewID', $value);
	}

	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;
		}
	}
}

?>