Introduced PasswordField.class.php to keep things simple.
[timetracker.git] / WEB-INF / lib / form / Form.class.php
index 50804dd..868a3b3 100644 (file)
@@ -37,75 +37,40 @@ class Form {
 
   var $name = '';
   var $elements = array();
-  // TODO: refactoring ongoing down from here.
 
-       var $mRequest;
-    
-    function __construct($formName) {
-        $this->name = $formName;
-    }
-    
-    function setRequest(&$request) {
-        $this->mRequest = &$request;
-    }
+  function __construct($formName) {
+    $this->name = $formName;
+  }
 
-    function &getElement($name) {
-        return $this->elements[$name];
-    }
-    
-    function &getElements() {
-        return $this->elements;
-    }
-    
-       //// FORM element
-       // action
-       // method - GET, POST
-       // enctype - enctype="multipart/form-data"
-       // name
-       // onsubmit
-       // onreset
-    function getName() { return $this->name; }
-    
-    function isSubmit()        {
-       if (!isset($this->mRequest)) return false;
-        $result = false;
-           foreach ($this->elements as $el) {
-               if (strtolower(get_class($el))=="submit") {
-                   $name = $el->getName();
-                   $value = $this->mRequest->getAttribute($name);
-                   if($value) {
-                      $result = true; 
-                   }
-               }
-           }
-        return $result;
-    }
-       
-       //// INPUT element
-       // type = TEXT | PASSWORD | CHECKBOX | RADIO | SUBMIT | RESET | FILE | HIDDEN | IMAGE | BUTTON
-       // name
-       // value
-       // checked - for type radio and checkbox
-       // size - width pixels or chars
-       // maxlength
-       // src - for type image
-       // tabindex - support  A, AREA, BUTTON, INPUT, OBJECT, SELECT, and TEXTAREA
-       // accesskey - support A, AREA, BUTTON, INPUT, LABEL, and LEGEND, and TEXTAREA
-       // onfocus
-       // onblur
-       // onselect -  INPUT and TEXTAREA
-       // onchange
-       function addInput($arguments) {
-               switch($arguments["type"]) {
-                   
-                       case "textfield":
-                       case "text":
-                           import('form.TextField');
-                           $el = new TextField($arguments["name"]);
-                           $el->setMaxLength(@$arguments["maxlength"]);
-                           if (isset($arguments["aspassword"])) $el->setAsPassword($arguments["aspassword"]);
-                           break;
-                           
+  function getElement($name) {
+    return $this->elements[$name];
+  }
+
+  function getElements() {
+    return $this->elements;
+  }
+
+  function getName() { return $this->name; }
+
+  // addInput - adds an input object to the form.
+  function addInput($arguments) {
+    switch($arguments['type']) {
+      case 'text':
+        import('form.TextField');
+        $el = new TextField($arguments['name']);
+        $el->setMaxLength(@$arguments['maxlength']);
+        if (isset($arguments['aspassword'])) $el->setAsPassword($arguments['aspassword']);
+        break;
+
+      case 'password':
+        import('form.PasswordField');
+        $el = new PasswordField($arguments['name']);
+        $el->setMaxLength(@$arguments['maxlength']);
+        break;
+
+// TODO: refactoring ongoing down from here.
+// aspassword - change this name to something better? Perhaps.
+// Change $arguments to something better too (maybe). $args or $params?
                        case "datefield":
                            import('form.DateField');
                            $el = new DateField($arguments["name"]);
@@ -182,9 +147,8 @@ class Form {
                if ($el!=null) {
                        $el->setFormName($this->name);
                        if (isset($arguments["id"])) $el->setId($arguments["id"]);
-                       if (isset($GLOBALS["I18N"])) $el->setLocalization($GLOBALS["I18N"]);
-                       if (isset($arguments["render"])) $el->setRenderable($arguments["render"]);
-                       if (isset($arguments["enable"])) $el->setEnable($arguments["enable"]);
+                       if (isset($GLOBALS["I18N"])) $el->localize($GLOBALS["I18N"]);
+                       if (isset($arguments["enable"])) $el->setEnabled($arguments["enable"]);
                        
                        if (isset($arguments["style"])) $el->setStyle($arguments["style"]);
                        if (isset($arguments["size"])) $el->setSize($arguments["size"]);
@@ -201,10 +165,10 @@ class Form {
        
        function addInputElement(&$el) {
                if ($el && is_object($el)) {
-                        if (isset($GLOBALS["I18N"])) $el->setLocalization($GLOBALS["I18N"]);
-
+                       if (isset($GLOBALS["I18N"])) $el->localize($GLOBALS["I18N"]);
+               
                        $el->setFormName($this->name);
-                       $this->elements[$el->getName()] = &$el;
+                       $this->elements[$el->name] = &$el;
                }
        }
        
@@ -228,9 +192,9 @@ class Form {
     
     function toStringCloseTag() {
        $html = "\n";
-        foreach ($this->elements as $elname=>$el) {
+       foreach ($this->elements as $elname=>$el) {
             if (strtolower(get_class($this->elements[$elname]))=="hidden") {
-                $html .= $this->elements[$elname]->toStringControl()."\n";
+                $html .= $this->elements[$elname]->getHtml()."\n";
             }
         }
         $html .= "</form>";
@@ -243,7 +207,7 @@ class Form {
         $vars['close'] = $this->toStringCloseTag();
         
         foreach ($this->elements as $elname=>$el) {
-            if (is_object($this->elements[$elname]))
+            if (is_object($this->elements[$elname])) 
                 $vars[$elname] = $this->elements[$elname]->toArray();
         }
 //print_r($vars);
@@ -251,12 +215,12 @@ class Form {
     }
     
     function getValueByElement($elname) {
-        return $this->elements[$elname]->getValue();
+       return $this->elements[$elname]->getValue();
     }
     
     function setValueByElement($elname, $value) {
-        if (isset($this->elements[$elname])) {
-            $this->elements[$elname]->setValue($value);
+       if (isset($this->elements[$elname])) {
+               $this->elements[$elname]->setValue($value);
        }
     }
 }