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