Index: trunk/core/units/general/helpers/priority_helper.php =================================================================== diff -u -N --- trunk/core/units/general/helpers/priority_helper.php (revision 7855) +++ trunk/core/units/general/helpers/priority_helper.php (revision 0) @@ -1,132 +0,0 @@ -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; - - } - - $items_count = $this->Conn->GetOne($sql); - if ($is_new) { - // add new item to the end of list - $items_count++; - $object->SetDBField('Priority', -$items_count); - $object->SetDBField('OldPriority', -$items_count); - } - - for ($i = 1; $i <= $items_count; $i++) { - $field_options['options'][-$i] = $i; - } - - $object->SetFieldOptions('Priority', $field_options); - } - - /** - * Updates priorities for changed items - * - * @param kEvent $event - * @param Array $changes = Array (CategoryID => Array ('parent' => ..., 'new' => ..., 'old' => ...), ...) - * @param Array $new_ids = Array (temp_id => live_id) - * @param string $constrain - */ - function updatePriorities(&$event, $changes, $new_ids, $constrain = '') - { - $id_field = $this->Application->getUnitOption($event->Prefix, 'IDField'); - $table_name = $this->Application->getUnitOption($event->Prefix, 'TableName'); - - $not_processed = array_keys($changes); - - foreach ($changes as $id => $pair) { - $constrain = '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.' CategoryId 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']) { - $q = ' SET Priority = Priority + 1 - WHERE '.$constrain.' - Priority >= '.$pair['new'].' - AND - Priority < '.$pair['old'].' - AND - CategoryId NOT IN ('.implode(',', $not_processed).')'; - } - elseif ($pair['new'] > $pair['old']) { - $q = ' SET Priority = Priority - 1 - WHERE '.$constrain.' - Priority > '.$pair['old'].' - AND - Priority <= '.$pair['new'].' - AND - CategoryId NOT IN ('.implode(',', $not_processed).')'; - } - else { - $q = 'SET Priority = '.$pair['new'].' WHERE '.$id_field.' = '.$id; - } - $q = 'UPDATE '.$table_name.' '.$q; - $this->Conn->Query($q); - - unset( $not_processed[array_search($id, $not_processed)] ); - } - } - - /** - * Recalculates priorities - * - * @param kEvent $event - * @param string $constrain - */ - function recalculatePriorities(&$event, $constrain = '') - { - $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).' - WHERE '.$id_field.' = '.$item_id; - $this->Conn->Query($sql); - } - } - - } -?> \ No newline at end of file