/** * Detects and corrects items with a sort column value of 0, by appending them to the bottom of the list * @param GridField $gridField Grid Field Reference * @param SS_List $dataList Data List of items to be checked */ protected function fixSortColumn($gridField, SS_List $dataList) { if (class_exists('UnsavedRelationList') && $dataList instanceof UnsavedRelationList) { return; } $list = clone $dataList; $list = $list->alterDataQuery(function ($query, SS_List $tmplist) { $query->limit(array()); return $query; }); $many_many = $list instanceof ManyManyList; if (!$many_many) { $sng = singleton($gridField->getModelClass()); $fieldType = $sng->db($this->sortColumn); if (!$fieldType || !($fieldType == 'Int' || is_subclass_of('Int', $fieldType))) { if (is_array($fieldType)) { user_error('Sort column ' . $this->sortColumn . ' could not be found in ' . $gridField->getModelClass() . '\'s ancestry', E_USER_ERROR); } else { user_error('Sort column ' . $this->sortColumn . ' must be an Int, column is of type ' . $fieldType, E_USER_ERROR); } exit; } } $max = $list->Max($this->sortColumn); $list = $list->filter($this->sortColumn, 0)->sort("Created,ID"); if ($list->Count() > 0) { $owner = $gridField->Form->getRecord(); $sortColumn = $this->sortColumn; $i = 1; if ($many_many) { list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many($gridField->getName()); $extraFields = $owner->many_many_extraFields($gridField->getName()); if (!$extraFields || !array_key_exists($this->sortColumn, $extraFields) || !($extraFields[$this->sortColumn] == 'Int' || is_subclass_of('Int', $extraFields[$this->sortColumn]))) { user_error('Sort column ' . $this->sortColumn . ' must be an Int, column is of type ' . $extraFields[$this->sortColumn], E_USER_ERROR); exit; } } else { //Find table containing the sort column $table = false; $class = $gridField->getModelClass(); $db = Config::inst()->get($class, "db", CONFIG::UNINHERITED); if (!empty($db) && array_key_exists($sortColumn, $db)) { $table = $class; } else { $classes = ClassInfo::ancestry($class, true); foreach ($classes as $class) { $db = Config::inst()->get($class, "db", CONFIG::UNINHERITED); if (!empty($db) && array_key_exists($sortColumn, $db)) { $table = $class; break; } } } if ($table === false) { user_error('Sort column ' . $this->sortColumn . ' could not be found in ' . $gridField->getModelClass() . '\'s ancestry', E_USER_ERROR); exit; } $baseDataClass = ClassInfo::baseDataClass($gridField->getModelClass()); } //Start transaction if supported if (DB::getConn()->supportsTransactions()) { DB::getConn()->transactionStart(); } $idCondition = null; if ($this->append_to_top && !$list instanceof RelationList) { $idCondition = '"ID" IN(\'' . implode("','", $dataList->getIDList()) . '\')'; } if ($this->append_to_top) { $topIncremented = array(); } foreach ($list as $obj) { if ($many_many) { if ($this->append_to_top) { //Upgrade all the records (including the last inserted from 0 to 1) DB::query('UPDATE "' . $table . '" SET "' . $sortColumn . '" = "' . $sortColumn . '"+1' . ' WHERE "' . $parentField . '" = ' . $owner->ID . (!empty($topIncremented) ? ' AND "' . $componentField . '" NOT IN(\'' . implode('\',\'', $topIncremented) . '\')' : '')); $topIncremented[] = $obj->ID; } else { //Append the last record to the bottom DB::query('UPDATE "' . $table . '" SET "' . $sortColumn . '" = ' . ($max + $i) . ' WHERE "' . $componentField . '" = ' . $obj->ID . ' AND "' . $parentField . '" = ' . $owner->ID); } } else { if ($this->append_to_top) { //Upgrade all the records (including the last inserted from 0 to 1) DB::query('UPDATE "' . $table . '" SET "' . $sortColumn . '" = "' . $sortColumn . '"+1' . ' WHERE ' . ($list instanceof RelationList ? '"' . $list->foreignKey . '" = ' . $owner->ID : $idCondition) . (!empty($topIncremented) ? ' AND "ID" NOT IN(\'' . implode('\',\'', $topIncremented) . '\')' : '')); //LastEdited DB::query('UPDATE "' . $baseDataClass . '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\'' . ' WHERE ' . ($list instanceof RelationList ? '"' . $list->foreignKey . '" = ' . $owner->ID : $idCondition) . (!empty($topIncremented) ? ' AND "ID" NOT IN(\'' . implode('\',\'', $topIncremented) . '\')' : '')); $topIncremented[] = $obj->ID; } else { //Append the last record to the bottom DB::query('UPDATE "' . $table . '" SET "' . $sortColumn . '" = ' . ($max + $i) . ' WHERE "ID" = ' . $obj->ID); //LastEdited DB::query('UPDATE "' . $baseDataClass . '" SET "LastEdited" = \'' . date('Y-m-d H:i:s') . '\'' . ' WHERE "ID" = ' . $obj->ID); } } $i++; } //End transaction if supported if (DB::getConn()->supportsTransactions()) { DB::getConn()->transactionEnd(); } } }