Location: PHPKode > scripts > Leopard Form > leopardForm.php
<?php

/**
 * Description of form
 *
 * @author Jakub Kuřitka
 */
class leopardForm {

    /**
     *
     * @var array of contents
     */
    private $contents = array();
    /**
     * @var array of validate errors
     */
    private $validateErrors = array();
    /**
     *
     * @var string
     */
    private $formOptions;

    public function __construct($name, $action, $method, array $options = null) {
        if (!is_null($options))
            $this->formOptions = $options;
        $this->formOptions["name"] = md5($name);
        $this->formOptions["action"] = $action;
        $this->formOptions["method"] = strtoupper($method);
    }

    /**
     *
     * @param array $options
     * @return string 
     */
    public function createOptions($options) {
        if (!is_array($options))
            return "";
        $optionsOut = "";
        foreach ($options as $option => $value) {
            if (is_numeric($option)) {
                $optionsOut .= " " . trim($value);
            } else if (is_string($option)) {
                $optionsOut .= " " . trim($option) . "=\"" . trim($value) . "\"";
            }
        }
        return $optionsOut;
    }

    public function validate() {
        if (!$this->requested())
            return false;

        foreach ($this->contents as $name => $content) {
            if (!array_key_exists("rules", $content))
                continue;

            $validateRules = $content["rules"];
            if($content["elemType"] != formElement::CHECKBOX){
                $validateResult = leopardValidator::validate($validateRules, $this->getRequestedData($name));
            } else {
                $validateResult = $this->validateCheckBox($content["checkboxes"], $content["rules"]);
            }
            if (!is_array($validateResult))
                continue;

            $this->validateErrors[$name] = $validateResult;
        }
        if (count($this->validateErrors) == 0)
            return true;

        return false;
    }
    
    private function validateCheckBox(array $checkboxes, array $rules){
        $out = array();
        foreach($checkboxes as $checkbox => $t){
            $name = $this->getName(formElement::CHECKBOX, $checkbox);
            $text = $this->getRequestedData($name);
            $validateResult = leopardValidator::validate($rules, $text);
            if(!is_array($validateResult))
                continue;
            
            $this->validateErrors[$name] = $validateResult;
        }
        if(count($out) > 0)
            return $out;
        return null;
    }
    
    public function __toString() {
        return $this->showForm();
    }

    public function requested() {
        $count = $this->getRequestedData(md5($this->formOptions["name"]) . "c");
        if (!is_null($count) && ($count == count($this->contents)))
            return true;
        return false;
    }

    private function getRequestedData($key) {
        if($this->formOptions["method"] == "GET"){
           return $_GET[trim($key)];
        } else if($this->formOptions["method"] == "POST){
           return $_POST[trim($key)];
        }
    }

    public function getData($type, $key) {
        return $this->getRequestedData(md5($type . $this->formOptions["name"] . trim($key)));
    }

    private function showForm() {
        $out = $this->formBegin();
        foreach ($this->contents as $content) {
            $method = "show" . ucfirst($content["elemType"]);
            if (method_exists("leopardForm", $method)) {
                $out .= $this->$method($content);
            }
        }
        return $out . $this->formEnd();
    }

    private function formBegin() {
        $out = "\n<form";
        $out .= $this->createOptions($this->formOptions);
        $out .= ">\n<table>";
        return $out;
    }

    private function formEnd() {
        $out = "\n\t<input type=\"hidden\" name=\"" . md5($this->formOptions["name"]) . "c\" value=\"" . count($this->contents) . "\">";
        $out .="\n</table>\n</form>";
        return $out;
    }

    private function rowBegin(array $options = null) {
        $out = "\n<tr";
        if(!is_null($options))
            $out .= $this->createOptions ($options);
        $out .= ">";
        return $out;
    }

    private function rowEnd() {
        return "\n</tr>";
    }

