Html::showCheckboxMatrix PHP Method

showCheckboxMatrix() static public method

Display choice matrix
static public showCheckboxMatrix ( array $columns, array $rows, array $options = [] ) : random
$columns array array of column field name => column label
$rows array array of field name => array( 'label' the label of the row 'columns' an array of specific information regaring current row and given column indexed by column field_name * a string if only have to display a string * an array('value' => ???, 'readonly' => ???) that is used to Dropdown::showYesNo()
$options array array possible: 'title' of the matrix 'first_cell' the content of the upper-left cell 'row_check_all' set to true to display a checkbox to check all elements of the row 'col_check_all' set to true to display a checkbox to check all elements of the col 'rand' random number to use for ids
return random value used to generate the ids
    static function showCheckboxMatrix(array $columns, array $rows, array $options = array())
    {
        $param['title'] = '';
        $param['first_cell'] = ' ';
        $param['row_check_all'] = false;
        $param['col_check_all'] = false;
        $param['rotate_column_titles'] = false;
        $param['rand'] = mt_rand();
        $param['table_class'] = 'tab_cadre_fixehov';
        $param['cell_class_method'] = NULL;
        if (is_array($options) && count($options)) {
            foreach ($options as $key => $val) {
                $param[$key] = $val;
            }
        }
        $cb_options = array('title' => __s('Check/uncheck all'));
        $number_columns = count($columns) + 1;
        if ($param['row_check_all']) {
            $number_columns += 1;
        }
        $width = round(100 / $number_columns);
        echo "\n<table class='" . $param['table_class'] . "'>\n";
        if (!empty($param['title'])) {
            echo "\t<tr>\n";
            echo "\t\t<th colspan='{$number_columns}'>" . $param['title'] . "</th>\n";
            echo "\t</tr>\n";
        }
        echo "\t<tr class='tab_bg_1'>\n";
        echo "\t\t<td>" . $param['first_cell'] . "</td>\n";
        foreach ($columns as $col_name => $column) {
            $nb_cb_per_col[$col_name] = array('total' => 0, 'checked' => 0);
            $col_id = Html::cleanId('col_label_' . $col_name . '_' . $param['rand']);
            echo "\t\t<td class='center b";
            if ($param['rotate_column_titles']) {
                echo " rotate";
            }
            echo "' id='{$col_id}' width='{$width}%'>";
            if (!is_array($column)) {
                $columns[$col_name] = $column = array('label' => $column);
            }
            if (isset($column['short']) && isset($column['long'])) {
                echo $column['short'];
                self::showToolTip($column['long'], array('applyto' => $col_id));
            } else {
                echo $column['label'];
            }
            echo "</td>\n";
        }
        if ($param['row_check_all']) {
            $col_id = Html::cleanId('col_of_table_' . $param['rand']);
            echo "\t\t<td class='center";
            if ($param['rotate_column_titles']) {
                echo " rotate";
            }
            echo "' id='{$col_id}'>" . __('Select/unselect all') . "</td>\n";
        }
        echo "\t</tr>\n";
        foreach ($rows as $row_name => $row) {
            if (!is_string($row) && !is_array($row)) {
                continue;
            }
            echo "\t<tr class='tab_bg_1'>\n";
            if (is_string($row)) {
                echo "\t\t<th colspan='{$number_columns}'>{$row}</th>\n";
            } else {
                $row_id = Html::cleanId('row_label_' . $row_name . '_' . $param['rand']);
                if (isset($row['class'])) {
                    $class = $row['class'];
                } else {
                    $class = '';
                }
                echo "\t\t<td class='b {$class}' id='{$row_id}'>";
                if (!empty($row['label'])) {
                    echo $row['label'];
                } else {
                    echo "&nbsp;";
                }
                echo "</td>\n";
                $nb_cb_per_row = array('total' => 0, 'checked' => 0);
                foreach ($columns as $col_name => $column) {
                    $class = '';
                    if (!empty($row['class']) && !empty($column['class'])) {
                        if (is_callable($param['cell_class_method'])) {
                            $class = $param['cell_class_method']($row['class'], $column['class']);
                        }
                    } else {
                        if (!empty($row['class'])) {
                            $class = $row['class'];
                        } else {
                            if (!empty($column['class'])) {
                                $class = $column['class'];
                            }
                        }
                    }
                    echo "\t\t<td class='center {$class}'>";
                    // Warning: isset return false if the value is NULL ...
                    if (array_key_exists($col_name, $row['columns'])) {
                        $content = $row['columns'][$col_name];
                        if (is_array($content) && array_key_exists('checked', $content)) {
                            if (!array_key_exists('readonly', $content)) {
                                $content['readonly'] = false;
                            }
                            $content['massive_tags'] = array();
                            if ($param['row_check_all']) {
                                $content['massive_tags'][] = 'row_' . $row_name . '_' . $param['rand'];
                            }
                            if ($param['col_check_all']) {
                                $content['massive_tags'][] = 'col_' . $col_name . '_' . $param['rand'];
                            }
                            if ($param['row_check_all'] && $param['col_check_all']) {
                                $content['massive_tags'][] = 'table_' . $param['rand'];
                            }
                            $content['name'] = $row_name . "[{$col_name}]";
                            $content['id'] = Html::cleanId('cb_' . $row_name . '_' . $col_name . '_' . $param['rand']);
                            Html::showCheckbox($content);
                            $nb_cb_per_col[$col_name]['total']++;
                            $nb_cb_per_row['total']++;
                            if ($content['checked']) {
                                $nb_cb_per_col[$col_name]['checked']++;
                                $nb_cb_per_row['checked']++;
                            }
                        } else {
                            if (is_string($content)) {
                                echo $content;
                            } else {
                                echo "&nbsp;";
                            }
                        }
                    } else {
                        echo "&nbsp;";
                    }
                    echo "</td>\n";
                }
            }
            if ($param['row_check_all'] && !is_string($row) && $nb_cb_per_row['total'] > 1) {
                $cb_options['criterion'] = array('tag_for_massive' => 'row_' . $row_name . '_' . $param['rand']);
                $cb_options['massive_tags'] = 'table_' . $param['rand'];
                $cb_options['id'] = Html::cleanId('cb_checkall_row_' . $row_name . '_' . $param['rand']);
                $cb_options['checked'] = $nb_cb_per_row['checked'] > $nb_cb_per_row['total'] / 2;
                echo "\t\t<td class='center'>" . Html::getCheckbox($cb_options) . "</td>\n";
            }
            if ($nb_cb_per_row['total'] == 1) {
                echo "\t\t<td class='center'></td>\n";
            }
            echo "\t</tr>\n";
        }
        if ($param['col_check_all']) {
            echo "\t<tr class='tab_bg_1'>\n";
            echo "\t\t<td>" . __('Select/unselect all') . "</td>\n";
            foreach ($columns as $col_name => $column) {
                echo "\t\t<td class='center'>";
                if ($nb_cb_per_col[$col_name]['total'] > 1) {
                    $cb_options['criterion'] = array('tag_for_massive' => 'col_' . $col_name . '_' . $param['rand']);
                    $cb_options['massive_tags'] = 'table_' . $param['rand'];
                    $cb_options['id'] = Html::cleanId('cb_checkall_col_' . $col_name . '_' . $param['rand']);
                    $cb_options['checked'] = $nb_cb_per_col[$col_name]['checked'] > $nb_cb_per_col[$col_name]['total'] / 2;
                    echo Html::getCheckbox($cb_options);
                } else {
                    echo "&nbsp;";
                }
                echo "</td>\n";
            }
            if ($param['row_check_all']) {
                $cb_options['criterion'] = array('tag_for_massive' => 'table_' . $param['rand']);
                $cb_options['massive_tags'] = '';
                $cb_options['id'] = Html::cleanId('cb_checkall_table_' . $param['rand']);
                echo "\t\t<td class='center'>" . Html::getCheckbox($cb_options) . "</td>\n";
            }
            echo "\t</tr>\n";
        }
        echo "</table>\n";
        return $param['rand'];
    }

Usage Example

Example #1
0
 /**
  * Display rights choice matrix
  *
  * @since version 0.85
  *
  * @param $rights array    possible:
  *             'itemtype'   => the type of the item to check (as passed to self::getRightsFor())
  *             'rights'     => when use of self::getRightsFor() is impossible
  *             'label'      => the label for the right
  *             'field'      => the name of the field inside the DB and HTML form (prefixed by '_')
  *             'html_field' => when $html_field != '_'.$field
  * @param $options array   possible:
  *             'title'         the title of the matrix
  *             'canedit'
  *             'default_class' the default CSS class used for the row
  *
  * @return random value used to generate the ids
  **/
 function displayRightsChoiceMatrix(array $rights, array $options = array())
 {
     $param = array();
     $param['title'] = '';
     $param['canedit'] = true;
     $param['default_class'] = '';
     if (is_array($options) && count($options)) {
         foreach ($options as $key => $val) {
             $param[$key] = $val;
         }
     }
     // To be completed before display to avoid non available rights in DB
     $availablerights = ProfileRight::getAllPossibleRights();
     $column_labels = array();
     $columns = array();
     $rows = array();
     foreach ($rights as $info) {
         if (is_string($info)) {
             $rows[] = $info;
             continue;
         }
         if (is_array($info) && (!empty($info['itemtype']) || !empty($info['rights'])) && !empty($info['label']) && !empty($info['field'])) {
             // Add right if it does not exists : security for update
             if (!isset($availablerights[$info['field']])) {
                 ProfileRight::addProfileRights(array($info['field']));
             }
             $row = array('label' => $info['label'], 'columns' => array());
             if (!empty($info['row_class'])) {
                 $row['class'] = $info['row_class'];
             } else {
                 $row['class'] = $param['default_class'];
             }
             if (isset($this->fields[$info['field']])) {
                 $profile_right = $this->fields[$info['field']];
             } else {
                 $profile_right = 0;
             }
             if (isset($info['rights'])) {
                 $rights = $info['rights'];
             } else {
                 $rights = self::getRightsFor($info['itemtype']);
             }
             foreach ($rights as $right => $label) {
                 if (!isset($column_labels[$right])) {
                     $column_labels[$right] = array();
                 }
                 if (is_array($label)) {
                     $long_label = $label['long'];
                 } else {
                     $long_label = $label;
                 }
                 if (!isset($column_labels[$right][$long_label])) {
                     $column_labels[$right][$long_label] = count($column_labels[$right]);
                 }
                 $right_value = $right . '_' . $column_labels[$right][$long_label];
                 $columns[$right_value] = $label;
                 $checked = ($profile_right & $right) == $right ? 1 : 0;
                 $row['columns'][$right_value] = array('checked' => $checked);
                 if (!$param['canedit']) {
                     $row['columns'][$right_value]['readonly'] = true;
                 }
             }
             if (!empty($info['html_field'])) {
                 $rows[$info['html_field']] = $row;
             } else {
                 $rows['_' . $info['field']] = $row;
             }
         }
     }
     uksort($columns, function ($a, $b) {
         $a = explode('_', $a);
         $b = explode('_', $b);
         // For standard rights sort by right
         if ($a[0] < 1024 || $b[0] < 1024) {
             if ($a[0] > $b[0]) {
                 return true;
             }
             if ($a[0] < $b[0]) {
                 return false;
             }
             return $a[1] > $b[1];
             // For extra right sort by type
         }
         return $a[1] > $b[1];
     });
     return Html::showCheckboxMatrix($columns, $rows, array('title' => $param['title'], 'row_check_all' => count($columns) > 1, 'col_check_all' => count($rows) > 1, 'rotate_column_titles' => false));
 }
Html