1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Ajde_Collection_View extends Ajde_Object_Standard |
4
|
|
|
{ |
5
|
|
|
private $_tableName; |
6
|
|
|
|
7
|
|
|
private $_rowCount; |
8
|
|
|
|
9
|
|
|
public function __construct($tableName, $listOptions = []) |
10
|
|
|
{ |
11
|
|
|
$this->_tableName = $tableName; |
12
|
|
|
|
13
|
|
|
$defaultOptions = [ |
14
|
|
|
'page' => 1, |
15
|
|
|
'pageSize' => 100, |
16
|
|
|
'filter' => [], |
17
|
|
|
'search' => null, |
18
|
|
|
'orderBy' => null, |
19
|
|
|
'orderDir' => Ajde_Query::ORDER_ASC, |
20
|
|
|
'viewType' => 'list', |
21
|
|
|
'filterVisible' => false, |
22
|
|
|
'disableFilter' => false, |
23
|
|
|
'mainFilter' => '', |
24
|
|
|
'mainFilterGrouper' => '', |
25
|
|
|
'columns' => [], |
26
|
|
|
]; |
27
|
|
|
$options = array_merge($defaultOptions, $listOptions); |
28
|
|
|
$this->setOptions($options); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function setOptions($options) |
32
|
|
|
{ |
33
|
|
|
foreach ($options as $key => $value) { |
34
|
|
|
$this->set($key, $value); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getPage() |
39
|
|
|
{ |
40
|
|
|
return parent::getPage(); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getPageSize() |
44
|
|
|
{ |
45
|
|
|
return parent::getPageSize(); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getSearch() |
49
|
|
|
{ |
50
|
|
|
return parent::getSearch(); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getOrderBy() |
54
|
|
|
{ |
55
|
|
|
return parent::getOrderBy(); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getOrderDir() |
59
|
|
|
{ |
60
|
|
|
return parent::getOrderDir(); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getFilter() |
64
|
|
|
{ |
65
|
|
|
return parent::getFilter(); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getViewType() |
69
|
|
|
{ |
70
|
|
|
return parent::getViewType(); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getFilterVisible() |
74
|
|
|
{ |
75
|
|
|
return parent::getFilterVisible(); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getDisableFilter() |
79
|
|
|
{ |
80
|
|
|
return parent::getDisableFilter(); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getMainFilter() |
84
|
|
|
{ |
85
|
|
|
return parent::getMainFilter(); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getMainFilterGrouper() |
89
|
|
|
{ |
90
|
|
|
return parent::getMainFilterGrouper(); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getFilterForField($fieldName) |
94
|
|
|
{ |
95
|
|
|
$filters = $this->getFilter(); |
96
|
|
|
if (!array_key_exists($fieldName, $filters)) { |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $filters[$fieldName]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getRowCount(Ajde_Collection $collection = null) |
104
|
|
|
{ |
105
|
|
|
if (isset($collection)) { |
106
|
|
|
return $collection->count(true); |
107
|
|
|
} |
108
|
|
|
if (!isset($this->_rowCount)) { |
109
|
|
|
$sql = 'SELECT COUNT(*) AS count FROM '.$this->_tableName; |
110
|
|
|
$connection = Ajde_Db::getInstance()->getConnection(); |
111
|
|
|
$statement = $connection->prepare($sql); |
112
|
|
|
$statement->execute(); |
113
|
|
|
$result = $statement->fetch(PDO::FETCH_ASSOC); |
114
|
|
|
$this->_rowCount = $result['count']; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->_rowCount; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function setColumns($columns) |
121
|
|
|
{ |
122
|
|
|
$this->set('columns', $columns); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getColumns() |
126
|
|
|
{ |
127
|
|
|
return $this->get('columns'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function getPageCount(Ajde_Collection $collection = null) |
131
|
|
|
{ |
132
|
|
|
return ceil($this->getRowCount($collection) / $this->getPageSize()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function getRowStart() |
136
|
|
|
{ |
137
|
|
|
return ($this->getPage() - 1) * $this->getPageSize(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: