Index: branches/RC/core/kernel/db/dbitem.php =================================================================== diff -u -r10122 -r10294 --- branches/RC/core/kernel/db/dbitem.php (.../dbitem.php) (revision 10122) +++ branches/RC/core/kernel/db/dbitem.php (.../dbitem.php) (revision 10294) @@ -77,9 +77,22 @@ return $this->DirtyFieldValues[$field_name]; } - function GetOriginalField($field_name) + function GetOriginalField($field_name, $formatted = false, $format=null) { - return $this->OriginalFieldValues[$field_name]; + $value = $this->OriginalFieldValues[$field_name]; + if (!$formatted) { + return $value; + } + + $options = $this->GetFieldOptions($field_name); + $res = $value; + if (array_key_exists('formatter', $options)) { + $formatter =& $this->Application->recallObject($options['formatter']); + /* @var $formatter kFormatter */ + + $res = $formatter->Format($value, $field_name, $this, $format); + } + return $res; } /** @@ -419,7 +432,7 @@ $affected = $this->Conn->getAffectedRows(); if (!$system_update && $affected == 1){ - $this->setModifiedFlag(); + $this->setModifiedFlag(clUPDATE); } $this->saveCustomFields(); @@ -772,7 +785,7 @@ $this->setID($insert_id); if (!$system_create){ - $this->setModifiedFlag(); + $this->setModifiedFlag(clCREATE); } $this->saveCustomFields(); @@ -800,7 +813,7 @@ $ret = $this->Conn->ChangeQuery($q); - $this->setModifiedFlag(); + $this->setModifiedFlag(clDELETE); if ($this->Conn->getAffectedRows() > 0) { // something was actually deleted @@ -929,6 +942,22 @@ --$new_id; $this->Conn->Query('UPDATE '.$this->TableName.' SET `'.$this->IDField.'` = '.$new_id.' WHERE `'.$this->IDField.'` = '.$this->GetID()); + + if ($this->ShouldLogChanges()) { + // Updating TempId in ChangesLog, if changes are disabled + $ses_var_name = $this->Application->GetTopmostPrefix($this->Prefix).'_changes_'.$this->Application->GetTopmostWid($this->Prefix); + $changes = $this->Application->RecallVar($ses_var_name); + $changes = $changes ? unserialize($changes) : Array (); + if ($changes) { + foreach ($changes as $key => $rec) { + if ($rec['Prefix'] == $this->Prefix && $rec['ItemId'] == $this->GetID()) { + $changes[$key]['ItemId'] = $new_id; + } + } + } + $this->Application->StoreVar($ses_var_name, serialize($changes)); + } + $this->SetID($new_id); } @@ -938,12 +967,99 @@ * @access private * @author Alexey */ - function setModifiedFlag() + function setModifiedFlag($mode = null) { $main_prefix = $this->Application->GetTopmostPrefix($this->Prefix); $this->Application->StoreVar($main_prefix.'_modified', '1'); + + if ($this->ShouldLogChanges()) { + $this->LogChanges($main_prefix, $mode); + if (!$this->IsTempTable()) { + $handler =& $this->Application->recallObject($this->Prefix.'_EventHandler'); + $ses_var_name = $main_prefix.'_changes_'.$this->Application->GetTopmostWid($this->Prefix); + $handler->SaveLoggedChanges($ses_var_name); + } + } } + function ShouldLogChanges() + { + /* @todo Replace true with global LogChanges option */ + return ($this->Application->getUnitOption($this->Prefix, 'LogChanges') || true) && !$this->Application->getUnitOption($this->Prefix, 'ForceDontLogChanges'); + } + + function LogChanges($main_prefix, $mode) + { + if (!$mode) { + return ; + } + + $ses_var_name = $main_prefix.'_changes_'.$this->Application->GetTopmostWid($this->Prefix); + $changes = $this->Application->RecallVar($ses_var_name); + $changes = $changes ? unserialize($changes) : array(); + + $general = array( + 'Prefix' => $this->Prefix, + 'ItemId' => $this->GetID(), + 'OccuredOn' => adodb_mktime(), + 'MasterPrefix' => $main_prefix, + 'MasterId' => $this->Prefix == $main_prefix ? $this->GetID() : $this->Application->GetVar($main_prefix.'_id'), // is that correct (Kostja)?? + 'Action' => $mode, + ); + switch ($mode) { + case clUPDATE: + $changes[] = array_merge($general, Array( + 'Changes' => serialize(array_merge($this->GetTitleField(), $this->GetChangedFields())), + )); + break; + case clCREATE: + $changes[] = array_merge($general, Array( + 'Changes' => serialize($this->GetTitleField()), + )); + break; + case clDELETE: + $changes[] = array_merge($general, Array( + 'Changes' => serialize(array_merge($this->GetTitleField(), $this->GetRealFields())), + )); + } + + $this->Application->StoreVar($ses_var_name, serialize($changes)); + } + + function GetTitleField() + { + $title_field = $this->Application->getUnitOption($this->Prefix, 'TitleField'); + if ($title_field && $this->GetField($title_field)) { + return Array($title_field => $this->GetField($title_field)); + } + } + + function GetRealFields() + { + if (function_exists('array_diff_key')) { + $db_fields = array_diff_key($this->FieldValues, $this->VirtualFields, $this->CalculatedFields); + } + else { + $db_fields = array(); + foreach ($this->FieldValues as $key => $value) { + if (array_key_exists($key, $this->VirtualFields) || array_key_exists($key, $this->CalculatedFields)) continue; + $db_fields[$key] = $value; + } + } + return $db_fields; + } + + function GetChangedFields() + { + $changes = array(); + + $diff = array_diff_assoc($this->GetRealFields(), $this->OriginalFieldValues); + foreach ($diff as $field => $new_value) { + $changes[$field] = array('old' => $this->GetOriginalField($field, true), 'new' => $this->GetField($field)); + } + return $changes; + } + /** * Returns ID of currently processed record *