Index: branches/RC/core/kernel/utility/temp_handler.php =================================================================== diff -u -r8929 -r9639 --- branches/RC/core/kernel/utility/temp_handler.php (.../temp_handler.php) (revision 8929) +++ branches/RC/core/kernel/utility/temp_handler.php (.../temp_handler.php) (revision 9639) @@ -67,8 +67,18 @@ function saveID($prefix, $special = '', $id = null) { + if (!isset($this->savedIDs[$prefix.($special ? '.' : '').$special])) { + $this->savedIDs[$prefix.($special ? '.' : '').$special] = array(); + } + if (is_array($id)) { + foreach ($id as $tmp_id => $live_id) { + $this->savedIDs[$prefix.($special ? '.' : '').$special][$tmp_id] = $live_id; + } + } + else { $this->savedIDs[$prefix.($special ? '.' : '').$special][] = $id; } + } /** * Get temp table name @@ -543,7 +553,7 @@ $this->Conn->Query($query); $insert_id = $id_to_copy == 0 ? $this->Conn->getInsertID() : $id_to_copy; - $this->saveID($master['Prefix'], '', $insert_id); + $this->saveID($master['Prefix'], '', array($id => $insert_id)); $this->RaiseEvent( 'OnAfterCopyToLive', $master['Prefix'], '', Array($insert_id), null, array('temp_id' => $id) ); $this->UpdateForeignKeys($master, $insert_id, $id); @@ -565,12 +575,33 @@ // or in parent table processing for sub-tables $this->RaiseEvent('OnBeforeCopyToLive', $master['Prefix'], '', $current_ids); - // reset ALL negative IDs to 0 so it get inserted into live table with autoincrement + $live_ids = array(); + foreach ($current_ids as $an_id) { + if ($an_id > 0) { + $live_ids[$an_id] = $an_id; + // positive (already live) IDs will be copied in on query all togather below, + // so we just store it here + continue; + } + else { // zero or negaitve ids should be copied one by one to get their InsertId + // reseting to 0 so it get inserted into live table with autoincrement $query = 'UPDATE '.$this->GetTempName($master['TableName']).' SET '.$master['IdField'].' = 0 - WHERE '.$master['IdField'].' < 0'; - if (isset($master['Constrain'])) $query .= ' AND '.$master['Constrain']; + WHERE '.$master['IdField'].' = '.$an_id; + // constrain is not needed here because ID is already unique $this->Conn->Query($query); + // copying + $query = 'INSERT INTO '.$master['TableName'].' + SELECT * FROM '.$this->GetTempName($master['TableName']).' + WHERE '.$master['IdField'].' = 0'; + $this->Conn->Query($query); + $live_ids[$an_id] = $this->Conn->getInsertID(); //storing newly created live id + //delete already copied record from master temp table + $query = 'DELETE FROM '.$this->GetTempName($master['TableName']).' + WHERE '.$master['IdField'].' = 0'; + $this->Conn->Query($query); + } + } // copy ALL records to live table $query = 'INSERT INTO '.$master['TableName'].' @@ -579,15 +610,11 @@ $this->Conn->Query($query); $this->CopiedTables[] = $table_sig; - /* + $this->RaiseEvent('OnAfterCopyToLive', $master['Prefix'], '', $live_ids); - !!! WE NEED TO FIND A WAY TO DETERMINE IF OnAfterCopyToLive is not an empty method, and do on-by-one insert - and pass Ids to OnAfterCopyToLive, otherwise it's not smart to do on-by-one insert for any object - OR WE COULD FIND A WAY TO GET ALL INSERTED IDS as an array and iterate them !!! + $this->saveID($master['Prefix'], '', $live_ids); - $this->RaiseEvent('OnAfterCopyToLive', IDS ??? ); - */ // no need to clear temp table - it will be dropped by next statement } @@ -696,6 +723,9 @@ function PrepareEdit() { $this->DoCopyLiveToTemp($this->Tables, $this->Tables['IDs']); + if ($this->Application->getUnitOption($this->Tables['Prefix'],'CheckSimulatniousEdit')) { + $this->CheckSimultaniousEdit(); + } } function SaveEdit($master_ids = Array()) @@ -713,6 +743,45 @@ } } } + function CheckSimultaniousEdit() + { + $tables = $this->Conn->GetCol('SHOW TABLES'); + $mask_edit_table = '/'.TABLE_PREFIX.'ses_(.*)_edit_'.$this->MasterTable.'$/'; + $sql='SELECT COUNT(*) FROM '.$this->TableName.' WHERE '.$this->IDField.' = \'%s\''; + $my_sid = $this->Application->GetSID(); + $ids = join(',',$this->Tables['IDs']); + $sids = array(); + if (!$ids) return ; + foreach($tables as $table) + { + if( preg_match($mask_edit_table,$table,$rets) ) + { + $sid = preg_replace('/(.*)_(.*)/', '\\1', $rets[1]); // remove popup's wid from sid + if ($sid == $my_sid) continue; + $found = $this->Conn->GetOne("SELECT COUNT({$this->Tables['IdField']}) FROM $table WHERE {$this->Tables['IdField']} IN ($ids)"); + if (!$found || in_array($sid, $sids)) continue; + $sids[] = $sid; + } + } + if ($sids) { + //detect who is it + $users = $this->Conn->GetCol( + ' SELECT + CONCAT(IF (s.PortalUserId = -1, \'root\', + IF (s.PortalUserId = -2, \'Guest\', + CONCAT(FirstName, \' \', LastName, \' (\', Login, \')\') + ) + ), \' IP: \', s.IpAddress, \'\') FROM '.TABLE_PREFIX.'UserSession AS s + LEFT JOIN '.TABLE_PREFIX.'PortalUser AS u + ON u.PortalUserId = s.PortalUserId + WHERE s.SessionKey IN ('.join(',',$sids).')'); + if ($users) { + $this->Application->SetVar('_simultanious_edit_message', + sprintf($this->Application->Phrase('la_record_being_edited_by'), join(",\n", $users)) + ); + } + } + } } ?> \ No newline at end of file