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();
19 function __construct()
21 parent::__construct(@$_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)) {
103 $aAttributes['why'] = $trigger;
104 $aAttributes['data'] = $mData;
105 $this->aCommands[] = $aAttributes;
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]);
120 elseif($com['n'] == 'up' && $com['t'] == '_page_body') {
122 foreach( $this->aCommands as $k=> $cmd) {
123 if ($cmd['n'] == 'fc' || $cmd['n'] == 'js') { // save focus
124 $cmds[] = $cmd; //break;
127 $this->aCommands = $cmds;
131 $GLOBALS['_RESULT'] = $this->aCommands;
137 return $Ajax->isActive();
140 // Returns absolute path of relative $url. To be used in ajax calls
141 // for proper redirection from any referer page.
143 function absolute_url($url)
145 return strpos($url, '..')===0 ? dirname($_SERVER['PHP_SELF']).'/'.$url : $url;