* @copyright PHP Ajax * @abstract * @version 1.0 */ class phpajax { /** * Class constructor * */ function phpajax() { } function setVar($name) { foreach($name as $k => $v) $this->$k = $v; } /** * PHP Ajax Initialize * * This method call the php ajax process. This method * could be called without a instance. * @access public */ function init() { $f = & $_POST['phpajax']; if ( isset($f) ) { $input = json_decode(stripslashes($f),true); if (! ( isset($input['fnc']) && isset($input['version']) )) return; /* create object */ $obj = new $input['fnc']; /* set variable */ $obj->setVar($input); /* execute */ $obj->main(); /************************************************************/ $s = new stdClass; /* aprint */ if ( isSet($GLOBALS['PHPAJAXINPUTPRINT']) ) foreach($GLOBALS['PHPAJAXINPUTPRINT'] as $k => $v) { $s->aprint[]=$k; $s->aprint[]=$v; } /* ahide - ashow */ if ( isSet($GLOBALS['PHPAJAXINPUTSHOWHIDE']) ) foreach($GLOBALS['PHPAJAXINPUTSHOWHIDE'] as $v) $s->ahideshow[]=$v; /* alert */ if ( isSet($GLOBALS['PHPAJAXALERT']) ) foreach($GLOBALS['PHPAJAXALERT'] as $v) $s->alert[]=$v; /************************************************************/ echo json_encode($s); exit; } } /** * Ajax Main * * This method is executed when an Ajax action is executed. * * @abstract */ function main(){} /** * Ajax Input * * This method is trigged for read all inputs from client. * * @abstract */ function input() {} /** * Shows a message in the client while the ajax request * is processing * * @abstract */ function loading() {} } /** * Ajax Read * * Transform the input required into javascript code. * * @access private */ function aread($p) { $t = & $GLOBALS['PHPAJAXINPUT']; $t .= "\tinput[i++] = '$p';\n"; } /** * Ajax Print * * Prints the result in a HTML object in the client * side. * * @param string $dst HTML object where print * @param string $txt Text to print */ function aprint($dst,$txt) { $v = & $GLOBALS['PHPAJAXINPUTPRINT'][$dst]; $v.= $txt; } /** * Show a message box in the client. * * @param string $text String to show */ function alert($txt) { $v = & $GLOBALS['PHPAJAXALERT']; $v[] = $txt; } /** * Ajax Hide * * Hide a HTML object. * * @param string $obj Object "id". */ function ahide($obj) { $v = & $GLOBALS['PHPAJAXINPUTSHOWHIDE']; $v[]= "ahide('$obj');"; } /** * Ajax Show * * Show a HTML object. * * @param string $obj Object "id". */ function ashow($obj) { $v = & $GLOBALS['PHPAJAXINPUTSHOWHIDE']; $v[]= "ashow('$obj');"; } /** * PHP Ajax Javascript * * Generate the Javascript code for PHP ajax could be executed in the client-side. * * @param string $dir Directory where is the phpajax package * @return String */ function phpajax_js($dir="./") { $f = get_declared_classes(); echo "\n"; echo "\n"; echo "\n"; echo "\n"; } /** * Print JS code * * @access private */ function imprimir($f) { echo "\t$f\n"; } /** * Encode String * * Encode string for print in a javascript statement. * * @access private */ function js_encode($str) { return str_replace(array("\r","\t","\n"),array('\r','\t','\n'),$str); } if ( !is_callable('json_encode') ) { require_once(PHPAJAX_DIR."/JSON.php"); /** * @access private */ function json_encode($obj) { $json = new JSON; $var = $json->serialize( $obj ); return $var; } } if ( !is_callable('json_decode') ) { require_once(PHPAJAX_DIR."/JSON.php"); /** * @access private */ function json_decode($obj,$arr=false) { $json = new JSON; $var = $json->unserialize( $obj ); if ( !$arr ) return $var; $f = get_object_vars($var); foreach($f as $property) $ret[$property] = $var->$property; return $ret; } } ?>