GridHandler::_fixColumnWidths PHP Méthode

_fixColumnWidths() private méthode

Method that grabs all the existing columns and makes sure the column widths add to exactly 100 N.B. We do some extra column fetching because PHP makes copies of arrays with foreach.
private _fixColumnWidths ( )
    private function _fixColumnWidths()
    {
        $columns =& $this->getColumns();
        $width = 0;
        $noSpecifiedWidthCount = 0;
        // Find the total width and how many columns do not specify their width.
        foreach ($columns as $column) {
            if ($column->hasFlag('width')) {
                $width += $column->getFlag('width');
            } else {
                $noSpecifiedWidthCount++;
            }
        }
        // Four cases: we have to add or remove some width, and either we have wiggle room or not.
        // First case, width less than 100 and some unspecified columns to add it to.
        if ($width < 100) {
            if ($noSpecifiedWidthCount > 0) {
                // We need to add width to columns that did not specify it.
                foreach ($columns as $column) {
                    if (!$column->hasFlag('width')) {
                        $modifyColumn = $this->getColumn($column->getId());
                        $modifyColumn->addFlag('width', round((100 - $width) / $noSpecifiedWidthCount));
                        unset($modifyColumn);
                    }
                }
            }
        }
        // Second case, width higher than 100 and all columns width specified.
        if ($width > 100) {
            if ($noSpecifiedWidthCount == 0) {
                // We need to remove width from all columns equally.
                $columnsToModify = $columns;
                foreach ($columns as $key => $column) {
                    // We don't want to change the indent column widht, so avoid it.
                    if ($column->getId() == 'indent') {
                        unset($columnsToModify[$key]);
                    }
                }
                // Calculate the value to remove from all columns.
                $difference = $width - 100;
                $columnsCount = count($columnsToModify);
                $removeValue = round($difference / $columnsCount);
                foreach ($columnsToModify as $column) {
                    $modifyColumn = $this->getColumn($column->getId());
                    if (end($columnsToModify) === $column) {
                        // Handle rounding problems.
                        $totalWidth = $width - $removeValue * $columnsCount;
                        if ($totalWidth < 100) {
                            $removeValue -= 100 - $totalWidth;
                        }
                    }
                    $modifyColumn->addFlag('width', $modifyColumn->getFlag('width') - $removeValue);
                }
            }
        }
    }