    private function cellBegin(array $options = null) {
        $out = "\n\t<td";
        if(!is_null($options))
            $out .= $this->createOptions ($options);
        $out .= ">";
        return $out; 
    }

    private function cellEnd() {
        return "</td>";
    }

    private function showErrors($key, $prefix = "", $postfix = "") {
        if (!$this->requested())
            return "";
        if (!array_key_exists($key, $this->validateErrors))
            return "";
        $out = "";
        foreach ($this->validateErrors[$key] as $error) { 
	   /**
	    * Now you can show errors. How you will do it, depends on you.
	    * $error is array
	    * $error["rule"] contains the rule 
	    * $error["argument"] conatins the argument
	    * Example:
	    * You set the rule -> array(RULES::MIN => 5)
	    * $error["rule"] = RULES::MIN
	    * $error["argument"] = 5
	    */
            if(!isset($error["argument"])){
                $out .= $error["rule"]) . "<br>";
            } else {
                $out .= $error["rule"] . ":" . $error["argument"] . "<br>";
            }
        }
        return $prefix . $out . $postfix;
    }

    public function getName($type, $name) {
        return md5($type . $this->formOptions["name"] . trim($name));
    }
    
    public function addText($text, array $options = array()) {

        $text = array(
            "elemType" => formElement::TEXT,
            "text" => trim($text),
            "options" => $options
        );

        $this->contents[] = $text;
    }

    private function showText(array $text) {
        $out = $this->rowBegin();
        $out .= $this->cellBegin();
        $out .= "<p " . $this->createOptions($text["options"]) . ">" . $text["text"] . "</p>";
        $out .= $this->cellEnd();
        $out .= $this->rowEnd();

        return $out;
    }
    
    public function addHidden($name, $text){
        $name = $this->getName(formElement::HIDDEN, $name);
        
        $options = array(
            "type" => "hidden",
            "name" => $name,
            "value" => $text
        );
        
        $hidden = array(
            "elemType" => formElement::HIDDEN,
            "options" => $options);
        
        $this->contents[$name] = $hidden;
    }
    
    private function showHidden(array $hidden){
        $out = $this->rowBegin();
        $out .= $this->cellBegin();
        $out .= "<input " . $this->createOptions($hidden["options"]) . ">";
        $out .= $this->cellEnd();
        $out .= $this->rowEnd();
        return $out;
    }

    public function addTextInput($name, $label, $defaultText = "", array $rules = null, array $options = array()) {
        $name = $this->getName(formElement::TEXTINPUT, $name);
        
        $options["type"] = "text";
        $options["name"] = $name;
        $options["value"] = trim($defaultText);
        
        $textInput = array(
            "elemType" => formElement::TEXTINPUT,
            "label" => trim($label),
            "options" => $options
        );

        
        if (!is_null($rules))
            $textInput["rules"] = $rules;

        $this->contents[$name] = $textInput;
    }

    private function showTextInput(array $textInput) {
        $errors = $this->showErrors($textInput["options"]["name"]);

        $out = $this->rowBegin();
        $out .= $this->cellBegin();
        $out .= $textInput["label"];
        $out .= $this->cellEnd();
        $out .= $this->cellBegin();

        if ($errors == "" && $this->requested()) {
            $textInput["options"]["value"] = $this->getRequestedData($textInput["options"]["name"]);
        }

        $out .= $errors;
        $out .= "<input" . $this->createOptions($textInput["options"]) . ">";
        $out .= $this->cellEnd();
        $out .= $this->rowEnd();

        return $out;
    }

    public function addPassword($name, $label, array $rules = null, array $options = array()) {
        $name = $this->getName(formElement::PASSWORD, $name);
        
        $options["type"] = "password";
        $options["name"] = $name;
        
        $password = array(
            "elemType" => formElement::PASSWORD,
            "label" => trim($label),
            "options" => $options
        );

        if (!is_null($rules))
            $password["rules"] = $rules;

        $this->contents[$name] = $password;
    }

    private function showPassword(array $password) {
        $errors = $this->showErrors($password["options"]["name"]);

        $out = $this->rowBegin();
        $out .= $this->cellBegin();
        $out .= $password["label"];
        $out .= $this->cellEnd();
        $out .= $this->cellBegin();
        $out .= $errors;
        $out .= "<input" . $this->createOptions($password["options"]) . ">";
        $out .= $this->cellEnd();
        $out .= $this->rowEnd();

        return $out;
    }

    public function addSubmit($text, array $options = array()) {
        $options["type"] = "submit";
        $options["value"] = trim($text);
        
        $submit = array(
            "elemType" => formElement::SUBMIT,
            "options" => $options
        );

        $this->contents[] = $submit;
    }

    private function showSubmit(array $submit) {
        $out = $this->rowBegin();
        $out .= $this->cellBegin();
        $out .= "<input" . $this->createOptions($submit["options"]) . ">";
        $out .= $this->cellEnd();
        $out .= $this->rowEnd();

        return $out;
    }

    public function addButton($text, $value, $type, array $options = array()) {
        $options["type"] = trim($type);
        $options["value"] = trim($value);
        $button = array(
            "elemType" => formElement::BUTTON,
            "text" => trim($text),
            "options" => $options
        );

        $this->contents[] = $button;
    }

    private function showButton(array $button) {
        $out = $this->rowBegin();
        $out .= $this->cellBegin();
        $out .= "<button" . $this->createOptions($button["options"]) . ">";
        $out .= $button["text"];
        $out .= "</button>";
        $out .= $this->cellEnd();
        $out .= $this->rowEnd();

        return $out;
    }

    public function addRadio($name, $label, array $radios, $checked = "", $inLine = false, array $rules = null, array $options = array()) {
        $name = $this->getName(formElement::RADIO, $name);
        
        $options["type"] = "radio";
        $options["name"] = $name;
        
        $radio = array(
            "elemType" => formElement::RADIO,
            "label" => trim($label),
            "radios" => $radios,
            "checked" => trim($checked),
            "inline" => $inLine,
            "options" => $options
        );

        if (!is_null($rules))
            $radio["rules"] = $rules;

        $this->contents[$name] = $radio;
    }

    private function showRadio(array $radio) {
        $options = $this->createOptions($radio["options"]);
        $errors = $this->showErrors($radio["options"]["name"]);

        $out = $this->rowBegin();
        $out .= $this->cellBegin();
        $out .= $radio["label"];
        $out .= $this->cellEnd();
        $out .= $this->cellBegin();
        $out .= $errors;

        if ($this->requested() && $errors == "")
            $radio["checked"] = $this->getRequestedData($radio["options"]["name"]);

        foreach ($radio["radios"] as $value => $text) {
            $out .= "<input" . $options . " value=\"" . $value . "\"";

            if ($radio["checked"] == $value)
                $out .= "checked=\"checked\"";

            $out .= ">";
            $out .= $text;

            if ((bool) $radio["inline"]) {
                $out .= " ";
            } else {
                $out .= "<br>";
            }
        }
        $out .= $this->cellEnd();
        $out .= $this->rowEnd();

        return $out;
    }
    
    public function addCheckBox($label, array $checkboxes, array $checked, $inLine = false, array $rules = null, array $options = array()){
        $options["type"] =  "checkbox";
        
        $checkBox = array(
            "elemType" => formElement::CHECKBOX,
            "label" => trim($label),
            "checkboxes" => $checkboxes,
            "checked" => $checked,
            "inLine" => (bool) $inLine,
            "options" => $options
        );
        
        if(!is_null($rules))
            $checkBox["rules"] = $rules;
        
        $this->contents[] = $checkBox;
    }
    
    public function showCheckBox(array $checkBox){
        $out = $this->rowBegin();
        $out .= $this->cellBegin();
        $out .= $checkBox["label"];
        $out .= $this->cellEnd();
        $out .= $this->cellBegin();
        foreach($checkBox["checkboxes"] as $name => $text){
            $name2 = $this->getName(formElement::CHECKBOX, $name);
            $errors = $this->showErrors($name2, "(", ")");
            
            if($this->requested() && $errors == "" && !is_null($this->getRequestedData($name2))){
                $checkBox["options"]["checked"] = "checked";
            }
            else if(in_array($name, $checkBox["checked"])){
                $checkBox["options"]["checked"] = "checked";
            }
            
            $checkBox["options"]["name"] = $name2;
            $out .= "<input" . $this->createOptions($checkBox["options"]) . ">";
            $out .= " " . trim($text);
            $out .= $errors;
            if(!$checkBox["inLine"]){
                $out .= "<br>";
            } else {
                $out .= " ";
            }
        }
        $out .= $this->cellEnd();
        $out .= $this->rowEnd();
        return $out;
    }

    public function addSelect($name, $label, array $selectOptions, $selected, array $rules = null, array $options = array()) {
        $name = $this->getName(formElement::SELECT, $name);
        
        $options["name"] = $name;
        
        $select = array(
            "elemType" => formElement::SELECT,
            "label" => $label,
            "selectOptions" => $selectOptions,
            "selected" => $selected,
            "options" => $options
        );


        if (!is_null($rules))
            $select["rules"] = $rules;

        $this->contents[$name] = $select;
    }

    private function showSelect(array $select) {
        $out = $this->rowBegin();
        $out .= $this->cellBegin();
        $out .= $select["label"];
        $out .= $this->cellEnd();
        $out .= $this->cellBegin();
        $out .= "<select" . $this->createOptions($select["options"]) . ">";
        
        if ($this->requested() && $errors == "")
            $select["selected"] = $this->getRequestedData($select["options"]["name"]);

        foreach ($select["selectOptions"] as $value => $text) {
            $out .= "<option value=\"" . $value . "\"";
            if ($select["selected"] == $value)
                $out .= " selected=\"selected\"";
            $out .= ">";
            $out .= $text;
            $out .= "</option>";
        }

        $out .= "</select>";
        $out .= $this->cellEnd();
        $out .= $this->rowEnd();
        return $out;
    }
    
    public function addTextArea($name, $label, $defaultText,array $rules = null, array $options = array()){
        $name = $this->getName(formElement::TEXTAREA, $name);
        
        $options["name"] = $name;
        
        $textArea = array(
          "elemType" => formElement::TEXTAREA,
          "label" => trim($label),
          "defaultText" => trim($defaultText),
          "options" => $options
        );
        
        if(!is_null($rules))
            $textArea["rules"] = $rules;
        
        $this->contents[$name] = $textArea;
    }
    
    public function showTextArea(array $textArea){
        $errors = $this->showErrors($textArea["options"]["name"]);
        
        
        $out = $this->rowBegin();
        $out .= $this->cellBegin();
        $out .= $textArea["label"];
        $out .= $this->cellEnd();
        $out .= $this->rowEnd();
        $out .= $this->rowBegin();
        $out .= $this->cellBegin(array("colspan" => "2"));
        $out .= $errors;
        $out .= "<textarea" . $this->createOptions($textArea["options"]) . ">";
        if($this->requested() && $errors == ""){
            $out .= $this->getRequestedData($textArea["options"]["name"]);
        } else {
            $out .= $textArea["defaultText"];
        }
        $out .= "</textarea>";
        $out .= $this->cellEnd();
        $out .= $this->rowEnd();
        
        return $out;
        
    }

}

class formElement {
    const TEXT = "text";
    const HIDDEN = "hidden";
    const TEXTINPUT = "textInput";
    const PASSWORD = "password";
    const SUBMIT = "submit";
    const BUTTON = "button";
    const RADIO = "radio";
    const CHECKBOX = "checkbox";
    const SELECT = "select";
    const TEXTAREA = "textarea";
}

?>
Return current item: Leopard Form