Location: PHPKode > projects > PHP Ajax Forms Framework > pajff/pajff_form.php
<?php
/**
* PAjFF form class
* 
* <p>This part of the framework controls forms.</p>
* 
* {@link http://sourceforge.net/projects/pajff Project home}
* 
* @package PAjFF
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* @author Atanas Markov hide@address.com
* @version 0.0.2
* @copyright 2006 by Atanas Markov
*
* @subpackage pajff_form
**/

include_once("pajff_common.php");
include_once("pajff_event.php");
include_once("pajff_session.php");

/**
 * Form class
 * 
 * @package PAjFF
 * @subpackage pajff_form
 */
class pajff_form{
    /**
     * form session
     *
     * @var pajff_session
     */
    public $session;
    /**
     * Form type name
     *
     * @var string
     */
    protected $formtype;
    /**
     * A flag whether to destroy form session on framework exit
     *
     * @var boolean
     */
    private $closeme= false;

    /**
     * Class constructor
     *
     * @param string $formtype form type name
     * @param pajff_session $parent parent session. Use it with new forms
     * @param string $id Use this to recreate old forms
     */
    public function __construct($formtype="",$parent=null,$id=null){
        $this->formtype = $formtype;
        $this->session = new pajff_session($parent);

        if ($id){
            $this->session->loadSession($id);
        }
        if ($this->session->formtype) {
            //For old sessions load the type of the form
            $this->formtype = $this->session->formtype;
        }
        $this->session->formtype=$this->formtype;
        //$this->session->parentsession=$parent;
        $this->session->sessionform= $this;
        $vals=get_object_vars($session);
        $this->loadFormValues($vals);
    }

    /**
     * Load form values form array/object. I don't know why I wrote it
     *
     * @param mixed $values
     */
    function loadFormValues($values=null){
        if ($values){
            //values comes from AJAX using POST method
            $arr = array();
            if (is_object($values)){
                $arr= get_object_vars($values);
            } else if (is_array($values)) {
                $arr= $values;
            }

            foreach ($arr as $key => $value){
                if ($key!='sessionform'&&$key!='database'&&$key!='parentsession'){
                    $this->$key = $value;
                }
            }
        }
    }

    /**
     * change form session
     *
     * @param string $id new session id
     */
    public function setSession($id){
        $this->session->loadSession($id);
    }

    /**
     * Set closeme flag so form session to be destroyed
     *
     */
    public function closeForm(){
        //destroy the form session at destruction
        $this->closeme = true;
    }

    /**
     * Called by the event-handling mechanism. This reads the event definition file
     * and calls formevent($session,$event). Support of built-in events since v0.0.2
     *
     * @param pajff_event $formevent
     * @return mixed what was returned by the event handler
     */
    public function processEvent(&$formevent){

        if ($formevent->event[0]!='_'){
            //not a main function
            $filename = pajffRealPath().'handlers/'.$this->formtype.'.php';
            if (file_exists($filename)){
                //0.0.2 - pajff internal form types
                ob_start();
                include_once($filename);
                ob_end_clean();
            } else {
                //0.0.1 code - non internal forms
                $filename = pajffRealPath().'../handlers/'.$this->formtype.'.php';
                if (file_exists($filename)){
                    ob_start();
                    include_once($filename);
                    ob_end_clean();
                }
            }
            
            if (function_exists($this->formtype.$formevent->event)){
                return call_user_func($this->formtype.$formevent->event,$this->session,$formevent);
            } else {
                /*
                * v0.0.2 calls dialog instead hanging the system
                */
                return pajffDialogOk($this->session,"Handler definition <b>$filename:$this->formtype$formevent->event</b> not found!");
            }
        } else {
            $formevent->event= substr($formevent->event,1,strlen($formevent->event));
            //main event
            $filename = pajffRealPath().'pajff_handlers.php';
            if (file_exists($filename)){
                ob_start();
                include_once($filename);
                ob_end_clean();
            }

            $filename = pajffRealPath().'../handlers/pajff.php';
            if (file_exists($filename)){
                ob_start();
                include_once($filename);
                ob_end_clean();
            }
        }

        if (function_exists('pajffmain'.$formevent->event)){
            return call_user_func('pajffmain'.$formevent->event,$this->session,$formevent);
        } else {
            return pajffDialogOk($this->session,"Handler definition <b>pajffmain$formevent->event</b> not found!");
        }

    }

    /**
     * Draw the form. Reads form definition file and calls formForm($session)
     *
     * @return string form HTML
     */
    public function drawForm(){
        //0.0.2- internal form types - modal dialogs
        $filename = pajffRealPath()."forms/".$this->formtype.".php";
        if (file_exists($filename)){
            ob_start();
            include_once($filename);
            ob_end_clean();
        } else {
            //old 0.0.1 code for non internal forms
            $filename = pajffRealPath()."../forms/".$this->formtype.".php";
            //	print $filename;

            if (file_exists($filename)){
                ob_start();
                include_once($filename);
                ob_end_clean();
            }
        }

        /**
         * @global $pajffmainsession
         */
        global $pajffmainsession;

        if ($this->session->parentId()){
            $res= "<p align=\"right\"><input type=\"button\" value=\"x\" onclick=\"".
            pajffSimpleEventReg('_closeform',$this->session,'','',false)
            ."\"></p>";
        }

        if (function_exists($this->formtype.'Form')){
            $res.= "
			<form name=\"$this->formtype\"
			id=\"$this->formtype\"
			method=\"POST\"
			onsubmit=\"ajaxSubmit();\"
			action=\"javascript::void(null)\"
			>".
            call_user_func($this->formtype.'Form',$this->session).
            "</form>";

            return $res;
        } else {
            return pajffDialogOk($this->session,"Form definition file for <b>$this->formtype</b> not found!");
        }
    }

    /**
     * Destructor that cleans form session on closeme flag. Otherwise it saves
     * form session for next use.
     *
     */
    public function __destruct(){
        if ($this->closeme) {
            $this->session->clearSession();
        } else {
            //save form variables to its session
            $arr= get_object_vars($obj);
            foreach ($arr as $key => $value){
                if ($key!='session'){
                    $this->session->$key=$value;
                }
            }
            $this->session->saveSession();
        }
    }
}

//print "pajff_form";

?>
Return current item: PHP Ajax Forms Framework