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