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'),$url);
33 // Adds an executable Javascript code.
35 function addScript($trigger, $sJS)
37 $this->_addCommand($trigger, array('n'=>'js'),$sJS);
41 // Assign target attribute with data.
43 function addAssign($trigger, $sTarget,$sAttribute,$sData)
45 $this->_addCommand($trigger, array('n'=>'as','t'=>$sTarget,'p'=>$sAttribute),$sData);
49 // Updates input element or label with data.
51 function addUpdate($trigger, $sTarget, $sData)
53 $this->_addCommand($trigger, array('n'=>'up','t'=>$sTarget),$sData);
57 // Set disable state of element.
59 function addDisable($trigger, $sTarget, $sData=true)
61 $this->_addCommand($trigger, array('n'=>'di','t'=>$sTarget),$sData);
65 // Set state of element to enabled.
67 function addEnable($trigger, $sTarget, $sData=true)
69 $this->_addCommand($trigger, array('n'=>'di','t'=>$sTarget), !$sData);
75 function addFocus($trigger, $sTarget)
77 $this->_addCommand($trigger, array('n'=>'fc'),$sTarget);
81 // Internal procedure adding command to response.
83 function _addCommand($trigger, $aAttributes, $mData)
85 if ($this->isActive() && ($trigger !== false)) {
86 // display_error('adding '.$trigger.':'.htmlentities($mData));
88 $aAttributes['why'] = $trigger;
89 $aAttributes['data'] = $mData;
90 $this->aCommands[] = $aAttributes;
94 * Register binds function with ajax call parameter
96 function register($trigger, $function)
98 if (isset($_REQUEST[$trigger])) {
105 if (!$this->isActive()) return;
107 // remove not active commands
108 foreach ($this->aCommands as $idx => $com) {
109 // If we should reload whole page content ignore all commands but the update.
110 // This is page repost equivalent, although header and footer are not reloaded.
111 if ($com['why'] !== true && !isset($this->triggers[$com['why']])) {
112 unset($this->aCommands[$idx]);
113 // display_error('unset '.$com['t']);
116 if($com['n'] == 'up' && $com['t'] == '_page_body') {
117 $this->aCommands = array($com);
121 // display_error('Activate:'.htmlentities(print_r($this->triggers, true)));
122 // display_error('Commands :'.htmlentities(print_r($this->aCommands, true)));
123 $GLOBALS['_RESULT'] = $this->aCommands;
130 return $Ajax->isActive();