2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 require_once($path_to_root. "/includes/JsHttpRequest.php");
14 class Ajax extends JsHttpRequest {
16 var $aCommands = array();
17 var $triggers = array();
21 $this->JsHttpRequest(@$_SESSION['language']->encoding);
24 // This function is used in ctrl routines to activate
25 // update of ajaxified html element selected by given name/id.
27 function activate($trigname) {
29 $this->triggers[$trigname] = true;
33 // Javascript clientside redirection.
34 // This is the last command added to reponse (if any).
36 function redirect($url) {
38 $this->_addCommand(true, array('n'=>'rd'), absolute_url($url));
43 // Popup window (target=_blank)
45 function popup($url) {
46 $this->_addCommand(true, array('n'=>'pu'), absolute_url($url));
49 // Adds an executable Javascript code.
51 function addScript($trigger, $sJS)
53 $this->_addCommand($trigger, array('n'=>'js'),$sJS);
57 // Assign target attribute with data.
59 function addAssign($trigger, $sTarget,$sAttribute,$sData)
61 $this->_addCommand($trigger, array('n'=>'as','t'=>$sTarget,'p'=>$sAttribute),$sData);
65 // Updates input element or label with data.
67 function addUpdate($trigger, $sTarget, $sData)
69 $this->_addCommand($trigger, array('n'=>'up','t'=>$sTarget),$sData);
73 // Set disable state of element.
75 function addDisable($trigger, $sTarget, $sData=true)
77 $this->_addCommand($trigger, array('n'=>'di','t'=>$sTarget),$sData);
81 // Set state of element to enabled.
83 function addEnable($trigger, $sTarget, $sData=true)
85 $this->_addCommand($trigger, array('n'=>'di','t'=>$sTarget), !$sData);
91 function addFocus($trigger, $sTarget)
93 $this->_addCommand($trigger, array('n'=>'fc'),$sTarget);
97 // Internal procedure adding command to response.
99 function _addCommand($trigger, $aAttributes, $mData)
101 if ($this->isActive() && ($trigger !== false)) {
102 // display_error('adding '.$trigger.':'.htmlentities($mData));
104 $aAttributes['why'] = $trigger;
105 $aAttributes['data'] = $mData;
106 $this->aCommands[] = $aAttributes;
110 * Register binds function with ajax call parameter
112 function register($trigger, $function)
114 if (isset($_REQUEST[$trigger])) {
121 if (!$this->isActive()) return;
123 // remove not active commands
124 foreach ($this->aCommands as $idx => $com) {
125 // If we should reload whole page content ignore all commands but the update.
126 // This is page repost equivalent, although header and footer are not reloaded.
127 if ($com['why'] !== true && !isset($this->triggers[$com['why']])) {
128 unset($this->aCommands[$idx]);
129 // display_error('unset '.$com['t']);
132 if($com['n'] == 'up' && $com['t'] == '_page_body') {
134 foreach( $this->aCommands as $k=> $cmd) {
135 if ($cmd['n'] == 'fc' || $cmd['n'] == 'js') { // save focus
136 $cmds[] = $cmd; //break;
139 $this->aCommands = $cmds;
143 // display_error('Activate:'.htmlentities(print_r($this->triggers, true)));
144 // display_error('Commands :'.htmlentities(print_r($this->aCommands, true)));
145 $GLOBALS['_RESULT'] = $this->aCommands;
152 return $Ajax->isActive();
155 // Returns absolute path of relative $url. To be used in ajax calls
156 // for proper redirection from any referer page.
158 function absolute_url($url)
160 return strpos($url, '..')===0 ? dirname($_SERVER['PHP_SELF']).'/'.$url : $url;