Profile::displayRightsChoiceMatrix PHP Method

displayRightsChoiceMatrix() public method

Display rights choice matrix
public displayRightsChoiceMatrix ( array $rights, array $options = [] ) : random
$rights array 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
$options array 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));
    }

Usage Example

 /**
  * Show profile form
  *
  * @param $items_id integer id of the profile
  * @param $target value url of target
  *
  * @return nothing
  **/
 function showForm($profiles_id = 0, $openform = TRUE, $closeform = TRUE)
 {
     echo "<div class='firstbloc'>";
     if (($canedit = Session::haveRightsOr(self::$rightname, array(CREATE, UPDATE, PURGE))) && $openform) {
         $profile = new Profile();
         echo "<form method='post' action='" . $profile->getFormURL() . "'>";
     }
     $profile = new Profile();
     $profile->getFromDB($profiles_id);
     $rights = array(array('itemtype' => 'PluginAddressingAddressing', 'label' => __('Generate reports', 'addressing'), 'field' => 'plugin_addressing'));
     $profile->displayRightsChoiceMatrix($rights, array('canedit' => $canedit, 'default_class' => 'tab_bg_2', 'title' => __('General')));
     echo "<table class='tab_cadre_fixehov'>";
     $effective_rights = ProfileRight::getProfileRights($profiles_id, array('plugin_addressing_use_ping_in_equipment'));
     echo "<tr class='tab_bg_2'>";
     echo "<td width='20%'>" . __('Use ping on equipment form', 'addressing') . "</td>";
     echo "<td colspan='5'>";
     Html::showCheckbox(array('name' => '_plugin_addressing_use_ping_in_equipment[1_0]', 'checked' => $effective_rights['plugin_addressing_use_ping_in_equipment']));
     echo "</td></tr>\n";
     echo "</table>";
     if ($canedit && $closeform) {
         echo "<div class='center'>";
         echo Html::hidden('id', array('value' => $profiles_id));
         echo Html::submit(_sx('button', 'Save'), array('name' => 'update'));
         echo "</div>\n";
         Html::closeForm();
     }
     echo "</div>";
 }
All Usage Examples Of Profile::displayRightsChoiceMatrix