Integration of JsHttpRequest ajax transport class
[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 //          display_error('Activate '. $trigger);
20                 $this->triggers[$trigname] = true;
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'),$url);
29                 $this->run();
30             }
31         }
32         //
33         // Adds an executable Javascript code.
34         //
35         function addScript($trigger, $sJS)
36         {
37                 $this->_addCommand($trigger, array('n'=>'js'),$sJS);
38                 return $this;
39         }
40         //
41         // Assign target attribute with data.
42         //
43         function addAssign($trigger, $sTarget,$sAttribute,$sData)
44         {
45                 $this->_addCommand($trigger, array('n'=>'as','t'=>$sTarget,'p'=>$sAttribute),$sData);
46                 return $this;
47         }
48         //
49         // Updates input element or label with data.
50         //
51         function addUpdate($trigger, $sTarget, $sData)
52         {
53                 $this->_addCommand($trigger, array('n'=>'up','t'=>$sTarget),$sData);
54                 return $this;
55         }
56         //
57         // Set disable state of element.
58         //
59         function addDisable($trigger, $sTarget, $sData=true)
60         {
61                 $this->_addCommand($trigger, array('n'=>'di','t'=>$sTarget),$sData);
62                 return $this;
63         }
64         //
65         // Set state of element to enabled.
66         //
67         function addEnable($trigger, $sTarget, $sData=true)
68         {
69                 $this->_addCommand($trigger, array('n'=>'di','t'=>$sTarget), !$sData);
70                 return $this;
71         }
72         //
73         // Internal procedure adding command to response.
74         //
75         function _addCommand($trigger, $aAttributes, $mData)
76         {
77           if ($this->isActive() && ($trigger !== false)) {
78 //              display_error('adding '.$trigger.':'.htmlentities($mData));
79
80                 $aAttributes['why'] = $trigger;
81                 $aAttributes['data'] = $mData;
82                 $this->aCommands[] = $aAttributes;
83           }
84         }
85         /*
86         * Register binds function with ajax call parameter
87         
88         function register($trigger, $function) 
89         {
90             if (isset($_REQUEST[$trigger])) {
91                 $function(&$this);
92             }
93         }
94         */
95         function run() {
96             
97             if (!$this->isActive()) return;
98 //              $this->addScript(true, "setFocus('".$_POST['_focus']."');");
99                 
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]);
104
105                 }
106
107             $GLOBALS['_RESULT'] = $this->aCommands;
108 //          exit();
109         }
110 }
111
112 function in_ajax() {
113     global $Ajax;
114     return $Ajax->isActive();
115 }
116
117 ?>