~oh-dev/openhealth/phit-tools

« back to all changes in this revision

Viewing changes to ihris-suite/lib/i2ce/modules/Stub/I2CE_Stub.php

  • Committer: litlfred at ibiblio
  • Date: 2009-10-23 12:59:28 UTC
  • Revision ID: litlfred@ibiblio.org-20091023125928-u5lkafz0urm9t8eq
updated ihris-suite to 4.0.1-prerelease -- not debugged

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
 * © Copyright 2007, 2008 IntraHealth International, Inc.
4
 
 * 
5
 
 * This File is part of I2CE 
6
 
 * 
7
 
 * I2CE is free software; you can redistribute it and/or modify 
8
 
 * it under the terms of the GNU General Public License as published by 
9
 
 * the Free Software Foundation; either version 3 of the License, or
10
 
 * (at your option) any later version.
11
 
 * 
12
 
 * This program is distributed in the hope that it will be useful, 
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
15
 
 * GNU General Public License for more details.
16
 
 * 
17
 
 * You should have received a copy of the GNU General Public License 
18
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
 */
20
 
/**
21
 
 *  I2CE_Stub
22
 
 * @package I2CE
23
 
 * @subpackage Core
24
 
 * @author Carl Leitner <litlfred@ibiblio.org>
25
 
 * @copyright Copyright &copy; 2007, 2008 IntraHealth International, Inc. 
26
 
 * This file is part of I2CE. I2CE is free software; you can redistribute it and/or modify it under 
27
 
 * the terms of the GNU General Public License as published by the Free Software Foundation; either 
28
 
 * version 3 of the License, or (at your option) any later version. I2CE is distributed in the hope 
29
 
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
30
 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have 
31
 
 * received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
32
 
 * @version 2.1
33
 
 * @access public
34
 
 */
35
 
 
36
 
 
37
 
class I2CE_Stub extends I2CE_Module{
38
 
        
39
 
    public static function getMethods() {
40
 
        return array(
41
 
            'I2CE_Page::addAjaxUpdate'=>'addAjaxUpdate',
42
 
            'I2CE_Template::addAjaxUpdate'=>'addAjaxUpdate',
43
 
            'I2CE_Page::addAjaxUpdateFunctionRequest'=>'addAjaxUpdateFunctionRequest',
44
 
            'I2CE_Template::addAjaxUpdateFunctionRequest'=>'addAjaxUpdateFunctionRequest',
45
 
            'I2CE_Page::addAjaxUpdateFunctionComplete'=>'addAjaxUpdateFunctionComplete',
46
 
            'I2CE_Template::addAjaxUpdateFunctionComplete'=>'addAjaxUpdateFunctionComplete',
47
 
            'I2CE_Page::hasAjax'=>'hasAjaxFuzzy',
48
 
            'I2CE_Template::hasAjax'=>'hasAjaxFuzzy'
49
 
            ); 
50
 
    }  
51
 
 
52
 
 
53
 
    public static function getHooks() {
54
 
        return array(
55
 
            'pre_page_action'=>'ajaxTest',
56
 
            'template_post_display'=> 'writeOutJS'
57
 
            );
58
 
    }
59
 
 
60
 
 
61
 
    public function __construct() {
62
 
        parent::__construct();
63
 
        $this->js = '';
64
 
        $this->js_func = '';
65
 
    }
66
 
 
67
 
 
68
 
