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 // display_error('Activate '. $trigger);
20 $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);
73 // Internal procedure adding command to response.
75 function _addCommand($trigger, $aAttributes, $mData)
77 if ($this->isActive() && ($trigger !== false)) {
78 // display_error('adding '.$trigger.':'.htmlentities($mData));
80 $aAttributes['why'] = $trigger;
81 $aAttributes['data'] = $mData;
82 $this->aCommands[] = $aAttributes;
86 * Register binds function with ajax call parameter
88 function register($trigger, $function)
90 if (isset($_REQUEST[$trigger])) {
97 if (!$this->isActive()) return;
98 // $this->addScript(true, "setFocus('".$_POST['_focus']."');");
100 // remove not active commands
101 foreach ($this->aCommands as $idx => $com) {
102 if ($com['why'] !== true && !isset($this->triggers[$com['why']]))
103 unset($this->aCommands[$idx]);
107 $GLOBALS['_RESULT'] = $this->aCommands;
114 return $Ajax->isActive();