Index: branches/RC/core/units/general/helpers/priority_helper.php =================================================================== diff -u -r8929 -r9639 --- branches/RC/core/units/general/helpers/priority_helper.php (.../priority_helper.php) (revision 8929) +++ branches/RC/core/units/general/helpers/priority_helper.php (.../priority_helper.php) (revision 9639) @@ -22,7 +22,10 @@ FROM '.$table_name; if ($constrain) { $sql .= ' WHERE '.$constrain; + } + if (!isset($object->Fields['OldPriority'])) { + $object->VirtualFields['OldPriority'] = Array('type' => 'int', 'default' => 0); } $items_count = $this->Conn->GetOne($sql); @@ -32,19 +35,23 @@ $object->SetDBField('Priority', -$items_count); $object->SetDBField('OldPriority', -$items_count); } + else { + $object->SetDBField('OldPriority', $object->GetDBField('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 * * @param kEvent $event - * @param Array $changes = Array (CategoryID => Array ('parent' => ..., 'new' => ..., 'old' => ...), ...) + * @param Array $changes = Array (ID => Array ('parent' => ..., 'new' => ..., 'old' => ...), ...) * @param Array $new_ids = Array (temp_id => live_id) * @param string $constrain */ @@ -55,8 +62,10 @@ $not_processed = array_keys($changes); + $ids = array(); foreach ($changes as $id => $pair) { - $constrain = 'ParentId = '.$pair['parent'].' AND '; + 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) @@ -65,7 +74,7 @@ $sql = 'SELECT MIN(Priority) FROM '.$table_name.' - WHERE '.$constrain.' CategoryId NOT IN ('.implode(',', $not_processed).')'; + WHERE '.$constrain.' '.$id_field.' NOT IN ('.implode(',', $not_processed).')'; $min_priority = (int)$this->Conn->GetOne($sql) - 1; if ($pair['new'] < $min_priority) { @@ -75,31 +84,34 @@ } if ($pair['new'] < $pair['old']) { - $q = ' SET Priority = Priority + 1 - WHERE '.$constrain.' + $set = ' SET Priority = Priority + 1'; + $where =' WHERE '.$constrain.' Priority >= '.$pair['new'].' AND Priority < '.$pair['old'].' AND - CategoryId NOT IN ('.implode(',', $not_processed).')'; + '.$id_field.' NOT IN ('.implode(',', $not_processed).')'; } elseif ($pair['new'] > $pair['old']) { - $q = ' SET Priority = Priority - 1 - WHERE '.$constrain.' + $set = ' SET Priority = Priority - 1'; + $where =' WHERE '.$constrain.' Priority > '.$pair['old'].' AND Priority <= '.$pair['new'].' AND - CategoryId NOT IN ('.implode(',', $not_processed).')'; + '.$id_field.' NOT IN ('.implode(',', $not_processed).')'; } else { - $q = 'SET Priority = '.$pair['new'].' WHERE '.$id_field.' = '.$id; + $set = 'SET Priority = '.$pair['new']; + $where = ' WHERE '.$id_field.' = '.$id; } - $q = 'UPDATE '.$table_name.' '.$q; + $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; } /** @@ -126,6 +138,7 @@ WHERE '.$id_field.' = '.$item_id; $this->Conn->Query($sql); } + return $items; } }