| [ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 <?php 2 /* 3 * PHP Base Library 4 * 5 * Copyright (c) 1998-2000 NetUSE AG 6 * Boris Erdmann, Kristian Koehntopp 7 * 8 * $Id: class.sqlquery.inc.php 20014 2005-11-27 01:17:28Z milosch $ 9 * 10 */ 11 12 class sqlquery 13 { 14 var $classname = 'sqlquery'; ## Persistence Support 15 16 var $persistent_slots = array( 17 'conditions', 18 'input_size', 19 'input_max', 20 'method', 21 'lang', 22 'translate', 23 'container', 24 'variable', 25 'query' 26 ); 27 28 var $query = '1=0'; // last WHERE clause used 29 var $conditions = 1; // Allow for that many Query Conditions 30 var $input_size = 35; // Used in text input field creation 31 var $input_max = 80; 32 33 var $method = 'post'; // Generate get or post form... 34 var $lang = 'en'; // HTML Widget language 35 36 var $translate = 'on'; // If set, translate column names 37 var $container = ''; // If set, create a container table 38 var $variable = 'on'; // if set, create variable size buttons 39 40 41 ## HTML Widget dictionary 42 /* use phpgw lang() now 43 var $dict = array( 44 "de" => array( 45 "searchfor" => "Suchen nach:", 46 "and" => "und", 47 "or" => "oder", 48 "like" => "enthält", 49 "reset" => "Neu", 50 "submit" => "Ausführen", 51 "less" => "Weniger", 52 "more" => "Mehr" 53 ), 54 55 "en" => array( 56 "searchfor" => "Search for:", 57 "and" => "and", 58 "or" => "or", 59 "like" => "contains", 60 "reset" => "Reset Query", 61 "submit" => "Submit Query", 62 "less" => "Fewer", 63 "more" => "More" 64 ) 65 ); 66 */ 67 ## SQL comparision dictionary 68 var $compare = array( 69 "like" => "like", 70 ">" => ">", 71 "<" => "<", 72 73 ">=" => ">=", 74 "<=" => "<=", 75 "=" => "=", 76 "<>" => "<>" 77 ); 78 79 80 function start($class = "") { 81 } 82 83 ## selection: 84 ## 85 ## Create a <select> tag of the class $class with the name $name. 86 ## The tag contains the options named in array $option. If $trans 87 ## is true, $option is exspected to be a hash of 88 ## "long name " => "sqlname" pairs. The option matching $old 89 ## is created with the attribute "selected". 90 ## 91 function selection($name, $option, $old = "", $trans = "", $class = "") { 92 $res = ""; 93 $res .= sprintf("<select%s name=\"%s\">\n", 94 ($class)?" class=$class":"", 95 $name); 96 reset($option); 97 while(list($k, $v) = each($option)) { 98 if (($trans == "" && $old == $v) 99 || ($trans != "" && $old == $k)) { 100 $selected = " selected"; 101 } else { 102 $selected = ""; 103 } 104 105 $res .= sprintf("<option value=\"%s\"%s%s>%s\n", 106 ($trans)?$k:$v, 107 ($class)?" class=$class":"", 108 ($selected)?" selected":"", 109 $v); 110 } 111 $res .= sprintf(" </select>"); 112 113 return $res; 114 } 115 116 ## fieldname: 117 ## 118 ## Given a basename $base, and attribute name $att and an attribute 119 ## number $num, this functions returns an input field name 120 ## $base[$name_$num]. 121 ## 122 ## This construct can be imported into a function namespace with a 123 ## single global instruction and the field name can be easily 124 ## exploded into component names and numbers. 125 126 function makename($base, $att, $num) { 127 return sprintf("%s[%s_%d]", $base, $att, $num); 128 } 129 130 ## form: 131 ## 132 ## Draw SQL Query selection form. 133 ## 134 function form($base, $option, $class = "", $target = "") { 135 global $sess; 136 137 ## 138 ## load the HTML results of this function into $res. 139 ## 140 $res = ""; 141 142 ## A hack. We cannot do language dependent initialisation of 143 ## static values. 144 if (isset($this->compare["like"])) { 145 $this->compare["like"] = lang('like'); 146 } 147 148 ## Prepare a self-directed container form 149 if ($this->container) { 150 $res .= sprintf("<table border=1%s><tr%s><td>\n", 151 ($class)?" class=$class":"", 152 ($class)?" class=$class":"", 153 ($class)?" class=$class":""); 154 } 155 $res .= sprintf("<form method=\"%s\" action=\"%s\">\n", 156 $this->method, 157 ($target)?$target:$sess->self_url()); 158 159 ## Prepare the inner table, laying out the selection elements 160 $res .= sprintf("<table%s>\n", ($class)?" class=$class":""); 161 162 ## Build $this->conditions many selection elements 163 for ($i=1; $i<= $this->conditions; $i++) { 164 $res .= sprintf(" <tr%s>\n", ($class)?" class=$class":""); 165 166 ## Build conjunction (first row does not have a conjunction) 167 if ($i == 1) { 168 $res .= sprintf(" <td%s>%s</td>\n", 169 ($class)?" class=$class":"",lang('Search for:')); 170 } else { 171 $res .= sprintf(" <td%s>%s</td>\n", 172 ($class)?" class=$class":"", 173 $this->selection($this->makename($base, "conj", $i), 174 array("and" => lang('and'), "or" => lang('or')), 175 $GLOBALS[$base]["conj_".$i], 176 "on", 177 $class)); 178 } 179 180 ## Build field selection 181 $res .= sprintf(" <td%s>%s</td>\n", 182 ($class)?" class=$class":"", 183 $this->selection( 184 $this->makename($base, "sel", $i), 185 $option, 186 $GLOBALS[$base]["sel_".$i], 187 $this->translate, 188 $class)); 189 190 ## Build comparison selection 191 $res .= sprintf(" <td%s>%s</td>\n", 192 ($class)?" class=$class":"", 193 $this->selection( 194 $this->makename($base, "comp", $i), 195 $this->compare, 196 $GLOBALS[$base]["comp_".$i], 197 "on", 198 $class)); 199 ## Create text input field. 200 $res .= sprintf(" <td%s><input type=\"text\" name=\"%s\" value=\"%s\" size=%d maxlength=%d%s></td>\n", 201 ($class)?" class=$class":"", 202 $this->makename($base, "input", $i), 203 stripslashes($GLOBALS[$base]["input_".$i]), 204 $this->input_size, 205 $this->input_max, 206 ($class)?" class=$class":""); 207 208 $res .= sprintf(" </tr>\n"); 209 } 210 211 ## Create variable size buttons 212 $res .= sprintf(" <tr%s>\n", ($class)?" class=$class":""); 213 $res .= sprintf(" <td%s> </td>\n", ($class)?" class=$class":""); 214 215 if ($this->variable) { 216 $res .= sprintf(" <td%s><input type=\"submit\" name=\"%s\" value=\"%s\"> ", 217 ($class)?" class=$class":"", 218 $this->makename($base, "more", 0), 219 lang('More')); 220 $res .= sprintf("<input type=\"submit\" name=\"%s\"value=\"%s\"></td>\n", 221 $this->makename($base, "less", 0), 222 lang('Fewer')); 223 } else { 224 $res .= sprintf(" <td%s> </td>\n", ($class)?" class=$class":""); 225 } 226 227 $res .= sprintf(" <td%s> </td>\n", ($class)?" class=$class":""); 228 $res .= sprintf(" <td%s><input type=\"reset\" value=\"%s\"> ", 229 ($class)?" class=$class":"", 230 lang('Clear')); 231 $res .= sprintf("<input type=\"submit\" name=\"%s\"value=\"%s\"></td>\n", 232 $this->makename($base, "submit", 0), 233 lang('Search')); 234 235 $res .= sprintf(" </tr>\n"); 236 $res .= sprintf("</table>\n"); 237 238 $res .= sprintf("</form>\n"); 239 if ($this->container) { 240 $res .= sprintf("</td></tr></table>\n"); 241 } 242 $res .= sprintf("<!-- End %s generated query form -->\n", $this->classname); 243 244 return $res; 245 } 246 247 ## plain_where: 248 ## 249 ## Given a base variable name, creates a condition suitable for 250 ## the where clause of a SQL query. 251 ## 252 function plain_where($base) { 253 for($i=1; $i<=$this->conditions; $i++) { 254 ## Only create conditions for used input fields 255 if ($GLOBALS[$base]["input_".$i] == "") 256 continue; 257 258 ## If necessary, add conjunction 259 if ($q != "") 260 $q .= sprintf(" %s ", $GLOBALS[$base]["conj_".$i]); 261 262 ## Handle "like" 263 if ($GLOBALS[$base]["comp_".$i] == "like") 264 $v = "%".$GLOBALS[$base]["input_".$i]."%"; 265 else 266 $v = $GLOBALS[$base]["input_".$i]; 267 268 ## Create subcondition 269 $q .= sprintf("%s %s '%s'", 270 $GLOBALS[$base]["sel_".$i], 271 $GLOBALS[$base]["comp_".$i], 272 $v); 273 } 274 275 if (!$q) { 276 $q = "1=0"; 277 } 278 279 return "( $q )"; 280 } 281 282 ## translated_plain_where: 283 ## 284 ## Given a base variable name, creates a translated version of 285 ## the where clause of a SQL query. 286 ## 287 function translated_plain_where($base, $field) { 288 for($i=1; $i<=$this->conditions; $i++) { 289 ## Only create conditions for used input fields 290 if ($GLOBALS[$base]["input_".$i] == "") 291 continue; 292 293 ## If necessary, add conjunction 294 if ($q != "") 295 $q .= sprintf(" %s ", lang($GLOBALS[$base]["conj_".$i])); 296 297 ## Handle "like" 298 if ($GLOBALS[$base]["comp_".$i] == "like") 299 $c = lang($GLOBALS[$base]["comp_".$i]); 300 else 301 $c = $this->compare[$GLOBALS[$base]["comp_".$i]]; 302 303 ## Create subcondition 304 $q .= sprintf("%s %s '%s'", 305 $field[$GLOBALS[$base]["sel_".$i]], 306 $c, 307 $GLOBALS[$base]["input_".$i]); 308 } 309 310 if (!$q) { 311 $q = "1=0"; 312 } 313 314 return "( $q )"; 315 } 316 317 ## where: 318 ## 319 ## Same as plain_where(), but also inspects the submit button 320 ## used to submit the query. Changes $this->conditions appropriately. 321 function where($base, $incr = 1) { 322 if (isset($GLOBALS[$base]["less_0"])) 323 $this->conditions -= $incr; 324 325 if (isset($GLOBALS[$base]["more_0"])) 326 $this->conditions += $incr; 327 328 if ($this->conditions < 1) 329 $this->conditions = 1; 330 331 return $this->plain_where($base); 332 } 333 } 334 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |