Passed
Push — master ( 9a2ce8...395129 )
by Patrick
02:01
created

TrapDirectorTable::renderPagingHeader()   B

Complexity

Conditions 9
Paths 81

Size

Total Lines 96
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 9
eloc 30
nc 81
nop 0
dl 0
loc 96
rs 8.0555
c 1
b 0
f 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Icinga\Module\Trapdirector\Tables;
4
5
use Icinga\Data\Fetchable;
0 ignored issues
show
Bug introduced by
The type Icinga\Data\Fetchable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Icinga\Data\Queryable;
0 ignored issues
show
Bug introduced by
The type Icinga\Data\Queryable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Icinga\Data\Selectable;
8
9
10
abstract class TrapDirectorTable
11
{
12
    /** @var array $titles table titles (name, display value) */
13
    protected $titles = null;
14
15
    /** @var array $content table content  (name, sb column name). name index must be the same as $titles*/
16
    protected $content = null;
17
    
18
    /** @var array $columnNames names of columns for filtering */
19
    protected $columnNames = array();
20
    
21
    /** @var Selectable $dbConn connection to database  */
22
    protected $dbConn = null;
23
    
24
    /** Current view **/
25
    protected $view;
26
    
27
    protected $urlPath;
28
    
29
    // Database stuff
30
   /** @var array $table (db ref, name) */
31
    protected $table = array();
32
    
33
    /** @var Queryable  $query Query in database; */
34
    protected $query = null;
35
    
36
    /** @var array $order : (db column, 'ASC' | 'DESC') */
37
    protected $order = array();
38
    protected $orderQuery = '';
39
    
40
    /********* Filter ********/
41
    /** @var string $filterString : string filter for db columns */
42
    protected $filterString = '';
43
    
44
    /** @var array $filterColumn : columns to apply filter to */
45
    protected $filterColumn = array();
46
    
47
    protected $filterQuery='';
48
    
49
    /*************** Paging *************/
50
    protected $maxPerPage = 25;
51
    
52
    protected $currentPage = 0;
53
    
54
    function __construct(array $table,array $titles, array $columns, array $columnNames, $dbConn , $view, $urlPath)
55
    {
56
        $this->table = $table;
57
        $this->titles = $titles;
58
        $this->content = $columns;
59
        $this->columnNames = $columnNames;
60
        $this->dbConn = $dbConn;
61
        
62
        $this->view = $view;
63
        $this->urlPath = $urlPath;
64
        
65
        return $this;
66
    }
67
68
    
69
    /************** GET variables and URLs *************/
70
    public function getParams(array $getVars)
71
    {
72
        $this->getFilterQuery($getVars);
73
        $this->getPagingQuery($getVars);
74
    }
75
    
76
    public function getCurrentURL()
77
    {
78
        return '?';
79
    }
80
    
81
    protected function getCurrentURLAndQS(string $caller)
82
    {
83
        $actionURL = $this->getCurrentURL() . '?' ;
84
        $QSList=array();
85
        if ($caller != 'filter' && $this->curFilterQuery() != '')
86
            array_push($QSList , $this->curFilterQuery());
87
        
88
        if ($caller != 'paging' && $caller != 'filter' && $this->curPagingQuery() != '')
89
            array_push($QSList , $this->curPagingQuery());
90
 
91
        if ($caller != 'order' && $this->curOrderQuery() != '')
92
            array_push($QSList , $this->curOrderQuery());
93
        
94
        if (count($QSList) != 0)
95
            $actionURL .=  implode('&', $QSList);
96
        
97
        return $actionURL;
98
    }
99
    
100
    /************* DB queries ******************/
101
    /**
102
     * Get base query in $this->query
103
     * @return  TrapDirectorTable
104
     */
105
    public function getBaseQuery()
106
    {
107
        $this->query = $this->dbConn->select();
108
        $this->query = $this->query->from(
109
            $this->table,
110
            $this->content
111
            );
112
        
113
        return $this;
114
    }
115
    
116
    public function fullQuery()
117
    {
118
        $this->getBaseQuery()
119
        ->applyFilter()
120
        ->applyPaging()
121
        ->applyOrder();
122
        
123
        return $this->dbConn->fetchAll($this->query);
124
        //return $this->query->fetchAll();
125
    }
126
    
127
    
128
    /**************** Filtering ******************/
129
    public function applyFilter()
130
    {
131
        if ($this->filterString == '' || count($this->filterColumn) == 0)
132
        {
133
            return $this;
134
        }
135
        $filter='';
136
        foreach ($this->filterColumn as $column)
137
        {
138
            if ($filter != "") $filter.=' OR ';
139
            //$filter .= "'" . $column . "' LIKE '%" . $this->filterString. "%'";
140
            $filter .= $column  . " LIKE '%" . $this->filterString. "%'";
141
        }
142
        //echo $filter;
143
        
144
        $this->query=$this->query->where($filter);
145
146
        return $this;
147
    }
148
149
    public function setFilter(string $filter, array $filterCol)
150
    {
151
        $this->filterString = $filter;
152
        $this->filterColumn = $filterCol;
153
        return $this;
154
    }
155
156
    
157
    public function renderFilter()
158
    {
159
        
160
        $html=' <form id="genfilter" name="mainFilterGen"
161
			enctype="application/x-www-form-urlencoded"
162
			action="'.$this->getCurrentURLAndQS('filter').'"
163
			method="get">';
164
        $html.='<input type="text" name="f" title="Search is simple! Try to combine multiple words"
165
	placeholder="Search..."  value="'.$this->filterQuery.'">';
166
167
        $html.='</form>';
168
        return $html;
169
    }
170
 
171
    public function getFilterQuery(array $getVars)
172
    {
173
        if (isset($getVars['f']))
174
        {
175
            $this->filterQuery = $getVars['f'];
176
            $this->setFilter(html_entity_decode($getVars['f']), $this->columnNames);
177
        }
178
    }
179
    
180
    protected function curFilterQuery()
181
    {
182
        if ($this->filterQuery == '') return '';
183
        return 'f='.$this->filterQuery;
184
    }
185
    
186
    /***************** Ordering ********************/
187
    public function applyOrder()
188
    {
189
        if (count($this->order) == 0)
190
        {
191
            return $this;
192
        }
193
        $orderSQL='';
194
        foreach ($this->order as $column => $direction)
195
        {
196
            if ($orderSQL != "") $orderSQL.=',';
197
            
198
            $orderSQL .= $column . ' ' . $direction;
199
        }
200
        $this->query = $this->query->order($orderSQL);
201
        
202
        return $this;
203
    }
204
    
205
    public function setOrder(array $order)
206
    {
207
        $this->order = $order;
208
        return $this;
209
    }
210
    
211
    public function getOrderQuery(array $getVars)
212
    {
213
        if (isset($getVars['o']))
214
        {
215
            $this->orderQuery = $getVars['o'];
216
            // TODO
217
        }
218
    }
219
    
220
    protected function curOrderQuery()
221
    {
222
        if ($this->orderQuery == '') return '';
223
        return 'o='.$this->orderQuery;
224
    }
225
    
226
    /*****************  Paging and counting *********/
227
    public function countQuery()
228
    {
229
        $this->query = $this->dbConn->select();
230
        $this->query = $this->query
231
            ->from(
232
                $this->table,
233
                array('COUNT(*)')
234
                );
235
        $this->applyFilter();                   
236
    }
237
    
238
    public function count()
239
    {
240
        $this->countQuery();
241
        return $this->dbConn->fetchOne($this->query);
242
    }
243
    
244
    public function setMaxPerPage(int $max)
245
    {
246
        $this->maxPerPage = $max;
247
    }
248
    
249
    protected function getPagingQuery(array $getVars)
250
    {
251
        if (isset($getVars['page']))
252
        {
253
            $this->currentPage = $getVars['page'];
254
        }
255
    }
256
257
    protected function curPagingQuery()
258
    {
259
        if ($this->currentPage == '') return '';
260
        return 'page='.$this->currentPage;
261
    }
262
    
263
    public function renderPagingHeader()
264
    {
265
        $count = $this->count();
266
        if ($count <= $this->maxPerPage )
267
        {
268
            return  'count : ' . $this->count() . '<br>';
269
        }
270
        
271
        if ($this->currentPage == 0) $this->currentPage = 1;
272
        
273
        $numPages = intdiv($count , $this->maxPerPage);
274
        if ($count % $this->maxPerPage != 0 ) $numPages++;
275
        
276
        $html = '<div class="pagination-control" role="navigation">';
277
        $html .= '<ul class="nav tab-nav">';
278
        if ($this->currentPage <=1)
279
        {
280
            $html .= '
281
                <li class="nav-item disabled" aria-hidden="true">
282
                    <span class="previous-page">
283
                            <span class="sr-only">Previous page</span>
284
                            <i aria-hidden="true" class="icon-angle-double-left"></i>            
285
                     </span>
286
                </li>
287
               ';
288
        }
289
        else 
290
        {
291
            $html .= '
292
                <li class="nav-item">
293
                    <a href="'. $this->getCurrentURLAndQS('paging') .'&page='. ($this->currentPage - 1 ).'" class="previous-page" >
294
                            <i aria-hidden="true" class="icon-angle-double-left"></i>            
295
                     </a>
296
                </li>
297
            ';
298
        }
299
        
300
        for ($i=1; $i <= $numPages ; $i++)
301
        {
302
            $active = ($this->currentPage == $i) ? 'active' : '';
303
            $first = ($i-1)*$this->maxPerPage+1;
304
            $last = $i * $this->maxPerPage;
305
            if ($last > $count) $last = $count;
306
            $display = 'Show rows '. $first . ' to '. $last .' of '. $count;
307
            $html .= '<li class="' . $active . ' nav-item">
308
                    <a href="'. $this->getCurrentURLAndQS('paging') .'&page='. $i .'" title="' . $display . '" aria-label="' . $display . '">
309
                    '.$i.'                
310
                    </a>
311
                </li>';
312
        }
313
        
314
        if ($this->currentPage == $numPages)
315
        {
316
            $html .= '
317
                <li class="nav-item disabled" aria-hidden="true">
318
                    <span class="previous-page">
319
                            <span class="sr-only">Previous page</span>
320
                            <i aria-hidden="true" class="icon-angle-double-right"></i>
321
                     </span>
322
                </li>
323
               ';
324
        }
325
        else
326
        {
327
            $html .= '
328
                <li class="nav-item">
329
                    <a href="'. $this->getCurrentURLAndQS('paging') .'&page='. ($this->currentPage + 1 ).'" class="next-page">
330
                            <i aria-hidden="true" class="icon-angle-double-right"></i>
331
                     </a>
332
                </li>
333
            ';
334
        }
335
        
336
        $html .= '</ul> </div>';
337
        
338
        $htmlOLD = '
0 ignored issues
show
Unused Code introduced by
The assignment to $htmlOLD is dead and can be removed.
Loading history...
339
            <ul class="nav tab-nav">
340
341
                <li class="active nav-item">
342
                    <a href="/icingaweb2/trapdirector/received/?q=&amp;page=1" title="Show rows 1 to 25 of 29" aria-label="Show rows 1 to 25 of 29">
343
                    1                
344
                    </a>
345
                </li>
346
                <li class="nav-item">
347
                    <a href="/icingaweb2/trapdirector/received/?q=&amp;page=2" title="Show rows 26 to 29 of 29" aria-label="Show rows 26 to 29 of 29">
348
                        2                
349
                    </a>
350
                </li>
351
                <li class="nav-item">
352
                    <a href="/icingaweb2/trapdirector/received/?q=&amp;page=2" title="Show rows 26 to 50 of 29" aria-label="Show rows 26 to 50 of 29" class="next-page">
353
                        <i aria-hidden="true" class="icon-angle-double-right"></i>
354
                     </a>
355
                 </li>
356
               </ul>
357
';
358
        return $html;
359
    }
360
    
361
    public function applyPaging()
362
    {
363
        $this->query->limitPage($this->currentPage,$this->maxPerPage);
364
        return $this;
365
    }
366
    
367
    /*************** Rendering *************************/
368
    public function renderTitles()
369
    {
370
        $html = "<thead>\n<tr>\n";
371
        foreach ($this->titles as $values)
372
        {
373
            $html .= '<th>' . $values . '</th>';            
374
        }
375
        $html .= "</tr>\n</thead>\n";
376
        return $html;
377
    }
378
    
379
    public function renderLine( $value)
380
    {
381
        $html = '';
382
        $titleNames = array_keys($this->titles);
383
        foreach ($titleNames as $name )
384
        {
385
            $html .= '<td>';
386
            $html .= $value->$name;
387
            $html .= "</td>\n";
388
        }
389
        return $html;
390
    }
391
    
392
    public function renderTable(array $values)
393
    {
394
       $html = '<tbody id="obj_table_body">';
395
       foreach($values as $value)
396
       {
397
           $html .= "<tr>\n";
398
           $html .= $this->renderLine($value);
399
           $html .= "</tr>\n";
400
       }
401
       $html .= '</tbody>';
402
       return $html;
403
    }
404
    
405
    public function render()
406
    {
407
        $html = '';
408
        
409
        
410
        $values = $this->fullQuery();
411
        
412
        $html.="<table class='simple common-table table-row-selectable'>\n";
413
        
414
        $html .= $this->renderTitles();
415
        $html .= $this->renderTable($values);
416
        $html .= '</table>'; 
417
        
418
419
        return $html;
420
    }
421
    
422
}