[0004212] Work Order Entry: fixed error when voided WO refence is reused.
[fa-stable.git] / includes / ajax.inc
1 <?php
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");
13
14 class Ajax extends JsHttpRequest {
15
16         var $aCommands = array();
17         var $triggers = array();
18         
19     function __construct() 
20     {
21           parent::__construct(@$_SESSION['language']->encoding);
22     }
23         //
24         //      This function is used in ctrl routines to activate
25         //      update of ajaxified html element selected by given name/id.
26         //
27         function activate($trigname) {
28           if (in_ajax()) {
29                 $this->triggers[$trigname] = true;
30           }
31         }
32         //
33         //      Javascript clientside redirection.
34         //      This is the last command added to reponse (if any).
35         //
36         function redirect($url) {
37             if(in_ajax()) {
38                         $this->_addCommand(true, array('n'=>'rd'), absolute_url($url));
39                         $this->run();
40             }
41         }
42         //
43         // Popup window (target=_blank)
44         //
45         function popup($url) {
46                   $this->_addCommand(true, array('n'=>'pu'), absolute_url($url));
47         }
48         //
49         // Adds an executable Javascript code.
50         //
51         function addScript($trigger, $sJS)
52         {
53                 $this->_addCommand($trigger, array('n'=>'js'),$sJS);
54                 return $this;
55         }
56         //
57         // Assign target attribute with data.
58         //
59         function addAssign($trigger, $sTarget,$sAttribute,$sData)
60         {
61                 $this->_addCommand($trigger, array('n'=>'as','t'=>$sTarget,'p'=>$sAttribute),$sData);
62                 return $this;
63         }
64         //
65         // Updates input element or label with data.
66         //
67         function addUpdate($trigger, $sTarget, $sData)
68         {
69                 $this->_addCommand($trigger, array('n'=>'up','t'=>$sTarget),$sData);
70                 return $this;
71         }
72         //
73         // Set disable state of element.
74         //
75         function addDisable($trigger, $sTarget, $sData=true)
76         {
77                 $this->_addCommand($trigger, array('n'=>'di','t'=>$sTarget),$sData);
78                 return $this;
79         }
80         //
81         // Set state of element to enabled.
82         //
83         function addEnable($trigger, $sTarget, $sData=true)
84         {
85                 $this->_addCommand($trigger, array('n'=>'di','t'=>$sTarget), !$sData);
86                 return $this;
87         }
88         //
89         // Set current focus.
90         //
91         function addFocus($trigger, $sTarget)
92         {
93                 $this->_addCommand($trigger, array('n'=>'fc'),$sTarget);
94                 return $this;
95         }
96         //
97         // Internal procedure adding command to response.
98         //
99         function _addCommand($trigger, $aAttributes, $mData)
100         {
101                 if ($this->isActive() && ($trigger !== false)) {
102
103                         $aAttributes['why'] = $trigger;
104                         $aAttributes['data'] = $mData;
105                         $this->aCommands[] = $aAttributes;
106                 }
107         }
108
109         function run() 
110         {
111             if (!$this->isActive()) return;
112
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                         }
120                         elseif($com['n'] == 'up' && $com['t'] == '_page_body') {
121                                 $cmds = array($com);
122                                 foreach( $this->aCommands as $k=> $cmd) {
123                                         if ($cmd['n'] == 'fc' || $cmd['n'] == 'js') {   // save focus
124                                                 $cmds[] = $cmd; //break;
125                                         }
126                                 }
127                                 $this->aCommands = $cmds;
128                                 break;
129                         }
130                 }
131             $GLOBALS['_RESULT'] = $this->aCommands;
132         }
133 }
134
135 function in_ajax() {
136     global $Ajax;
137     return $Ajax->isActive();
138 }
139
140 // Returns absolute path of relative $url. To be used in ajax calls
141 // for proper redirection from any referer page.
142 //
143 function absolute_url($url) 
144 {
145    return strpos($url, '..')===0 ? dirname($_SERVER['PHP_SELF']).'/'.$url : $url;
146 }