listform_actions.inc.php 13.8 KB
Newer Older
tbrehm's avatar
tbrehm committed
1 2 3
<?php

/*
redray's avatar
redray committed
4
Copyright (c) 2007, Till Brehm, projektfarm Gmbh
tbrehm's avatar
tbrehm committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of ISPConfig nor the names of its contributors
      may be used to endorse or promote products derived from this software without
      specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

class listform_actions {
	
	private $id;
	public $idx_key;
	public $DataRowColor;
	public  $SQLExtWhere = '';
	public  $SQLOrderBy = '';
	
	public function onLoad()
    {
		global $app, $conf, $list_def_file;
		
		$app->uses('tpl,listform,tform');
		
		//* Clear session variable that is used when lists are embedded with the listview plugin
		$_SESSION['s']['form']['return_to'] = '';
		
		// Load list definition
		$app->listform->loadListDef($list_def_file);
		
		if(!is_file('templates/'.$app->listform->listDef["name"].'_list.htm')) {
			$app->uses('listform_tpl_generator');
			$app->listform_tpl_generator->buildHTML($app->listform->listDef);
		}
		
		$app->tpl->newTemplate("listpage.tpl.htm");
		$app->tpl->setInclude('content_tpl','templates/'.$app->listform->listDef["name"].'_list.htm');
58 59 60
		
		//* Manipulate order by for sorting / Every list has a stored value
		//* Against notice error
61 62
		if(!isset($_SESSION['search'][$app->listform->listDef["name"].$app->listform->listDef['table']]['order'])){
		  $_SESSION['search'][$app->listform->listDef["name"].$app->listform->listDef['table']]['order'] = '';
63 64 65 66 67 68
		}

		if(!empty($_GET['orderby'])){
		  $order = str_replace('tbl_col_','',$_GET['orderby']);
		  //* Check the css class submited value
		  if (preg_match("/^[a-z\_]{1,}$/",$order)) {
69 70
		    if($_SESSION['search'][$app->listform->listDef["name"].$app->listform->listDef['table']]['order'] == $order){
		      $_SESSION['search'][$app->listform->listDef["name"].$app->listform->listDef['table']]['order'] = $order.' DESC';
71
		    } else {
72
		      $_SESSION['search'][$app->listform->listDef["name"].$app->listform->listDef['table']]['order'] = $order;
73 74 75
		    }
		  }
		}
tbrehm's avatar
tbrehm committed
76

77
		// If a manuel oder by like customers isset the sorting will be infront
78
		if(!empty($_SESSION['search'][$app->listform->listDef["name"].$app->listform->listDef['table']]['order'])){
79
		  if(empty($this->SQLOrderBy)){
80
		    $this->SQLOrderBy = "ORDER BY ".$_SESSION['search'][$app->listform->listDef["name"].$app->listform->listDef['table']]['order'];
81
		  } else {
82
		    $this->SQLOrderBy = str_replace("ORDER BY ","ORDER BY ".$_SESSION['search'][$app->listform->listDef["name"].$app->listform->listDef['table']]['order'].', ',$this->SQLOrderBy);
83 84 85
		  }
		}
		
tbrehm's avatar
tbrehm committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
		// Getting Datasets from DB
		$records = $app->db->queryAllRecords($this->getQueryString());

		$this->DataRowColor = "#FFFFFF";
		$records_new = '';
		if(is_array($records)) {
			$this->idx_key = $app->listform->listDef["table_idx"]; 
			foreach($records as $rec) {
				$records_new[] = $this->prepareDataRow($rec);
			}
		}

		$app->tpl->setLoop('records',$records_new);

		$this->onShow();
		
		
	}
	
	public function prepareDataRow($rec)
    {
		global $app;
		
		$rec = $app->listform->decode($rec);

		//* Alternating datarow colors
		$this->DataRowColor = ($this->DataRowColor == '#FFFFFF') ? '#EEEEEE' : '#FFFFFF';
		$rec['bgcolor'] = $this->DataRowColor;
		
		//* substitute value for select fields
116 117 118 119 120 121 122 123 124 125
		if(is_array($app->listform->listDef['item']) && count($app->listform->listDef['item']) > 0) {
			foreach($app->listform->listDef['item'] as $field) {
				$key = $field['field'];
				if(isset($field['formtype']) && $field['formtype'] == 'SELECT') {
					if(strtolower($rec[$key]) == 'y' or strtolower($rec[$key]) == 'n') {
						// Set a additional image variable for bolean fields
						$rec['_'.$key.'_'] = (strtolower($rec[$key]) == 'y')?'x16/tick_circle.png':'x16/cross_circle.png';
					}
					//* substitute value for select field
					$rec[$key] = @$field['value'][$rec[$key]];
tbrehm's avatar
tbrehm committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
				}
			}
		}
		
		//* The variable "id" contains always the index variable
		$rec['id'] = $rec[$this->idx_key];
		return $rec;
	}
	
	private function getQueryString() {
		global $app;
		$sql_where = '';

		//* Generate the search sql
		if($app->listform->listDef['auth'] != 'no') {
			if($_SESSION['s']['user']['typ'] == "admin") {
				$sql_where = '';
			} else {
				$sql_where = $app->tform->getAuthSQL('r').' and';
			}
		}		
		if($this->SQLExtWhere != '') {
			$sql_where .= ' '.$this->SQLExtWhere.' and';
		}

		$sql_where = $app->listform->getSearchSQL($sql_where);
		$app->tpl->setVar($app->listform->searchValues);
		
		$order_by_sql = $this->SQLOrderBy;

		//* Generate SQL for paging
		$limit_sql = $app->listform->getPagingSQL($sql_where);
		$app->tpl->setVar('paging',$app->listform->pagingHTML);

160 161
		$extselect = '';
		$join = '';
162 163
		if(!empty($_SESSION['search'][$app->listform->listDef["name"].$app->listform->listDef['table']]['order'])){
		  $order = str_replace(' DESC','',$_SESSION['search'][$app->listform->listDef["name"].$app->listform->listDef['table']]['order']);
164 165
		  if($order == 'server_id' && $app->listform->listDef['table'] != 'server'){
		    $join .= ' LEFT JOIN server as s ON '.$app->listform->listDef['table'].'.server_id = s.server_id ';
166
		    $order_by_sql = str_replace('server_id','s.server_name',$order_by_sql);
167 168
		  } elseif($order == 'client_id' && $app->listform->listDef['table'] != 'client'){
		    $join .= ' LEFT JOIN client as c ON '.$app->listform->listDef['table'].'.client_id = c.client_id ';
169
		    $order_by_sql = str_replace('client_id','c.contact_name',$order_by_sql);
170 171 172 173 174 175
		  } elseif($order == 'parent_domain_id'){
		    $join .= ' LEFT JOIN web_domain as wd ON '.$app->listform->listDef['table'].'.parent_domain_id = wd.domain_id ';
		    $order_by_sql = str_replace('parent_domain_id','wd.domain',$order_by_sql);
		    $sql_where = str_replace('type',$app->listform->listDef['table'].'.type',$sql_where);
		  } elseif($order == 'sys_groupid'){
		    $join .= ' LEFT JOIN sys_group as sg ON '.$app->listform->listDef['table'].'.sys_groupid = sg.groupid ';
176
		    $order_by_sql = str_replace('sys_groupid','sg.name',$order_by_sql);
177 178
		  } elseif($order == 'rid'){
		    $join .= ' LEFT JOIN spamfilter_users as su ON '.$app->listform->listDef['table'].'.rid = su.id ';
179
		    $order_by_sql = str_replace('rid','su.email',$order_by_sql);
180 181
		  } elseif($order == 'policy_id'){
		    $join .= ' LEFT JOIN spamfilter_policy as sp ON '.$app->listform->listDef['table'].'.policy_id = sp.id ';
182
		    $order_by_sql = str_replace('policy_id','sp.policy_name',$order_by_sql);
183 184
		  } elseif($order == 'web_folder_id'){
		    $join .= ' LEFT JOIN web_folder as wf ON '.$app->listform->listDef['table'].'.web_folder_id = wf.web_folder_id ';
185
		    $order_by_sql = str_replace('web_folder_id','wf.path',$order_by_sql);
186 187
		  } elseif($order == 'ostemplate_id' && $app->listform->listDef['table'] != 'openvz_ostemplate'){
		    $join .= ' LEFT JOIN openvz_ostemplate as oo ON '.$app->listform->listDef['table'].'.ostemplate_id = oo.ostemplate_id ';
188
		    $order_by_sql = str_replace('ostemplate_id','oo.template_name',$order_by_sql);
189 190
		  } elseif($order == 'template_id' && $app->listform->listDef['table'] != 'openvz_template'){
		    $join .= ' LEFT JOIN openvz_template as ot ON '.$app->listform->listDef['table'].'.template_id = ot.template_id ';
191
		    $order_by_sql = str_replace('template_id','ot.template_name',$order_by_sql);
192 193
		  } elseif($order == 'sender_id' && $app->listform->listDef['table'] != 'sys_user'){
		    $join .= ' LEFT JOIN sys_user as su ON '.$app->listform->listDef['table'].'.sender_id = su.userid ';
194
		    $order_by_sql = str_replace('sender_id','su.username',$order_by_sql);
195 196 197 198
		  } elseif($order == 'web_traffic_last_month'){
		    $tmp_year = date('Y',mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
		    $tmp_month = date('m',mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
		    $extselect .= ', SUM(wt.traffic_bytes) as calctraffic';
199
		    $join .= ' INNER JOIN web_traffic as wt ON '.$app->listform->listDef['table'].'.domain = wt.hostname ';
200 201 202 203 204 205 206
		    $sql_where .= " AND YEAR(wt.traffic_date) = '$tmp_year' AND MONTH(wt.traffic_date) = '$tmp_month'";
		    $order_by_sql = str_replace('web_traffic_last_month','calctraffic',$order_by_sql);
		    $order_by_sql = "GROUP BY domain ".$order_by_sql;
		  } elseif($order == 'web_traffic_this_month'){
		    $tmp_year = date('Y');
		    $tmp_month = date('m');
		    $extselect .= ', SUM(wt.traffic_bytes) as calctraffic';
207
		    $join .= ' INNER JOIN web_traffic as wt ON '.$app->listform->listDef['table'].'.domain = wt.hostname ';
208 209 210 211 212 213
		    $sql_where .= " AND YEAR(wt.traffic_date) = '$tmp_year' AND MONTH(wt.traffic_date) = '$tmp_month'";
		    $order_by_sql = str_replace('web_traffic_this_month','calctraffic',$order_by_sql);
		    $order_by_sql = "GROUP BY domain ".$order_by_sql;
		  } elseif($order == 'web_traffic_last_year'){
		    $tmp_year = date('Y',mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
		    $extselect .= ', SUM(wt.traffic_bytes) as calctraffic';
214
		    $join .= ' INNER JOIN web_traffic as wt ON '.$app->listform->listDef['table'].'.domain = wt.hostname ';
215 216 217 218 219 220
		    $sql_where .= " AND YEAR(wt.traffic_date) = '$tmp_year'";
		    $order_by_sql = str_replace('web_traffic_last_year','calctraffic',$order_by_sql);
		    $order_by_sql = "GROUP BY domain ".$order_by_sql;
		  } elseif($order == 'web_traffic_this_year'){
		    $tmp_year = date('Y');
		    $extselect .= ', SUM(wt.traffic_bytes) as calctraffic';
221
		    $join .= ' INNER JOIN web_traffic as wt ON '.$app->listform->listDef['table'].'.domain = wt.hostname ';
222 223 224 225 226
		    $sql_where .= " AND YEAR(wt.traffic_date) = '$tmp_year'";
		    $order_by_sql = str_replace('web_traffic_this_year','calctraffic',$order_by_sql);
		    $order_by_sql = "GROUP BY domain ".$order_by_sql;
		  } elseif($order == 'mail_traffic_last_month'){
		    $tmp_date = date('Y-m',mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
227
		    $join .= ' INNER JOIN mail_traffic as mt ON '.$app->listform->listDef['table'].'.mailuser_id = mt.mailuser_id ';
228 229 230 231
		    $sql_where .= " AND mt.month like '$tmp_date%'";
		    $order_by_sql = str_replace('mail_traffic_last_month','traffic',$order_by_sql);
		  } elseif($order == 'mail_traffic_this_month'){
		    $tmp_date = date('Y-m');
232
		    $join .= ' INNER JOIN mail_traffic as mt ON '.$app->listform->listDef['table'].'.mailuser_id = mt.mailuser_id ';
233 234 235 236 237
		    $sql_where .= " AND mt.month like '$tmp_date%'";
		    $order_by_sql = str_replace('mail_traffic_this_month','traffic',$order_by_sql);
		  } elseif($order == 'mail_traffic_last_year'){
		    $tmp_date = date('Y',mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
		    $extselect .= ', SUM(mt.traffic) as calctraffic';
238
		    $join .= ' INNER JOIN mail_traffic as mt ON '.$app->listform->listDef['table'].'.mailuser_id = mt.mailuser_id ';
239 240 241 242 243 244
		    $sql_where .= " AND mt.month like '$tmp_date%'";;
		    $order_by_sql = str_replace('mail_traffic_last_year','calctraffic',$order_by_sql);
		    $order_by_sql = "GROUP BY mailuser_id ".$order_by_sql;
		  } elseif($order == 'mail_traffic_this_year'){
		    $tmp_date = date('Y');
		    $extselect .= ', SUM(mt.traffic) as calctraffic';
245
		    $join .= ' INNER JOIN mail_traffic as mt ON '.$app->listform->listDef['table'].'.mailuser_id = mt.mailuser_id ';
246 247 248 249 250 251
		    $sql_where .= " AND mt.month like '$tmp_date%'";
		    $order_by_sql = str_replace('mail_traffic_this_year','calctraffic',$order_by_sql);
		    $order_by_sql = "GROUP BY mailuser_id ".$order_by_sql;
		  }
		}
		return 'SELECT '.$app->listform->listDef['table'].'.*'.$extselect.' FROM '.$app->listform->listDef['table']."$join WHERE $sql_where $order_by_sql $limit_sql";
tbrehm's avatar
tbrehm committed
252 253 254
	}
	
	
255
	public function onShow()
tbrehm's avatar
tbrehm committed
256 257 258 259 260 261 262 263 264 265
    {
		global $app;
		
		//* Set global Language File
		$lng_file = ISPC_LIB_PATH.'/lang/'.$_SESSION['s']['language'].'.lng';
		if(!file_exists($lng_file))
		$lng_file = ISPC_LIB_PATH.'/lang/en.lng';
		include($lng_file);
		$app->tpl->setVar($wb);
		
266 267 268 269
		//* Limit each page
		$limits = array('5'=>'5','15'=>'15','25'=>'25','50'=>'50','100'=>'100','999999999' => 'all');

		//* create options and set selected, if default -> 15 is selected
xaver's avatar
xaver committed
270 271

		$options = '';
272 273 274
		foreach($limits as $key => $val){
		  $options .= '<option value="'.$key.'" '.(isset($_SESSION['search']['limit']) &&  $_SESSION['search']['limit'] == $key ? 'selected="selected"':'' ).(!isset($_SESSION['search']['limit']) && $key == '15' ? 'selected="selected"':'').'>'.$val.'</option>';
		}
275
		$app->tpl->setVar('search_limit','<select name="search_limit" class="search_limit">'.$options.'</select>');
276
		
277
		$app->tpl->setVar('toolsarea_head_txt',$app->lng('toolsarea_head_txt'));
tbrehm's avatar
tbrehm committed
278 279 280 281 282 283 284
		$app->tpl->setVar($app->listform->wordbook);
		$app->tpl->setVar('form_action', $app->listform->listDef['file']);
		
		//* Parse the templates and send output to the browser
		$this->onShowEnd();
	}
	
285
	public function onShowEnd()
tbrehm's avatar
tbrehm committed
286 287 288 289 290 291 292
    {
		global $app;
		$app->tpl_defaults();
		$app->tpl->pparse();
	}
}

tbrehm's avatar
tbrehm committed
293
?>