GridFieldOrderableRows::executeReorder PHP Method

executeReorder() protected method

protected executeReorder ( GridField $grid, array $sortedIDs ) : boolean
$grid GridField
$sortedIDs array List of IDS, where the key is the sort field value to save
return boolean
    protected function executeReorder(GridField $grid, $sortedIDs)
    {
        if (!is_array($sortedIDs)) {
            return false;
        }
        $field = $this->getSortField();
        $sortterm = '';
        if ($this->extraSortFields) {
            if (is_array($this->extraSortFields)) {
                foreach ($this->extraSortFields as $col => $dir) {
                    $sortterm .= "{$col} {$dir}, ";
                }
            } else {
                $sortterm = $this->extraSortFields . ', ';
            }
        }
        $list = $grid->getList();
        $sortterm .= '"' . $this->getSortTable($list) . '"."' . $field . '"';
        $items = $list->filter('ID', $sortedIDs)->sort($sortterm);
        // Ensure that each provided ID corresponded to an actual object.
        if (count($items) != count($sortedIDs)) {
            return false;
        }
        // Populate each object we are sorting with a sort value.
        $this->populateSortValues($items);
        // Generate the current sort values.
        if ($items instanceof ManyManyList) {
            $current = array();
            foreach ($items->toArray() as $record) {
                // NOTE: _SortColumn0 is the first ->sort() field
                //		 used by SS when functions are detected in a SELECT
                //	     or CASE WHEN.
                if (isset($record->_SortColumn0)) {
                    $current[$record->ID] = $record->_SortColumn0;
                } else {
                    $current[$record->ID] = $record->{$field};
                }
            }
        } else {
            $current = $items->map('ID', $field)->toArray();
        }
        // Perform the actual re-ordering.
        $this->reorderItems($list, $current, $sortedIDs);
        return true;
    }