1 |
<?php |
2 |
/** |
3 |
* This contains the TextNav widget |
4 |
* |
5 |
* $Id: MessageBoxWidget.inc,v 1.2 2003/11/04 19:43:44 hemna Exp $ |
6 |
* |
7 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
8 |
* @package phpHtmlLib |
9 |
* |
10 |
*/ |
11 |
|
12 |
/** |
13 |
* This class requires the StandardDialogWidget |
14 |
*/ |
15 |
require_once($phphtmllib."/widgets/StandardDialogWidget.inc"); |
16 |
|
17 |
|
18 |
class MessageBoxWidget extends StandardDialogWidget { |
19 |
|
20 |
/** |
21 |
* The _message to display |
22 |
*/ |
23 |
var $_message = NULL; |
24 |
|
25 |
/** |
26 |
* Do we need to render a form? |
27 |
* |
28 |
*/ |
29 |
var $_has_form = FALSE; |
30 |
|
31 |
/** |
32 |
* The form method type |
33 |
* GET or POST only |
34 |
*/ |
35 |
var $_form_method = "POST"; |
36 |
|
37 |
/** |
38 |
* This stores additionnal hidden variables. |
39 |
*/ |
40 |
var $_hidden_vars = array(); |
41 |
|
42 |
/** |
43 |
* constructor |
44 |
* |
45 |
* @param string - the title of the window |
46 |
* @param mixed - the width of the object |
47 |
* @param string - the message string |
48 |
*/ |
49 |
function MessageBoxWidget($title, $width="600", $message="") { |
50 |
|
51 |
$this->set_message($message); |
52 |
$this->StandardDialogWidget($title, $width, 'center'); |
53 |
} |
54 |
|
55 |
/** |
56 |
* We override the render function |
57 |
* because we want to wrap the table |
58 |
* inside a form tag |
59 |
*/ |
60 |
function render($indent_level=1, $output_debug=0) { |
61 |
|
62 |
// add the messages |
63 |
$this->add_block(NULL, $this->get_message()); |
64 |
|
65 |
if ($this->_has_form) { |
66 |
// we need to render a form |
67 |
$form = new FORMtag(array("method" => $this->_form_method, |
68 |
"action" => $_SERVER["PHP_SELF"])); |
69 |
|
70 |
$form->add(parent::render($indent_level, $output_debug)); |
71 |
|
72 |
$vars = array_merge($_REQUEST, $this->_hidden_vars); |
73 |
foreach ($vars as $key=>$value) { |
74 |
if (is_array($value)) { |
75 |
foreach ($value as $k => $v) { |
76 |
$form->add(form_hidden($key."[".$k."]", $v)); |
77 |
} |
78 |
} else { |
79 |
$form->add(form_hidden($key, $value)); |
80 |
} |
81 |
} |
82 |
|
83 |
return $form->render($indent_level, $output_debug); |
84 |
} |
85 |
|
86 |
else return parent::render($indent_level, $output_debug); |
87 |
|
88 |
} |
89 |
|
90 |
/** |
91 |
* This sets the _message for the _message |
92 |
* window. |
93 |
* |
94 |
* @param string - the _message to display |
95 |
*/ |
96 |
function set_message($mesg) { |
97 |
$this->_message = $mesg; |
98 |
} |
99 |
|
100 |
/** |
101 |
* Gets the current value of the _message |
102 |
* |
103 |
* @return string |
104 |
*/ |
105 |
function get_message() { |
106 |
if (is_string($this->_message)) |
107 |
return str_replace("\n", "<br>", $this->_message); |
108 |
else |
109 |
return $this->_message; |
110 |
} |
111 |
|
112 |
/** |
113 |
* Add a single extra hidden variable |
114 |
* |
115 |
* @param string - key of the hidden variable |
116 |
* @param string - value of the variable |
117 |
*/ |
118 |
function add_hidden_variable($key, $value) { |
119 |
$this->_hidden_vars[$key] = $value; |
120 |
} |
121 |
|
122 |
/** |
123 |
* Add extra hidden variables |
124 |
* |
125 |
* @param array an array of key => value hidden |
126 |
* variables |
127 |
* @return none |
128 |
*/ |
129 |
function add_hidden_variables($vars) { |
130 |
if (!empty($vars)) { |
131 |
foreach( $vars as $key => $value) { |
132 |
$this->_hidden_vars[$key] = $value; |
133 |
} |
134 |
} |
135 |
} |
136 |
|
137 |
/** |
138 |
* Adds a single button to the dialog |
139 |
* that will be display at the bottom |
140 |
* of the widget |
141 |
* |
142 |
* @param string - button title |
143 |
* @param string - button action, if NULL, then submit button is created |
144 |
* @access public |
145 |
*/ |
146 |
function add_button($title, $action=NULL) { |
147 |
|
148 |
if (strstr($action, ".php") && !strstr($action, "javascript")) { |
149 |
if (strstr($action, "&")) { |
150 |
$js = "window.location='" . $action ."'"; |
151 |
} else { |
152 |
$js = "document.forms[0].action='".$action . "';"; |
153 |
$js .= "document.forms[0].submit();"; |
154 |
|
155 |
$this->_has_form = TRUE; |
156 |
} |
157 |
$action = "javascript: ".$js; |
158 |
} else { |
159 |
$action = $action; |
160 |
} |
161 |
|
162 |
if (empty($action)) { |
163 |
$action = "document.forms[0].submit();"; |
164 |
$this->_has_form = TRUE; |
165 |
} |
166 |
|
167 |
parent::add_button($this->form_action_button($title, $action)); |
168 |
} |
169 |
|
170 |
/** |
171 |
* build an tag of type button |
172 |
* which executes a javaScript function |
173 |
* |
174 |
* @param string $content |
175 |
* @param string $action |
176 |
* @param array $attrib - additional attributes |
177 |
* |
178 |
* @return object |
179 |
*/ |
180 |
function form_action_button($content, $action = NULL, $attrib = NULL) { |
181 |
$attributes = array("type" => "button", |
182 |
"value" => $content); |
183 |
if (!isset($attrib["style"])) { |
184 |
$attributes["style"] = "width:90px;"; |
185 |
} |
186 |
|
187 |
$attributes["onclick"] = $action; |
188 |
|
189 |
if ($action) $attributes["type"] = "button"; |
190 |
else $attributes["type"] = "submit"; |
191 |
|
192 |
if (is_array($attrib)) { |
193 |
$attributes = array_merge($attributes, $attrib); |
194 |
} |
195 |
|
196 |
$button = new INPUTtag($attributes); |
197 |
|
198 |
return $button; |
199 |
} |
200 |
} |
201 |
|
202 |
/** |
203 |
* This is a class for building a MessageBox with |
204 |
* an OK button. |
205 |
* |
206 |
* |
207 |
*/ |
208 |
class MessageBoxOK extends MessageBoxWidget { |
209 |
/** |
210 |
* |
211 |
* @param string the title |
212 |
* @param string the width |
213 |
* @param string the message for the box |
214 |
* @param string the ok action to perform. history.back() by default |
215 |
* @param array an array of hidden form variables to post along with |
216 |
* the form. |
217 |
*/ |
218 |
function MessageBoxOK($title, $width, $message, |
219 |
$ok_action = "javascript:history.back();", |
220 |
$hidden_values = array()) { |
221 |
$this->MessageBoxWidget($title, $width, $message); |
222 |
$this->add_button("OK", $ok_action); |
223 |
$this->add_hidden_variables($hidden_values); |
224 |
} |
225 |
} |
226 |
|
227 |
/** |
228 |
* This is a class for building a MessageBox with |
229 |
* an OK and Cancel button. |
230 |
* |
231 |
*/ |
232 |
class MessageBoxOKCancel extends MessageBoxOK { |
233 |
/** |
234 |
* |
235 |
* @param string the title |
236 |
* @param string the width |
237 |
* @param string the message for the box |
238 |
* @param string the ok action to perform. history.back() by default |
239 |
* @param string the cancel action to perform. history.back() by default |
240 |
* @param array an array of hidden form variables to post along with |
241 |
* the form. |
242 |
*/ |
243 |
function MessageBoxOKCancel($title, $width, $message, |
244 |
$ok_action = "", |
245 |
$cancel_action = "javascript: history.back();", |
246 |
$hidden_values = array()) { |
247 |
$this->MessageBoxOK($title, $width, $message, $ok_action); |
248 |
$this->add_button("Cancel", $ok_action); |
249 |
$this->add_hidden_variables($hidden_values); |
250 |
} |
251 |
} |
252 |
|
253 |
?> |