3
* © Copyright 2007, 2008 IntraHealth International, Inc.
5
* This File is part of I2CE
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.
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.
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/>.
24
* @author Carl Leitner <litlfred@ibiblio.org>
25
* @copyright Copyright © 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/>.
37
class I2CE_Stub extends I2CE_Module{
39
public static function getMethods() {
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'
53
public static function getHooks() {
55
'pre_page_action'=>'ajaxTest',
56
'template_post_display'=> 'writeOutJS'
61
public function __construct() {
62
parent::__construct();
68
public function ajaxTest($page) {
69
if (!isset($_SESSION['stub_ajax_test'])) {
70
$_SESSION['stub_ajax_test'] = 'unknown0';
72
if (is_bool($_SESSION['stub_ajax_test'])) {
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
80
$_SESSION['stub_ajax_test'] = 'unknown' . $matches[1];
83
$_SESSION['stub_ajax_test'] = 'unknown0';
85
$stub_url = "stub-ajax";
86
if (getenv("I2CE_Rewritten") !== "On") {
87
$stub_url = "index.php/$stub_url";
92
var Stub_Ajax_Test = {
94
new Ajax('$stub_url',{method:'get'}).request();
97
window.addEvent('load',Stub_Ajax_Test.start);
99
$template = $page->getTemplate();
100
$template->addHeaderLink('mootools.js');
101
$template->addHeaderText($startJS,'script',"stub_ajax_test");
104
public static function hasAjax() {
105
if (!isset($_SESSION['stub_ajax_test'])) {
108
if (is_bool($_SESSION['stub_ajax_test'])) {
109
return $_SESSION['stub_ajax_test'];
114
public function hasAjaxFuzzy($obj) {
115
return $this->hasAjax();
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
132
public function addAjaxUpdate($obj,$args) {
135
$remove_events = false;
136
if (count($args) == 5) {
137
list($id_replace,$id_action,$event, $requested_page,$id_content) = $args;
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;
146
I2CE::raiseError("Improper number of arguments to addAjaxUpdate -- expecting five or six");
149
if ((preg_match('/^[a-zA-Z]+:\/\//',$request_page)) || ($requested_page[0] == '/')) {
152
if ($s_nodes !== false) {
153
if ($js_nodes === true) {
154
$js_nodes = 'keep_javascripts';
156
if (!is_array($js_nodes)) {
157
$js_nodes = array($js_nodes);
159
$js_nodes = 'keep_javascripts=' . implode('|',$js_nodes);
161
if (strpos($requested_page,'?') !== false) {
162
$requested_page .= '&' . $js_nodes;
164
$requested_page .= '?' . $js_nodes;
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";
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";
176
public function writeOutJS($args) {
177
if (empty($this->js) && empty($this->js_func)) {
181
"\tvar Stub_Events = {
182
\t\tstart: function(){
183
{$this->js_func_request}
187
\tif (window.addEvent) {
188
\t\twindow.addEvent('load',Stub_Events.start);
190
{$this->js_func_complete}
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");
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.
205
public function addAjaxUpdateFunctionComplete($obj,$args) {
206
if (count($args) != 2) {
207
I2CE::raiseError("addAjaxUpdateFunctionComplete needs two arguments");
210
list($id,$func) = $args;
211
$func = preg_replace('{([\'\\\\])}', '\\\\$1', $func);
212
$this->js_func_complete .= "Stub.updateFunctionComplete('$id','$func');\n";
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.
221
public function addAjaxUpdateFunctionRequest($obj,$args) {
222
if (count($args) != 2) {
223
I2CE::raiseError("addAjaxUpdateFunctionRequest needs two arguments");
226
list($id,$func) = $args;
227
$func = preg_replace('{([\'\\\\])}', '\\\\$1', $func);
228
$this->js_func_request .= "Stub.updateFunctionRequest('$id','$func');\n";
234
# c-default-style: "bsd"
235
# indent-tabs-mode: nil