1: | <?php |
2: | |
3: | namespace Zippy\Html\DataList; |
4: | |
5: | use Zippy\Html\HtmlContainer; |
6: | |
7: | /** |
8: | * Базовый клас для компонентов - списков(таблиц) данных |
9: | */ |
10: | abstract class AbstractList extends HtmlContainer |
11: | { |
12: | protected $pagesize = PHP_INT_MAX; |
13: | protected $currentpage = 1; |
14: | protected $DataSource; |
15: | protected $pagerowscount = 0; |
16: | protected $rowscount = -1; |
17: | protected $sortf = null; |
18: | protected $sortd = null; |
19: | |
20: | /** |
21: | * Конструктор |
22: | * @param ID компонента |
23: | * @param Zippy\Interfaces\DataSource Источник данных |
24: | */ |
25: | public function __construct($id, $DataSource) { |
26: | HtmlContainer::__construct($id); |
27: | $this->DataSource = $DataSource; |
28: | $this->pagesize = PHP_INT_MAX; |
29: | } |
30: | |
31: | /** |
32: | * Возвращает количество всех строк в наборе |
33: | */ |
34: | public function getAllRowsCount() { |
35: | |
36: | if ($this->rowscount == -1) { |
37: | $this->rowscount = $this->DataSource->getItemCount(); |
38: | } |
39: | return $this->rowscount; |
40: | } |
41: | |
42: | /** |
43: | * Возвращает количество всех строк на текущей странице |
44: | */ |
45: | public function getPageRowsCount() { |
46: | return $this->pagerowscount; |
47: | } |
48: | |
49: | /** |
50: | * Возвращает размер страницы |
51: | */ |
52: | public function getPageSize() { |
53: | return $this->pagesize; |
54: | } |
55: | |
56: | /** |
57: | * Устанавливает размер страницы данных |
58: | * @param int Количество строк в странице данных |
59: | */ |
60: | public function setPageSize($pagesize) { |
61: | if ($pagesize > 0) { |
62: | $this->pagesize = $pagesize; |
63: | } else { |
64: | $this->pagesize = 20; |
65: | } |
66: | |
67: | $this->currentpage = 1; |
68: | // $this->Reload(); |
69: | } |
70: | |
71: | /** |
72: | * Возвращает номер текущей страницы |
73: | * @return int |
74: | */ |
75: | public function getCurrentPage() { |
76: | return $this->currentpage; |
77: | } |
78: | |
79: | /** |
80: | * Устанавливает текущую страницу |
81: | * @param int Номер страницы |
82: | * @see Paginator |
83: | */ |
84: | public function setCurrentPage($page) { |
85: | if ($page > 0 && $page <= $this->getPageCount()) { |
86: | $this->currentpage = $page; |
87: | } else { |
88: | $this->currentpage = 1; |
89: | } |
90: | |
91: | // $start = ($this->currentpage - 1) * $this->pagesize; |
92: | // $this->setPageRows($start, $this->pagesize); |
93: | // $this->Reload(); |
94: | } |
95: | |
96: | /** |
97: | * Количество страниц в списке |
98: | */ |
99: | final public function getPageCount() { |
100: | |
101: | $rowcount = $this->getAllRowsCount(); |
102: | return ceil($rowcount / $this->pagesize); |
103: | } |
104: | |
105: | /** |
106: | * Обновляет данные с провайдера |
107: | */ |
108: | public function Reload($resetpage = true) { |
109: | |
110: | if ($resetpage) { |
111: | $this->setCurrentPage(1); |
112: | $this->rowscount = -1; |
113: | } |
114: | } |
115: | |
116: | /** |
117: | * Возвращает ссылку на источник данных |
118: | * |
119: | */ |
120: | public function getDataSource() { |
121: | return $this->DataSource; |
122: | } |
123: | |
124: | public function setDataSource($ds) { |
125: | $this->DataSource = $ds; |
126: | } |
127: | |
128: | |
129: | /** |
130: | * Возвращает данные текущей страницы |
131: | */ |
132: | protected function getItems() { |
133: | $list = $this->DataSource->getItems($this->pagesize * ($this->currentpage - 1), $this->getPageSize(), $this->sortf, $this->sortd); |
134: | $this->pagerowscount = count($list); |
135: | return is_array($list) ? $list : array(); |
136: | } |
137: | |
138: | /** |
139: | * Возвращает массив строк |
140: | * return array |
141: | */ |
142: | public function getDataRows() { |
143: | $list = array(); |
144: | foreach ($this->components as $child) { |
145: | if ($child instanceof DataRow) { |
146: | $list[] = $child; |
147: | } |
148: | } |
149: | return $list; |
150: | } |
151: | |
152: | /** |
153: | * @see HtmlComponent |
154: | */ |
155: | public function Render() { |
156: | |
157: | $this->beforeRender(); |
158: | $this->RenderImpl(); |
159: | $this->afterRender(); |
160: | } |
161: | |
162: | /** |
163: | * Устанавливает имя поля и направление сортировки. |
164: | * Установленные параметры передаютя провайдеру данных |
165: | */ |
166: | final public function setSorting($field, $dir = 'asc') { |
167: | $this->sortf = $field; |
168: | $this->sortd = $dir; |
169: | } |
170: | |
171: | final public function getSorting() { |
172: | return array('field'=>$this->sortf,'dir'=>$this->sortd); |
173: | } |
174: | |
175: | |
176: | |
177: | |
178: | } |
179: |