lastbind=$lastbind;
$this->parseCache();
$this->lookForAction();
}
/**
* setUpdating
*
* Sets an html code while the AJAX request is being made.
* @access protected
* @param string $code html code to show while making the request.
*/
function setUpdating($code)
{
$this->updating=$code;
}
/**
* setCurrentFile
*
* Sets filename of the extended class that inherits of AjaxCore.
* @access protected
* @param string $file filename of the extended class.
*/
function setCurrentFile($file)
{
$this->currentfile=$file;
}
/**
* setJSCode
*
* Sets specific JavaScript code to execute before and after the AJAX request is made.
* @access public
* @param string $id Html Id
* @param string $before JavaScript code to execute before the AJAX request is being made.
* @param string $after JavaScript code to execute before the AJAX request is being made.
*/
function setJSCode($id,$before,$after)
{
$this->JSCode[$id]=array($before,$after);
}
/**
* setDebug
*
* set whether it should print JavaScript error when occurrs.
* @access public
* @param bool $debug boolean value.
*/
function setDebug($debug)
{
$this->debug=$debug;
}
/**
* setPlaceHolder
*
* sets the
ID that will be used as placeholder.
* @access protected
* @param string $placeholder
used to return Html results.
*/
function setPlaceHolder($placeHolder)
{
$this->placeholder=$placeHolder;
}
/**
* setMethod
*
* Sets whether the method should be Get or Post.
* @param string $method Get or Post.
* @access protected
*/
function setMethod($method)
{
$this->method=$method;
}
/**
* setCache
*
* Sets whether should use cache or not.
* @param bool $cache boolean value
* @access protected
*/
function setCache($cache)
{
$this->cache=$cache;
}
/**
* parseCache
*
* Parses the current cache.
* @access private
*/
function parseCache()
{
if($this->cache==false)
{
header ("Cache-Control: no-cache, must-revalidate");
}
// header ("Content-Type: text/html; charset=UTF-8");
}
/**
* getJSCode
*
* returns string header JavaScript code for main placeHolder.
* @access public
*/
function getJSCode()
{
$code=array();
$code[]="";
$appended="";
foreach($code as $ech )
$appended.=$ech;
return $appended;
}
/**
* bind
*
* Does the bind between an Html object and PHP function, request will be made when appropiate JavaScript event is triggered.
* @access public
* @param string $id Html ID object
* @param string $event JavaScript event that will cause AJAX request ( onfocus onblur onmouseover onmouseout onmousedown onmouseup onsubmit onclick onload onchange onkeypress onkeydown onkeyup )
* @param string $bindto PHP function that handles the AJAX request
* @param string $params ID of the Html elements that needs to be send within the request, static values (not html elements ) should be sent as _XXX=YYY , whether XXX represents variable name, and YYY value.
* @return string JavaScript code to handle the binding.
*/
function bind($id,$event,$bindto,$params="")
{
if(strlen($params)>0)
$params.=",_AjaxCore=".$this->version;
else
$params.="_AjaxCore=".$this->version;
if(!empty($id) && !empty($event) && !empty($bindto)) // check for empty
{
$code=array();
$arrayparams=explode(",",$params);
$code[]="";
$appended="";
foreach($code as $ech )
$appended.=$ech;
return $appended;
}
}
/**
* onLoad
*
* Does a request to a PHP function, request will be made when onLoad JavaScript event occurs.
* @access public
* @param string $bindto PHP function that handles the AJAX request
* @param string $params ID of the Html elements that needs to be send within the request, static values (not html elements ) should be sent as _XXX=YYY , whether XXX represents variable name, and YYY value.
* @return string JavaScript code to handle the binding.
*/
function onLoad($bindto,$params="")
{
if(strlen($params)>0)
$params.=",_AjaxCore=".$this->version;
else
$params.="_AjaxCore=".$this->version;
if(!empty($bindto)) // check for empty
{
$code=array();
$arrayparams=explode(",",$params);
$code[]="";
$appended="";
foreach($code as $ech )
$appended.=$ech;
return $appended;
}
}
/**
* bindTimer
*
* Does the bind between an Html object and PHP function, request will be made when onLoad JavaScript event occurs and timer expires.
* @access public
* @param string $id Html ID object
* @param string $event JavaScript event that will cause AJAX request ( onfocus onblur onmouseover onmouseout onmousedown onmouseup onsubmit onclick onload onchange onkeypress onkeydown onkeyup )
* @param string $bindto PHP function that handles the AJAX request
* @param string $params ID of the Html elements that needs to be send within the request, static values (not html elements ) should be sent as _XXX=YYY , whether XXX represents variable name, and YYY value.
* @param string $timername name of the timer
* @param int $timerms expiration time in milliseconds
* @return string JavaScript code to handle the binding.
*/
function bindTimer($id,$event,$bindto,$timername,$timerms,$params="")
{
if(strlen($params)>0)
$params.=",_AjaxCore=".$this->version;
else
$params.="_AjaxCore=".$this->version;
if(!empty($id) && !empty($event) && !empty($bindto) && !empty($timername)&& !empty($timerms)) // para no agregar eventos que den error
{
$code=array();
$arrayparams=explode(",",$params);
$code[]="";
$appended="";
foreach($code as $ech )
$appended.=$ech;
return $appended;
}
}
/**
* bindPeriodicalTimer
*
* Does the bind between an Html object and PHP function, request will be made when onLoad JavaScript event occurs and will keep repeating when timer expires.
* @access public
* @param string $id Html ID object
* @param string $event JavaScript event that will cause AJAX request ( onfocus onblur onmouseover onmouseout onmousedown onmouseup onsubmit onclick onload onchange onkeypress onkeydown onkeyup )
* @param string $bindto PHP function that handles the AJAX request
* @param string $params ID of the Html elements that needs to be send within the request, static values (not html elements ) should be sent as _XXX=YYY , whether XXX represents variable name, and YYY value.
* @param string $timername name of the timer
* @param int $timerms expiration time in milliseconds
* @return string JavaScript code to handle the binding.
*/
function bindPeriodicalTimer($id,$event,$bindto,$timername,$timerms,$params="")
{
if(strlen($params)>0)
$params.=",_AjaxCore=".$this->version;
else
$params.="_AjaxCore=".$this->version;
$code=array();
$arrayparams=explode(",",$params);
$code[]="";
$appended="";
foreach($code as $ech )
$appended.=$ech;
return $appended;
}
/**
* lookForAction
*
* Determines what PHP function should be called upon each AJAX request
* @access private
*/
function lookForAction()
{
$this->getRequest();
if(!empty($this->request['bind']) && method_exists($this,$this->request['bind']))
{
$method=$this->request['bind'];
$this->initialize();
$this->$method();
}
}
/**
* intialize
*
* Method that is called just before any PHP function, useful to initialize databases and so on.
* @access protected
*/
function initialize() {} // abstract
/**
* getRequest
*
* Returns Get or Post array.
* @access protected
* @return array request
*/
function getRequest()
{
if($this->method=="get")
$this->request=&$_GET;
else
$this->request=&$_POST;
}
/**
* phpArrayToJS
*
* converts an array from php to JavaScript.
* @access public
* @param array $array php array
* @return string JavaScript array
*/
function phpArrayToJS($array)
{
$items = array();
foreach ($array as $key => $value)
{
if (is_array($value))
$items[]=$this->phpArrayToJS($value);
else if (is_int($value))
$items[]=$value;
else
$items[]="'".$this->escapeJS($value)."'";
}
return '[' . implode(',', $items) . ']';
}
/**
* escapeJS (borrowed from Smarty)
*
* Escape the string to JavaScript
* @access protected
* @param string $string String unscaped
* @return string escaped string
* @link http://smarty.php.net/manual/en/language.modifier.escape.php escape (Smarty online manual)
* @author Monte Ohrt
*/
function escapeJS($string)
{
// escape quotes and backslashes, newlines, etc.
return strtr($string, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n',''=>'<\/'));
}
/**
* alert
*
* return JavaScript Alert Message
* @access private
* @param string $message message to alert
*/
function alert($message,$die=true)
{
$message=$this->escapeJS($message);
$alert="alert('$message');";
if($die)
die($alert);
else return $alert;
}
/**
* arrayToString
*
* returns a sentence form an array
* @access protected
* @param array $array of sentences
* @return string string with sentences
*/
function arrayToString($array)
{
$app="";
foreach ($array as $arr)
$app.=$arr;
return $app;
}
/**
* htmlDisable
*
* disables an html element
* @access protected
* @param string $element is the ID of the element
* @return string sentence to disable element
*/
function htmlDisable($element)
{
return "\$('".$element."').disabled=true;";
}
/**
* htmlEnable
*
* enables an html element
* @access protected
* @param string $element is the ID of the element
* @return string sentence to disable element
*/
function htmlEnable($element)
{
return "\$('".$element."').disabled=false;";
}
/**
* htmlInner
*
* enables an html element
* @access protected
* @param string $element is the ID of the element
* @param string $value is the text to put in
* @return string sentence to disable element
*/
function htmlInner($element,$value)
{
return "\$('".$element."').innerHTML = '".$this->escapeJS($value)."';";
}
}
?>