Index: branches/RC/core/units/general/helpers/priority_helper.php =================================================================== diff -u -N -r9639 -r11576 --- branches/RC/core/units/general/helpers/priority_helper.php (.../priority_helper.php) (revision 9639) +++ branches/RC/core/units/general/helpers/priority_helper.php (.../priority_helper.php) (revision 11576) @@ -1,52 +1,57 @@ getObject(); - + $field_options = $object->GetFieldOptions('Priority'); $table_name = $this->Application->getUnitOption($event->Prefix, 'TableName'); - + $sql = 'SELECT COUNT(*) FROM '.$table_name; if ($constrain) { - $sql .= ' WHERE '.$constrain; + $sql .= ' WHERE '.$constrain; } - + if (!isset($object->Fields['OldPriority'])) { $object->VirtualFields['OldPriority'] = Array('type' => 'int', 'default' => 0); } - + $items_count = $this->Conn->GetOne($sql); + $current_priority = is_a($object, 'kDBList') ? 0 : $object->GetDBField('Priority'); + + if ($is_new || $current_priority == -($items_count+1)) { + $items_count++; + } + if ($is_new) { // add new item to the end of list - $items_count++; $object->SetDBField('Priority', -$items_count); $object->SetDBField('OldPriority', -$items_count); } else { - $object->SetDBField('OldPriority', $object->GetDBField('Priority')); + $object->SetDBField('OldPriority', $current_priority); } - + for ($i = 1; $i <= $items_count; $i++) { $field_options['options'][-$i] = $i; } - + $object->SetFieldOptions('Priority', $field_options); // storing prioriry right after load for comparing when updating } - + /** * Updates priorities for changed items * @@ -59,30 +64,33 @@ { $id_field = $this->Application->getUnitOption($event->Prefix, 'IDField'); $table_name = $this->Application->getUnitOption($event->Prefix, 'TableName'); - + $not_processed = array_keys($changes); - + if(!$not_processed || !getArrayValue($not_processed, 0)) { + return $not_processed; + } + $ids = array(); foreach ($changes as $id => $pair) { array_push($ids, $id); $constrain = isset($pair['parent']) ? 'ParentId = '.$pair['parent'].' AND ' : ''; - + if ($pair['old'] == 'new') { // replace 0 with newly created item id (from $new_ids mapping) $not_processed[ array_search($id, $not_processed) ] = $new_ids[$id]; $id = $new_ids[$id]; - + $sql = 'SELECT MIN(Priority) FROM '.$table_name.' WHERE '.$constrain.' '.$id_field.' NOT IN ('.implode(',', $not_processed).')'; $min_priority = (int)$this->Conn->GetOne($sql) - 1; - + if ($pair['new'] < $min_priority) { $pair['new'] = $min_priority; } $pair['old'] = $min_priority; } - + if ($pair['new'] < $pair['old']) { $set = ' SET Priority = Priority + 1'; $where =' WHERE '.$constrain.' @@ -108,12 +116,12 @@ $ids = array_merge($ids, $this->Conn->GetCol('SELECT '.$id_field.' FROM '.$table_name.$where)); $q = 'UPDATE '.$table_name.' '.$set.$where; $this->Conn->Query($q); - + unset( $not_processed[array_search($id, $not_processed)] ); } return $ids; } - + /** * Recalculates priorities * @@ -124,14 +132,14 @@ { $id_field = $this->Application->getUnitOption($event->Prefix, 'IDField'); $table_name = $this->Application->getUnitOption($event->Prefix, 'TableName'); - + $sql = 'SELECT '.$id_field.' FROM '.$table_name. ($constrain ? ' WHERE '.$constrain : '').' ORDER BY Priority DESC'; - + $items = $this->Conn->GetCol($sql); - + foreach ($items as $item_number => $item_id) { $sql = 'UPDATE '.$table_name.' SET Priority = '.-($item_number + 1).' @@ -140,6 +148,6 @@ } return $items; } - + } ?> \ No newline at end of file