public function SettingsController_DashboardData_Handler(&$Sender) { $ConversationModel = new ConversationModel(); // Number of Conversations $CountConversations = $ConversationModel->GetCountWhere(); $Sender->AddDefinition('CountConversations', $CountConversations); $Sender->BuzzData[T('Conversations')] = number_format($CountConversations); // Number of New Conversations in the last day $Sender->BuzzData[T('New conversations in the last day')] = number_format($ConversationModel->GetCountWhere(array('DateInserted >=' => Gdn_Format::ToDateTime(strtotime('-1 day'))))); // Number of New Conversations in the last week $Sender->BuzzData[T('New conversations in the last week')] = number_format($ConversationModel->GetCountWhere(array('DateInserted >=' => Gdn_Format::ToDateTime(strtotime('-1 week'))))); $ConversationMessageModel = new ConversationMessageModel(); // Number of Messages $CountMessages = $ConversationMessageModel->GetCountWhere(); $Sender->AddDefinition('CountConversationMessages', $CountMessages); $Sender->BuzzData[T('Conversation Messages')] = number_format($CountMessages); // Number of New Messages in the last day $Sender->BuzzData[T('New messages in the last day')] = number_format($ConversationMessageModel->GetCountWhere(array('DateInserted >=' => Gdn_Format::ToDateTime(strtotime('-1 day'))))); // Number of New Messages in the last week $Sender->BuzzData[T('New messages in the last week')] = number_format($ConversationMessageModel->GetCountWhere(array('DateInserted >=' => Gdn_Format::ToDateTime(strtotime('-1 week'))))); }
/** * Save conversation from form submission. * * @since 2.0.0 * @access public * * @param array $FormPostValues Values submitted via form. * @param ConversationMessageModel $MessageModel Message starting the conversation. * @return int Unique ID of conversation created or updated. */ public function Save($FormPostValues, $MessageModel) { $Session = Gdn::Session(); // Define the primary key in this model's table. $this->DefineSchema(); $MessageModel->DefineSchema(); if (!GetValue('RecipientUserID', $FormPostValues) && isset($FormPostValues['To'])) { $To = explode(',', $FormPostValues['To']); $To = array_map('trim', $To); $RecipientUserIDs = $this->SQL->Select('UserID')->From('User')->WhereIn('Name', $To)->Get(); $RecipientUserIDs = ConsolidateArrayValuesByKey($RecipientUserIDs, 'UserID'); $FormPostValues['RecipientUserID'] = $RecipientUserIDs; } if (C('Garden.ForceInputFormatter')) { $FormPostValues['Format'] = C('Garden.InputFormatter'); } // Add & apply any extra validation rules: $this->Validation->ApplyRule('Body', 'Required'); $MessageModel->Validation->ApplyRule('Body', 'Required'); // Make sure that there is at least one recipient $this->Validation->AddRule('OneOrMoreArrayItemRequired', 'function:ValidateOneOrMoreArrayItemRequired'); $this->Validation->ApplyRule('RecipientUserID', 'OneOrMoreArrayItemRequired'); // Add insert/update fields $this->AddInsertFields($FormPostValues); $this->AddUpdateFields($FormPostValues); // Validate the form posted values $ConversationID = FALSE; if ($this->Validate($FormPostValues) && $MessageModel->Validate($FormPostValues)) { $Fields = $this->Validation->ValidationFields(); // All fields on the form that relate to the schema // Define the recipients, and make sure that the sender is in the list $RecipientUserIDs = GetValue('RecipientUserID', $Fields, 0); if (!in_array($Session->UserID, $RecipientUserIDs)) { $RecipientUserIDs[] = $Session->UserID; } // Also make sure there are no duplicates in the recipient list $RecipientUserIDs = array_unique($RecipientUserIDs); sort($RecipientUserIDs); $Fields = $this->Validation->SchemaValidationFields(); // All fields on the form that relate to the schema $ConversationID = $this->SQL->Insert($this->Name, $Fields); $FormPostValues['ConversationID'] = $ConversationID; $MessageID = $MessageModel->Save($FormPostValues); $this->SQL->Update('Conversation')->Set('FirstMessageID', $MessageID)->Where('ConversationID', $ConversationID)->Put(); // Now that the message & conversation have been inserted, insert all of the recipients foreach ($RecipientUserIDs as $UserID) { $CountReadMessages = $UserID == $Session->UserID ? 1 : 0; $this->SQL->Options('Ignore', TRUE)->Insert('UserConversation', array('UserID' => $UserID, 'ConversationID' => $ConversationID, 'LastMessageID' => $MessageID, 'CountReadMessages' => $CountReadMessages, 'DateConversationUpdated' => $FormPostValues['DateUpdated'])); } // And update the CountUnreadConversations count on each user related to the discussion. $this->UpdateUserUnreadCount($RecipientUserIDs, TRUE); $this->UpdateParticipantCount($ConversationID); $this->EventArguments['Recipients'] = $RecipientUserIDs; $Conversation = $this->GetID($ConversationID); $this->EventArguments['Conversation'] = $Conversation; $Message = $MessageModel->GetID($MessageID, DATASET_TYPE_ARRAY); $this->EventArguments['Message'] = $Message; $this->FireEvent('AfterAdd'); // Add notifications (this isn't done by the conversationmessagemodule // because the conversation has not yet been created at the time they are // inserted) $UnreadData = $this->SQL->Select('uc.UserID')->From('UserConversation uc')->Where('uc.ConversationID', $ConversationID)->Where('uc.UserID <>', $Session->UserID)->Get(); $Activity = array('ActivityType' => 'ConversationMessage', 'ActivityUserID' => $Session->UserID, 'HeadlineFormat' => T('HeadlineFormat.ConversationMessage', '{ActivityUserID,User} sent you a <a href="{Url,html}">message</a>'), 'RecordType' => 'Conversation', 'RecordID' => $ConversationID, 'Story' => GetValue('Body', $FormPostValues), 'Format' => GetValue('Format', $FormPostValues, C('Garden.InputFormatter')), 'Route' => "/messages/{$ConversationID}#{$MessageID}"); $Subject = GetValue('Subject', $Fields); if ($Subject) { $Activity['HeadlineFormat'] = $Subject; } $ActivityModel = new ActivityModel(); foreach ($UnreadData->Result() as $User) { $Activity['NotifyUserID'] = $User->UserID; $ActivityModel->Queue($Activity, 'ConversationMessage'); } $ActivityModel->SaveQueue(); } else { // Make sure that all of the validation results from both validations are present for view by the form foreach ($MessageModel->ValidationResults() as $FieldName => $Results) { foreach ($Results as $Result) { $this->Validation->AddValidationResult($FieldName, $Result); } } } return $ConversationID; }
/** * Return the singleton instance of this class. */ public static function instance() { if (!isset(self::$instance)) { self::$instance = new ConversationMessageModel(); } return self::$instance; }
/** * Save conversation from form submission. * * @param array $formPostValues Values submitted via form. * @param array $settings Not used. * @return int Unique ID of conversation created or updated. */ public function save($formPostValues, $settings = []) { if ($settings instanceof ConversationMessageModel) { deprecated('ConversationModel->save(array, ConversationMessageModel)'); $MessageModel = $settings; } else { $MessageModel = ConversationMessageModel::instance(); } // Define the primary key in this model's table. $this->defineSchema(); $MessageModel->defineSchema(); $this->EventArguments['FormPostValues'] = $formPostValues; $this->fireEvent('BeforeSaveValidation'); if (!val('RecipientUserID', $formPostValues) && isset($formPostValues['To'])) { $To = explode(',', $formPostValues['To']); $To = array_map('trim', $To); $RecipientUserIDs = $this->SQL->select('UserID')->from('User')->whereIn('Name', $To)->get()->resultArray(); $RecipientUserIDs = array_column($RecipientUserIDs, 'UserID'); $formPostValues['RecipientUserID'] = $RecipientUserIDs; } if (c('Garden.ForceInputFormatter')) { $formPostValues['Format'] = c('Garden.InputFormatter'); } // Add & apply any extra validation rules: $this->Validation->applyRule('Body', 'Required'); $MessageModel->Validation->applyRule('Body', 'Required'); // Make sure that there is at least one recipient $this->Validation->addRule('OneOrMoreArrayItemRequired', 'function:ValidateOneOrMoreArrayItemRequired'); $this->Validation->applyRule('RecipientUserID', 'OneOrMoreArrayItemRequired'); // Add insert/update fields $this->addInsertFields($formPostValues); $this->addUpdateFields($formPostValues); // Validate the form posted values $ConversationID = false; if ($this->validate($formPostValues) && $MessageModel->validate($formPostValues) && !$this->checkForSpam('Conversation')) { $Fields = $this->Validation->validationFields(); // All fields on the form that relate to the schema // Define the recipients, and make sure that the sender is in the list $RecipientUserIDs = val('RecipientUserID', $Fields, 0); if (!in_array($formPostValues['InsertUserID'], $RecipientUserIDs)) { $RecipientUserIDs[] = $formPostValues['InsertUserID']; } // Also make sure there are no duplicates in the recipient list $RecipientUserIDs = array_unique($RecipientUserIDs); sort($RecipientUserIDs); $Fields = $this->Validation->schemaValidationFields(); // All fields on the form that relate to the schema $ConversationID = $this->SQL->insert($this->Name, $Fields); $formPostValues['ConversationID'] = $ConversationID; // Notify the message model that it's being called as a direct result // of a new conversation being created. As of now, this is being used // so that spam checks between new conversations and conversation // messages each have a separate counter. Without this, a new // conversation will cause itself AND the message model spam counter // to increment by 1. $MessageID = $MessageModel->save($formPostValues, null, array('NewConversation' => true)); $this->SQL->update('Conversation')->set('FirstMessageID', $MessageID)->where('ConversationID', $ConversationID)->put(); // Now that the message & conversation have been inserted, insert all of the recipients foreach ($RecipientUserIDs as $UserID) { $CountReadMessages = $UserID == $formPostValues['InsertUserID'] ? 1 : 0; $this->SQL->options('Ignore', true)->insert('UserConversation', array('UserID' => $UserID, 'ConversationID' => $ConversationID, 'LastMessageID' => $MessageID, 'CountReadMessages' => $CountReadMessages, 'DateConversationUpdated' => $formPostValues['DateUpdated'])); } // And update the CountUnreadConversations count on each user related to the discussion. $this->updateUserUnreadCount(array_diff($RecipientUserIDs, array($formPostValues['InsertUserID']))); $this->updateParticipantCount($ConversationID); $body = val('Body', $formPostValues, ''); $subject = val('Subject', $Fields, ''); $this->EventArguments['Recipients'] = $RecipientUserIDs; $Conversation = $this->getID($ConversationID); $this->EventArguments['Conversation'] = $Conversation; $Message = $MessageModel->getID($MessageID, DATASET_TYPE_ARRAY); $this->EventArguments['Message'] = $Message; $this->EventArguments['Body'] =& $body; $this->EventArguments['Subject'] =& $subject; $this->fireEvent('AfterAdd'); // Add notifications (this isn't done by the conversationmessagemodule // because the conversation has not yet been created at the time they are // inserted) $UnreadData = $this->SQL->select('uc.UserID')->from('UserConversation uc')->where('uc.ConversationID', $ConversationID)->where('uc.UserID <>', $formPostValues['InsertUserID'])->get(); $Activity = array('ActivityType' => 'ConversationMessage', 'ActivityUserID' => $formPostValues['InsertUserID'], 'HeadlineFormat' => t('HeadlineFormat.ConversationMessage', '{ActivityUserID,User} sent you a <a href="{Url,html}">message</a>'), 'RecordType' => 'Conversation', 'RecordID' => $ConversationID, 'Story' => $body, 'ActionText' => t('Reply'), 'Format' => val('Format', $formPostValues, c('Garden.InputFormatter')), 'Route' => "/messages/{$ConversationID}#Message_{$MessageID}"); if ($subject) { $Activity['Story'] = sprintf(t('Re: %s'), $subject) . '<br>' . $body; } $ActivityModel = new ActivityModel(); foreach ($UnreadData->result() as $User) { $Activity['NotifyUserID'] = $User->UserID; $ActivityModel->queue($Activity, 'ConversationMessage'); } $ActivityModel->saveQueue(); } else { // Make sure that all of the validation results from both validations are present for view by the form foreach ($MessageModel->validationResults() as $FieldName => $Results) { foreach ($Results as $Result) { $this->Validation->addValidationResult($FieldName, $Result); } } } return $ConversationID; }