<?

/*
нота - если надо перекрыть конструктор - создаем че надо в диалоге, и только ПОТОМ parent::constructor()
*/

class Dialog extends Component {

    var $params = array("target");
    var $silent = false;
    var $validators = array();
    var $form_valid = true;
    var $isReadyForAction = false;


    function action() {
        if($this->isReadyForAction && !empty($_REQUEST["action"])) {
            if(method_exists($this, $action = "action_".$_REQUEST["action"]))
                call_user_func(array(&$this, $action));
            $this->remove_temporary_files();
        }
    }


    function handleRequest() {
        parent::handleRequest();

        if(empty($_SESSION[_FORM_DATA]) && (count($_POST) > 0)) {

            $_SESSION[_FORM_DATA] = $_REQUEST;

            if (!empty($_FILES)) {
                ### file uploads
                foreach ($_FILES as $field_name => $file) {
                    ### copy to temporary directory
                    $uploaddir = "var/tmp/";
                    $uploadfile = $uploaddir.session_id()."_".basename($_FILES[$field_name]['name']);
                    $_SESSION[_FORM_DATA][_FILE][$field_name] = $_FILES[$field_name];

                    if (!empty($_FILES[$field_name]['tmp_name']))
                        if (move_uploaded_file($_FILES[$field_name]['tmp_name'], 
                                           $uploadfile)) 
                            $_SESSION[_FORM_DATA][_FILE][$field_name]['tmp_name'] = $uploadfile;
                } // foreach
            }// if (!empty($_FILES)) 
            
            $param_str = array();
            foreach($this->params as $param) 
                if(!empty($_REQUEST[$param])) {
                    $param_str[] = $param."=".$_REQUEST[$param];
                }
            $param_str = implode("&", $param_str);
            if(!empty($param_str)) {
                $param_str = "?".$param_str;
            }

            $this->redirect((isset($_REQUEST['idomain_name']) ? $_REQUEST['idomain_name'] : $_SERVER["SERVER_NAME"]).$_SERVER["PHP_SELF"].$param_str);
        } 
        else {

            if(isset($_SESSION[_FORM_DATA])) {
                $_REQUEST = array_merge($_REQUEST, $_SESSION[_FORM_DATA]);
                unset($_SESSION[_FORM_DATA]);
            }

            // проверяем форму
            if( !empty($_REQUEST["action"]) ) {
                foreach($_REQUEST as $field => $class_name) {
                    if (is_integer(strpos($field,"validator_"))) {   
                        $field_name = substr($field,10,100);
                        $validator =& $this->addComponent($class_name, $field_name);

                        if(!$validator->doValid()) {
                            $this->form_valid = false;                            
#                            break; // all doValid must be processed
                        }                       
                  }
                }
             }

             // если все ок - экшен

             if($this->form_valid) {
                 $this->isReadyForAction = true;
             } 
             else {
                 $this->form_fields = $_REQUEST;
                 $this->remove_temporary_files();
             }
        }
    }



    function &addComponent($class_name, $name) {

        if (!isset($this->$name)) {
            $this->$name =& func_new($class_name);
            $this->$name->name = $name;

            $this->$name->dialog =& $this;

            foreach($this->params as $p) {
                $this->$name->$p = empty($this->$p) ? "" : (string) $this->$p;
            }

        } else {
            $this->$name->is_loaded = true;
        }
        return $this->$name;
    }



    function getSilent() {
        return $this->silent;
    } 

    // removes temporary files $_FILES[]
    function remove_temporary_files() {
        if (isset($_REQUEST[_FILE]) && (count($_REQUEST[_FILE])>0))
            foreach ($_REQUEST[_FILE] as $field_name => $fdata) 
                if (!empty($fdata['tmp_name']))
                    unlink($fdata['tmp_name']);                       
    }

    function getAccessLevel() {
        return 0;
    }


    function clearForm() {
        $this->form_fields = array();
    }
}
?>