summaryrefslogtreecommitdiff
path: root/framework/Data/SqlMap/DataMapper/TSqlMapPagedList.php
blob: b15a1d4c4b5d3eda6e8822be9b4ad94956801662 (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
<?php

Prado::using('System.Collections.TPagedList');

/**
 * TSQLMapPagedList
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version $Id$
 * @package System.Web.UI.WebControls
 * @since 3.0
 */
class TSqlMapPagedList extends TPagedList
{
	private $_statement;
	private $_parameter;
	private $_prevPageList;
	private $_nextPageList;
	private $_delegate=null;

	public function __construct(IMappedStatement $statement,
									$parameter, $pageSize, $delegate=null)
	{
		parent::__construct();
		parent::setCustomPaging(true);
		$this->initialize($statement,$parameter, $pageSize);
		$this->_delegate=$delegate;
	}

	protected function initialize($statement, $parameter, $pageSize)
	{
		$this->_statement = $statement;
		$this->_parameter = $parameter;
		$this->setPageSize($pageSize);
		$this->attachEventHandler('OnFetchData', array($this, 'fetchDataFromStatement'));
		$this->gotoPage(0);
	}

	public function setCustomPaging($value)
	{
		throw new TDataMapperException('sqlmap_must_enable_custom_paging');
	}

	protected function fetchDataFromStatement($sender, $param)
	{
		$limit = $this->getOffsetAndLimit($param);
		$connection = $this->_statement->getManager()->getDbConnection();
		$data = $this->_statement->executeQueryForList($connection,
						$this->_parameter, null, $limit[0], $limit[1], $this->_delegate);
		$this->populateData($param, $data);
	}

	public function nextPage()
	{
		if($this->getIsNextPageAvailable())
			return parent::nextPage();
		else
			return false;
	}

	public function previousPage()
	{
		if($this->getIsPreviousPageAvailable())
			return parent::previousPage();
		else
			return false;
	}

	protected function populateData($param, $data)
	{
		$total = $data instanceof TList ? $data->getCount() : count($data);
		$pageSize = $this->getPageSize();
		if($total < 1)
		{
			$param->setData($data);
			$this->_prevPageList = null;
			$this->_nextPageList = null;
			return;
		}

		if($param->getNewPageIndex() < 1)
		{
			$this->_prevPageList = null;
			if($total <= $pageSize)
			{
				$param->setData($data);
				$this->_nextPageList = null;
			}
			else
			{
				$param->setData($this->sublist($data, 0, $pageSize));
				$this->_nextPageList = $this->sublist($data, $pageSize,$total);
			}
		}
		else
		{
			if($total <= $pageSize)
			{
				$this->_prevPageList = $this->sublist($data, 0, $total);
				$param->setData(array());
				$this->_nextPageList = null;
			}
			else if($total <= $pageSize*2)
			{
				$this->_prevPageList = $this->sublist($data, 0, $pageSize);
				$param->setData($this->sublist($data, $pageSize, $total));
				$this->_nextPageList = null;
			}
			else
			{
				$this->_prevPageList = $this->sublist($data, 0, $pageSize);
				$param->setData($this->sublist($data, $pageSize, $pageSize*2));
				$this->_nextPageList = $this->sublist($data, $pageSize*2, $total);
			}
		}
	}

	protected function sublist($data, $from, $to)
	{
		$array = array();
		for($i = $from; $i<$to; $i++)
			$array[] = $data[$i];
		return $array;
	}

	protected function getOffsetAndLimit($param)
	{
		$index = $param->getNewPageIndex();
		$pageSize = $this->getPageSize();
		if($index < 1)
			return array($index, $pageSize*2);
		else
			return array(($index-1)*$pageSize, $pageSize*3);
	}

	public function getIsNextPageAvailable()
	{
		return !is_null($this->_nextPageList);
	}

	public function getIsPreviousPageAvailable()
	{
		return !is_null($this->_prevPageList);
	}

	public function getIsLastPage()
	{
		return is_null($this->_nextPageList)
				|| $this->_nextPageList->getCount() < 1;
	}

	public function getIsMiddlePage()
	{
		return !($this->getIsFirstPage() || $this->getIsLastPage());
	}
}

?>