Passed
Push — master ( cb2842...994f5c )
by Patrick
02:04
created

TrapDirectorTableFilter::applyFilter()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 18
rs 9.6111
1
<?php
2
3
namespace Icinga\Module\Trapdirector\Tables;
4
5
6
trait TrapDirectorTableFilter
7
{
8
    
9
    /********* Filter ********/
10
    /** @var string $filterString : string filter for db columns */
11
    protected $filterString = '';
12
    
13
    /** @var array $filterColumn : columns to apply filter to */
14
    protected $filterColumn = array();
15
    
16
    protected $filterQuery='';
17
    
18
    
19
    /**************** Filtering ******************/
20
    
21
    public function applyFilter()
22
    {
23
        if ($this->filterString == '' || count($this->filterColumn) == 0)
24
        {
25
            return $this;
26
        }
27
        $filter='';
28
        foreach ($this->filterColumn as $column)
29
        {
30
            if ($filter != "") $filter.=' OR ';
31
            //$filter .= "'" . $column . "' LIKE '%" . $this->filterString. "%'";
32
            $filter .= $column  . " LIKE '%" . $this->filterString. "%'";
33
        }
34
        //echo $filter;
35
        
36
        $this->query=$this->query->where($filter);
0 ignored issues
show
Bug Best Practice introduced by
The property query does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
38
        return $this;
39
    }
40
41
    public function setFilter(string $filter, array $filterCol)
42
    {
43
        $this->filterString = $filter;
44
        $this->filterColumn = $filterCol;
45
        return $this;
46
    }
47
   
48
    public function renderFilter()
49
    {
50
        
51
        $html=' <form id="genfilter" name="mainFilterGen"
52
			enctype="application/x-www-form-urlencoded"
53
			action="'.$this->getCurrentURLAndQS('filter').'"
0 ignored issues
show
Bug introduced by
It seems like getCurrentURLAndQS() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
			action="'.$this->/** @scrutinizer ignore-call */ getCurrentURLAndQS('filter').'"
Loading history...
54
			method="get">';
55
        $html.='<input type="text" name="f" title="Search is simple! Try to combine multiple words"
56
	placeholder="Search..."  value="'.$this->filterQuery.'">';
57
58
        $html.='</form>';
59
        return $html;
60
    }
61
 
62
    public function getFilterQuery(array $getVars)
63
    {
64
        if (isset($getVars['f']))
65
        {
66
            $this->filterQuery = $getVars['f'];
67
            $this->setFilter(html_entity_decode($getVars['f']), $this->columnNames);
68
        }
69
    }
70
    
71
    protected function curFilterQuery()
72
    {
73
        if ($this->filterQuery == '') return '';
74
        return 'f='.$this->filterQuery;
75
    }
76
    
77
}