commit | author | age
|
8500be
|
1 |
<?php |
T |
2 |
|
|
3 |
/* |
436ed8
|
4 |
Copyright (c) 2007, Till Brehm, projektfarm Gmbh |
8500be
|
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 searchform { |
|
32 |
|
|
33 |
public $debug = 0; |
|
34 |
public $errorMessage; |
|
35 |
public $listDef; |
|
36 |
public $searchValues; |
|
37 |
public $pagingHTML; |
|
38 |
public $pagingValues; |
|
39 |
public $searchChanged = 0; |
|
40 |
public $module; |
|
41 |
|
|
42 |
public function loadListDef($file, $module = '') |
|
43 |
{ |
|
44 |
global $app, $conf; |
|
45 |
if(!is_file($file)){ |
|
46 |
die("List-Definition: $file not found."); |
|
47 |
} |
|
48 |
include_once($file); |
|
49 |
$this->listDef = $liste; |
|
50 |
$this->module = $module; |
|
51 |
|
|
52 |
//* Fill datasources |
|
53 |
foreach($this->listDef['item'] as $key => $field) { |
|
54 |
if(is_array($field['datasource'])) { |
|
55 |
$this->listDef['item'][$key]['value'] = $this->getDatasourceData($field); |
|
56 |
} |
|
57 |
} |
|
58 |
return true; |
|
59 |
} |
|
60 |
|
|
61 |
/** |
|
62 |
* Get the key => value array of a form filed from a datasource definitiom |
|
63 |
* |
|
64 |
* @param field = array with field definition |
|
65 |
* @param record = Dataset as array |
|
66 |
* @return key => value array for the value field of a form |
|
67 |
*/ |
|
68 |
|
|
69 |
public function getDatasourceData($field) |
|
70 |
{ |
|
71 |
global $app; |
|
72 |
$values = array(); |
|
73 |
|
|
74 |
if($field['datasource']['type'] == 'SQL') { |
|
75 |
//* Preparing SQL string. We will replace some common placeholders |
|
76 |
$querystring = $field['datasource']['querystring']; |
|
77 |
$querystring = str_replace('{USERID}', $_SESSION['s']['user']['userid'], $querystring); |
|
78 |
$querystring = str_replace('{GROUPID}', $_SESSION['s']['user']['default_group'], $querystring); |
|
79 |
$querystring = str_replace('{GROUPS}', $_SESSION['s']['user']['groups'], $querystring); |
|
80 |
$table_idx = $this->formDef['db_table_idx']; |
|
81 |
//$querystring = str_replace('{RECORDID}',$record[$table_idx],$querystring); |
|
82 |
$app->uses('tform'); |
|
83 |
$querystring = str_replace('{AUTHSQL}', $app->tform->getAuthSQL('r'), $querystring); |
|
84 |
|
|
85 |
//* Getting the records |
|
86 |
$tmp_records = $app->db->queryAllRecords($querystring); |
|
87 |
if($app->db->errorMessage != ''){ |
|
88 |
die($app->db->errorMessage); |
|
89 |
} |
|
90 |
if(is_array($tmp_records)) { |
|
91 |
$key_field = $field['datasource']['keyfield']; |
|
92 |
$value_field = $field['datasource']['valuefield']; |
|
93 |
foreach($tmp_records as $tmp_rec) { |
|
94 |
$values[$tmp_rec[$key_field]] = $tmp_rec[$value_field]; |
|
95 |
} |
|
96 |
} |
|
97 |
} |
|
98 |
if($field['datasource']['type'] == 'CUSTOM') { |
|
99 |
//* Calls a custom class to validate this record |
|
100 |
if($field['datasource']['class'] != '' and $field['datasource']['function'] != '') { |
|
101 |
$datasource_class = $field['datasource']['class']; |
|
102 |
$datasource_function = $field['datasource']['function']; |
|
103 |
$app->uses($datasource_class); |
|
104 |
$record = array(); |
|
105 |
$values = $app->$datasource_class->$datasource_function($field, $record); |
|
106 |
}else{ |
|
107 |
$this->errorMessage .= "Custom datasource class or function is empty<br>\r\n"; |
|
108 |
} |
|
109 |
} |
|
110 |
return $values; |
|
111 |
} |
|
112 |
|
|
113 |
public function getSearchSQL($sql_where = '') |
|
114 |
{ |
|
115 |
global $db; |
|
116 |
|
|
117 |
//* Config vars |
|
118 |
$list_name = $this->listDef['name']; |
|
119 |
$search_prefix = $this->listDef['search_prefix']; |
|
120 |
|
|
121 |
//* store retrieval query |
|
122 |
foreach($this->listDef['item'] as $i) { |
|
123 |
$field = $i['field']; |
|
124 |
|
|
125 |
//* TODO ? hat sich die suche ge�ndert - has itself search ? |
|
126 |
$ki = $search_prefix.$field; |
|
127 |
if(isset($_REQUEST) and $_REQUEST[$ki] != $_SESSION['search'][$list_name][$ki]){ |
|
128 |
$this->searchChanged = 1; |
|
129 |
} |
|
130 |
|
|
131 |
//* suchfield in session store. |
|
132 |
if(isset($_REQUEST[$ki])){ |
|
133 |
$_SESSION['search'][$list_name][$ki] = $_REQUEST[$ki]; |
|
134 |
} |
|
135 |
|
|
136 |
if($i['formtype'] == 'SELECT'){ |
|
137 |
if(is_array($i['value'])) { |
|
138 |
$out = '<option value=""></option>'; |
|
139 |
foreach($i['value'] as $k => $v) { |
|
140 |
$selected = ($k == $_SESSION['search'][$list_name][$ki] && $_SESSION['search'][$list_name][$ki] != '') ? ' SELECTED' : ''; |
|
141 |
$out .= "<option value='$k'$selected>$v</option>\r\n"; |
|
142 |
} |
|
143 |
} |
|
144 |
$this->searchValues[$ki] = $out; |
|
145 |
}else{ |
|
146 |
$this->searchValues[$ki] = $_SESSION['search'][$list_name][$ki]; |
|
147 |
} |
|
148 |
} |
|
149 |
|
|
150 |
//* store variables in object. $this->searchValues = $_SESSION["search"][$list_name]; |
|
151 |
foreach($this->listDef['item'] as $i) { |
|
152 |
$field = $i['field']; |
|
153 |
//if($_REQUEST[$search_prefix.$field] != '') $sql_where .= " $field ".$i["op"]." '".$i["prefix"].$_REQUEST[$search_prefix.$field].$i["suffix"]."' and"; |
|
154 |
if($_SESSION['search'][$list_name][$ki] != ''){ |
|
155 |
$sql_where .= " $field ".$i['op']." '".$i['prefix'].$_SESSION['search'][$list_name][$ki].$i['suffix']."' and"; |
|
156 |
} |
|
157 |
} |
|
158 |
return ($sql_where != '') ? substr($sql_where, 0, -3) : '1'; |
|
159 |
} |
|
160 |
|
|
161 |
public function getPagingSQL($sql_where = '1') { |
|
162 |
global $app, $conf; |
|
163 |
|
|
164 |
$list_name = $this->listDef['name']; |
|
165 |
$search_prefix = $this->listDef['search_prefix']; |
|
166 |
$records_per_page = $this->listDef['records_per_page']; |
|
167 |
$table = $this->listDef['table']; |
|
168 |
|
|
169 |
//* set page to seror id session not set |
|
170 |
if($_SESSION['search'][$list_name]['page'] == '') $_SESSION['search'][$list_name]['page'] = 0; |
|
171 |
|
|
172 |
//* Set page size to request if set |
|
173 |
if(isset($_REQUEST['page'])) $_SESSION['search'][$list_name]['page'] = $_REQUEST['page']; |
|
174 |
|
|
175 |
//* TODO PAGE to 0 set, if look for themselves ge?ndert. = page auf 0 setzen, wenn suche sich ge�ndert hat. |
|
176 |
if($this->searchChanged == 1) $_SESSION['search'][$list_name]['page'] = 0; |
|
177 |
|
|
178 |
$sql_von = $_SESSION['search'][$list_name]['page'] * $records_per_page; |
|
179 |
$record_count = $app->db->queryOneRecord("SELECT count(*) AS anzahl FROM $table WHERE $sql_where"); |
|
180 |
$pages = intval(($record_count['anzahl'] - 1) / $records_per_page); |
|
181 |
|
|
182 |
$vars['list_file'] = $this->listDef['file']; |
|
183 |
$vars['page'] = $_SESSION['search'][$list_name]['page']; |
|
184 |
$vars['last_page'] = $_SESSION['search'][$list_name]['page'] - 1; |
|
185 |
$vars['next_page'] = $_SESSION['search'][$list_name]['page'] + 1; |
|
186 |
$vars['pages'] = $pages; |
|
187 |
$vars['max_pages'] = $pages + 1; |
|
188 |
$vars['records_gesamt'] = $record_count['anzahl']; |
|
189 |
$vars['page_params'] = $this->listDef['page_params']; |
|
190 |
|
|
191 |
if($_SESSION['search'][$list_name]['page'] > 0) $vars['show_page_back'] = 1; |
|
192 |
if($_SESSION['search'][$list_name]['page'] <= $vars['pages'] - 1) $vars['show_page_next'] = 1; |
|
193 |
|
|
194 |
$this->pagingValues = $vars; |
|
195 |
$this->pagingHTML = $this->getPagingHTML($vars); |
|
196 |
|
|
197 |
return "LIMIT $sql_von, $records_per_page"; |
|
198 |
} |
|
199 |
|
|
200 |
public function getPagingHTML($vars) { |
|
201 |
global $app; |
|
202 |
$page_params = $vars['page_params']; |
|
203 |
$list_file = $vars['list_file']; |
|
204 |
$content = '<a href="'.$list_file.'?page=0'.$page_params.'"><img src="../themes/iprg/images/btn_left.png" border="0"></a> '; |
|
205 |
if($vars['show_page_back'] == 1){ |
|
206 |
$content .= '<a href="'.$list_file.'?page='.$vars['last_page'].$page_params.'"><img src="../themes/iprg/images/btn_back.png" border="0"></a> '; |
|
207 |
} |
|
208 |
$content .= ' '.$app->lng('Page').' '.$vars['next_page'].' '.$app->lng('of').' '.$vars['max_pages'].' '; |
|
209 |
if($vars['show_page_next'] == 1){ |
|
210 |
$content .= '<a href="'.$list_file.'?page='.$vars['next_page'].$page_params.'"><img src="../themes/iprg/images/btn_next.png" border="0"></a> '; |
|
211 |
} |
|
212 |
$content .= '<a href="'.$list_file.'?page='.$vars['pages'].$page_params.'"> <img src="../themes/iprg/images/btn_right.png" border="0"></a>'; |
|
213 |
return $content; |
|
214 |
} |
|
215 |
|
|
216 |
public function getPagingHTMLasTXT($vars) |
|
217 |
{ |
|
218 |
global $app; |
|
219 |
$page_params = $vars['page_params']; |
|
220 |
$list_file = $vars['list_file']; |
|
221 |
$content = '[<a href="'.$list_file.'?page=0'.$page_params.'">|<< </a>]'; |
|
222 |
if($vars['show_page_back'] == 1){ |
|
223 |
$content .= '[<< <a href="'.$list_file.'?page='.$vars['last_page'].$page_params.'">'.$app->lng('Back').'</a>] '; |
|
224 |
} |
|
225 |
$content .= ' '.$app->lng('Page').' '.$vars['next_page'].' '.$app->lng('of').' '.$vars['max_pages'].' '; |
|
226 |
if($vars['show_page_next'] == 1){ |
|
227 |
$content .= '[<a href="'.$vars['list_file'].'?page='.$vars['next_page'].$page_params.'">'.$app->lng('Next').' >></a>] '; |
|
228 |
} |
|
229 |
$content .= '[<a href="'.$list_file.'?page='.$vars['pages'].$page_params.'"> >>|</a>]'; |
|
230 |
return $content; |
|
231 |
} |
|
232 |
|
|
233 |
public function getSortSQL() |
|
234 |
{ |
|
235 |
$sort_field = $this->listDef['sort_field']; |
|
236 |
$sort_direction = $this->listDef['sort_direction']; |
|
237 |
return ($sort_field != '' && $sort_direction != '') ? "ORDER BY $sort_field $sort_direction" : ''; |
|
238 |
} |
|
239 |
|
|
240 |
public function saveSearchSettings($searchresult_name) |
|
241 |
{ |
|
242 |
global $app, $conf; |
|
243 |
|
|
244 |
$list_name = $this->listDef['name']; |
|
245 |
$settings = $_SESSION['search'][$list_name]; |
|
246 |
unset($settings['page']); |
|
247 |
$data = mysql_real_escape_string(serialize($settings)); |
|
248 |
|
|
249 |
$userid = $_SESSION['s']['user']['userid']; |
|
250 |
$groupid = $_SESSION['s']['user']['default_group']; |
|
251 |
$sys_perm_user = 'riud'; |
|
252 |
$sys_perm_group = 'r'; |
|
253 |
$sys_perm_other = ''; |
|
254 |
$module = $_SESSION['s']['module']['name']; |
|
255 |
$searchform = $this->listDef['name']; |
|
256 |
$title = $searchresult_name; |
|
257 |
|
|
258 |
$sql = 'INSERT INTO `searchform` ( ' |
|
259 |
.'`sys_userid` , `sys_groupid` , `sys_perm_user` , `sys_perm_group` , `sys_perm_other` , `module` , `searchform` , `title` , `data` ' |
|
260 |
.')VALUES (' |
|
261 |
."'$userid', '$groupid', '$sys_perm_user', '$sys_perm_group', '$sys_perm_other', '$module', '$searchform', '$title', '$data')"; |
|
262 |
$app->db->query($sql); |
|
263 |
} |
|
264 |
|
|
265 |
public function decode($record) |
|
266 |
{ |
|
267 |
if(is_array($record)) { |
|
268 |
foreach($this->listDef['item'] as $field) { |
|
269 |
$key = $field['field']; |
|
270 |
switch ($field['datatype']) |
|
271 |
{ |
|
272 |
case 'DATE': |
|
273 |
if($val > 0) { |
|
274 |
$record[$key] = date($this->dateformat, $record[$key]); |
|
275 |
} |
|
276 |
break; |
|
277 |
|
|
278 |
case 'INTEGER': |
|
279 |
$record[$key] = intval($record[$key]); |
|
280 |
break; |
|
281 |
|
|
282 |
case 'DOUBLE': |
|
283 |
$record[$key] = $record[$key]; |
|
284 |
break; |
|
285 |
|
|
286 |
case 'CURRENCY': |
|
287 |
$record[$key] = number_format($record[$key], 2, ',', ''); |
|
288 |
break; |
|
289 |
|
|
290 |
|
|
291 |
case 'VARCHAR': |
|
292 |
case 'TEXT': |
|
293 |
default: |
|
294 |
$record[$key] = stripslashes($record[$key]); |
|
295 |
break; |
|
296 |
} |
|
297 |
} |
|
298 |
} |
|
299 |
return $record; |
|
300 |
} |
|
301 |
|
|
302 |
public function encode($record) |
|
303 |
{ |
|
304 |
if(is_array($record)) { |
|
305 |
foreach($this->listDef['item'] as $field) { |
|
306 |
$key = $field['field']; |
|
307 |
switch ($field['datatype']) |
|
308 |
{ |
|
309 |
case 'VARCHAR': |
|
310 |
case 'TEXT': |
|
311 |
if(!is_array($record[$key])) { |
|
312 |
$record[$key] = mysql_real_escape_string($record[$key]); |
|
313 |
} else { |
|
314 |
$record[$key] = implode($this->tableDef[$key]['separator'],$record[$key]); |
|
315 |
} |
|
316 |
break; |
|
317 |
|
|
318 |
case 'DATE': |
|
319 |
if($record[$key] > 0) { |
|
320 |
list($tag, $monat, $jahr) = explode('.', $record[$key]); |
|
321 |
$record[$key] = mktime(0, 0, 0, $monat, $tag, $jahr); |
|
322 |
} |
|
323 |
break; |
|
324 |
|
|
325 |
case 'INTEGER': |
|
326 |
$record[$key] = intval($record[$key]); |
|
327 |
break; |
|
328 |
|
|
329 |
case 'DOUBLE': |
|
330 |
$record[$key] = mysql_real_escape_string($record[$key]); |
|
331 |
break; |
|
332 |
|
|
333 |
case 'CURRENCY': |
|
334 |
$record[$key] = str_replace(',', '.', $record[$key]); |
|
335 |
break; |
|
336 |
} |
|
337 |
} |
|
338 |
} |
|
339 |
return $record; |
|
340 |
} |
|
341 |
} |
|
342 |
|
25ece9
|
343 |
?> |