/** * Log an operation into the log table. * * @param string $Operation The operation being performed. This is usually one of: * - Delete: The record has been deleted. * - Edit: The record has been edited. * - Spam: The record has been marked spam. * - Moderate: The record requires moderation. * - Pending: The record needs pre-moderation. * @param string $RecordType The type of record being logged. This usually correspond to the tablename of the record. * @param array $Data The record data. * - If you are logging just one row then pass the row as an array. * - You can pass an additional _New element to tell the logger what the new data is. * @return int The log id. */ public static function insert($Operation, $RecordType, $Data, $Options = array()) { if ($Operation === false) { return; } // Check to see if we are storing two versions of the data. if (($InsertUserID = self::_LogValue($Data, 'Log_InsertUserID')) === null) { $InsertUserID = Gdn::session()->UserID; } if (($InsertIPAddress = self::_LogValue($Data, 'Log_InsertIPAddress')) == null) { $InsertIPAddress = Gdn::request()->IPAddress(); } // Do some known translations for the parent record ID. if (($ParentRecordID = self::_LogValue($Data, 'ParentRecordID')) === null) { switch ($RecordType) { case 'Activity': $ParentRecordID = self::_LogValue($Data, 'CommentActivityID', 'CommentActivityID'); break; case 'Comment': $ParentRecordID = self::_LogValue($Data, 'DiscussionID', 'DiscussionID'); break; } } // Get the row information from the data or determine it based on the type. $LogRow = array('Operation' => $Operation, 'RecordType' => $RecordType, 'RecordID' => self::_LogValue($Data, 'RecordID', $RecordType . 'ID'), 'RecordUserID' => self::_LogValue($Data, 'RecordUserID', 'UpdateUserID', 'InsertUserID'), 'RecordIPAddress' => self::_LogValue($Data, 'RecordIPAddress', 'LastIPAddress', 'InsertIPAddress'), 'RecordDate' => self::_LogValue($Data, 'RecordDate', 'DateUpdated', 'DateInserted'), 'InsertUserID' => $InsertUserID, 'InsertIPAddress' => $InsertIPAddress, 'DateInserted' => Gdn_Format::toDateTime(), 'ParentRecordID' => $ParentRecordID, 'CategoryID' => self::_LogValue($Data, 'CategoryID'), 'OtherUserIDs' => implode(',', val('OtherUserIDs', $Options, array())), 'Data' => serialize($Data)); if ($LogRow['RecordDate'] == null) { $LogRow['RecordDate'] = Gdn_Format::toDateTime(); } $GroupBy = val('GroupBy', $Options); // Make sure we aren't grouping by null values. if (is_array($GroupBy)) { foreach ($GroupBy as $Name) { if (val($Name, $LogRow) === null) { $GroupBy = false; break; } } } if ($GroupBy) { $GroupBy[] = 'Operation'; $GroupBy[] = 'RecordType'; // Check to see if there is a record already logged here. $Where = array_combine($GroupBy, arrayTranslate($LogRow, $GroupBy)); $LogRow2 = Gdn::sql()->getWhere('Log', $Where)->firstRow(DATASET_TYPE_ARRAY); if ($LogRow2) { $LogID = $LogRow2['LogID']; $Set = array(); $Data = array_merge(unserialize($LogRow2['Data']), $Data); $OtherUserIDs = explode(',', $LogRow2['OtherUserIDs']); if (!is_array($OtherUserIDs)) { $OtherUserIDs = array(); } if (!$LogRow2['InsertUserID']) { $Set['InsertUserID'] = $InsertUserID; } elseif ($InsertUserID != $LogRow2['InsertUserID'] && !in_array($InsertUserID, $OtherUserIDs)) { $OtherUserIDs[] = $InsertUserID; } if (array_key_exists('OtherUserIDs', $Options)) { $OtherUserIDs = array_merge($OtherUserIDs, $Options['OtherUserIDs']); $OtherUserIDs = array_unique($OtherUserIDs); $OtherUserIDs = array_diff($OtherUserIDs, array($InsertUserID)); $Count = count($OtherUserIDs) + 1; } else { $Count = (int) $LogRow2['CountGroup'] + 1; } $Set['OtherUserIDs'] = implode(',', $OtherUserIDs); $Set['CountGroup'] = $Count; $Set['Data'] = serialize($Data); $Set['DateUpdated'] = Gdn_Format::toDateTime(); if (self::$_TransactionID > 0) { $Set['TransactionLogID'] = self::$_TransactionID; } elseif (self::$_TransactionID === true) { if ($LogRow2['TransactionLogID']) { self::$_TransactionID = $LogRow2['TransactionLogID']; } else { self::$_TransactionID = $LogID; $Set['TransactionLogID'] = $LogID; } } Gdn::sql()->put('Log', $Set, array('LogID' => $LogID)); } else { $L = self::_Instance(); $L->EventArguments['Log'] =& $LogRow; $L->fireEvent('BeforeInsert'); if (self::$_TransactionID > 0) { $LogRow['TransactionLogID'] = self::$_TransactionID; } $LogID = Gdn::sql()->insert('Log', $LogRow); if (self::$_TransactionID === true) { // A new transaction was started and needs to assigned. self::$_TransactionID = $LogID; Gdn::sql()->put('Log', array('TransactionLogID' => $LogID), array('LogID' => $LogID)); } $L->EventArguments['LogID'] = $LogID; $L->fireEvent('AfterInsert'); } } else { if (self::$_TransactionID > 0) { $LogRow['TransactionLogID'] = self::$_TransactionID; } // Insert the log entry. $L = self::_Instance(); $L->EventArguments['Log'] =& $LogRow; $L->fireEvent('BeforeInsert'); $LogID = Gdn::sql()->insert('Log', $LogRow); if (self::$_TransactionID === true) { // A new transaction was started and needs to assigned. self::$_TransactionID = $LogID; Gdn::sql()->put('Log', array('TransactionLogID' => $LogID), array('LogID' => $LogID)); } $L->EventArguments['LogID'] = $LogID; $L->fireEvent('AfterInsert'); } return $LogID; }