Dropdown::showYesNo PHP Method

showYesNo() static public method

Make a select box for a boolean choice (Yes/No) or display a checkbox. Add a 'use_checkbox' = true to the $params array to display a checkbox instead a select box
static public showYesNo ( $name, $value, $restrict_to, $params = [] ) : rand
$name select name
$value preselected value. (default 0)
$restrict_to allows to display only yes or no in the dropdown (default -1)
$params Array of optional options (passed to showFromArray)
return rand value
    static function showYesNo($name, $value = 0, $restrict_to = -1, $params = array())
    {
        if (!array_key_exists('use_checkbox', $params)) {
            // TODO: switch to true when Html::showCheckbox() is validated
            $params['use_checkbox'] = false;
        }
        if ($params['use_checkbox']) {
            if (!empty($params['rand'])) {
                $rand = $params['rand'];
            } else {
                $rand = mt_rand();
            }
            $options = array('name' => $name, 'id' => Html::cleanId("dropdown_" . $name . $rand));
            switch ($restrict_to) {
                case 0:
                    $options['checked'] = false;
                    $options['readonly'] = true;
                    break;
                case 1:
                    $options['checked'] = true;
                    $options['readonly'] = true;
                    break;
                default:
                    $options['checked'] = $value ? 1 : 0;
                    $options['readonly'] = false;
                    break;
            }
            Html::showCheckbox($options);
            return $rand;
        }
        if ($restrict_to != 0) {
            $options[0] = __('No');
        }
        if ($restrict_to != 1) {
            $options[1] = __('Yes');
        }
        $params['value'] = $value;
        $params['width'] = "65px";
        return self::showFromArray($name, $options, $params);
    }

Usage Example

 static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0)
 {
     return;
     $profile = new Profile();
     $found_profiles = $profile->find("`interface` = 'central'");
     $tab_profile = new self();
     $found_tab_profiles = $tab_profile->find("`plugin_custom_tabs_id` = " . $item->getID());
     echo "<form method='POST' action='tabprofile.form.php' />";
     echo "<table class='tab_cadre_fixe'>";
     echo "<tr><th colspan='4'>" . __("Visibility") . "</th></tr>";
     $odd = 0;
     foreach ($found_profiles as $profiles_id => $profile_fields) {
         if ($odd % 2 === 0) {
             echo "<tr>";
         }
         echo "<td>" . $profile_fields['name'] . "</td>";
         echo "<td>";
         Dropdown::showYesNo("tab_profile[{$profiles_id}]", 0);
         echo "</td>";
         if ($odd % 2 === 1) {
             echo "</tr>";
         }
         $odd++;
     }
     if ($odd % 2 === 0) {
         echo "</tr>";
     }
     echo "<tr><td colspan='4'><div class='center'>";
     echo "<input type='submit' name='update' value=\"" . _sx('button', 'Post') . "\" class='submit'>";
     echo "</div></td></tr>";
     echo "</table>";
     Html::closeForm();
     return true;
 }
All Usage Examples Of Dropdown::showYesNo