Index: trunk/core/kernel/utility/temp_handler.php =================================================================== diff -u -r5431 -r6093 --- trunk/core/kernel/utility/temp_handler.php (.../temp_handler.php) (revision 5431) +++ trunk/core/kernel/utility/temp_handler.php (.../temp_handler.php) (revision 6093) @@ -143,8 +143,42 @@ $this->SetTables($tables); } + /** + * Searches through TempHandler tables info for required prefix + * + * @param string $prefix + * @param Array $master + * @return mixed + */ + function SearchTable($prefix, $master = null) + { + if (is_null($master)) { + $master = $this->Tables; + } + + if ($master['Prefix'] == $prefix) { + return $master; + } + + if (isset($master['SubTables'])) { + foreach ($master['SubTables'] as $sub_table) { + $found = $this->SearchTable($prefix, $sub_table); + if ($found !== false) { + return $found; + } + } + } + + return false; + } + function AddTables($prefix, &$tables) { + if (!$this->Application->prefixRegistred($prefix)) { + // allows to skip subitem processing if subitem module not enabled/installed + return ; + } + $tmp = Array( 'TableName' => $this->Application->getUnitOption($prefix,'TableName'), 'IdField' => $this->Application->getUnitOption($prefix,'IDField'), @@ -183,7 +217,7 @@ $tables['SubTables'][] = $tmp; } - function CloneItems($prefix, $special, $ids, $master=null, $foreign_key=null, $parent_prefix = null) + function CloneItems($prefix, $special, $ids, $master = null, $foreign_key = null, $parent_prefix = null, $skip_filenames = false) { if (!isset($master)) $master = $this->Tables; @@ -208,7 +242,13 @@ $object->Load($id); $original_values = $object->FieldValues; - $object->NameCopy($master, $foreign_key); + if (!$skip_filenames) { + $object->NameCopy($master, $foreign_key); + } + elseif ($master['TableName'] == $this->MasterTable) { + // kCatDBItem class only has this attribute + $object->useFilenames = false; + } if (isset($foreign_key)) { $master_foreign_key_field = is_array($master['ForeignKey']) ? $master['ForeignKey'][$parent_prefix] : $master['ForeignKey']; @@ -227,7 +267,9 @@ // remember original => clone mapping for dual ForeignKey updating $this->AlreadyProcessed[$master['TableName']][$id] = $object->GetId(); } - if($object->mode == 't') $object->setTempID(); + if ($object->mode == 't') { + $object->setTempID(); + } if ($mode == 'create') { $this->RaiseEvent('OnAfterClone', $master['Prefix'], $special, Array($object->GetId()), $foreign_key, array('original_id' => $id) ); $this->saveID($master['Prefix'], $special, $object->GetID()); @@ -416,8 +458,10 @@ $live_foreign_key = $this->Conn->GetCol($query); if (isset($temp_id)) { + // because DoCopyTempToOriginal resets negative IDs to 0 in temp table (one by one) before copying to live + $temp_key = $temp_id < 0 ? 0 : $temp_id; $query = 'SELECT '.$parent_key_field.' FROM '.$this->GetTempName($master['TableName']).' - WHERE '.$master['IdField'].' IN ('.join(',', $temp_id).')'; + WHERE '.$master['IdField'].' IN ('.join(',', $temp_key).')'; $temp_foreign_key = $this->Conn->GetCol($query); } else { @@ -439,11 +483,13 @@ } } - function DoCopyTempToOriginal($master, $parent_prefix=null) + function DoCopyTempToOriginal($master, $parent_prefix = null, $current_ids = Array()) { - $query = 'SELECT '.$master['IdField'].' FROM '.$this->GetTempName($master['TableName']); - if (isset($master['Constrain'])) $query .= ' WHERE '.$master['Constrain']; - $current_ids = $this->Conn->GetCol($query); + if (!$current_ids) { + $query = 'SELECT '.$master['IdField'].' FROM '.$this->GetTempName($master['TableName']); + if (isset($master['Constrain'])) $query .= ' WHERE '.$master['Constrain']; + $current_ids = $this->Conn->GetCol($query); + } $table_sig = $master['TableName'].(isset($master['Constrain']) ? $master['Constrain'] : ''); @@ -452,7 +498,7 @@ // because items from Sub Tables get deteleted in CopySubTablesToLive !BY ForeignKey! if ($master['TableName'] == $this->MasterTable) { $this->RaiseEvent( 'OnBeforeDeleteFromLive', $master['Prefix'], '', $current_ids ); - + $query = 'DELETE FROM '.$master['TableName'].' WHERE '.$master['IdField'].' IN ('.join(',', $current_ids).')'; $this->Conn->Query($query); } @@ -486,6 +532,7 @@ $this->Conn->Query($query); $insert_id = $id_to_copy == 0 ? $this->Conn->getInsertID() : $id_to_copy; + $this->saveID($master['Prefix'], '', $insert_id); $this->RaiseEvent( 'OnAfterCopyToLive', $master['Prefix'], '', Array($insert_id), null, array('temp_id' => $id) ); $this->UpdateForeignKeys($master, $insert_id, $id); @@ -543,6 +590,12 @@ } }*/ $this->DropTempTable($master['TableName']); + + if (!isset($this->savedIDs[ $master['Prefix'] ])) { + $this->savedIDs[ $master['Prefix'] ] = Array(); + } + + return $this->savedIDs[ $master['Prefix'] ]; } function UpdateForeignKeys($master, $live_id, $temp_id) { @@ -574,23 +627,17 @@ $foreign_key_field = is_array($sub_table['ForeignKey']) ? getArrayValue($sub_table, 'ForeignKey', $master['Prefix']) : $sub_table['ForeignKey']; if (!$foreign_key_field) continue; $foreign_keys = $this->GetForeignKeys($master, $sub_table, $current_ids); - // Do not delete live sub-items with 0 id - // otherwise home category permissions got deleted when creating new category - $zero_id = array_search(0, $foreign_keys); - if ($zero_id !== false) { - array_splice($foreign_keys, $zero_id, 1); - } if (count($foreign_keys) > 0) { $query = 'SELECT '.$sub_table['IdField'].' FROM '.$sub_table['TableName'].' WHERE '.$foreign_key_field.' IN ('.join(',', $foreign_keys).')'; if (isset($sub_table['Constrain'])) $query .= ' AND '.$sub_table['Constrain']; - $this->RaiseEvent( 'OnBeforeDeleteFromLive', $sub_table['Prefix'], '', $this->Conn->GetCol($query) ); - - $query = 'DELETE FROM '.$sub_table['TableName'].' - WHERE '.$foreign_key_field.' IN ('.join(',', $foreign_keys).')'; - if (isset($sub_table['Constrain'])) $query .= ' AND '.$sub_table['Constrain']; - $this->Conn->Query($query); + if ( $this->RaiseEvent( 'OnBeforeDeleteFromLive', $sub_table['Prefix'], '', $this->Conn->GetCol($query), $foreign_keys ) ){ + $query = 'DELETE FROM '.$sub_table['TableName'].' + WHERE '.$foreign_key_field.' IN ('.join(',', $foreign_keys).')'; + if (isset($sub_table['Constrain'])) $query .= ' AND '.$sub_table['Constrain']; + $this->Conn->Query($query); + } } } //sub_table passed here becomes master in the method, and recursively updated and copy its sub tables @@ -618,6 +665,7 @@ } $this->Application->HandleEvent($event); } + return $event->status == erSUCCESS; } function DropTempTable($table) @@ -637,9 +685,9 @@ $this->DoCopyLiveToTemp($this->Tables, $this->Tables['IDs']); } - function SaveEdit($skip_master=0) + function SaveEdit($master_ids = Array()) { - $this->DoCopyTempToOriginal($this->Tables); + return $this->DoCopyTempToOriginal($this->Tables, null, $master_ids); } function CancelEdit($master=null)