Add radio button group widget type SMOPT_TYPE_STRLIST_RADIO
[squirrelmail.git] / functions / options.php
index 02638c97bf81b95c247215ea96497ec88a20e206..0bca18e608ad5943b9ffaec1b6979ffb772e757a 100644 (file)
@@ -30,6 +30,8 @@ define('SMOPT_TYPE_FLDRLIST_MULTI', 9);
 define('SMOPT_TYPE_EDIT_LIST', 10);
 define('SMOPT_TYPE_STRLIST_MULTI', 11);
 define('SMOPT_TYPE_BOOLEAN_CHECKBOX', 12);
+define('SMOPT_TYPE_BOOLEAN_RADIO', 13);
+define('SMOPT_TYPE_STRLIST_RADIO', 14);
 
 /* Define constants for the options refresh levels. */
 define('SMOPT_REFRESH_NONE', 0);
@@ -94,6 +96,20 @@ class SquirrelOption {
      * @var string
      */
     var $trailing_text;
+    /**
+     * Text that overrides the "Yes" label for boolean 
+     * radio option widgets
+     *
+     * @var string
+     */
+    var $yes_text;
+    /**
+     * Text that overrides the "No" label for boolean 
+     * radio option widgets
+     *
+     * @var string
+     */
+    var $no_text;
     /**
      * text displayed to the user
      *
@@ -177,6 +193,8 @@ class SquirrelOption {
         $this->htmlencoded = $htmlencoded;
         $this->size = SMOPT_SIZE_MEDIUM;
         $this->trailing_text = '';
+        $this->yes_text = '';
+        $this->no_text = '';
         $this->comment = '';
         $this->aExtraAttribs = array();
         $this->post_script = '';
@@ -243,6 +261,22 @@ class SquirrelOption {
         $this->trailing_text = $trailing_text;
     }
 
+    /**
+     * Set the yes_text for this option.
+     * @param string $yes_text
+     */
+    function setYesText($yes_text) {
+        $this->yes_text = $yes_text;
+    }
+
+    /**
+     * Set the no_text for this option.
+     * @param string $no_text
+     */
+    function setNoText($no_text) {
+        $this->no_text = $no_text;
+    }
+
     /**
      * Set the comment for this option.
      * @param string $comment
@@ -276,7 +310,7 @@ class SquirrelOption {
     }
 
     /**
-     * Set the trailing_text for this option.
+     * Set the folder_filter for this option.
      * @param string $folder_filter
      * @since 1.5.1
      */
@@ -324,6 +358,9 @@ class SquirrelOption {
             case SMOPT_TYPE_BOOLEAN_CHECKBOX:
                 $result = $this->createWidget_Boolean(TRUE);
                 break;
+            case SMOPT_TYPE_BOOLEAN_RADIO:
+                $result = $this->createWidget_Boolean(FALSE);
+                break;
             case SMOPT_TYPE_HIDDEN:
                 $result = $this->createWidget_Hidden();
                 break;
@@ -342,6 +379,9 @@ class SquirrelOption {
             case SMOPT_TYPE_STRLIST_MULTI:
                 $result = $this->createWidget_StrList(TRUE);
                 break;
+            case SMOPT_TYPE_STRLIST_RADIO:
+                $result = $this->createWidget_StrList(FALSE, TRUE);
+                break;
             default:
                 error_box ( 
                     sprintf(_("Option Type '%s' Not Found"), $this->type)
@@ -383,27 +423,57 @@ class SquirrelOption {
                 $width = 25;
         }
 
-        return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text);
+        return addInput('new_' . $this->name, $this->value, $width, 0, $this->aExtraAttribs) . htmlspecialchars($this->trailing_text); 
     }
 
     /**
-     * Create selection box
+     * Create selection box or radio button group
      *
      * When $this->htmlencoded is TRUE, the keys and values in 
      * $this->possible_values are assumed to be display-safe.  
      * Use with care!
      *
-     * @param boolean $multiple_select When TRUE, the select widget 
+     * Note that when building radio buttons instead of a select
+     * widget, if the "size" attribute is SMOPT_SIZE_TINY, the
+     * radio buttons will be output one after another without
+     * linebreaks between them.  Otherwise, each radio button
+     * goes on a line of its own.
+     *
+     * @param boolean $multiple_select When TRUE, the select widget
      *                                 will allow multiple selections
-     *                                 (OPTIONAL; default is FALSE 
+     *                                 (OPTIONAL; default is FALSE
      *                                 (single select list))
+     * @param boolean $radio_buttons   When TRUE, the widget will
+     *                                 instead be built as a group
+     *                                 of radio buttons (and
+     *                                 $multiple_select will be
+     *                                 forced to FALSE) (OPTIONAL;
+     *                                 default is FALSE (select widget))
      *
-     * @return string html formated selection box
+     * @return string html formated selection box or radio buttons
      *
      */
-    function createWidget_StrList($multiple_select=FALSE) {
+    function createWidget_StrList($multiple_select=FALSE, $radio_buttons=FALSE) {
 //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
 
+        // radio buttons instead of select widget?
+        //
+        if ($radio_buttons) {
+
+            global $br, $nbsp;
+            $result = '';
+            foreach ($this->possible_values as $real_value => $disp_value) {
+                $result .= addRadioBox('new_' . $this->name, ($this->value == $real_value), $real_value, array_merge(array('id' => 'new_' . $this->name . '_' . $real_value), $this->aExtraAttribs)) . $nbsp . create_label($disp_value, 'new_' . $this->name . '_' . $real_value);
+                if ($this->size != SMOPT_SIZE_TINY)
+                    $result .= $br;
+            }
+
+            return $result;
+        }
+
+
+        // everything below applies to select widgets
+        //
         switch ($this->size) {
 //FIXME: not sure about these sizes... seems like we could add another on the "large" side...
             case SMOPT_SIZE_TINY:
@@ -541,16 +611,20 @@ class SquirrelOption {
     /**
      * Create boolean widget
      *
+     * When creating Yes/No radio buttons, the "yes_text"
+     * and "no_text" option attributes are used to override
+     * the typical "Yes" and "No" text.
+     *
      * @param boolean $checkbox When TRUE, the widget will be
      *                          constructed as a checkbox,
      *                          otherwise it will be a set of
      *                          Yes/No radio buttons (OPTIONAL;
-     *                          default is FALSE (radio buttons)).
+     *                          default is TRUE (checkbox)).
      *
      * @return string html formated boolean widget
      *
      */
-    function createWidget_Boolean($checkbox=FALSE) {
+    function createWidget_Boolean($checkbox=TRUE) {
 
         global $oTemplate, $nbsp;
 
@@ -566,10 +640,10 @@ class SquirrelOption {
         else {
 
             /* Build the yes choice. */
-            $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');
+            $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((!empty($this->yes_text) ? $this->yes_text : _("Yes")), 'new_' . $this->name . '_yes');
 
             /* Build the no choice. */
-            $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');
+            $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((!empty($this->no_text) ? $this->no_text : _("No")), 'new_' . $this->name . '_no');
 
             /* Build the combined "boolean widget". */
             $result = "$yes_option$nbsp$nbsp$nbsp$nbsp$no_option";
@@ -781,6 +855,16 @@ function create_option_groups($optgrps, $optvals) {
                 $next_option->setTrailingText($optset['trailing_text']);
             }
 
+            /* If provided, set the yes_text for this option. */
+            if (isset($optset['yes_text'])) {
+                $next_option->setYesText($optset['yes_text']);
+            }
+
+            /* If provided, set the no_text for this option. */
+            if (isset($optset['no_text'])) {
+                $next_option->setNoText($optset['no_text']);
+            }
+
             /* If provided, set the comment for this option. */
             if (isset($optset['comment'])) {
                 $next_option->setComment($optset['comment']);