00001 <?php 00002 00003 # SiT! forms 00004 00008 class Form 00009 { 00010 var $formheading; 00011 var $row = array(); 00012 var $name; 00013 var $submitLabell; 00014 var $tableName; 00015 var $type; // UPDATE, ADD 00016 var $returnURLSuccess; 00017 var $returnURLFailure; 00018 var $keyField; 00019 var $keyValue; 00020 var $debug = false; 00021 00022 public function __construct($name, $submitLabel, $tableName, $type, $formheading) 00023 { 00024 $this->name = $name; 00025 $this->submitLabel = $submitLabel; 00026 $this->tableName = $tableName; 00027 $this->type = $type; 00028 $this->formheading = $formheading; 00029 00030 $this->returnURLSuccess = $_SERVER['PHP_SELF']; 00031 $this->returnURLFailure = $_SERVER['PHP_SELF']; 00032 } 00033 00034 00035 public function addRow(/*Row*/ $row) 00036 { 00037 $this->row[] = $row; 00038 } 00039 00040 00041 public function setReturnURLSuccess($returnURL) 00042 { 00043 $this->returnURLSuccess = $returnURL; 00044 } 00045 00046 00047 public function setReturnURLFailure($returnURL) 00048 { 00049 $this->returnURLFailure = $returnURL; 00050 } 00051 00052 00053 public function setDebug($debug) 00054 { 00055 $this->debug = $debug; 00056 } 00057 00058 00059 public function setKey($keyField, $keyValue) 00060 { 00061 $this->keyField = $keyField; 00062 $this->keyValue = $keyValue; 00063 } 00064 00065 00066 private function generateHTML() 00067 { 00068 global $strSubmit; 00069 00070 echo "<h2>{$this->formheading}</h2>"; 00071 00072 echo "<form action='{$_SERVER['PHP_SELF']}' id='{$this->name}' name='{$this->name}' method='post'>"; 00073 echo "<table class='vertical'>"; 00074 foreach($this->row AS $r) 00075 { 00076 echo $r->generateHTML(); 00077 } 00078 echo "</table>"; 00079 echo "<p align='center'><input type='submit' id='{$this->name}submit' name='submit' value='{$this->submitLabel}' /></p>"; 00080 echo "</form>"; 00081 } 00082 00083 00084 private function processForm() 00085 { 00086 $toReturn = array(); 00087 foreach ($this->row AS $r) 00088 { 00089 $toReturn = array_merge ($toReturn, $r->getDB()); 00090 } 00091 00092 // print_r($toReturn); 00093 switch ($this->type) 00094 { 00095 case 'insert': 00096 $sql = "INSERT INTO `{$this->tableName}` "; 00097 if (count($toReturn) > 0) 00098 { 00099 $sql .= " ("; 00100 foreach ($toReturn AS $d) 00101 { 00102 $a[] = "{$d->field}"; 00103 } 00104 $sql .= implode(",", $a); 00105 00106 unset($a); 00107 $sql .= ") VALUES ("; 00108 foreach ($toReturn AS $d) 00109 { 00110 $v = cleanvar($_REQUEST[$d->name]); 00111 $a[] = "'{$v}'"; 00112 } 00113 $sql .= implode(",", $a); 00114 $sql .= ")"; 00115 } 00116 00117 break; 00118 00119 case 'update': 00120 $sql = "UPDATE `{$this->tableName}` "; 00121 if (count($toReturn) > 0) 00122 { 00123 $sql .= " SET "; 00124 foreach ($toReturn AS $d) 00125 { 00126 $v = cleanvar($_REQUEST[$d->name]); 00127 $a[] .= "{$d->field} = '{$v}'"; 00128 } 00129 $sql .= implode(", ", $a); 00130 00131 $sql .= "WHERE {$this->keyField} = '{$this->keyValue}'"; 00132 } 00133 00134 break; 00135 } 00136 00137 if ($this->debug) echo $sql; 00138 $result = mysql_query($sql); 00139 if (mysql_error()) trigger_error(mysql_error(),E_USER_ERROR); 00140 if (mysql_affected_rows() <= 0) 00141 { 00142 html_redirect($this->returnURLFailure, FALSE); 00143 exit; 00144 } 00145 else 00146 { 00147 html_redirect($this->returnURLSuccess, TRUE); 00148 exit; 00149 } 00150 } 00151 00152 00153 public function run() 00154 { 00155 $submit = cleanvar($_REQUEST['submit']); 00156 00157 if (empty($submit)) 00158 { 00159 echo $this->generateHTML(); 00160 } 00161 else 00162 { 00163 echo $this->processForm(); 00164 } 00165 } 00166 } 00167 00168 00169 abstract class Component 00170 { 00171 var $name; 00172 var $value; 00173 var $dbFieldName; 00174 var $mandatory; 00175 abstract function generateHTML(); 00176 abstract function getDB(); // Returns array 00177 function isMandatory($mandatory) 00178 { 00179 $this->mandatory = $mandatory; // Boolean 00180 } 00181 } 00182 00183 00184 class db 00185 { 00186 var $name; 00187 var $field; 00188 00189 public function __construct($name, $field) 00190 { 00191 $this->name = $name; 00192 $this->field = $field; 00193 } 00194 } 00195 00196 00197 class Row extends Component 00198 { 00199 var $components; 00200 00201 public function addComponent(/*Component*/ $component) 00202 { 00203 $this->components[] = $component; 00204 } 00205 00206 00207 public function generateHTML() 00208 { 00209 $toReturn = "<tr>"; 00210 00211 foreach ($this->components AS $comp) 00212 { 00213 $toReturn .= $comp->generateHTML(); 00214 } 00215 00216 return $toReturn."</tr>"; 00217 } 00218 00219 00220 public function getDB() 00221 { 00222 $toReturn = array(); 00223 foreach ($this->components AS $comp) 00224 { 00225 $toReturn = array_merge($toReturn, $comp->getDB()); 00226 } 00227 00228 return $toReturn; 00229 } 00230 }// ROW 00231 00232 00233 class HiddenRow extends Component 00234 { 00235 public function addComponent(/*Component*/ $component) 00236 { 00237 $this->components[] = $component; 00238 } 00239 00240 00241 public function generateHTML() 00242 { 00243 foreach ($this->components AS $comp) 00244 { 00245 $toReturn .= $comp->generateHTML(); 00246 } 00247 00248 return $toReturn; 00249 } 00250 00251 00252 public function getDB() 00253 { 00254 $toReturn = array(); 00255 foreach ($this->components AS $comp) 00256 { 00257 $toReturn = array_merge($toReturn, $comp->getDB()); 00258 } 00259 00260 return $toReturn; 00261 } 00262 } 00263 00264 00265 class Cell extends Component 00266 { 00267 var $components = array(); 00268 var $isHeader = false; 00269 00270 public function addComponent(/*component*/ $component) 00271 { 00272 $this->components[] = $component; 00273 } 00274 00275 public function setIsHeader($header = TRUE) 00276 { 00277 $this->isHeader = $header; 00278 } 00279 00280 00281 public function generateHTML() 00282 { 00283 $toReturn = ""; 00284 foreach ($this->components AS $component) 00285 { 00286 $toReturn .= $component->generateHTML(); 00287 } 00288 00289 if ($this->isHeader) $toReturn = "<th>{$toReturn}</th>"; 00290 else $toReturn = "<td>{$toReturn}</td>"; 00291 00292 return $toReturn; 00293 } 00294 00295 00296 public function getDB() 00297 { 00298 $toReturn = array(); 00299 foreach ($this->components AS $comp) 00300 { 00301 $toReturn = array_merge($toReturn, $comp->getDB()); 00302 } 00303 00304 return $toReturn; 00305 } 00306 } 00307 00308 00309 class Label extends Component 00310 { 00311 var $label = ""; 00312 public function __construct($label = "") 00313 { 00314 $this->label = $label; 00315 } 00316 00317 public function generateHTML() 00318 { 00319 return "{$this->label}"; 00320 } 00321 00322 public function getDB() 00323 { 00324 return array(); 00325 } 00326 } // LABEL 00327 00328 00329 class SingleLineEntry extends Component 00330 { 00331 var $size = 30; 00332 00333 public function __construct($name = "text", $size = 30, $dbField, $value='') 00334 { 00335 $this->name = $name; 00336 $this->value = $value; 00337 $this->size = $size; 00338 $this->dbFieldName = $dbField; 00339 } 00340 00341 00342 public function generateHTML() 00343 { 00344 return "<input type='text' id='{$this->name}' name='{$this->name}' size='{$this->size}' value='{$this->value}' />"; 00345 } 00346 00347 00348 public function getDB() 00349 { 00350 $db = new db($this->name, $this->dbFieldName); 00351 00352 return array($db); 00353 } 00354 } 00355 00356 00357 class HiddenEntry extends Component 00358 { 00359 public function __construct($name = "text", $dbField, $value) 00360 { 00361 $this->name = $name; 00362 $this->value = $value; 00363 $this->dbFieldName = $dbField; 00364 } 00365 00366 00367 public function generateHTML() 00368 { 00369 return "<input type='hidden' id='{$this->name}' name='{$this->name}' value='{$this->value}' />"; 00370 } 00371 00372 00376 public function getDB() 00377 { 00378 if (empty($this->dbFieldName)) 00379 { 00380 return array(); 00381 } 00382 else 00383 { 00384 $db = new db($this->name, $this->dbFieldName); 00385 00386 return array($db); 00387 } 00388 } 00389 } 00390 00391 00392 class DatePicker extends Component 00393 { 00394 var $name; 00395 00396 public function __construct($name) 00397 { 00398 $this->name = $name; 00399 } 00400 00401 00402 public function generateHTML() 00403 { 00404 global $CONFIG, $iconset; 00405 00406 $divid = "datediv".str_replace('.','',$this->name); 00407 $html = "<img src='{$CONFIG['application_webpath']}images/icons/{$iconset}/16x16/pickdate.png' "; 00408 $html .= "onmouseup=\"toggleDatePicker('$divid','{$this->name}')\" width='16' height='16' alt='date picker' style='cursor: pointer; vertical-align: bottom;' />"; 00409 $html .= "\n<div id='$divid' style='position: absolute;'></div>\n"; 00410 return $html; 00411 } 00412 00413 00414 public function getDB() 00415 { 00416 return array(); 00417 } 00418 } 00419 00420 00421 class DateC extends Component 00422 { 00423 var $components = array(); 00424 public function __construct($name) 00425 { 00426 $this->components[] = new SingleLineEntry(name,10, "test"); 00427 $this->components[] = new DatePicker("{$name}picker"); 00428 } 00429 00430 00431 public function generateHTML() 00432 { 00433 $toReturn = ""; 00434 foreach ($this->components AS $component) $toReturn .= $component->generateHTML(); 00435 return $toReturn; 00436 } 00437 00438 00439 public function getDB() 00440 { 00441 return array(); 00442 } 00443 } 00444 00445 ?>
For more help developing with SiT! see http://sitracker.org/wiki/DevelopmentHowTo