/
Helper.php
136 lines (125 loc) · 3.39 KB
/
Helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* HTML/JavaScript Generation Helper
*
* @category HTML
* @package AJAX
* @author Joshua Eichorn <josh@bluga.net>
* @copyright 2005 Joshua Eichorn
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version Release: @package_version@
*/
/**
* HTML/JavaScript Generation Helper
*
* @category HTML
* @package AJAX
* @author Joshua Eichorn <josh@bluga.net>
* @copyright 2005 Joshua Eichorn
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version Release: @package_version@
* @link http://pear.php.net/package/HTML_AJAX
*/
class HTML_AJAX_Helper
{
/**
* URL where an HTML_AJAX_Server instance is serving up clients and taking ajax requests
*/
var $serverUrl = 'server.php';
/**
* JS libraries to include
*
* @var array
*/
var $jsLibraries = array('Util','Main','Request','HttpClient','Dispatcher','Behavior','Loading','JSON','iframe');
/**
* Remote class stubs to include
*/
var $stubs = array();
/**
* Include all needed libraries, stubs, and set defaultServer
*
* @return string
*/
function setupAJAX()
{
$libs = array(0=>array());
foreach($this->jsLibraries as $library) {
if (is_array($library)) {
$libs[] = implode(',',$library);
}
else {
$libs[0][] = $library;
}
}
$libs[0] = implode(',',$libs[0]);
$ret = '';
foreach($libs as $list) {
$ret .= "<script type='text/javascript' src='{$this->serverUrl}?client={$list}'></script>\n";
}
if (count($this->stubs) > 0) {
$stubs = implode(',',$this->stubs);
$ret .= "<script type='text/javascript' src='{$this->serverUrl}?stub={$stubs}'></script>\n";
}
$ret .= $this->encloseInScript('HTML_AJAX.defaultServerUrl = '.$this->escape($this->serverUrl));
return $ret;
}
/**
* Create a custom Loading message
*
* @param string $body HTML body of the loading div
* @param string $class CSS class of the div
* @param string $style style tag of the loading div
*/
function loadingMessage($body, $class = 'HTML_AJAX_Loading',
$style = 'position: absolute; top: 0; right: 0; background-color: red; width: 80px; padding: 4px; display: none')
{
return "<div id='HTML_AJAX_LOADING' class='{$class}' style=\"{$style}\">{$body}</div>\n";
}
/**
* Update the contents of an element using ajax
*
* @param string $id id of the element to update
* @param string|array $update Either a url to update with or a array like array('class','method')
* @param string $type replace or append
* @param boolean $enclose
*/
function updateElement($id, $update, $type, $enclose = false) {
if (is_array($update)) {
$updateStr = "";
$comma = '';
foreach($update as $item) {
$updateStr .= $comma.$this->escape($item);
$comma = ',';
}
}
else {
$updateStr = $this->escape($update);
}
$ret = "HTML_AJAX.{$type}(".$this->escape($id).",{$updateStr});\n";
if ($enclose) {
$ret = $this->encloseInScript($ret);
}
return $ret;
}
/**
* Escape a string and add quotes allowing it to be a javascript paramater
*
* @param string $input
* @return string
* @todo do something here besides a quick hack
*/
function escape($input) {
return "'".addslashes($input)."'";
}
/**
* Enclose a string in a script block
*
* @param string $input
* @return string
*/
function encloseInScript($input) {
return '<script type="text/javascript">'.$input."</script>\n";
}
}
?>