/**
  * Handles requests to reorder a set of IDs in a specific order.
  *
  * @param GridField $grid
  * @param SS_HTTPRequest $request
  * @return SS_HTTPResponse
  */
 public function handleReorder($grid, $request)
 {
     $list = $grid->getList();
     $modelClass = $grid->getModelClass();
     if ($list instanceof ManyManyList && !singleton($modelClass)->canView()) {
         $this->httpError(403);
     } else {
         if (!$list instanceof ManyManyList && !singleton($modelClass)->canEdit()) {
             $this->httpError(403);
         }
     }
     $ids = $request->postVar('order');
     $field = $this->getSortField();
     if (!is_array($ids)) {
         $this->httpError(400);
     }
     $sortterm = '';
     if ($this->extraSortFields) {
         if (is_array($this->extraSortFields)) {
             foreach ($this->extraSortFields as $col => $dir) {
                 $sortterm .= "{$col} {$dir}, ";
             }
         } else {
             $sortterm = $this->extraSortFields . ', ';
         }
     }
     $sortterm .= '"' . $this->getSortTable($list) . '"."' . $field . '"';
     $items = $list->filter('ID', $ids)->sort($sortterm);
     // Ensure that each provided ID corresponded to an actual object.
     if (count($items) != count($ids)) {
         $this->httpError(404);
     }
     // Save any un-comitted changes to the gridfield
     if (($form = $grid->getForm()) && ($record = $form->getRecord())) {
         $form->loadDataFrom($request->requestVars(), true);
         $grid->saveInto($record);
     }
     // 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, $ids);
     return $grid->FieldHolder();
 }
 /**
  * Handles requests to reorder a set of IDs in a specific order.
  *
  * @param GridField $grid
  * @param SS_HTTPRequest $request
  * @return SS_HTTPResponse
  */
 public function handleReorder($grid, $request)
 {
     if (!$this->immediateUpdate) {
         $this->httpError(400);
     }
     $list = $grid->getList();
     $modelClass = $grid->getModelClass();
     if ($list instanceof ManyManyList && !singleton($modelClass)->canView()) {
         $this->httpError(403);
     } else {
         if (!$list instanceof ManyManyList && !singleton($modelClass)->canEdit()) {
             $this->httpError(403);
         }
     }
     // Save any un-committed changes to the gridfield
     if (($form = $grid->getForm()) && ($record = $form->getRecord())) {
         $form->loadDataFrom($request->requestVars(), true);
         $grid->saveInto($record);
     }
     // Get records from the `GridFieldEditableColumns` column
     $data = $request->postVar($grid->getName());
     $sortedIDs = $this->getSortedIDs($data);
     if (!$this->executeReorder($grid, $sortedIDs)) {
         $this->httpError(400);
     }
     Controller::curr()->getResponse()->addHeader('X-Status', rawurlencode('Records reordered.'));
     return $grid->FieldHolder();
 }