Marius Cramer
2015-08-06 37b29231e47a0c4458dc1c15d98588f16f07e1e2
commit | author | age
fb02f0 1 <?php
T 2
3 /*
436ed8 4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
fb02f0 5 All rights reserved.
T 6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 class listform_actions {
b1a6a5 32
fb02f0 33     private $id;
T 34     public $idx_key;
35     public $DataRowColor;
656a1c 36     public $SQLExtWhere = '';
F 37     public $SQLOrderBy = '';
38     public $SQLExtSelect = '';
615a0a 39     private $sortKeys;
b1a6a5 40
MC 41     private function _sort($aOne, $aTwo) {
42         if(!is_array($aOne) || !is_array($aTwo)) return 0;
43
44         if(!is_array($this->sortKeys)) $this->sortKeys = array($this->sortKeys);
45         foreach($this->sortKeys as $sKey => $sDir) {
46             if(is_numeric($sKey)) {
47                 $sKey = $sDir;
48                 $sDir = 'ASC';
49             }
50             $a = $aOne[$sKey];
51             $b = $aTwo[$sKey];
52             if(is_string($a)) $a = strtolower($a);
53             if(is_string($b)) $b = strtolower($b);
54             if($a < $b) return $sDir == 'DESC' ? 1 : -1;
55             elseif($a > $b) return $sDir == 'DESC' ? -1 : 1;
56         }
57         return 0;
58     }
59
fb02f0 60     public function onLoad()
b1a6a5 61     {
fb02f0 62         global $app, $conf, $list_def_file;
b1a6a5 63
fb02f0 64         $app->uses('tpl,listform,tform');
b1a6a5 65
fb02f0 66         //* Clear session variable that is used when lists are embedded with the listview plugin
T 67         $_SESSION['s']['form']['return_to'] = '';
b1a6a5 68
fb02f0 69         // Load list definition
T 70         $app->listform->loadListDef($list_def_file);
b1a6a5 71
fb02f0 72         if(!is_file('templates/'.$app->listform->listDef["name"].'_list.htm')) {
T 73             $app->uses('listform_tpl_generator');
74             $app->listform_tpl_generator->buildHTML($app->listform->listDef);
75         }
b1a6a5 76
fb02f0 77         $app->tpl->newTemplate("listpage.tpl.htm");
b1a6a5 78         $app->tpl->setInclude('content_tpl', 'templates/'.$app->listform->listDef["name"].'_list.htm');
MC 79
8145fb 80         //* Manipulate order by for sorting / Every list has a stored value
X 81         //* Against notice error
615a0a 82         if(!isset($_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order'])){
b1a6a5 83             $_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order'] = '';
8145fb 84         }
b1a6a5 85
MC 86         $php_sort = false;
87
8145fb 88         if(!empty($_GET['orderby'])){
b1a6a5 89             $order = str_replace('tbl_col_', '', $_GET['orderby']);
MC 90
91             //* Check the css class submited value
92             if (preg_match("/^[a-z\_]{1,}$/", $order)) {
93
94                 if(isset($app->listform->listDef['phpsort']) && is_array($app->listform->listDef['phpsort']) && in_array($order, $app->listform->listDef['phpsort'])) {
95                     $php_sort = true;
96                 } else {
97                     // prepend correct table
98                     $prepend_table = $app->listform->listDef['table'];
99                     if(trim($app->listform->listDef['additional_tables']) != '' && is_array($app->listform->listDef['item']) && count($app->listform->listDef['item']) > 0) {
100                         foreach($app->listform->listDef['item'] as $field) {
101                             if($field['field'] == $order && $field['table'] != ''){
102                                 $prepend_table = $field['table'];
103                                 break;
104                             }
105                         }
106                     }
107                     $order = $prepend_table.'.'.$order;
108                 }
109
110                 if($_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order'] == $order){
111                     $_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order'] = $order.' DESC';
112                 } else {
113                     $_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order'] = $order;
114                 }
115                 $_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order_in_php'] = $php_sort;
116             }
8145fb 117         }
X 118
119         // If a manuel oder by like customers isset the sorting will be infront
615a0a 120         if(!empty($_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order']) && !$_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order_in_php']){
b1a6a5 121             if(empty($this->SQLOrderBy)){
MC 122                 $this->SQLOrderBy = "ORDER BY ".$_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order'];
123             } else {
124                 $this->SQLOrderBy = str_replace("ORDER BY ", "ORDER BY ".$_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order'].', ', $this->SQLOrderBy);
125             }
8145fb 126         }
b1a6a5 127
7d52e0 128         if($_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order_in_php']) $php_sort = true;
b1a6a5 129
fb02f0 130         // Getting Datasets from DB
202df8 131         $records = $app->db->queryAllRecords($this->getQueryString($php_sort));
fb02f0 132
T 133         $this->DataRowColor = "#FFFFFF";
134         $records_new = '';
135         if(is_array($records)) {
b1a6a5 136             $this->idx_key = $app->listform->listDef["table_idx"];
fb02f0 137             foreach($records as $rec) {
T 138                 $records_new[] = $this->prepareDataRow($rec);
139             }
140         }
b1a6a5 141
MC 142         if(!empty($_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order']) && $_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order_in_php']) {
143             $order_by = $_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order'];
144             $order_dir = 'ASC';
145             if(substr($order_by, -5) === ' DESC') {
146                 $order_by = substr($order_by, 0, -5);
147                 $order_dir = 'DESC';
148             }
149             $this->sortKeys = array($order_by => $order_dir);
150             uasort($records_new, array($this, '_sort'));
151         }
152         if($php_sort) {
202df8 153             $records_new = array_slice($records_new, $app->listform->getPagingValue('offset'), $app->listform->getPagingValue('records_per_page'));
MC 154         }
b1a6a5 155
MC 156         $app->tpl->setLoop('records', $records_new);
fb02f0 157
T 158         $this->onShow();
b1a6a5 159
MC 160
fb02f0 161     }
b1a6a5 162
fb02f0 163     public function prepareDataRow($rec)
b1a6a5 164     {
fb02f0 165         global $app;
b1a6a5 166
fb02f0 167         $rec = $app->listform->decode($rec);
T 168
169         //* Alternating datarow colors
170         $this->DataRowColor = ($this->DataRowColor == '#FFFFFF') ? '#EEEEEE' : '#FFFFFF';
171         $rec['bgcolor'] = $this->DataRowColor;
b1a6a5 172
fb02f0 173         //* substitute value for select fields
8ab3cd 174         if(is_array($app->listform->listDef['item']) && count($app->listform->listDef['item']) > 0) {
T 175             foreach($app->listform->listDef['item'] as $field) {
176                 $key = $field['field'];
177                 if(isset($field['formtype']) && $field['formtype'] == 'SELECT') {
178                     if(strtolower($rec[$key]) == 'y' or strtolower($rec[$key]) == 'n') {
179                         // Set a additional image variable for bolean fields
180                         $rec['_'.$key.'_'] = (strtolower($rec[$key]) == 'y')?'x16/tick_circle.png':'x16/cross_circle.png';
181                     }
182                     //* substitute value for select field
183                     $rec[$key] = @$field['value'][$rec[$key]];
fb02f0 184                 }
T 185             }
186         }
b1a6a5 187
fb02f0 188         //* The variable "id" contains always the index variable
T 189         $rec['id'] = $rec[$this->idx_key];
190         return $rec;
191     }
b1a6a5 192
2af58c 193     /* TODO: maybe rewrite SQL */
202df8 194     public function getQueryString($no_limit = false) {
fb02f0 195         global $app;
T 196         $sql_where = '';
197
198         //* Generate the search sql
199         if($app->listform->listDef['auth'] != 'no') {
200             if($_SESSION['s']['user']['typ'] == "admin") {
201                 $sql_where = '';
202             } else {
b1a6a5 203                 $sql_where = $app->tform->getAuthSQL('r', $app->listform->listDef['table']).' and';
MC 204                 //$sql_where = $app->tform->getAuthSQL('r').' and';
fb02f0 205             }
b1a6a5 206         }
fb02f0 207         if($this->SQLExtWhere != '') {
T 208             $sql_where .= ' '.$this->SQLExtWhere.' and';
209         }
b1a6a5 210
fb02f0 211         $sql_where = $app->listform->getSearchSQL($sql_where);
615a0a 212         if($app->listform->listDef['join_sql']) $sql_where .= ' AND '.$app->listform->listDef['join_sql'];
fb02f0 213         $app->tpl->setVar($app->listform->searchValues);
b1a6a5 214
fb02f0 215         $order_by_sql = $this->SQLOrderBy;
T 216
217         //* Generate SQL for paging
218         $limit_sql = $app->listform->getPagingSQL($sql_where);
b1a6a5 219         $app->tpl->setVar('paging', $app->listform->pagingHTML);
fb02f0 220
8145fb 221         $extselect = '';
X 222         $join = '';
b1a6a5 223
8f9240 224         if($this->SQLExtSelect != '') {
b1a6a5 225             if(substr($this->SQLExtSelect, 0, 1) != ',') $this->SQLExtSelect = ','.$this->SQLExtSelect;
8f9240 226             $extselect .= $this->SQLExtSelect;
F 227         }
b1a6a5 228
615a0a 229         $table_selects = array();
T 230         $table_selects[] = trim($app->listform->listDef['table']).'.*';
231         $app->listform->listDef['additional_tables'] = trim($app->listform->listDef['additional_tables']);
232         if($app->listform->listDef['additional_tables'] != ''){
233             $additional_tables = explode(',', $app->listform->listDef['additional_tables']);
234             foreach($additional_tables as $additional_table){
235                 $table_selects[] = trim($additional_table).'.*';
236             }
237         }
238         $select = implode(', ', $table_selects);
8f9240 239
202df8 240         $sql = 'SELECT '.$select.$extselect.' FROM '.$app->listform->listDef['table'].($app->listform->listDef['additional_tables'] != ''? ','.$app->listform->listDef['additional_tables'] : '')."$join WHERE $sql_where $order_by_sql";
MC 241         if($no_limit == false) $sql .= " $limit_sql";
615a0a 242         //echo $sql;
T 243         return $sql;
fb02f0 244     }
b1a6a5 245
MC 246
d8b8b0 247     public function onShow()
b1a6a5 248     {
fb02f0 249         global $app;
b1a6a5 250
fb02f0 251         //* Set global Language File
T 252         $lng_file = ISPC_LIB_PATH.'/lang/'.$_SESSION['s']['language'].'.lng';
253         if(!file_exists($lng_file))
b1a6a5 254             $lng_file = ISPC_LIB_PATH.'/lang/en.lng';
MC 255         include $lng_file;
fb02f0 256         $app->tpl->setVar($wb);
b1a6a5 257
8145fb 258         //* Limit each page
b1a6a5 259         $limits = array('5'=>'5', '15'=>'15', '25'=>'25', '50'=>'50', '100'=>'100', '999999999' => 'all');
8145fb 260
X 261         //* create options and set selected, if default -> 15 is selected
b9ce1a 262
X 263         $options = '';
8145fb 264         foreach($limits as $key => $val){
b1a6a5 265             $options .= '<option value="'.$key.'" '.(isset($_SESSION['search']['limit']) &&  $_SESSION['search']['limit'] == $key ? 'selected="selected"':'' ).(!isset($_SESSION['search']['limit']) && $key == '15' ? 'selected="selected"':'').'>'.$val.'</option>';
8145fb 266         }
b1a6a5 267         $app->tpl->setVar('search_limit', '<select name="search_limit" class="search_limit">'.$options.'</select>');
MC 268
269         $app->tpl->setVar('toolsarea_head_txt', $app->lng('toolsarea_head_txt'));
fb02f0 270         $app->tpl->setVar($app->listform->wordbook);
T 271         $app->tpl->setVar('form_action', $app->listform->listDef['file']);
b1a6a5 272
MC 273         if(isset($_SESSION['show_info_msg'])) {
274             $app->tpl->setVar('show_info_msg', $_SESSION['show_info_msg']);
275             unset($_SESSION['show_info_msg']);
276         }
277         if(isset($_SESSION['show_error_msg'])) {
278             $app->tpl->setVar('show_error_msg', $_SESSION['show_error_msg']);
279             unset($_SESSION['show_error_msg']);
280         }
281
fb02f0 282         //* Parse the templates and send output to the browser
T 283         $this->onShowEnd();
284     }
b1a6a5 285
6ac26d 286     public function onShowEnd()
b1a6a5 287     {
fb02f0 288         global $app;
T 289         $app->tpl_defaults();
290         $app->tpl->pparse();
291     }
b1a6a5 292
fb02f0 293 }
T 294
b1a6a5 295 ?>