Index: core/units/helpers/priority_helper.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/helpers/priority_helper.php (revision Local Version) +++ core/units/helpers/priority_helper.php (revision Shelved Version) @@ -19,36 +19,29 @@ /** * Prepares options for priority dropdown * - * @param kEvent $event - * @param bool $is_new for newly created items add new priority to the end - * @param string $constrain constrain for priority selection (if any) - * @param string $joins left joins, used by constrain (if any) + * @param kEvent $event Event. + * @param boolean $is_new For newly created items add new priority to the end. + * @param string $constrain Constrain for priority selection (if any). + * @param string $joins Left joins, used by constrain (if any). * + * @return void */ - function preparePriorities($event, $is_new = false, $constrain = '', $joins = '') + public function preparePriorities(kEvent $event, $is_new = false, $constrain = '', $joins = '') { /** @var kDBItem $object */ $object = $event->getObject(); - $field_options = $object->GetFieldOptions('Priority'); - $table_name = $this->Application->getUnitOption($event->Prefix, 'TableName'); - - $sql = 'SELECT COUNT(*) - FROM ' . $table_name . ' item_table - ' . $joins; - - if ( $constrain ) { - $sql .= ' WHERE ' . $this->normalizeConstrain($constrain); - } + // we need to introduce mode in "OnGetConstainInfo" = auto (matching object), live (always live) + $temp_items_count = $object->IsTempTable() ? $this->getItemCount($event, $constrain, $joins, true) : 0; + $items_count = max($temp_items_count, $this->getItemCount($event, $constrain, $joins, false)); if ( !$object->isField('OldPriority') ) { - // make sure, then OldPriority field is defined + // Make sure, then OldPriority field is defined, $virtual_fields = $object->getVirtualFields(); - $virtual_fields['OldPriority'] = Array ('type' => 'int', 'default' => 0); + $virtual_fields['OldPriority'] = array('type' => 'int', 'default' => 0); $object->setVirtualFields($virtual_fields); } - $items_count = $this->Conn->GetOne($sql); $current_priority = $object instanceof kDBList ? 0 : $object->GetDBField('Priority'); if ( $is_new || $current_priority == -($items_count + 1) || $this->isTempTableOnly($object) ) { @@ -56,22 +49,53 @@ } if ( $is_new ) { - // add new item to the end of list + // Add new item to the end of list. $object->SetDBField('Priority', -$items_count); $object->SetDBField('OldPriority', -$items_count); } else { - // storing priority right after load for comparing when updating + // Storing priority right after load for comparing when updating. $object->SetDBField('OldPriority', $current_priority); } - for ($i = 1; $i <= $items_count; $i++) { + $field_options = $object->GetFieldOptions('Priority'); + + for ( $i = 1; $i <= $items_count; $i++ ) { $field_options['options'][-$i] = $i; } $object->SetFieldOptions('Priority', $field_options); } + /** + * Prepares options for priority dropdown + * + * @param kEvent $event Event. + * @param string $constrain Constrain for priority selection (if any). + * @param string $joins Left joins, used by constrain (if any). + * @param boolean $is_temp Use temp table. + * + * @return integer + */ + protected function getItemCount(kEvent $event, $constrain = '', $joins = '', $is_temp) + { + $table_name = $this->Application->getUnitOption($event->Prefix, 'TableName'); + + if ( $is_temp ) { + $table_name = $this->Application->GetTempName($table_name, 'prefix:' . $event->Prefix); + } + + $sql = 'SELECT COUNT(*) + FROM ' . $table_name . ' item_table + ' . $joins; + + if ( $constrain ) { + $sql .= ' WHERE ' . $this->normalizeConstrain($constrain); + } + + return $this->Conn->GetOne($sql); + } + /** * Determines if an item only exists in temp table. * Index: core/units/priorites/priority_eh.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/priorites/priority_eh.php (revision Local Version) +++ core/units/priorites/priority_eh.php (revision Shelved Version) @@ -70,7 +70,7 @@ 'Conditional' => false, 'HookToPrefix' => '', 'HookToSpecial' => '*', - 'HookToEvent' => Array('OnAfterItemLoad', 'OnPreCreate', 'OnListBuild'), + 'HookToEvent' => Array('OnAfterItemLoad', 'OnPreCreate', 'OnNew', 'OnListBuild'), 'DoPrefix' => 'priority', 'DoSpecial' => '*', 'DoEvent' => 'OnPreparePriorities', @@ -81,7 +81,7 @@ 'Conditional' => false, 'HookToPrefix' => '', 'HookToSpecial' => '*', - 'HookToEvent' => Array('OnPreSaveCreated'), + 'HookToEvent' => Array('OnPreSaveCreated', 'OnBeforeItemCreate', 'OnBeforeItemUpdate'), 'DoPrefix' => 'priority', 'DoSpecial' => '*', 'DoEvent' => 'OnPreparePriorities', @@ -169,7 +169,10 @@ $priority_helper = $this->Application->recallObject('PriorityHelper'); list ($constrain, $joins) = $this->getConstrainInfo($event); - $is_new = $event->MasterEvent->Name == 'OnPreCreate' || $event->MasterEvent->Name == 'OnPreSaveCreated'; + $is_new = $event->MasterEvent->Name == 'OnPreCreate' + || $event->MasterEvent->Name == 'OnNew' + || $event->MasterEvent->Name == 'OnPreSaveCreated' + || $event->MasterEvent->Name == 'OnCreate'; $priority_helper->preparePriorities($event->MasterEvent, $is_new, $constrain, $joins); }