Issues (48)

library/Trapdirector/Tables/TrapTableList.php (1 issue)

1
<?php
2
3
namespace Icinga\Module\TrapDirector\Tables;
4
5
6
use Icinga\Web\Request;
7
use Icinga\Web\Url;
8
use Icinga\Web\Widget;
9
use Icinga\Web\Widget\Paginator;
10
11
use Icinga\Module\Trapdirector\Tables\TrapTable;
12
13
14
class TrapTableList extends TrapTable
15
{	
16
	/** @var mixed $connection Db connection : getConnection / setConnection */
17
	protected $connection;
18
	
19
	// Filters 
20
	
21
    protected $filter;
22
    protected $enforcedFilters = array();
23
    protected $searchColumns = array();
24
	
25
	protected function getTitles() {
26
		// TODO : check moduleconfig is set
27
		return $this->moduleConfig->getTrapListTitles();
28
	}
29
		
30
	// ******************  Render table in html  
31
    public function __toString()
32
    {
33
        return $this->render();
34
    }
35
	
36
	public function render()
37
	{
38
		$data=$this->getTable();
39
		$view = $this->getView();
40
		$this->columnCount = count($this->getTitles());
41
		$this->lastDay=null;
42
		// Table start
43
		$htm  = '<table class="simple common-table table-row-selectable">';
44
		
45
		// Titles
46
		$htm .= "<thead>\n  <tr>\n";
47
		$titles = $this->getTitles();
48
		foreach ($titles as $title) 
49
		{
50
			$htm .= '    <th>' . $view->escape($view->translate($title)) . "</th>\n";
51
		}
52
		$htm .= "  </tr>\n</thead>\n";
53
		
54
		// Rows
55
		$htm .= "<tbody>\n";
56
		
57
		foreach ($data as $row) 
58
		{
59
			$firstCol = true;
60
			// Put date header
61
			$htm .= $this->renderDayIfNew($row->timestamp);
62
			
63
			
64
			// Render row
65
			$htm .= '<tr>';
66
			foreach ( $titles as $rowkey => $title) 
67
			{
68
				// Check missing value
69
				if (property_exists($row, $rowkey)) 
70
				{
71
					$val = ($rowkey=='timestamp') ?  strftime('%T',$row->$rowkey) : $row->$rowkey;
72
				} else {
73
					$val = '-';
74
				}
75
				if ($firstCol == true) { // Put link in first column for trap detail.
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
76
					$htm .= '<td>' 
77
							. $view->qlink(
78
									$view->escape($val),  
79
									Url::fromPath(
80
										$this->moduleConfig->urlPath() . '/received/trapdetail', 
81
										array('id' => $row->id)
82
									)
83
							)
84
							. '</td>';
85
				} else {
86
					$htm .= '<td>' . $view->escape($val) . '</td>';
87
				}
88
				$firstCol=false;
89
			}
90
			$htm .= "<tr>\n";
91
		}
92
		$htm .= "</tbody></table>\n";
93
		//$htm .= "Filter : " . $this->filter."<br>\n";
94
		return $htm;
95
96
	}
97
98
    public function count()
99
    {
100
        $db=$this->db();
101
		
102
		$query = $db->select()->from(
103
            $this->moduleConfig->getTrapTableName(),
104
            array('COUNT(*)')
105
        );
106
		$this->applyFiltersToQuery($query);
107
		
108
        return $db->fetchOne($query);
109
    }
110
	
111
    public function getPaginator()
112
    {
113
        $paginator = new Paginator();
114
        $paginator->setQuery($this);
115
116
        return $paginator;
117
    }
118
	
119
	// ****************** DB connection and query
120
	
121
	protected function getTable()
122
	{
123
		$db=$this->db();
124
		
125
		$query = $this->getBaseQuery();
126
		$this->applyFiltersToQuery($query);
127
       if ($this->hasLimit() || $this->hasOffset()) {
128
            $query->limit($this->getLimit(), $this->getOffset());
129
        }		
130
		
131
		return $db->fetchAll($query);
132
	}
133
	 
134
    public function getBaseQuery()
135
    {
136
		$db=$this->db();
137
		
138
		$query = $db->select()->from(
139
            $this->moduleConfig->getTrapTableName(),
140
            $this->moduleConfig->getTrapListDisplayColumns()
141
        )->order('timestamp DESC');
142
143
        return $query;
144
    }	 
145
	
146
	// ****************** Filters
147
148
	protected $filter_Handler;
149
	protected $filter_query='';
150
	protected $filter_done='';
151
	protected $filter_query_list=array('q','done');
152
	public function renderFilterHTML()
153
	{
154
		$htm=' <form id="filter" name="mainFilter" 
155
				enctype="application/x-www-form-urlencoded" 
156
				action="'.$this->filter_Handler.'" 
157
				method="get">';
158
		$htm.='<input type="text" name="q" title="Search is simple! Try to combine multiple words" 
159
		placeholder="Search..." class="search" value="'.$this->filter_query.'">';
160
		$htm.='<input type="checkbox" id="checkbox_done" name="done" value="1" class="autosubmit" ';
161
		if	($this->filter_done == 1) { $htm.=' checked ';}
162
		$htm.='> <label for="checkbox_done">Hide processed traps</label>';
163
		$htm.='</form>';
164
		return $htm;
165
	}
166
	
167
	public function updateFilter($handler,$filter)
168
	{
169
		$this->filter_Handler=$handler->remove($this->filter_query_list)->__toString();
170
		$this->filter_query=(isset($filter['q']))?$this->filter_query=$filter['q']:'';
171
		$this->filter_done=(isset($filter['done']))?$this->filter_done=$filter['done']:0;
172
	}
173
	
174
    protected function getSearchColumns()
175
    {
176
        return $this->getColumns();
177
    }
178
	
179
	public function getColumns()
180
	{
181
		return $this->moduleConfig->getTrapListDisplayColumns();
182
	}
183
184
    public function setFilter($filter)
185
    {
186
        $this->filter = $filter;
187
        return $this;
188
    }
189
	
190
	public function getFilterEditor(Request $request)
191
    {
192
        $filterEditor = Widget::create('filterEditor')
193
            ->setColumns(array_keys($this->getColumns()))
194
            ->setSearchColumns(array_keys($this->getSearchColumns()))
195
            ->preserveParams('limit', 'sort', 'dir', 'view', 'backend')
196
            ->ignoreParams('page')
197
            ->handleRequest($request);
198
199
        $filter = $filterEditor->getFilter();
200
        $this->setFilter($filter);
201
202
        return $filterEditor;
203
    }
204
	
205
    protected function applyFiltersToQuery($query)
206
    {
207
		
208
		$sql='';
209
		if ($this->filter_query != '')
210
		{
211
			$sql.='(';
212
			$first=1;
213
			foreach($this->moduleConfig->getTrapListSearchColumns() as $column)
214
			{
215
				if ($first==0) $sql.=' OR ';
216
				$first=0;
217
				$sql.=" CAST(".$column." AS  char(100))  LIKE  '%".$this->filter_query."%' ";
218
			}
219
			$sql.=')';			
220
		}
221
		if ($this->filter_done == 1)
222
		{
223
			if ($sql != '') $sql.=' AND ';
224
			$sql.="(status != 'done')";
225
		}
226
		if ($sql != '') $query->where($sql);	
227
        return $query;
228
    }	
229
230
}