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