/**
* Start a new conversation.
*
* @since 2.0.0
* @access public
*
* @param string $Recipient Username of the recipient.
*/
public function add($Recipient = '')
{
$this->permission('Conversations.Conversations.Add');
$this->Form->setModel($this->ConversationModel);
// Set recipient limit
if (!checkPermission('Garden.Moderation.Manage') && c('Conversations.MaxRecipients')) {
$this->addDefinition('MaxRecipients', c('Conversations.MaxRecipients'));
$this->setData('MaxRecipients', c('Conversations.MaxRecipients'));
}
if ($this->Form->authenticatedPostBack()) {
$RecipientUserIDs = array();
$To = explode(',', $this->Form->getFormValue('To', ''));
$UserModel = new UserModel();
foreach ($To as $Name) {
if (trim($Name) != '') {
$User = $UserModel->getByUsername(trim($Name));
if (is_object($User)) {
$RecipientUserIDs[] = $User->UserID;
}
}
}
// Enforce MaxRecipients
if (!$this->ConversationModel->addUserAllowed(0, count($RecipientUserIDs))) {
// Reuse the Info message now as an error.
$this->Form->addError(sprintf(plural($this->data('MaxRecipients'), "You are limited to %s recipient.", "You are limited to %s recipients."), c('Conversations.MaxRecipients')));
}
$this->EventArguments['Recipients'] = $RecipientUserIDs;
$this->fireEvent('BeforeAddConversation');
$this->Form->setFormValue('RecipientUserID', $RecipientUserIDs);
$ConversationID = $this->Form->save($this->ConversationMessageModel);
if ($ConversationID !== false) {
$Target = $this->Form->getFormValue('Target', 'messages/' . $ConversationID);
$this->RedirectUrl = url($Target);
$Conversation = $this->ConversationModel->getID($ConversationID, Gdn::session()->UserID);
$NewMessageID = val('FirstMessageID', $Conversation);
$this->EventArguments['MessageID'] = $NewMessageID;
$this->fireEvent('AfterConversationSave');
}
} else {
if ($Recipient != '') {
$this->Form->setValue('To', $Recipient);
}
}
if ($Target = Gdn::request()->get('Target')) {
$this->Form->addHidden('Target', $Target);
}
Gdn_Theme::section('PostConversation');
$this->title(t('New Conversation'));
$this->setData('Breadcrumbs', array(array('Name' => t('Inbox'), 'Url' => '/messages/inbox'), array('Name' => $this->data('Title'), 'Url' => 'messages/add')));
$this->CssClass = 'NoPanel';
$this->render();
}