1 | <?php |
||
2 | |||
3 | namespace Icinga\Module\TrapDirector\Tables; |
||
4 | |||
5 | use Icinga\Application\Icinga; |
||
6 | |||
7 | use Icinga\Data\Selectable; |
||
8 | use Icinga\Data\Paginatable; |
||
9 | |||
10 | use Icinga\Web\Request; |
||
11 | |||
12 | use Icinga\Web\Widget; |
||
13 | use Icinga\Web\Widget\Paginator; |
||
14 | |||
15 | |||
16 | abstract class TrapTable implements Paginatable |
||
17 | { |
||
18 | // View : getView / setView |
||
19 | protected $view; |
||
20 | |||
21 | // Db connection : getConnection / setConnection |
||
22 | protected $connection; |
||
23 | |||
24 | // Static configuration |
||
25 | protected $moduleConfig; |
||
26 | |||
27 | // view limits |
||
28 | protected $limit; |
||
29 | protected $offset; |
||
30 | |||
31 | // Used for rendering days in list |
||
32 | protected $columnCount; |
||
33 | protected $lastDay; |
||
34 | |||
35 | // Filters |
||
36 | |||
37 | protected $filter; |
||
38 | protected $enforcedFilters = array(); |
||
39 | protected $searchColumns = array(); |
||
40 | |||
41 | |||
42 | // Must return titles in array ( id => display_name ) |
||
43 | abstract protected function getTitles(); |
||
44 | // ****************** Day header |
||
45 | protected function renderDayIfNew($timestamp) |
||
46 | { |
||
47 | $view = $this->getView(); |
||
48 | |||
49 | // Check for date local format |
||
50 | if (in_array(setlocale(LC_ALL, 0), array('en_US.UTF-8', 'C'))) { |
||
51 | $day = date('l, jS F Y', (int) $timestamp); |
||
52 | } else { |
||
53 | $day = strftime('%A, %e. %B, %Y', (int) $timestamp); |
||
54 | } |
||
55 | |||
56 | if ($this->lastDay === $day) { |
||
57 | return; |
||
58 | } |
||
59 | |||
60 | if ($this->lastDay === null) { |
||
61 | $htm = "<thead>\n <tr>\n"; |
||
62 | } else { |
||
63 | $htm = "</tbody>\n<thead>\n <tr>\n"; |
||
64 | } |
||
65 | |||
66 | if ($this->columnCount === null) { |
||
67 | $this->columnCount = count($this->getTitles()); |
||
68 | } |
||
69 | |||
70 | $htm .= '<th colspan="' . $this->columnCount . '">' . $view->escape($day) . '</th>' . "\n"; |
||
71 | if ($this->lastDay === null) { |
||
72 | $htm .= " </tr>\n"; |
||
73 | } else { |
||
74 | $htm .= " </tr>\n</thead>\n"; |
||
75 | } |
||
76 | |||
77 | $this->lastDay = $day; |
||
78 | |||
79 | return $htm . "<tbody>\n"; |
||
80 | } |
||
81 | // ****************** Render table in html |
||
82 | public function __toString() |
||
83 | { |
||
84 | return $this->render(); |
||
85 | } |
||
86 | |||
87 | abstract public function render(); |
||
88 | |||
89 | // ****************** Static config set |
||
90 | public function setConfig($conf) |
||
91 | { |
||
92 | $this->moduleConfig = $conf; |
||
93 | } |
||
94 | // ****************** View get/set |
||
95 | protected function getView() |
||
96 | { |
||
97 | if ($this->view === null) { |
||
98 | $this->view = Icinga::app()->getViewRenderer()->view; |
||
99 | } |
||
100 | return $this->view; |
||
101 | } |
||
102 | |||
103 | public function setView($view) |
||
104 | { |
||
105 | $this->view = $view; |
||
106 | } |
||
107 | |||
108 | // Limits |
||
109 | |||
110 | public function limit($count = null, $offset = null) |
||
111 | { |
||
112 | $this->limit = $count; |
||
113 | $this->offset = $offset; |
||
114 | |||
115 | return $this; |
||
116 | } |
||
117 | |||
118 | public function hasLimit() |
||
119 | { |
||
120 | return $this->limit !== null; |
||
121 | } |
||
122 | |||
123 | public function getLimit() |
||
124 | { |
||
125 | return $this->limit; |
||
126 | } |
||
127 | |||
128 | public function hasOffset() |
||
129 | { |
||
130 | return $this->offset !== null; |
||
131 | } |
||
132 | |||
133 | public function getOffset() |
||
134 | { |
||
135 | return $this->offset; |
||
136 | } |
||
137 | |||
138 | abstract function count(); |
||
0 ignored issues
–
show
|
|||
139 | |||
140 | public function getPaginator() |
||
141 | { |
||
142 | $paginator = new Paginator(); |
||
143 | $paginator->setQuery($this); |
||
144 | |||
145 | return $paginator; |
||
146 | } |
||
147 | |||
148 | // ****************** DB connection and query |
||
149 | |||
150 | public function setConnection(Selectable $connection) |
||
151 | { |
||
152 | $this->connection = $connection; |
||
153 | return $this; |
||
154 | } |
||
155 | |||
156 | protected function connection() |
||
157 | { |
||
158 | return $this->connection; |
||
159 | } |
||
160 | |||
161 | protected function db() |
||
162 | { |
||
163 | return $this->connection()->getDbAdapter(); |
||
164 | } |
||
165 | |||
166 | protected function getTable() |
||
167 | { |
||
168 | $db=$this->db(); |
||
169 | |||
170 | $query = $this->getBaseQuery(); |
||
171 | |||
172 | if ($this->hasLimit() || $this->hasOffset()) { |
||
173 | $query->limit($this->getLimit(), $this->getOffset()); |
||
174 | } |
||
175 | |||
176 | return $db->fetchAll($query); |
||
177 | } |
||
178 | |||
179 | abstract public function getBaseQuery(); |
||
180 | |||
181 | // ****************** Filters |
||
182 | |||
183 | protected function getSearchColumns() |
||
184 | { |
||
185 | return $this->getColumns(); |
||
186 | } |
||
187 | |||
188 | abstract public function getColumns(); |
||
189 | |||
190 | public function setFilter($filter) |
||
191 | { |
||
192 | $this->filter = $filter; |
||
193 | return $this; |
||
194 | } |
||
195 | |||
196 | public function getFilterEditor(Request $request) |
||
197 | { |
||
198 | $filterEditor = Widget::create('filterEditor') |
||
199 | ->setColumns(array_keys($this->getColumns())) |
||
200 | ->setSearchColumns(array_keys($this->getSearchColumns())) |
||
201 | ->preserveParams('limit', 'sort', 'dir', 'view', 'backend') |
||
202 | ->ignoreParams('page') |
||
203 | ->handleRequest($request); |
||
204 | |||
205 | $filter = $filterEditor->getFilter(); |
||
206 | $this->setFilter($filter); |
||
207 | |||
208 | return $filterEditor; |
||
209 | } |
||
210 | |||
211 | protected function renderFilter($filter) |
||
212 | { // TODO : create a better filter wher user can choose host/oid to filter |
||
213 | } |
||
214 | |||
215 | protected function applyFiltersToQuery($query) |
||
216 | { |
||
217 | /* |
||
218 | $filter = null; |
||
219 | $enforced = $this->enforcedFilters; |
||
220 | if ($this->filter && ! $this->filter->isEmpty()) { |
||
221 | $filter = $this->filter; |
||
222 | } elseif (! empty($enforced)) { |
||
223 | $filter = array_shift($enforced); |
||
224 | } |
||
225 | if ($filter) { |
||
226 | foreach ($enforced as $f) { |
||
227 | $filter->andFilter($f); |
||
228 | } |
||
229 | //$this->renderFilter($filter); |
||
230 | $query->where($this->renderFilter($filter)); |
||
231 | } |
||
232 | */ |
||
233 | return $query; |
||
234 | } |
||
235 | |||
236 | } |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.