    public function ajaxTest($page) {
69
 
        if (!isset($_SESSION['stub_ajax_test'])) {
70
 
            $_SESSION['stub_ajax_test'] = 'unknown0';
71
 
        }
72
 
        if (is_bool($_SESSION['stub_ajax_test'])) {
73
 
            return;
74
 
        } 
75
 
        if (preg_match('/^unknown(\d*)/',$_SESSION['stub_ajax_test'],$matches)) {
76
 
            if ($matches[1] > 5) {
77
 
                $_SESSION['stub_ajax_test'] = false; //assume after 5 page request we never got a response
78
 
            } else {
79
 
                $matches[1]++;
80
 
                $_SESSION['stub_ajax_test'] = 'unknown' . $matches[1];
81
 
            }
82
 
        } else {
83
 
            $_SESSION['stub_ajax_test'] = 'unknown0';
84
 
        }
85
 
        $stub_url = "stub-ajax";
86
 
        if (getenv("I2CE_Rewritten") !== "On") {
87
 
            $stub_url = "index.php/$stub_url";
88
 
        }
89
 
            
90
 
        $startJS = 
91
 
            "
92
 
var Stub_Ajax_Test = {
93
 
   start: function(){   
94
 
        new Ajax('$stub_url',{method:'get'}).request();
95
 
   }
96
 
};
97
 
window.addEvent('load',Stub_Ajax_Test.start);
98
 
";
99
 
        $template = $page->getTemplate();
100
 
        $template->addHeaderLink('mootools.js');
101
 
        $template->addHeaderText($startJS,'script',"stub_ajax_test");
102
 
    }
103
 
 
104
 
    public static function hasAjax() {
105
 
        if (!isset($_SESSION['stub_ajax_test'])) {
106
 
            return false;
107
 
        }
108
 
        if (is_bool($_SESSION['stub_ajax_test'])) {
109
 
            return $_SESSION['stub_ajax_test'];
110
 
        }
111
 
        return false;
112
 
    }
113
 
 
114
 
    public function hasAjaxFuzzy($obj) {
115
 
        return $this->hasAjax();
116
 
    }
117
 
 
118
 
    /**
119
 
     * Called from a fuzzy page or fuzzy template as
120
 
     * addAjaxUpdate($id_replace,$id_action,$event,$requested_page,$id_content,$js_node = false , $form_id = '', $remove_events =false) -- the first five arguments are strings
121
 
     * $id_replace is the  id of the node we wish to replace in the page
122
 
     * $id_action is the id of the node in the page we wish to add the event to
123
 
     * $event is the event we wish to add on
124
 
     * $requested_page is the page we wish to make the ajax request from
125
 
     * $id_content is the id of the node we wish to pull the content from in the requested page.
126
 
     * $js_node boolean -- if true, we will include any javascript generated by the I2CE_Stub in the $request. 
127
 
     * $form_id -- if set a the id of form we will pull varaibles from.
128
 
     * if it is a string or an array of strings it will concatenate all javascript nodes with id given by the strings and
129
 
     * include in the request.  defaults to false meaning do not return any javascript.
130
 
     * $remove_events. defaults to false.  if true, we remove all events from the action element
131
 
     */
132
 
    public function addAjaxUpdate($obj,$args) {
133
 
        $form_id = '';
134
 
        $js_nodes = false;
135
 
        $remove_events = false;
136
 
        if (count($args) == 5) {
137
 
            list($id_replace,$id_action,$event, $requested_page,$id_content) = $args;           
138
 
            $js_nodes = false;
139
 
        } else if (count($args) == 6) {
140
 
            list($id_replace,$id_action,$event, $requested_page,$id_content,$js_nodes) = $args;         
141
 
        } else if (count($args) == 7) {
142
 
            list($id_replace,$id_action,$event, $requested_page,$id_content,$js_nodes,$form_id) = $args;         
143
 
        } else if (count($args) == 8) {
144
 
            list($id_replace,$id_action,$event, $requested_page,$id_content,$js_nodes,$form_id,$remove_events) = $args;         
145
 
        } else {
146
 
            I2CE::raiseError("Improper number of arguments to addAjaxUpdate -- expecting five or six");
147
 
            return;
148
 
        }
149
 
        if ((preg_match('/^[a-zA-Z]+:\/\//',$request_page))  || ($requested_page[0] == '/')) {
150
 
            return;
151
 
        }
152
 
        if ($s_nodes !== false) {
153
 
            if ($js_nodes === true) {
154
 
                $js_nodes = 'keep_javascripts';
155
 
            } else {
156
 
                if (!is_array($js_nodes)) {
157
 
                    $js_nodes = array($js_nodes);
158
 
                }
159
 
                $js_nodes = 'keep_javascripts=' . implode('|',$js_nodes);
160
 
            }
161
 
            if (strpos($requested_page,'?') !== false) {
162
 
                $requested_page .= '&' . $js_nodes;
163
 
            } else { 
164
 
                $requested_page .= '?' . $js_nodes;
165
 
            } 
166
 
        }
167
 
        $this->js .="\t\t\tvar actionObj = $('{$id_action}');\n\t\t\tif (actionObj) { \n";
168
 
        if ($remove_events) {
169
 
            $this->js .="\t\t\t\tactionObj.removeEvents('$event');\n";
170
 
        }
171
 
        $this->js .="\t\t\t\tactionObj.addEvent('$event',function(e){e = new Event(e).stop();Stub.updateById('$id_action','$id_replace','{$requested_page}','$id_content','$form_id')});\n"; 
172
 
        $this->js .="\t\t\t}\n";
173
 
    } 
174
 
 
175
 
        
176
 
