Beispiel #1
0
 /**
  * If not saving data directly to the model, this method allows you to
  * utilize a model's schema to validate a form's inputs regardless.
  *
  * ie. A sign-in form that just needs to compare data to the model and still
  * enforce it's rules. Returns the number of errors that were recorded
  * through validation.
  *
  * @return int
  */
 public function ValidateModel()
 {
     $this->_Model->DefineSchema();
     if ($this->_Model->Validation->Validate($this->FormValues()) === FALSE) {
         $this->_ValidationResults = $this->_Model->ValidationResults();
     }
     return $this->ErrorCount();
 }
 /**
  * Save conversation from form submission.
  * 
  * @since 2.0.0
  * @access public
  *
  * @param array $FormPostValues Values submitted via form.
  * @param object $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;
     }
     // 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
         $Fields['Contributors'] = Gdn_Format::Serialize($RecipientUserIDs);
         $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);
         // 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();
         $ActivityModel = new ActivityModel();
         foreach ($UnreadData->Result() as $User) {
             // Notify the users of the new message.
             $ActivityID = $ActivityModel->Add($Session->UserID, 'ConversationMessage', '', $User->UserID, '', "/messages/{$ConversationID}#{$MessageID}", FALSE);
             $Story = ArrayValue('Body', $FormPostValues, '');
             $ActivityModel->SendNotification($ActivityID, $Story);
         }
     } 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;
 }