Profile::getLinearRightChoice PHP Method

getLinearRightChoice() static public method

Get right linear right choice.
static public getLinearRightChoice ( array $elements, array $options = [] ) : content
$elements array array all pair identifier => label
$options array array possible: 'canedit' 'field' name of the HTML field 'value' the value inside the database 'max_per_line' maximum number of elements per line 'check_all' add a checkbox to check or uncheck every checkbox 'rand' random value used to generate the ids 'zero_on_empty' do we send 0 when checkbox is not checked ? 'display' 'check_method' method used to check the right
return content if !display
    static function getLinearRightChoice(array $elements, array $options = array())
    {
        $param = array();
        $param['canedit'] = true;
        $param['field'] = '';
        $param['value'] = '';
        $param['max_per_line'] = 10;
        $param['check_all'] = false;
        $param['rand'] = mt_rand();
        $param['zero_on_empty'] = true;
        $param['display'] = true;
        $param['check_method'] = function ($element, $field) {
            return ($field & $element) == $element;
        };
        if (is_array($options) && count($options)) {
            foreach ($options as $key => $val) {
                $param[$key] = $val;
            }
        }
        if (empty($param['field'])) {
            return;
        }
        $nb_cbs = count($elements);
        $cb_options = array('readonly' => !$param['canedit']);
        if ($param['check_all']) {
            $nb_cbs++;
            $massive_tag = 'checkall_' . $param['field'] . '_' . $param['rand'];
            $cb_options['massive_tags'] = $massive_tag;
        }
        $nb_lines = ceil($nb_cbs / $param['max_per_line']);
        $nb_item_per_line = ceil($nb_cbs / $nb_lines);
        $out = '';
        $count = 0;
        $nb_checked = 0;
        foreach ($elements as $element => $label) {
            if ($count != 0) {
                if ($count % $nb_item_per_line == 0) {
                    $out .= "<br>\n";
                } else {
                    $out .= "&nbsp;-\n\t\t&nbsp;";
                }
            } else {
                $out .= "\n\t\t";
            }
            $out .= $label . '&nbsp;';
            $cb_options['name'] = $param['field'] . '[' . $element . ']';
            $cb_options['id'] = Html::cleanId('checkbox_linear_' . $cb_options['name'] . '_' . $param['rand']);
            $cb_options['zero_on_empty'] = $param['zero_on_empty'];
            $cb_options['checked'] = $param['check_method']($element, $param['value']);
            $out .= Html::getCheckbox($cb_options);
            $count++;
            if ($cb_options['checked']) {
                $nb_checked++;
            }
        }
        if ($param['check_all']) {
            $cb_options = array('criterion' => array('tag_for_massive' => $massive_tag), 'id' => Html::cleanId('checkbox_linear_' . $param['rand']));
            if ($nb_checked > count($elements) / 2) {
                $cb_options['checked'] = true;
            }
            $out .= "&nbsp;-&nbsp;<i><b>" . __('Select/unselect all') . "</b></i>&nbsp;" . Html::getCheckbox($cb_options);
        }
        if (!$param['display']) {
            return $out;
        }
        echo $out;
    }