Location: PHPKode > projects > PHP Ajax Forms Framework > pajff/pajff.php
<?php
/**
* PAjFF main engine.
* 
* <p>This is the main engine to run the framework.</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
* 
* Started on 2006-05-12. 
*
**/

include_once("pajff_common.php");
include_once("pajff_session.php");
include_once("pajff_form.php");
include_once("pajff_event.php");
include_once("pajff_debuger.php");
include_once("pajff_dialogs.php");

/**
 * PAjFF version
 * 
 * @return string
 * 
 * <code>
 * print pajffVersion();
 * </code>
 */
function pajffVersion(){
    return "PAjFF v0.0.2 / 20.05.2006";
}

/**
 * PAjFF engine codepage- short name. If defined MYSQL "set names" is called 
 * @global string $pajffcharset 
 */
global $pajffcharset;

if ($pajffcharset) {
    $pajffdefaultdatabase->setQuery( "set names '$pajffcharset'" );
    $pajffdefaultdatabase->query();
}

/**
 * Path to find XAJAX library
 * @global string $pajffxajaxpath
 */
if (!$pajffxajaxpath){
    $pajffxajaxpath='pajff/ajax';
}

/*
* Initialize XAJAX
*/

/**
 * PAjFF framework charset for XML communication and page text
 * @global string $pajffcharsetlong
 */
global $pajffcharsetlong;
ob_start();
//print pajffRealPath().'../ajax/xajax.inc.php';
if ($pajffcharsetlong){
    //et default XML response charset
    define ('XAJAX_DEFAULT_CHAR_ENCODING', $pajffcharsetlong );
}
require_once (pajffRealPath().'ajax/xajax.inc.php');

ob_end_clean();

//PHP session support
if(session_id() == "")
{
    session_start();
}
else
{
    //already in it
}

global $pajffappname;

/**
 * XAJAX object used by the framework
 * @global xajax $xajax
 */
global $xajax;

/**
 * PAjFF main application file name
 * @global string $pajffappname
 */

$xajax = new xajax(pajffPath().'../'.$pajffappname.'.php');

/*
* XAJAX initialization block
*/
if ($xajax) {
    if ($pajffcharset){
        $xajax->setCharEncoding($pajffcharset);
        $xajax->decodeUTF8InputOn();
    }
    if ($pajffdebug){
        $xajax->debugOn();
    }
    $xajax->registerFunction('pajffHandleEvent');
    /*
    register more ajax functions here if needed :) define registerAJAX in the
    main module of your application. Every ajax called function must
    define a global variable called ajaxcall and set it to true
    most pjaff stuff has to use pajff
    */
    if (function_exists('registerAJAX')) { registerAJAX($xajax); }
}

/* End XAJAX support */

/**
 * insert a javascript by its URL
 * @param string $scripturl URL for the script
 * 
 * <code>
 * includejs('http://dir.bg/a.js');
 * </code>
 */
function includejs($scripturl){
	?>
	<script type="text/javascript" language="javascript">
	<?php
	include($scripturl);
	?>
	</script>
	<?php
}

/**
 * Register PAjFF event with form submition.
 * @param string $event Event name
 * @param pajff_session $session form session to handle the event
 * @return string HTML code to register a javascript call to DoClick
 * 
 * <code>
 * $result .= "<input type=\"button\" onclick=\"".pajffFormEventReg('ok',$session)."\">";
 * </code>
 */
function pajffFormEventReg($event,$session){
    //submit button pressed. passes all form values to the event handler.
    $res= "return submitClick('$event','".$session->formtype."','".$session->sessionID()."')";
    return $res;
}

/**
 * Register an event- basic
 *
 * @param string $event Event name
 * @param pajff_session $session form session to handle the event
 * @param string $eventcontainer name of an HTML element containing event data
 * @param string $eventvalue value to pass if no event container
 * @param boolean $passform a flag if PAjFF has to submit form data in $event->moreparams
 * @return string HTML code to register a javascript to call DoClick
 * 
 * <code>
 * //Pass 'value' to the event handler and all form data in $event->moreparams
 * $html .= "<a href=\"javascript::void(null)\" onclick=\"".
 *		    pajffSimpleEventReg('anevent',$session,'','value',true).
 *          "\">try </a>";
 * </code>
 * 
 * <code>
 * //Get the 'textfield' element value and pass only it to the event handler
 * $html .= "<a href=\"javascript::void(null)\" onclick=\"".
 *		    pajffSimpleEventReg('anevent',$session,'textfield','').
 *          "\"> try </a>";
 * </code>
 * 
 */
function pajffSimpleEventReg($event,$session,$eventcontainer,$eventvalue, $passform= false){
    /*
    a link or something else. here we give a container to get the event param value or
    just a value.
    used when we want to change just a simple div or make a basic validation or sth like that.
    generates
    return doClick(event,formtype,sesid,container,value,pass)
    */
    $res= "doClick('$event','".$session->formtype."','".$session->sessionID()."',
	'$eventcontainer','$eventvalue'";
    if ($passform) {
        $res .= ",true";
    } else {
        $res .= ",false";
    }
    $res .= ")";
    return $res;
}

/**
 * PAjFF main session
 * @global pajff_session $pajffmainsession
 */
global $pajffmainsession;

/**
 * Add a debug message to framework debugger queue
 *
 * @param string $message message text
 * <code>
 * debugMessage('message');
 * </code>
 */
function debugMessage($message){
    global $pajffmainsession;
    if ($pajffmainsession->debuger){
        $pajffmainsession->debuger->addMessage($message);
    }
}


global $pajffcharset;
if ($pajffcharset) {
    $pajffdefaultdatabase->setQuery( "set names '$pajffcharset'" );
    $pajffdefaultdatabase->query();
}

/**
 * A flag to indicate whether the framework is called by AJAX
 * @global boolean $ajaxcall
 */
global $ajaxcall;
$ajaxcall= false;
$xajax->processRequests();

/**
 * PAjFF page header. It's called internally by
 * the framework.
 *
 * @param pajffmainapp $app
 * @return string
 */
function pajffHeader($app){
    $result = "";
    ob_start();
    include pajffRealPath()."pajff_header.php";
    ob_end_clean();
    return $result;
}

/**
 * PAjFF page footer. It's called internally by the
 * framework.
 *
 * @param pajffmainapp $app
 * @return string
 */
function pajffFooter($app){
    $result = "";
    ob_start();
    include pajffRealPath()."pajff_footer.php";
    ob_end_clean();
    return $result;
}

/*
* Start application if no AJAX call
*/
if (!$ajaxcall){
    /*
    No ajax call has been made. So start the application itself.
    */
    //print "no ajax beg";
    //Main session for the framework
    global $pajffmainsession;
    if ($_GET['mainsession']){
        $pajffmainsession= new pajff_session(null,null,$_GET['mainsession']);
    } else {
        $pajffmainsession= new pajff_session();
        //the element where to store forms
        $pajffmainsession->htmlelement="div".$pajffmainsession->sessionID();
        $pajffmainsession->debugelement= "dbg".$pajffmainsession->sessionID();
        $pajffmainsession->debuger= new pajff_debuger();
    }

    if (class_exists('pajffmainapp')) {
        $mainapp= new pajffmainapp();
        print pajffHeader($mainapp);
        print $mainapp->runApp();
        print pajffFooter($mainapp);
    } else {
        print "No application is defined!";
    }

    $pajffmainsession->saveSession();
}

?>
Return current item: PHP Ajax Forms Framework