    public function writeOutJS($args) {
177
 
        if (empty($this->js) && empty($this->js_func)) { 
178
 
            return;
179
 
        }
180
 
        $startJS = 
181
 
            "\tvar Stub_Events = {
182
 
\t\tstart: function(){  
183
 
{$this->js_func_request}
184
 
{$this->js}           
185
 
\t\t}
186
 
\t}; 
187
 
\tif (window.addEvent) {
188
 
    \t\twindow.addEvent('load',Stub_Events.start);
189
 
    \t}
190
 
{$this->js_func_complete}
191
 
";
192
 
                $template = $args['template'];
193
 
                $template->addHeaderLink('mootools.js');
194
 
                $template->addHeaderLink("stubs.js");
195
 
                $template->addHeaderLink("stub.css");
196
 
                $template->addHeaderText($startJS,'script',"stub_events");
197
 
        }
198
 
 
199
 
 
200
 
        /**
201
 
         * This fuzzy method takes two arguments.  The first arguement is the id of a tag
202
 
         * for which there is an event given by addAjaxUpdate.  The second argument is a javascript function to call.
203
 
         * Once the addAjaxUpdate request has been competed this function is evaled.
204
 
         */
205
 
        public function addAjaxUpdateFunctionComplete($obj,$args) {
206
 
                if (count($args) != 2) {
207
 
                        I2CE::raiseError("addAjaxUpdateFunctionComplete needs two arguments");
208
 
                        return;
209
 
                }
210
 
                list($id,$func) = $args;
211
 
                $func = preg_replace('{([\'\\\\])}', '\\\\$1', $func);
212
 
                $this->js_func_complete .= "Stub.updateFunctionComplete('$id','$func');\n";
213
 
        }
214
 
 
215
 
        /**
216
 
         * This fuzzy method takes two arguments.  The first arguement is the id of a tag
217
 
         * for which there is an event given by addAjaxUpdate.  The second argument is a javascript function to call.
218
 
         * Once the addAjaxUpdate request has been initiated this function is evaled.  Though to be honest, this
219
 
         * function may be evaled just before the request has been initiated -- mootools in none to clear on this detail.
220
 
         */
221
 
        public function addAjaxUpdateFunctionRequest($obj,$args) {
222
 
                if (count($args) != 2) {
223
 
                        I2CE::raiseError("addAjaxUpdateFunctionRequest needs two arguments");
224
 
                        return;
225
 
                }
226
 
                list($id,$func) = $args;
227
 
                $func = preg_replace('{([\'\\\\])}', '\\\\$1', $func);
228
 
                $this->js_func_request .= "Stub.updateFunctionRequest('$id','$func');\n";
229
 
        }
230
 
}
231
 
 
232
 
# Local Variables:
233
 
# mode: php
234
 
# c-default-style: "bsd"
235
 
# indent-tabs-mode: nil
236
 
# c-basic-offset: 4
237
 
# End: