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