2 require_once($path_to_root. "/includes/JsHttpRequest.php");
4 class Ajax extends JsHttpRequest {
6 var $aCommands = array();
7 var $triggers = array();
11 $this->JsHttpRequest($_SESSION['language']->encoding);
14 // This function is used in ctrl routines to activate
15 // update of ajaxified html element selected by given name/id.
17 function activate($trigname) {
19 $this->triggers[$trigname] = true;
23 // Javascript clientside redirection.
24 // This is the last command added to reponse (if any).
26 function redirect($url) {
28 $this->_addCommand(true, array('n'=>'rd'), absolute_url($url));
33 // Popup window (target=_blank)
35 function popup($url) {
36 $this->_addCommand(true, array('n'=>'pu'), absolute_url($url));
39 // Adds an executable Javascript code.
41 function addScript($trigger, $sJS)
43 $this->_addCommand($trigger, array('n'=>'js'),$sJS);
47 // Assign target attribute with data.
49 function addAssign($trigger, $sTarget,$sAttribute,$sData)
51 $this->_addCommand($trigger, array('n'=>'as','t'=>$sTarget,'p'=>$sAttribute),$sData);
55 // Updates input element or label with data.
57 function addUpdate($trigger, $sTarget, $sData)
59 $this->_addCommand($trigger, array('n'=>'up','t'=>$sTarget),$sData);
63 // Set disable state of element.
65 function addDisable($trigger, $sTarget, $sData=true)
67 $this->_addCommand($trigger, array('n'=>'di','t'=>$sTarget),$sData);
71 // Set state of element to enabled.
73 function addEnable($trigger, $sTarget, $sData=true)
75 $this->_addCommand($trigger, array('n'=>'di','t'=>$sTarget), !$sData);
81 function addFocus($trigger, $sTarget)
83 $this->_addCommand($trigger, array('n'=>'fc'),$sTarget);
87 // Internal procedure adding command to response.
89 function _addCommand($trigger, $aAttributes, $mData)
91 if ($this->isActive() && ($trigger !== false)) {
92 // display_error('adding '.$trigger.':'.htmlentities($mData));
94 $aAttributes['why'] = $trigger;
95 $aAttributes['data'] = $mData;
96 $this->aCommands[] = $aAttributes;
100 * Register binds function with ajax call parameter
102 function register($trigger, $function)
104 if (isset($_REQUEST[$trigger])) {
111 if (!$this->isActive()) return;
113 // remove not active commands
114 foreach ($this->aCommands as $idx => $com) {
115 // If we should reload whole page content ignore all commands but the update.
116 // This is page repost equivalent, although header and footer are not reloaded.
117 if ($com['why'] !== true && !isset($this->triggers[$com['why']])) {
118 unset($this->aCommands[$idx]);
119 // display_error('unset '.$com['t']);
122 if($com['n'] == 'up' && $com['t'] == '_page_body') {
124 foreach( $this->aCommands as $k=> $cmd) {
125 if ($cmd['n'] == 'fc') { // save focus
126 $cmds[] = $cmd; break;
129 $this->aCommands = $cmds;
133 // display_error('Activate:'.htmlentities(print_r($this->triggers, true)));
134 // display_error('Commands :'.htmlentities(print_r($this->aCommands, true)));
135 $GLOBALS['_RESULT'] = $this->aCommands;
142 return $Ajax->isActive();
145 // Returns absolute path of relative $url. To be used in ajax calls
146 // for proper redirection from any referer page.
148 function absolute_url($url)
150 return strpos($url, '..')===0 ? dirname($_SERVER['PHP_SELF']).'/'.$url : $url;