/**
* Shows all uncleared messages within a conversation for the viewing user
*
* @since 2.0.0
* @access public
*
* @param int $ConversationID Unique ID of conversation to view.
* @param int $Offset Number to skip.
* @param int $Limit Number to show.
*/
public function index($ConversationID = false, $Offset = -1, $Limit = '')
{
$this->Offset = $Offset;
$Session = Gdn::session();
Gdn_Theme::section('Conversation');
// Figure out Conversation ID
if (!is_numeric($ConversationID) || $ConversationID < 0) {
$ConversationID = 0;
}
// Form setup for adding comments
$this->Form->setModel($this->ConversationMessageModel);
$this->Form->addHidden('ConversationID', $ConversationID);
// Check permissions on the recipients.
$InConversation = $this->ConversationModel->inConversation($ConversationID, Gdn::session()->UserID);
if (!$InConversation) {
// Conversation moderation must be enabled and they must have permission
if (!c('Conversations.Moderation.Allow', false)) {
throw permissionException();
}
$this->permission('Conversations.Moderation.Manage');
}
$this->Conversation = $this->ConversationModel->getID($ConversationID);
$this->Conversation->Participants = $this->ConversationModel->getRecipients($ConversationID);
$this->setData('Conversation', $this->Conversation);
// Bad conversation? Redirect
if ($this->Conversation === false) {
throw notFoundException('Conversation');
}
// Get limit
if ($Limit == '' || !is_numeric($Limit) || $Limit < 0) {
$Limit = Gdn::config('Conversations.Messages.PerPage', 50);
}
// Calculate counts
if (!is_numeric($this->Offset) || $this->Offset < 0) {
// Round down to the appropriate offset based on the user's read messages & messages per page
$CountReadMessages = $this->Conversation->CountMessages - $this->Conversation->CountNewMessages;
if ($CountReadMessages < 0) {
$CountReadMessages = 0;
}
if ($CountReadMessages > $this->Conversation->CountMessages) {
$CountReadMessages = $this->Conversation->CountMessages;
}
// (((67 comments / 10 perpage) = 6.7) rounded down = 6) * 10 perpage = offset 60;
$this->Offset = floor($CountReadMessages / $Limit) * $Limit;
// Send the hash link in.
if ($CountReadMessages > 1) {
$this->addDefinition('LocationHash', '#Item_' . $CountReadMessages);
}
}
// Fetch message data
$this->MessageData = $this->ConversationMessageModel->get($ConversationID, $Session->UserID, $this->Offset, $Limit);
$this->EventArguments['MessageData'] = $this->MessageData;
$this->fireEvent('beforeMessages');
$this->setData('Messages', $this->MessageData);
// Figure out who's participating.
$ParticipantTitle = ConversationModel::participantTitle($this->Conversation, true);
$this->Participants = $ParticipantTitle;
$this->title(strip_tags($this->Participants));
// $CountMessages = $this->ConversationMessageModel->getCount($ConversationID, $Session->UserID);
// Build a pager
$PagerFactory = new Gdn_PagerFactory();
$this->Pager = $PagerFactory->getPager('MorePager', $this);
$this->Pager->MoreCode = 'Newer Messages';
$this->Pager->LessCode = 'Older Messages';
$this->Pager->ClientID = 'Pager';
$this->Pager->configure($this->Offset, $Limit, $this->Conversation->CountMessages, 'messages/' . $ConversationID . '/%1$s/%2$s/');
// Mark the conversation as ready by this user.
$this->ConversationModel->markRead($ConversationID, $Session->UserID);
// Deliver json data if necessary
if ($this->_DeliveryType != DELIVERY_TYPE_ALL) {
$this->setJson('LessRow', $this->Pager->toString('less'));
$this->setJson('MoreRow', $this->Pager->toString('more'));
$this->View = 'messages';
}
// Add modules.
$ClearHistoryModule = new ClearHistoryModule($this);
$ClearHistoryModule->conversationID($ConversationID);
$this->addModule($ClearHistoryModule);
$InThisConversationModule = new InThisConversationModule($this);
$InThisConversationModule->setData($this->Conversation->Participants);
$this->addModule($InThisConversationModule);
// Doesn't make sense for people who can't even start conversations to be adding people
if (checkPermission('Conversations.Conversations.Add')) {
$this->addModule('AddPeopleModule');
}
$Subject = $this->data('Conversation.Subject');
if (!$Subject) {
$Subject = t('Message');
}
$this->Data['Breadcrumbs'][] = array('Name' => $Subject, 'Url' => url('', '//'));
// Render view
$this->render();
}