Resolve confusing var names for preferences.
[squirrelmail.git] / functions / options.php
index c6d305ecb90463f378ea7acfa561eba6fda3dbfc..a8236919370cd4a2290b9c126cf8b4919fb312a0 100644 (file)
@@ -3,13 +3,13 @@
 /**
  * options.php
  *
- * Copyright (c) 1999-2004 The SquirrelMail Project Team
- * Licensed under the GNU GPL. For full terms see the file COPYING.
- *
  * Functions needed to display the options pages.
  *
+ * @copyright © 1999-2006 The SquirrelMail Project Team
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package squirrelmail
+ * @subpackage prefs
  */
 
 /**********************************************/
@@ -44,7 +44,7 @@ define('SMOPT_SAVE_DEFAULT', 'save_option');
 define('SMOPT_SAVE_NOOP', 'save_option_noop');
 
 /**
- * SquirrelOption: An option for Squirrelmail.
+ * SquirrelOption: An option for SquirrelMail.
  *
  * @package squirrelmail
  * @subpackage prefs
@@ -68,7 +68,7 @@ class SquirrelOption {
      */
     var $type;
     /**
-     * Indicates if a link should be shown to refresh part 
+     * Indicates if a link should be shown to refresh part
      * or all of the window
      *
      * See SMOPT_REFRESH_* defines
@@ -83,9 +83,9 @@ class SquirrelOption {
      */
     var $size;
     /**
-     * Text that follows a text input or 
+     * Text that follows a text input or
      * select list input on the preferences page
-     * 
+     *
      * useful for indicating units, meanings of special values, etc.
      * @var string
      */
