tbrehm
2005-11-24 cc37455955991b05d5734039ec7b329aad991f6d
commit | author | age
b5a2f8 1 <?php
T 2
3 /*
4 Copyright (c) 2005, Till Brehm, projektfarm Gmbh
5 All rights reserved.
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 /**
32 * Listenbehandlung
33 *
34 * @package listform
35 * @author Till Brehm
36 * @version 1.1
37 */
38
39 class listform {
40
41     var $debug = 0;
42     var $errorMessage;
43     var $listDef;
44     var $searchValues;
45     var $pagingHTML;
46     var $pagingValues;
47     var $searchChanged = 0;
48     var $module;
49     
50     function loadListDef($file,$module = '') {
51         global $app,$conf;
52         if(!is_file($file)) die("List-Definition: $file not found.");
53         include_once($file);
54         $this->listDef = $liste;
55         $this->module = $module;
56         return true;
57     }
58     
59     function getSearchSQL($sql_where = "") {
60         global $db;
61         
62         // Hole Config Variablen
63         $list_name = $this->listDef["name"];
64         $search_prefix = $this->listDef["search_prefix"];
65         
66         // speichere Suchanfrage
67         foreach($this->listDef["item"] as $i) {
68             $field = $i["field"];
69             
70             // hat sich die suche geändert
71             if(isset($_REQUEST[$search_prefix.$field]) and $_REQUEST[$search_prefix.$field] != $_SESSION["search"][$list_name][$search_prefix.$field]) $this->searchChanged = 1;
72             
73             // suchfeld in session speichern.
74             if(isset($_REQUEST[$search_prefix.$field])) $_SESSION["search"][$list_name][$search_prefix.$field] = $_REQUEST[$search_prefix.$field];
75             
76             if($i["formtype"] == "SELECT") {
77                 if(is_array($i['value'])) {
78                     $out = '<option value=""></option>';
79                     foreach($i['value'] as $k => $v) {
80                         $selected = ($k == $_SESSION["search"][$list_name][$search_prefix.$field])?' SELECTED':'';
81                         $out .= "<option value='$k'$selected>$v</option>\r\n";
82                     }
83                 }
84                 $this->searchValues[$search_prefix.$field] = $out;
85             } else {
86                 $this->searchValues[$search_prefix.$field] = $_SESSION["search"][$list_name][$search_prefix.$field];
87             }
88         }
89         
90         // Speichere Variablen in Objekt zum späteren einparsen in Template
91         // $this->searchValues = $_SESSION["search"][$list_name];
92         
93         foreach($this->listDef["item"] as $i) {
94             $field = $i["field"];
95             if($_REQUEST[$search_prefix.$field] != '') $sql_where .= " $field ".$i["op"]." '".$i["prefix"].$_REQUEST[$search_prefix.$field].$i["suffix"]."' and";
96         }
97         
98         if($sql_where != '') {
99             $sql_where = substr($sql_where,0,-3);
100         } else {
101             $sql_where = "1";
102         }
103         
104         
105         return $sql_where;
106     }
107     
108     function getPagingSQL($sql_where = "1") {
109         global $app, $conf;
110         
111         // Hole Config Variablen
112         $list_name             = $this->listDef["name"];
113         $search_prefix         = $this->listDef["search_prefix"];
114         $records_per_page     = $this->listDef["records_per_page"];
115         $table                 = $this->listDef["table"];
116         
117         // setze page auf null, wenn in session nicht gesetzt
118         if($_SESSION["search"][$list_name]["page"] == '') $_SESSION["search"][$list_name]["page"] = 0;
119         
120         // setze page auf wert der request variablen "page"
121         if(isset($_REQUEST["page"])) $_SESSION["search"][$list_name]["page"] = $_REQUEST["page"];
122         
123         // page auf 0 setzen, wenn suche sich geändert hat.
124         if($this->searchChanged == 1) $_SESSION["search"][$list_name]["page"] = 0;
125         
126         $sql_von = $_SESSION["search"][$list_name]["page"] * $records_per_page;
127         $record_count = $app->db->queryOneRecord("SELECT count(*) AS anzahl FROM $table WHERE $sql_where");
128         $pages = intval($record_count["anzahl"] / $records_per_page);
129         
130         
131         $vars["list_file"] = $this->listDef["file"];
132         $vars["page"] = $_SESSION["search"][$list_name]["page"];
133         $vars["last_page"] = $_SESSION["search"][$list_name]["page"] - 1;
134         $vars["next_page"] = $_SESSION["search"][$list_name]["page"] + 1;
135         $vars["pages"] = $pages;
136         $vars["max_pages"] = $pages + 1;
137         $vars["records_gesamt"] = $record_count["anzahl"];
138         $vars["page_params"] = $this->listDef["page_params"];
139         
140         
141         if($_SESSION["search"][$list_name]["page"] > 0) $vars["show_page_back"] = 1;
142         if($_SESSION["search"][$list_name]["page"] <= $seiten - 1) $vars["show_page_next"] = 1;
143         
144         $this->pagingValues = $vars;
145         $this->pagingHTML = $this->getPagingHTML($vars);
146         
147         $limit_sql = "LIMIT $sql_von, $records_per_page";
148         
149         return $limit_sql;
150     }
151     
152     function getPagingHTML($vars) {
153         global $app;
cc3745 154         $content = '[ <a href="'.$vars["list_file"].'?page=0'.$vars["page_params"].'">|&lt;&lt; </a>]';
b5a2f8 155         if($vars["show_page_back"] == 1) $content .= '[<< <a href="'.$vars["list_file"].'?page='.$vars["last_page"].$vars["page_params"].'">'.$app->lng('Back').'</a>] ';
T 156         $content .= ' '.$app->lng('Page').' '.$vars["next_page"].' '.$app->lng('of').' '.$vars["max_pages"].' ';
157         if($vars["show_page_next"] == 1) $content .= '[<a href="'.$vars["list_file"].'?page='.$vars["next_page"].$vars["page_params"].'">'.$app->lng('Next').' >></a>] ';
cc3745 158         $content .= '[<a href="'.$vars["list_file"].'?page='.$vars["pages"].$vars["page_params"].'"> &gt;&gt;| </a>]';
b5a2f8 159         
T 160         return $content;
161     }
162     
163     function getSortSQL() {
164         global $app, $conf;
165         
166         // Hole Config Variablen
167         $sort_field = $this->listDef["sort_field"];
168         $sort_direction = $this->listDef["sort_direction"];
169         
170         $sql_sort = '';
171         
172         if($sort_field != '' && $sort_direction != '') {
173             $sql_sort = "ORDER BY $sort_field $sort_direction";
174         }
175         
176         return $sql_sort;
177     }
178     
179     function decode($record) {
180         if(is_array($record)) {
181             foreach($this->listDef["item"] as $field) {
182                 $key = $field["field"];
183                 switch ($field['datatype']) {
184                 case 'VARCHAR':
185                     $record[$key] = stripslashes($record[$key]);
186                 break;
187                 
188                 case 'TEXT':
189                     $record[$key] = stripslashes($record[$key]);
190                 break;
191                 
192                 case 'DATE':
193                     if($val > 0) {
194                         $record[$key] = date($this->dateformat,$record[$key]);
195                     }
196                 break;
197                 
198                 case 'INTEGER':
199                     $record[$key] = intval($record[$key]);
200                 break;
201                 
202                 case 'DOUBLE':
203                     $record[$key] = $record[$key];
204                 break;
205                 
206                 case 'CURRENCY':
207                     $record[$key] = number_format($record[$key], 2, ',', '');
208                 break;
209                 
210                 default:
211                     $record[$key] = stripslashes($record[$key]);
212                 }
213             }
214             
215         }
216     return $record;
217     }
218     
219     
220     function encode($record) {
221         
222         if(is_array($record)) {
223             foreach($this->listDef["item"] as $field) {
224                 $key = $field["field"];
225                 switch ($field['datatype']) {
226                 case 'VARCHAR':
227                     if(!is_array($record[$key])) {
228                         $record[$key] = addslashes($record[$key]);
229                     } else {
230                         $record[$key] = implode($this->tableDef[$key]['separator'],$record[$key]);
231                     }
232                 break;
233                 case 'TEXT':
234                     if(!is_array($record[$key])) {
235                         $record[$key] = addslashes($record[$key]);
236                     } else {
237                         $record[$key] = implode($this->tableDef[$key]['separator'],$record[$key]);
238                     }
239                 break;
240                 case 'DATE':
241                     if($record[$key] > 0) {
242                         list($tag,$monat,$jahr) = explode('.',$record[$key]);
243                         $record[$key] = mktime(0,0,0,$monat,$tag,$jahr);
244                     }
245                 break;
246                 case 'INTEGER':
247                     $record[$key] = intval($record[$key]);
248                 break;
249                 case 'DOUBLE':
250                     $record[$key] = addslashes($record[$key]);
251                 break;
252                 case 'CURRENCY':
253                     $record[$key] = str_replace(",",".",$record[$key]);
254                 break;
255                 }
256                 
257             }
258         }
259         return $record;
260     }
261     
262 }
263
264 ?>