44ef0f47 |
1 | <?php |
2ba13803 |
2 | |
35586184 |
3 | /** |
4 | * options.php |
5 | * |
35586184 |
6 | * Functions needed to display the options pages. |
7 | * |
4b5049de |
8 | * @copyright © 1999-2007 The SquirrelMail Project Team |
4b4abf93 |
9 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
31841a9e |
10 | * @version $Id$ |
d6c32258 |
11 | * @package squirrelmail |
ca479ad1 |
12 | * @subpackage prefs |
35586184 |
13 | */ |
a3ec3c91 |
14 | |
15 | /**********************************************/ |
16 | /* Define constants used in the options code. */ |
17 | /**********************************************/ |
18 | |
19 | /* Define constants for the various option types. */ |
20 | define('SMOPT_TYPE_STRING', 0); |
21 | define('SMOPT_TYPE_STRLIST', 1); |
7e6d5ea3 |
22 | define('SMOPT_TYPE_TEXTAREA', 2); |
a3ec3c91 |
23 | define('SMOPT_TYPE_INTEGER', 3); |
24 | define('SMOPT_TYPE_FLOAT', 4); |
25 | define('SMOPT_TYPE_BOOLEAN', 5); |
2a50fbd7 |
26 | define('SMOPT_TYPE_HIDDEN', 6); |
bbcafebd |
27 | define('SMOPT_TYPE_COMMENT', 7); |
be2d5495 |
28 | define('SMOPT_TYPE_FLDRLIST', 8); |
42b7c9d4 |
29 | define('SMOPT_TYPE_FLDRLIST_MULTI', 9); |
38d93650 |
30 | define('SMOPT_TYPE_EDIT_LIST', 10); |
40268626 |
31 | define('SMOPT_TYPE_STRLIST_MULTI', 11); |
1b7db98b |
32 | define('SMOPT_TYPE_BOOLEAN_CHECKBOX', 12); |
8b2726c5 |
33 | define('SMOPT_TYPE_BOOLEAN_RADIO', 13); |
a3ec3c91 |
34 | |
35 | /* Define constants for the options refresh levels. */ |
36 | define('SMOPT_REFRESH_NONE', 0); |
37 | define('SMOPT_REFRESH_FOLDERLIST', 1); |
38 | define('SMOPT_REFRESH_ALL', 2); |
39 | |
bbcafebd |
40 | /* Define constants for the options size. */ |
41 | define('SMOPT_SIZE_TINY', 0); |
42 | define('SMOPT_SIZE_SMALL', 1); |
43 | define('SMOPT_SIZE_MEDIUM', 2); |
44 | define('SMOPT_SIZE_LARGE', 3); |
45 | define('SMOPT_SIZE_HUGE', 4); |
88cb1b4d |
46 | define('SMOPT_SIZE_NORMAL', 5); |
bbcafebd |
47 | |
cbe5423b |
48 | define('SMOPT_SAVE_DEFAULT', 'save_option'); |
49 | define('SMOPT_SAVE_NOOP', 'save_option_noop'); |
50 | |
9962527a |
51 | /** |
598294a7 |
52 | * SquirrelOption: An option for SquirrelMail. |
9962527a |
53 | * |
8f6f9ba5 |
54 | * @package squirrelmail |
b4856b14 |
55 | * @subpackage prefs |
9962527a |
56 | */ |
57 | class SquirrelOption { |
b4856b14 |
58 | /** |
59 | * The name of this setting |
60 | * @var string |
61 | */ |
9962527a |
62 | var $name; |
b4856b14 |
63 | /** |
64 | * The text that prefaces setting on the preferences page |
65 | * @var string |
66 | */ |
9962527a |
67 | var $caption; |
b4856b14 |
68 | /** |
69 | * The type of INPUT element |
70 | * |
71 | * See SMOPT_TYPE_* defines |
72 | * @var integer |
73 | */ |
9962527a |
74 | var $type; |
b4856b14 |
75 | /** |
598294a7 |
76 | * Indicates if a link should be shown to refresh part |
b4856b14 |
77 | * or all of the window |
78 | * |
79 | * See SMOPT_REFRESH_* defines |
80 | * @var integer |
81 | */ |
a3ec3c91 |
82 | var $refresh_level; |
b4856b14 |
83 | /** |
84 | * Specifies the size of certain input items |
85 | * |
86 | * See SMOPT_SIZE_* defines |
87 | * @var integer |
88 | */ |
bbcafebd |
89 | var $size; |
b4856b14 |
90 | /** |
598294a7 |
91 | * Text that follows a text input or |
b4856b14 |
92 | * select list input on the preferences page |
598294a7 |
93 | * |
b4856b14 |
94 | * useful for indicating units, meanings of special values, etc. |
95 | * @var string |
96 | */ |
361d6e1b |
97 | var $trailing_text; |
b4856b14 |
98 | /** |
99 | * text displayed to the user |
100 | * |
101 | * Used with SMOPT_TYPE_COMMENT options |
102 | * @var string |
103 | */ |
bbcafebd |
104 | var $comment; |
b4856b14 |
105 | /** |
0177059f |
106 | * additional javascript or other widget attributes added to the |
107 | * user input; must be an array where keys are attribute names |
108 | * ("onclick", etc) and values are the attribute values. |
109 | * @var array |
b4856b14 |
110 | */ |
0177059f |
111 | var $aExtraAttribs; |
b4856b14 |
112 | /** |
598294a7 |
113 | * script (usually Javascript) that will be placed after (outside of) |
b4856b14 |
114 | * the INPUT tag |
115 | * @var string |
116 | */ |
6ae9e729 |
117 | var $post_script; |
cbe5423b |
118 | |
b4856b14 |
119 | /** |
120 | * The name of the Save Function for this option. |
121 | * @var string |
122 | */ |
cbe5423b |
123 | var $save_function; |
9962527a |
124 | |
125 | /* The various 'values' for this options. */ |
b4856b14 |
126 | /** |
127 | * default/preselected value for this option |
128 | * @var mixed |
129 | */ |
9962527a |
130 | var $value; |
b4856b14 |
131 | /** |
132 | * new option value |
133 | * @var mixed |
134 | */ |
9962527a |
135 | var $new_value; |
b4856b14 |
136 | /** |
598294a7 |
137 | * associative array, where each key is an actual input value |
b4856b14 |
138 | * and the corresponding value is what is displayed to the user |
139 | * for that list item in the drop-down list |
140 | * @var array |
141 | */ |
a3ec3c91 |
142 | var $possible_values; |
b4856b14 |
143 | /** |
144 | * disables html sanitizing. |
598294a7 |
145 | * |
146 | * WARNING - don't use it, if user input is possible in option |
0177059f |
147 | * or use own sanitizing functions. Currently only works for SMOPT_TYPE_STRLIST. |
b4856b14 |
148 | * @var bool |
149 | */ |
28520c87 |
150 | var $htmlencoded=false; |
99ecf044 |
151 | /** |
42b7c9d4 |
152 | * Controls folder list limits in SMOPT_TYPE_FLDRLIST and |
153 | * SMOPT_TYPE_FLDRLIST_MULTI widgets. |
99ecf044 |
154 | * See $flag argument in sqimap_mailbox_option_list() function. |
155 | * @var string |
156 | * @since 1.5.1 |
157 | */ |
158 | var $folder_filter='noselect'; |
9962527a |
159 | |
b4856b14 |
160 | /** |
161 | * Constructor function |
162 | * @param string $name |
163 | * @param string $caption |
164 | * @param integer $type |
165 | * @param integer $refresh_level |
166 | * @param mixed $initial_value |
167 | * @param array $possible_values |
168 | * @param bool $htmlencoded |
169 | */ |
9962527a |
170 | function SquirrelOption |
28520c87 |
171 | ($name, $caption, $type, $refresh_level, $initial_value = '', $possible_values = '', $htmlencoded = false) { |
9962527a |
172 | /* Set the basic stuff. */ |
173 | $this->name = $name; |
174 | $this->caption = $caption; |
9962527a |
175 | $this->type = $type; |
a3ec3c91 |
176 | $this->refresh_level = $refresh_level; |
177 | $this->possible_values = $possible_values; |
28520c87 |
178 | $this->htmlencoded = $htmlencoded; |
bbcafebd |
179 | $this->size = SMOPT_SIZE_MEDIUM; |
361d6e1b |
180 | $this->trailing_text = ''; |
bbcafebd |
181 | $this->comment = ''; |
0177059f |
182 | $this->aExtraAttribs = array(); |
6ae9e729 |
183 | $this->post_script = ''; |
a3ec3c91 |
184 | |
991c88e7 |
185 | //Check for a current value. |
186 | if (isset($GLOBALS[$name])) { |
a3ec3c91 |
187 | $this->value = $GLOBALS[$name]; |
17f3d242 |
188 | } else if (!empty($initial_value)) { |
189 | $this->value = $initial_value; |
a3ec3c91 |
190 | } else { |
191 | $this->value = ''; |
192 | } |
9962527a |
193 | |
a3ec3c91 |
194 | /* Check for a new value. */ |
b4856b14 |
195 | if ( !sqgetGlobalVar("new_$name", $this->new_value, SQ_POST ) ) { |
a3ec3c91 |
196 | $this->new_value = ''; |
44ef0f47 |
197 | } |
cbe5423b |
198 | |
199 | /* Set the default save function. */ |
2a50fbd7 |
200 | if (($type != SMOPT_TYPE_HIDDEN) && ($type != SMOPT_TYPE_COMMENT)) { |
cbe5423b |
201 | $this->save_function = SMOPT_SAVE_DEFAULT; |
202 | } else { |
203 | $this->save_function = SMOPT_SAVE_NOOP; |
204 | } |
205 | } |
206 | |
5a42c101 |
207 | /** Convenience function that identifies which types of |
208 | widgets are stored as (serialized) array values. */ |
209 | function is_multiple_valued() { |
210 | return ($this->type == SMOPT_TYPE_FLDRLIST_MULTI |
211 | || $this->type == SMOPT_TYPE_STRLIST_MULTI |
212 | || $this->type == SMOPT_TYPE_EDIT_LIST); |
213 | } |
214 | |
b4856b14 |
215 | /** |
216 | * Set the value for this option. |
217 | * @param mixed $value |
218 | */ |
cbe5423b |
219 | function setValue($value) { |
220 | $this->value = $value; |
221 | } |
222 | |
b4856b14 |
223 | /** |
224 | * Set the new value for this option. |
225 | * @param mixed $new_value |
226 | */ |
cbe5423b |
227 | function setNewValue($new_value) { |
228 | $this->new_value = $new_value; |
9962527a |
229 | } |
44ef0f47 |
230 | |
b4856b14 |
231 | /** |
232 | * Set the size for this option. |
233 | * @param integer $size |
234 | */ |
bbcafebd |
235 | function setSize($size) { |
236 | $this->size = $size; |
237 | } |
238 | |
b4856b14 |
239 | /** |
240 | * Set the trailing_text for this option. |
241 | * @param string $trailing_text |
242 | */ |
361d6e1b |
243 | function setTrailingText($trailing_text) { |
244 | $this->trailing_text = $trailing_text; |
245 | } |
246 | |
b4856b14 |
247 | /** |
248 | * Set the comment for this option. |
249 | * @param string $comment |
250 | */ |
bbcafebd |
251 | function setComment($comment) { |
252 | $this->comment = $comment; |
253 | } |
254 | |
b4856b14 |
255 | /** |
0177059f |
256 | * Set the extra attributes for this option. |
257 | * @param array $aExtraAttribs |
b4856b14 |
258 | */ |
0177059f |
259 | function setExtraAttributes($aExtraAttribs) { |
260 | $this->aExtraAttribs = $aExtraAttribs; |
cbe5423b |
261 | } |
262 | |
b4856b14 |
263 | /** |
264 | * Set the "post script" for this option. |
265 | * @param string $post_script |
266 | */ |
6ae9e729 |
267 | function setPostScript($post_script) { |
268 | $this->post_script = $post_script; |
269 | } |
270 | |
b4856b14 |
271 | /** |
272 | * Set the save function for this option. |
273 | * @param string $save_function |
274 | */ |
cbe5423b |
275 | function setSaveFunction($save_function) { |
276 | $this->save_function = $save_function; |
277 | } |
278 | |
99ecf044 |
279 | /** |
280 | * Set the trailing_text for this option. |
281 | * @param string $folder_filter |
282 | * @since 1.5.1 |
283 | */ |
284 | function setFolderFilter($folder_filter) { |
285 | $this->folder_filter = $folder_filter; |
286 | } |
287 | |
b4856b14 |
288 | /** |
289 | * Creates fields on option pages according to option type |
290 | * |
9786ea94 |
291 | * This is the function that calls all other createWidget* functions. |
292 | * |
293 | * @return string The formated option field |
294 | * |
b4856b14 |
295 | */ |
9786ea94 |
296 | function createWidget() { |
ce68b76b |
297 | global $color; |
cbe5423b |
298 | |
62f7daa5 |
299 | // Use new value if available |
74e44765 |
300 | if (!empty($this->new_value)) { |
301 | $tempValue = $this->value; |
302 | $this->value = $this->new_value; |
303 | } |
304 | |
cbe5423b |
305 | /* Get the widget for this option type. */ |
a3ec3c91 |
306 | switch ($this->type) { |
307 | case SMOPT_TYPE_STRING: |
37a3ed17 |
308 | $result = $this->createWidget_String(); |
a3ec3c91 |
309 | break; |
310 | case SMOPT_TYPE_STRLIST: |
37a3ed17 |
311 | $result = $this->createWidget_StrList(); |
a3ec3c91 |
312 | break; |
7e6d5ea3 |
313 | case SMOPT_TYPE_TEXTAREA: |
37a3ed17 |
314 | $result = $this->createWidget_TextArea(); |
a3ec3c91 |
315 | break; |
316 | case SMOPT_TYPE_INTEGER: |
37a3ed17 |
317 | $result = $this->createWidget_Integer(); |
a3ec3c91 |
318 | break; |
319 | case SMOPT_TYPE_FLOAT: |
37a3ed17 |
320 | $result = $this->createWidget_Float(); |
a3ec3c91 |
321 | break; |
322 | case SMOPT_TYPE_BOOLEAN: |
37a3ed17 |
323 | $result = $this->createWidget_Boolean(); |
a3ec3c91 |
324 | break; |
1b7db98b |
325 | case SMOPT_TYPE_BOOLEAN_CHECKBOX: |
326 | $result = $this->createWidget_Boolean(TRUE); |
327 | break; |
8b2726c5 |
328 | case SMOPT_TYPE_BOOLEAN_RADIO: |
329 | $result = $this->createWidget_Boolean(FALSE); |
330 | break; |
2a50fbd7 |
331 | case SMOPT_TYPE_HIDDEN: |
37a3ed17 |
332 | $result = $this->createWidget_Hidden(); |
a3ec3c91 |
333 | break; |
bbcafebd |
334 | case SMOPT_TYPE_COMMENT: |
37a3ed17 |
335 | $result = $this->createWidget_Comment(); |
bbcafebd |
336 | break; |
be2d5495 |
337 | case SMOPT_TYPE_FLDRLIST: |
37a3ed17 |
338 | $result = $this->createWidget_FolderList(); |
be2d5495 |
339 | break; |
42b7c9d4 |
340 | case SMOPT_TYPE_FLDRLIST_MULTI: |
341 | $result = $this->createWidget_FolderList(TRUE); |
342 | break; |
38d93650 |
343 | case SMOPT_TYPE_EDIT_LIST: |
344 | $result = $this->createWidget_EditList(); |
345 | break; |
40268626 |
346 | case SMOPT_TYPE_STRLIST_MULTI: |
347 | $result = $this->createWidget_StrList(TRUE); |
348 | break; |
a3ec3c91 |
349 | default: |
fb8c4296 |
350 | error_box ( |
351 | sprintf(_("Option Type '%s' Not Found"), $this->type) |
352 | ); |
a3ec3c91 |
353 | } |
354 | |
6ae9e729 |
355 | /* Add the "post script" for this option. */ |
356 | $result .= $this->post_script; |
62f7daa5 |
357 | |
74e44765 |
358 | // put correct value back if need be |
359 | if (!empty($this->new_value)) { |
360 | $this->value = $tempValue; |
361 | } |
362 | |
a3ec3c91 |
363 | /* Now, return the created widget. */ |
9786ea94 |
364 | return $result; |
a3ec3c91 |
365 | } |
366 | |
b4856b14 |
367 | /** |
368 | * Create string field |
369 | * @return string html formated option field |
370 | */ |
37a3ed17 |
371 | function createWidget_String() { |
bbcafebd |
372 | switch ($this->size) { |
88cb1b4d |
373 | case SMOPT_SIZE_TINY: |
374 | $width = 5; |
375 | break; |
376 | case SMOPT_SIZE_SMALL: |
377 | $width = 12; |
378 | break; |
379 | case SMOPT_SIZE_LARGE: |
380 | $width = 38; |
381 | break; |
382 | case SMOPT_SIZE_HUGE: |
383 | $width = 50; |
384 | break; |
bbcafebd |
385 | case SMOPT_SIZE_NORMAL: |
88cb1b4d |
386 | default: |
387 | $width = 25; |
bbcafebd |
388 | } |
389 | |
0177059f |
390 | return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text); |
a3ec3c91 |
391 | } |
392 | |
b4856b14 |
393 | /** |
394 | * Create selection box |
0177059f |
395 | * |
396 | * When $this->htmlencoded is TRUE, the keys and values in |
397 | * $this->possible_values are assumed to be display-safe. |
398 | * Use with care! |
399 | * |
40268626 |
400 | * @param boolean $multiple_select When TRUE, the select widget |
401 | * will allow multiple selections |
402 | * (OPTIONAL; default is FALSE |
403 | * (single select list)) |
404 | * |
b4856b14 |
405 | * @return string html formated selection box |
40268626 |
406 | * |
b4856b14 |
407 | */ |
40268626 |
408 | function createWidget_StrList($multiple_select=FALSE) { |
98e88751 |
409 | //FIXME: Currently, $this->htmlencoded is ignored here -- was removed when changing to template-based output; a fix is available as part of proposed centralized sanitizing patch |
40268626 |
410 | |
411 | switch ($this->size) { |
412 | //FIXME: not sure about these sizes... seems like we could add another on the "large" side... |
413 | case SMOPT_SIZE_TINY: |
414 | $height = 3; |
415 | break; |
416 | case SMOPT_SIZE_SMALL: |
417 | $height = 8; |
418 | break; |
419 | case SMOPT_SIZE_LARGE: |
420 | $height = 15; |
421 | break; |
422 | case SMOPT_SIZE_HUGE: |
423 | $height = 25; |
424 | break; |
425 | case SMOPT_SIZE_NORMAL: |
426 | default: |
427 | $height = 5; |
428 | } |
429 | |
430 | return addSelect('new_' . $this->name, $this->possible_values, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height, !$this->htmlencoded) . htmlspecialchars($this->trailing_text); |
a3ec3c91 |
431 | |
a3ec3c91 |
432 | } |
433 | |
b4856b14 |
434 | /** |
435 | * Create folder selection box |
42b7c9d4 |
436 | * |
437 | * @param boolean $multiple_select When TRUE, the select widget |
438 | * will allow multiple selections |
439 | * (OPTIONAL; default is FALSE |
440 | * (single select list)) |
441 | * |
b4856b14 |
442 | * @return string html formated selection box |
42b7c9d4 |
443 | * |
b4856b14 |
444 | */ |
42b7c9d4 |
445 | function createWidget_FolderList($multiple_select=FALSE) { |
be2d5495 |
446 | |
38d93650 |
447 | switch ($this->size) { |
448 | //FIXME: not sure about these sizes... seems like we could add another on the "large" side... |
449 | case SMOPT_SIZE_TINY: |
450 | $height = 3; |
451 | break; |
452 | case SMOPT_SIZE_SMALL: |
453 | $height = 8; |
454 | break; |
455 | case SMOPT_SIZE_LARGE: |
456 | $height = 15; |
457 | break; |
458 | case SMOPT_SIZE_HUGE: |
459 | $height = 25; |
460 | break; |
461 | case SMOPT_SIZE_NORMAL: |
462 | default: |
463 | $height = 5; |
464 | } |
465 | |
0177059f |
466 | // possible values might include a nested array of |
467 | // possible values (list of folders) |
468 | // |
469 | $option_list = array(); |
470 | foreach ($this->possible_values as $value => $text) { |
62f7daa5 |
471 | |
0177059f |
472 | // list of folders (boxes array) |
473 | // |
474 | if (is_array($text)) { |
42b7c9d4 |
475 | $option_list = array_merge($option_list, sqimap_mailbox_option_array(0, 0, $text, $this->folder_filter)); |
62f7daa5 |
476 | |
0177059f |
477 | // just one option here |
478 | // |
479 | } else { |
480 | $option_list = array_merge($option_list, array($value => $text)); |
be2d5495 |
481 | } |
0177059f |
482 | |
62f7daa5 |
483 | } |
0177059f |
484 | if (empty($option_list)) |
485 | $option_list = array('ignore' => _("unavailable")); |
99ecf044 |
486 | |
487 | |
38d93650 |
488 | return addSelect('new_' . $this->name, $option_list, $this->value, TRUE, $this->aExtraAttribs, $multiple_select, $height) . htmlspecialchars($this->trailing_text); |
0177059f |
489 | |
be2d5495 |
490 | } |
491 | |
b4856b14 |
492 | /** |
493 | * Creates textarea |
494 | * @return string html formated textarea field |
495 | */ |
37a3ed17 |
496 | function createWidget_TextArea() { |
bbcafebd |
497 | switch ($this->size) { |
498 | case SMOPT_SIZE_TINY: $rows = 3; $cols = 10; break; |
499 | case SMOPT_SIZE_SMALL: $rows = 4; $cols = 30; break; |
500 | case SMOPT_SIZE_LARGE: $rows = 10; $cols = 60; break; |
501 | case SMOPT_SIZE_HUGE: $rows = 20; $cols = 80; break; |
502 | case SMOPT_SIZE_NORMAL: |
503 | default: $rows = 5; $cols = 50; |
504 | } |
ba556ce5 |
505 | return addTextArea('new_' . $this->name, $this->value, $cols, $rows, $this->aExtraAttribs); |
a3ec3c91 |
506 | } |
507 | |
b4856b14 |
508 | /** |
509 | * Creates field for integer |
510 | * |
511 | * Difference from createWidget_String is visible only when javascript is enabled |
512 | * @return string html formated option field |
513 | */ |
37a3ed17 |
514 | function createWidget_Integer() { |
0d08ea5a |
515 | |
b65d1a08 |
516 | // add onChange javascript handler to a regular string widget |
517 | // which will strip out all non-numeric chars |
83aff890 |
518 | if (checkForJavascript()) |
0177059f |
519 | $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; ' |
b65d1a08 |
520 | . 'for (i=0;i<origVal.length;i++) { if (origVal.charAt(i)>=\'0\' ' |
521 | . '&& origVal.charAt(i)<=\'9\') newVal += origVal.charAt(i); } ' |
0177059f |
522 | . 'this.value=newVal;'; |
523 | |
524 | return $this->createWidget_String(); |
a3ec3c91 |
525 | } |
526 | |
b4856b14 |
527 | /** |
528 | * Creates field for floating number |
529 | * Difference from createWidget_String is visible only when javascript is enabled |
530 | * @return string html formated option field |
531 | */ |
37a3ed17 |
532 | function createWidget_Float() { |
37a3ed17 |
533 | |
b65d1a08 |
534 | // add onChange javascript handler to a regular string widget |
62f7daa5 |
535 | // which will strip out all non-numeric (period also OK) chars |
83aff890 |
536 | if (checkForJavascript()) |
0177059f |
537 | $this->aExtraAttribs['onchange'] = 'origVal=this.value; newVal=\'\'; ' |
b65d1a08 |
538 | . 'for (i=0;i<origVal.length;i++) { if ((origVal.charAt(i)>=\'0\' ' |
539 | . '&& origVal.charAt(i)<=\'9\') || origVal.charAt(i)==\'.\') ' |
0177059f |
540 | . 'newVal += origVal.charAt(i); } this.value=newVal;'; |
541 | |
542 | return $this->createWidget_String(); |
a3ec3c91 |
543 | } |
544 | |
b4856b14 |
545 | /** |
1b7db98b |
546 | * Create boolean widget |
547 | * |
548 | * @param boolean $checkbox When TRUE, the widget will be |
549 | * constructed as a checkbox, |
550 | * otherwise it will be a set of |
551 | * Yes/No radio buttons (OPTIONAL; |
3b6a455c |
552 | * default is TRUE (checkbox)). |
1b7db98b |
553 | * |
554 | * @return string html formated boolean widget |
555 | * |
b4856b14 |
556 | */ |
3b6a455c |
557 | function createWidget_Boolean($checkbox=TRUE) { |
0177059f |
558 | |
5f88daeb |
559 | global $oTemplate, $nbsp; |
fd87494d |
560 | |
fd87494d |
561 | |
1b7db98b |
562 | // checkbox... |
563 | // |
564 | if ($checkbox) { |
565 | $result = addCheckbox('new_' . $this->name, ($this->value != SMPREF_NO), SMPREF_YES, array_merge(array('id' => 'new_' . $this->name), $this->aExtraAttribs)) . $nbsp . create_label($this->trailing_text, 'new_' . $this->name); |
566 | } |
567 | |
568 | // radio buttons... |
569 | // |
570 | else { |
571 | |
572 | /* Build the yes choice. */ |
573 | $yes_option = addRadioBox('new_' . $this->name, ($this->value != SMPREF_NO), SMPREF_YES, array_merge(array('id' => 'new_' . $this->name . '_yes'), $this->aExtraAttribs)) . $nbsp . create_label(_("Yes"), 'new_' . $this->name . '_yes'); |
574 | |
575 | /* Build the no choice. */ |
576 | $no_option = addRadioBox('new_' . $this->name, ($this->value == SMPREF_NO), SMPREF_NO, array_merge(array('id' => 'new_' . $this->name . '_no'), $this->aExtraAttribs)) . $nbsp . create_label(_("No"), 'new_' . $this->name . '_no'); |
577 | |
578 | /* Build the combined "boolean widget". */ |
579 | $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option"; |
580 | |
581 | } |
fd87494d |
582 | |
fd87494d |
583 | return ($result); |
a3ec3c91 |
584 | } |
585 | |
b4856b14 |
586 | /** |
587 | * Creates hidden field |
588 | * @return string html formated hidden input field |
589 | */ |
37a3ed17 |
590 | function createWidget_Hidden() { |
0177059f |
591 | return addHidden('new_' . $this->name, $this->value, $this->aExtraAttribs); |
a3ec3c91 |
592 | } |
593 | |
b4856b14 |
594 | /** |
595 | * Creates comment |
596 | * @return string comment |
597 | */ |
37a3ed17 |
598 | function createWidget_Comment() { |
bbcafebd |
599 | $result = $this->comment; |
600 | return ($result); |
601 | } |
602 | |
38d93650 |
603 | /** |
604 | * Creates an edit list |
605 | * @return string html formated list of edit fields and |
606 | * their associated controls |
607 | */ |
608 | function createWidget_EditList() { |
609 | |
cb606dfd |
610 | global $oTemplate; |
38d93650 |
611 | |
612 | switch ($this->size) { |
613 | //FIXME: not sure about these sizes... seems like we could add another on the "large" side... |
614 | case SMOPT_SIZE_TINY: |
615 | $height = 3; |
616 | break; |
617 | case SMOPT_SIZE_SMALL: |
618 | $height = 8; |
619 | break; |
620 | case SMOPT_SIZE_LARGE: |
621 | $height = 15; |
622 | break; |
623 | case SMOPT_SIZE_HUGE: |
624 | $height = 25; |
625 | break; |
626 | case SMOPT_SIZE_NORMAL: |
627 | default: |
628 | $height = 5; |
629 | } |
630 | |
dd6f9627 |
631 | if (empty($this->possible_values)) $this->possible_values = array(); |
2ebfc729 |
632 | if (!is_array($this->possible_values)) $this->possible_values = array($this->possible_values); |
dd6f9627 |
633 | |
cb606dfd |
634 | //FIXME: $this->aExtraAttribs probably should only be used in one place |
635 | $oTemplate->assign('input_widget', addInput('add_' . $this->name, '', 38, 0, $this->aExtraAttribs)); |
636 | $oTemplate->assign('trailing_text', $this->trailing_text); |
637 | $oTemplate->assign('select_widget', addSelect('new_' . $this->name, $this->possible_values, $this->value, FALSE, !checkForJavascript() ? $this->aExtraAttribs : array_merge(array('onchange' => 'if (typeof(window.addinput) == \'undefined\') { var f = document.forms.length; var i = 0; var pos = -1; while( pos == -1 && i < f ) { var e = document.forms[i].elements.length; var j = 0; while( pos == -1 && j < e ) { if ( document.forms[i].elements[j].type == \'text\' && document.forms[i].elements[j].name == \'add_' . $this->name . '\' ) { pos = j; } j++; } i++; } if( pos >= 0 ) { window.addinput = document.forms[i-1].elements[pos]; } } for (x = 0; x < this.length; x++) { if (this.options[x].selected) { window.addinput.value = this.options[x].value; break; } }'), $this->aExtraAttribs), TRUE, $height)); |
638 | $oTemplate->assign('checkbox_widget', addCheckBox('delete_' . $this->name, FALSE, SMPREF_YES, array_merge(array('id' => 'delete_' . $this->name), $this->aExtraAttribs))); |
639 | $oTemplate->assign('name', $this->name); |
640 | return $oTemplate->fetch('edit_list_widget.tpl'); |
38d93650 |
641 | |
642 | } |
643 | |
b4856b14 |
644 | /** |
645 | * |
646 | */ |
cbe5423b |
647 | function save() { |
648 | $function = $this->save_function; |
649 | $function($this); |
44ef0f47 |
650 | } |
cbe5423b |
651 | |
b4856b14 |
652 | /** |
653 | * |
654 | */ |
cbe5423b |
655 | function changed() { |
38d93650 |
656 | |
657 | // edit lists have a lot going on, so we'll always process them |
658 | // |
659 | if ($this->type == SMOPT_TYPE_EDIT_LIST) return TRUE; |
660 | |
6206f6c4 |
661 | return ($this->value != $this->new_value); |
cbe5423b |
662 | } |
b4856b14 |
663 | } /* End of SquirrelOption class*/ |
cbe5423b |
664 | |
b4856b14 |
665 | /** |
f2aba536 |
666 | * Saves the option value (this is the default save function |
667 | * unless overridden by the user) |
668 | * |
b4856b14 |
669 | * @param object $option object that holds option name and new_value |
670 | */ |
cbe5423b |
671 | function save_option($option) { |
f2aba536 |
672 | |
673 | // Can't save the pref if we don't have the username |
674 | // |
dac16606 |
675 | if ( !sqgetGlobalVar('username', $username, SQ_SESSION ) ) { |
dac16606 |
676 | return; |
0b97a708 |
677 | } |
f2aba536 |
678 | |
0b97a708 |
679 | global $data_dir; |
f2aba536 |
680 | |
38d93650 |
681 | // edit lists: first add new elements to list, then |
682 | // remove any selected ones (note that we must add |
683 | // before deleting because the javascript that populates |
684 | // the "add" textbox when selecting items in the list |
685 | // (for deletion)) |
686 | // |
687 | if ($option->type == SMOPT_TYPE_EDIT_LIST) { |
688 | |
8eb63bb6 |
689 | if (empty($option->possible_values)) $option->possible_values = array(); |
95dbbd91 |
690 | if (!is_array($option->possible_values)) $option->possible_values = array($option->possible_values); |
8eb63bb6 |
691 | |
38d93650 |
692 | // add element if given |
693 | // |
694 | if (sqGetGlobalVar('add_' . $option->name, $new_element, SQ_POST)) { |
695 | $new_element = trim($new_element); |
696 | if (!empty($new_element) |
697 | && !in_array($new_element, $option->possible_values)) |
698 | $option->possible_values[] = $new_element; |
699 | } |
700 | |
701 | // delete selected elements if needed |
702 | // |
703 | if (is_array($option->new_value) |
704 | && sqGetGlobalVar('delete_' . $option->name, $ignore, SQ_POST)) |
705 | $option->possible_values = array_diff($option->possible_values, $option->new_value); |
706 | |
707 | // save full list (stored in "possible_values") |
708 | // |
709 | setPref($data_dir, $username, $option->name, serialize($option->possible_values)); |
710 | |
f2aba536 |
711 | // Certain option types need to be serialized because |
712 | // they are not scalar |
713 | // |
5a42c101 |
714 | } else if ($option->is_multiple_valued()) |
f2aba536 |
715 | setPref($data_dir, $username, $option->name, serialize($option->new_value)); |
38d93650 |
716 | |
f2aba536 |
717 | else |
718 | setPref($data_dir, $username, $option->name, $option->new_value); |
719 | |
cbe5423b |
720 | } |
721 | |
b4856b14 |
722 | /** |
723 | * save function that does not save |
724 | * @param object $option |
725 | */ |
cbe5423b |
726 | function save_option_noop($option) { |
727 | /* Do nothing here... */ |
9962527a |
728 | } |
44ef0f47 |
729 | |
b4856b14 |
730 | /** |
731 | * Create hidden 'optpage' input field with value set by argument |
732 | * @param string $optpage identification of option page |
733 | * @return string html formated hidden input field |
734 | */ |
cbe5423b |
735 | function create_optpage_element($optpage) { |
0177059f |
736 | return addHidden('optpage', $optpage); |
cbe5423b |
737 | } |
738 | |
b4856b14 |
739 | /** |
740 | * Create hidden 'optmode' input field with value set by argument |
741 | * @param string $optmode |
742 | * @return string html formated hidden input field |
743 | */ |
cbe5423b |
744 | function create_optmode_element($optmode) { |
0177059f |
745 | return addHidden('optmode', $optmode); |
cbe5423b |
746 | } |
747 | |
b4856b14 |
748 | /** |
749 | * @param array $optgrps |
750 | * @param array $optvals |
751 | * @return array |
752 | */ |
cbe5423b |
753 | function create_option_groups($optgrps, $optvals) { |
a3ec3c91 |
754 | /* Build a simple array with which to start. */ |
755 | $result = array(); |
756 | |
bbcafebd |
757 | /* Create option group for each option group name. */ |
758 | foreach ($optgrps as $grpkey => $grpname) { |
759 | $result[$grpkey] = array(); |
760 | $result[$grpkey]['name'] = $grpname; |
761 | $result[$grpkey]['options'] = array(); |
762 | } |
763 | |
a3ec3c91 |
764 | /* Create a new SquirrelOption for each set of option values. */ |
bbcafebd |
765 | foreach ($optvals as $grpkey => $grpopts) { |
766 | foreach ($grpopts as $optset) { |
28520c87 |
767 | /* Create a new option with all values given. */ |
768 | $next_option = new SquirrelOption( |
7390e240 |
769 | $optset['name'], |
770 | $optset['caption'], |
771 | $optset['type'], |
772 | (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE), |
773 | (isset($optset['initial_value']) ? $optset['initial_value'] : ''), |
774 | (isset($optset['posvals']) ? $optset['posvals'] : ''), |
775 | (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false) |
776 | ); |
bbcafebd |
777 | |
778 | /* If provided, set the size for this option. */ |
779 | if (isset($optset['size'])) { |
780 | $next_option->setSize($optset['size']); |
781 | } |
782 | |
361d6e1b |
783 | /* If provided, set the trailing_text for this option. */ |
784 | if (isset($optset['trailing_text'])) { |
785 | $next_option->setTrailingText($optset['trailing_text']); |
786 | } |
787 | |
bbcafebd |
788 | /* If provided, set the comment for this option. */ |
789 | if (isset($optset['comment'])) { |
790 | $next_option->setComment($optset['comment']); |
791 | } |
792 | |
cbe5423b |
793 | /* If provided, set the save function for this option. */ |
794 | if (isset($optset['save'])) { |
795 | $next_option->setSaveFunction($optset['save']); |
796 | } |
797 | |
0177059f |
798 | /* If provided, set the extra attributes for this option. */ |
799 | if (isset($optset['extra_attributes'])) { |
800 | $next_option->setExtraAttributes($optset['extra_attributes']); |
cbe5423b |
801 | } |
802 | |
6ae9e729 |
803 | /* If provided, set the "post script" for this option. */ |
804 | if (isset($optset['post_script'])) { |
805 | $next_option->setPostScript($optset['post_script']); |
806 | } |
807 | |
99ecf044 |
808 | /* If provided, set the folder_filter for this option. */ |
809 | if (isset($optset['folder_filter'])) { |
810 | $next_option->setFolderFilter($optset['folder_filter']); |
811 | } |
812 | |
bbcafebd |
813 | /* Add this option to the option array. */ |
814 | $result[$grpkey]['options'][] = $next_option; |
a3ec3c91 |
815 | } |
816 | } |
817 | |
818 | /* Return our resulting array. */ |
819 | return ($result); |
820 | } |
821 | |