@@ -103,7 +103,7 @@ class SquirrelOption {
      */
     var $script;
     /**
-     * script (usually Javascript) that will be placed after (outside of) 
+     * script (usually Javascript) that will be placed after (outside of)
      * the INPUT tag
      * @var string
      */
@@ -127,7 +127,7 @@ class SquirrelOption {
      */
     var $new_value;
     /**
-     * associative array, where each key is an actual input value 
+     * associative array, where each key is an actual input value
      * and the corresponding value is what is displayed to the user
      * for that list item in the drop-down list
      * @var array
@@ -135,13 +135,20 @@ class SquirrelOption {
     var $possible_values;
     /**
      * disables html sanitizing.
-     * 
-     * WARNING - don't use it, if user input is possible in option 
-     * or use own sanitizing functions. Currently works only with 
+     *
+     * WARNING - don't use it, if user input is possible in option
+     * or use own sanitizing functions. Currently works only with
      * SMOPT_TYPE_STRLIST.
      * @var bool
      */
     var $htmlencoded=false;
+    /**
+     * Controls folder list limits in SMOPT_TYPE_FLDRLIST widget.
+     * See $flag argument in sqimap_mailbox_option_list() function.
+     * @var string
+     * @since 1.5.1
+     */
+    var $folder_filter='noselect';
 
     /**
      * Constructor function
@@ -168,11 +175,23 @@ class SquirrelOption {
         $this->script = '';
         $this->post_script = '';
 
-        /* Check for a current value. */
+        /**
+         * Check for a current value.  If the $GLOBALS[] value exists, we also
+         * need to make sure it is the same data type as the initial value to
+         * make sure we are looking at the correct preference.
+         */
+        $var_type = NULL;        
         if (!empty($initial_value)) {
-            $this->value = $initial_value;
-        } else if (isset($GLOBALS[$name])) {
+            $var_type = gettype($initial_value);
+        } elseif (is_array($possible_values)) {
+            list($index, $x) = each ($possible_values);
+            $var_type = gettype($index);
+        }
+        
+        if (isset($GLOBALS[$name]) && (is_null($var_type) ? true : $var_type == gettype($GLOBALS[$name]))) {
             $this->value = $GLOBALS[$name];
+        } else if (!empty($initial_value)) {
+            $this->value = $initial_value;
         } else {
             $this->value = '';
         }
@@ -232,7 +251,7 @@ class SquirrelOption {
 
     /**
      * Set the script for this option.
-     * @param string $script 
+     * @param string $script
      */
     function setScript($script) {
         $this->script = $script;
@@ -254,11 +273,20 @@ class SquirrelOption {
         $this->save_function = $save_function;
     }
 
+    /**
+     * Set the trailing_text for this option.
+     * @param string $folder_filter
+     * @since 1.5.1
+     */
+    function setFolderFilter($folder_filter) {
+        $this->folder_filter = $folder_filter;
+    }
+
     /**
      * Creates fields on option pages according to option type
      *
      * Function that calls other createWidget* functions.
-     * @return string html formated option field 
+     * @return string html formated option field
      */
     function createHTMLWidget() {
         global $color;
@@ -383,14 +411,14 @@ class SquirrelOption {
     function createWidget_FolderList() {
         $selected = array(strtolower($this->value));
 
-        /* Begin the select tag. */
-        $result = "<select name=\"new_$this->name\" $this->script>\n";
+        /* set initial value */
+        $result = '';
 
         /* Add each possible value to the select list. */
         foreach ($this->possible_values as $real_value => $disp_value) {
             if ( is_array($disp_value) ) {
               /* For folder list, we passed in the array of boxes.. */
-              $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value);
+              $new_option = sqimap_mailbox_option_list(0, $selected, 0, $disp_value, $this->folder_filter);
             } else {
               /* Start the next new option string. */
               $new_option = '<option value="' . htmlspecialchars($real_value) . '"';
@@ -406,9 +434,19 @@ class SquirrelOption {
             /* And add the new option string to our select tag. */
             $result .= $new_option;
         }
-        /* Close the select tag and return our happy result. */
-        $result .= "</select>\n";
-        return ($result);
+
+
+        if (empty($result)) {
+            // string is displayed when interface can't build folder selection box
+            return _("unavailable");
+        } else {
+            /* Begin the select tag. */
+            $ret = "<select name=\"new_$this->name\" $this->script>\n";
+            $ret.= $result;
+            /* Close the select tag and return our happy result. */
+            $ret.= "</select>\n";
+            return ($ret);
+        }
     }
 
     /**
@@ -610,14 +648,14 @@ function create_option_groups($optgrps, $optvals) {
         foreach ($grpopts as $optset) {
             /* Create a new option with all values given. */
             $next_option = new SquirrelOption(
-                 $optset['name'],
-                 $optset['caption'],
-                 $optset['type'],
-                 (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
-                 (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
-                 (isset($optset['posvals']) ? $optset['posvals'] : ''),
-                 (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
-            );
+                    $optset['name'],
+                    $optset['caption'],
+                    $optset['type'],
+                    (isset($optset['refresh']) ? $optset['refresh'] : SMOPT_REFRESH_NONE),
+                    (isset($optset['initial_value']) ? $optset['initial_value'] : ''),
+                    (isset($optset['posvals']) ? $optset['posvals'] : ''),
+                    (isset($optset['htmlencoded']) ? $optset['htmlencoded'] : false)
+                    );
 
             /* If provided, set the size for this option. */
             if (isset($optset['size'])) {
@@ -649,6 +687,11 @@ function create_option_groups($optgrps, $optvals) {
                 $next_option->setPostScript($optset['post_script']);
             }
 
+            /* If provided, set the folder_filter for this option. */
+            if (isset($optset['folder_filter'])) {
+                $next_option->setFolderFilter($optset['folder_filter']);
+            }
+
             /* Add this option to the option array. */
             $result[$grpkey]['options'][] = $next_option;
         }
@@ -669,7 +712,7 @@ function print_option_groups($option_groups) {
             echo html_tag( 'tr', "\n".
                         html_tag( 'td',
                             '<b>' . $next_optgrp['name'] . '</b>' ,
-                        'center' ,'', 'valign="middle" colspan="2" nowrap' )
+                        'center' ,'', 'valign="middle" colspan="2" style="white-space: nowrap;"' )
                     ) ."\n";
         }
 
@@ -703,4 +746,3 @@ function OptionSubmit( $name ) {
 }
 
 // vim: et ts=4
-?>
\ No newline at end of file