Exemplo n.º 1
0
 /**
  * @param PostController $Sender
  * @param array $Args
  * @return mixed
  */
 public function PostController_Comment_Create($Sender, $Args = array())
 {
     if ($Sender->Form->AuthenticatedPostBack()) {
         $Sender->Form->SetModel($Sender->CommentModel);
         // Grab the discussion for use later.
         $DiscussionID = $Sender->Form->GetFormValue('DiscussionID');
         $DiscussionModel = new DiscussionModel();
         $Discussion = $DiscussionModel->GetID($DiscussionID);
         // Check to see if the discussion is supposed to be in private...
         $WhisperConversationID = GetValueR('Attributes.WhisperConversationID', $Discussion);
         if ($WhisperConversationID === TRUE) {
             // There isn't a conversation so we want to create one.
             $Sender->Form->SetFormValue('Whisper', TRUE);
             $WhisperUserIDs = GetValueR('Attributes.WhisperUserIDs', $Discussion);
             $Sender->Form->SetFormValue('RecipientUserID', $WhisperUserIDs);
         } elseif ($WhisperConversationID) {
             // There is already a conversation.
             $Sender->Form->SetFormValue('Whisper', TRUE);
             $Sender->Form->SetFormValue('ConversationID', $WhisperConversationID);
         }
         $Whisper = $Sender->Form->GetFormValue('Whisper') && GetIncomingValue('Type') != 'Draft';
         $WhisperTo = trim($Sender->Form->GetFormValue('To'));
         $ConversationID = $Sender->Form->GetFormValue('ConversationID');
         // If this isn't a whisper then post as normal.
         if (!$Whisper) {
             return call_user_func_array(array($Sender, 'Comment'), $Args);
         }
         $ConversationModel = new ConversationModel();
         $ConversationMessageModel = new ConversationMessageModel();
         if ($ConversationID > 0) {
             $Sender->Form->SetModel($ConversationMessageModel);
         } else {
             // We have to remove the blank conversation ID or else the model won't validate.
             $FormValues = $Sender->Form->FormValues();
             unset($FormValues['ConversationID']);
             $FormValues['Subject'] = GetValue('Name', $Discussion);
             $Sender->Form->FormValues($FormValues);
             $Sender->Form->SetModel($ConversationModel);
             $ConversationModel->Validation->ApplyRule('DiscussionID', 'Required');
         }
         $ID = $Sender->Form->Save($ConversationMessageModel);
         if ($Sender->Form->ErrorCount() > 0) {
             $Sender->ErrorMessage($Sender->Form->Errors());
         } else {
             if ($WhisperConversationID === TRUE) {
                 $Discussion->Attributes['WhisperConversationID'] = $ID;
                 $DiscussionModel->SetProperty($DiscussionID, 'Attributes', serialize($Discussion->Attributes));
             }
             $LastCommentID = GetValue('LastCommentID', $Discussion);
             $MessageID = GetValue('LastMessageID', $ConversationMessageModel, FALSE);
             // Randomize the querystring to force the browser to refresh.
             $Rand = mt_rand(10000, 99999);
             if ($LastCommentID) {
                 // Link to the last comment.
                 $HashID = $MessageID ? 'w' . $MessageID : $LastCommentID;
                 $Sender->RedirectUrl = Url("discussion/comment/{$LastCommentID}?rand={$Rand}#Comment_{$HashID}", TRUE);
             } else {
                 // Link to the discussion.
                 $Hash = $MessageID ? "Comment_w{$MessageID}" : 'Item_1';
                 $Name = rawurlencode(GetValue('Name', $Discussion, 'x'));
                 $Sender->RedirectUrl = Url("discussion/{$DiscussionID}/{$Name}?rand={$Rand}#{$Hash}", TRUE);
             }
         }
         $Sender->Render();
     } else {
         return call_user_func_array(array($Sender, 'Comment'), $Args);
     }
 }