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

TrapDirectorTable   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 87
c 2
b 0
f 1
dl 0
loc 192
rs 10
wmc 25

11 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 16 1
A __construct() 0 12 1
A fullQuery() 0 8 1
A renderTitles() 0 33 5
A renderLine() 0 11 2
B getCurrentURLAndQS() 0 17 9
A getBaseQuery() 0 9 1
A getParams() 0 5 1
A renderTable() 0 13 2
A titleOrder() 0 3 1
A getCurrentURL() 0 3 1
1
<?php
2
3
namespace Icinga\Module\Trapdirector\Tables;
4
5
6
abstract class TrapDirectorTable
7
{
8
    
9
    use TrapDirectorTableFilter;
10
    use TrapDirectorTablePaging;
11
    use TrapDirectorTableOrder;
12
    use TrapDirectorTableGrouping;
13
    
14
    /** @var array $titles table titles (name, display value) */
15
    protected $titles = null;
16
17
    /** @var array $content table content  (name, sb column name). name index must be the same as $titles*/
18
    protected $content = null;
19
    
20
    /** @var array $columnNames names of columns for filtering */
21
    protected $columnNames = array();
22
    
23
    /** @var mixed $dbConn connection to database  */
24
    protected $dbConn = null;
25
    
26
    /** Current view **/
27
    protected $view;
28
    
29
    protected $urlPath;
30
    
31
    // Database stuff
32
   /** @var array $table (db ref, name) */
33
    protected $table = array();
34
    
35
    /** @var mixed  $query Query in database; */
36
    protected $query = null;
37
    
38
    function __construct(array $table,array $titles, array $columns, array $columnNames, $dbConn , $view, $urlPath)
39
    {
40
        $this->table = $table;
41
        $this->titles = $titles;
42
        $this->content = $columns;
43
        $this->columnNames = $columnNames;
44
        $this->dbConn = $dbConn;
45
        
46
        $this->view = $view;
47
        $this->urlPath = $urlPath;
48
        
49
        return $this;
50
    }
51
52
    
53
    /************** GET variables and URLs *************/
54
    public function getParams(array $getVars)
55
    {
56
        $this->getFilterQuery($getVars);
57
        $this->getPagingQuery($getVars);
58
        $this->getOrderQuery($getVars);
59
    }
60
    
61
    public function getCurrentURL()
62
    {
63
        return '?';
64
    }
65
    
66
    protected function getCurrentURLAndQS(string $caller)
67
    {
68
        $actionURL = $this->getCurrentURL() . '?' ;
69
        $QSList=array();
70
        if ($caller != 'filter' && $this->curFilterQuery() != '')
71
            array_push($QSList , $this->curFilterQuery());
72
        
73
        if ($caller != 'paging' && $caller != 'filter' && $this->curPagingQuery() != '')
74
            array_push($QSList , $this->curPagingQuery());
75
 
76
        if ($caller != 'order' && $this->curOrderQuery() != '')
77
            array_push($QSList , $this->curOrderQuery());
78
        
79
        if (count($QSList) != 0)
80
            $actionURL .=  implode('&', $QSList) . '&';
81
        
82
        return $actionURL;
83
    }
84
    
85
    /************* DB queries ******************/
86
    /**
87
     * Get base query in $this->query
88
     * @return  TrapDirectorTable
89
     */
90
    public function getBaseQuery()
91
    {
92
        $this->query = $this->dbConn->select();
93
        $this->query = $this->query->from(
94
            $this->table,
95
            $this->content
96
            );
97
        
98
        return $this;
99
    }
100
    
101
    public function fullQuery()
102
    {
103
        $this->getBaseQuery()
104
        ->applyFilter()
105
        ->applyPaging()
106
        ->applyOrder();
107
        
108
        return $this->dbConn->fetchAll($this->query);
109
        //return $this->query->fetchAll();
110
    }
111
     
112
    /*************** Rendering *************************/
113
    
114
    public function titleOrder($name)
115
    {
116
        return $this->content[$name];
117
    }
118
    
119
    public function renderTitles()
120
    {
121
        $html = "<thead>\n<tr>\n";
122
        foreach ($this->titles as $name => $values)
123
        {
124
            $titleOrder = $this->titleOrder($name);  // TODO : put this as function of order trait
125
            if ($titleOrder != NULL)
126
            {
127
                if (isset($this->order[$titleOrder]))
128
                {
129
                    if ($this->order[$titleOrder] == 'ASC')
130
                    {
131
                        $titleOrder.='DESC';
132
                    }
133
                    else
134
                    {
135
                        $titleOrder.='ASC';
136
                    }
137
                }
138
                else 
139
                {
140
                    $titleOrder.='ASC';
141
                }
142
                $actionURL = $this->getCurrentURLAndQS('order').'o='.$titleOrder;
143
                $html .= '<th><a href="'.$actionURL.'">' . $values . '</a></th>';
144
            }
145
            else 
146
            {
147
                $html .= '<th>' . $values . '</th>';
148
            }          
149
        }
150
        $html .= "</tr>\n</thead>\n";
151
        return $html;
152
    }
153
    
154
    public function renderLine( $value)
155
    {
156
        $html = '';
157
        $titleNames = array_keys($this->titles);
158
        foreach ($titleNames as $name )
159
        {
160
            $html .= '<td>';
161
            $html .= $value->$name;
162
            $html .= "</td>\n";
163
        }
164
        return $html;
165
    }
166
    
167
    public function renderTable(array $values)
168
    {
169
       $html = '<tbody id="obj_table_body">';
170
       foreach($values as $value)
171
       {
172
           $html .= $this->groupingNextLine($value);
173
           
174
           $html .= "<tr>\n";
175
           $html .= $this->renderLine($value);
176
           $html .= "</tr>\n";
177
       }
178
       $html .= '</tbody>';
179
       return $html;
180
    }
181
    
182
    public function render()
183
    {
184
        $html = '';
185
        
186
        
187
        $values = $this->fullQuery();
188
        $this->initGrouping();
189
        
190
        $html.="<table class='simple common-table table-row-selectable'>\n";
191
        
192
        $html .= $this->renderTitles();
193
        $html .= $this->renderTable($values);
194
        $html .= '</table>'; 
195
        
196
197
        return $html;
198
    }
199
    
200
}