<?
class Component extends Object {

var $form_elements = array();
var $params = array();
var $form_fields = array();
var $elements_buf = array();
var $is_loaded = false;
    
    function constructor() {
        parent::constructor();
        foreach($this->params as $p) {
            $this->$p = empty($_GET[$p]) ? "" : (string) $_GET[$p];
        }
        $this->handleRequest();
        $this->templater =& new HTML_Template_Flexy($GLOBALS['_FLEXY_CONFIG']);
    }



    function makeData() {

    }

    
    function display($template = null) {
        $this->elements_buf[] = $this->templater->elements;

        if(!empty($template)) {
            $this->templater->compile($template);
        } else {
            $this->templater->compile($this->getTemplate());
        }

        $this->fillForm();

        $this->templater->outputObject($this, $this->form_elements);

        $this->templater->elements = array_pop($this->elements_buf);
    }



    function fillForm() {                    
        $elements =& $this->templater->getElements();

        if(empty($elements)) {
            return;
        }

        foreach($elements as $name => $element) 
            if( ($element->attributes["type"] != "hidden") && empty($this->form_elements[$name])) {

                $value = empty($this->form_fields[$name]) ? "" : $this->form_fields[$name];

                if(!empty($value)) {
                    $this->form_elements[$name] = new HTML_Template_Flexy_Element;
                    $this->form_elements[$name]->setValue($value);
                }
            }
    }



    function handleRequest() {
        if(empty($_REQUEST["target"])) {
            $this->target = "";
        } else {
            $this->target = $_REQUEST["target"];
        }
    }



    function isValid() {
        return true;
    }


    function getTemplate() {
        return $this->template;
    }


    function setTemplate($template) {
        $this->template = $template;
    }


    function redirect($url = null) {
        $this->redirect_url = $url;
        if(!empty($this->redirect_url)) {
            header("Location: http://".$this->redirect_url);
        }
    }



    function getRequest($name) {
        return !isset($_REQUEST[$name]) || empty($_REQUEST[$name]) ? '' : $_REQUEST[$name];
    }


    function addFormFields($variant) {
        $this->form_fields = $variant + $this->form_fields;
    }

    // logical funcs

    function eq($a, $b) {
        return $a == $b;
    }

    function mod($a, $b) {
        return ($a % $b) == 0;
    }

}
?>