_setMessage('Message has been sent', self::MSG_ADDED); $this->_messageTemplates[self::MSG_FAILED_TO_SEND_MAIL] = 'Failed to send mail'; $this->_getDataObj()->setUser($this->_doc->getAuthData()); return $this; } protected function _initAction() { parent::_initAction(); if (null === $this->_restAlias) { return $this; } $matches = []; $labelIds = array_map('preg_quote', array_keys($this->getConfigArray('labels'))); $pattern = '/^(' . implode('|', $labelIds) . ')(?:\/([\d]+))?$/'; if (!preg_match($pattern, $this->_restAlias, $matches)) { $this->_action = '404'; return $this; } $type = $matches[1]; $this->setListType($type); if (!empty($matches[2])) { $this->_getDataObj()->setPrimaryKey($matches[2]); $this->_action = 'view'; return $this; } switch ($type) { case Entity::LABEL_INBOX: $this->_getDataObj()->setFilter('toUserId', $this->_doc->getAuthData('id')); $this->_action = 'list'; break; case Entity::LABEL_SENT: $this->_getDataObj()->setFilter('fromUserId', $this->_doc->getAuthData('id')); $this->_action = 'list'; break; default: $this->_action = '404'; return $this; } return $this; } protected function _callAction() { $this->_initNavigation(); return parent::_callAction(); } protected function _initNavigation() { if ('summary' === $this->_action) { return $this; } $options = [ 'page' => Qs_SiteMap::findFirst(['id' => $this->_doc->getOption('id')]), 'action' => $this->_action, 'itemName' => $this->getConfig('itemName'), 'itemsName' => $this->getConfig('itemsName'), 'templates' => [ Navigation::ADD => 'New {itemName}', Navigation::PREVIEW => '{subject}', ], ]; $nav = new Navigation($options); $nav->enable(); $nav->addPage(); if ($this->getListType() && ($label = Qs_Array::get($this->getConfigArray('labels'), $this->getListType()))) { $nav->add($label, $this->finalUrl() . '/' . $this->getListType()); } if ($this->_getDataObj()->getPrimaryKey()) { $nav->setEntity($this->_getDataObj()->getData()); } $nav->addAction(); return $this; } protected function _getDefaultLinks() { return [ [ 'title' => 'Compose', 'url' => $this->finalUrl(['action' => 'new']), ], ]; } protected function _getFormOptions($type = null) { $options = parent::_getFormOptions($type); if ('new' === $type) { if (($toUserId = Qs_Request::getGetValue('toUserId')) && ($user = $this->_getDataObj()->findUser($toUserId)) ) { $options['userName'] = $user['fullName']; $options['defaults']['toUserId'] = $toUserId; } $options['autocompleteUrl'] = UserAutocompleteView::getUserAutocompleteUrl4WebMail(); $options['loggedInUserId'] = $this->_doc->getAuthData('id'); } return $options; } protected function _doInsert() { $form = $this->_getNewForm(['defaults' => Qs_Request::getPost()]); if (!$form->validate()) { $this->_addFormItem($form); return $this; } $data = $form->getValues(); $data['fromUserId'] = $this->_doc->getAuthData('id'); $sender = $this->_getDataObj()->findUser($data['fromUserId']); $recipient = $this->_getDataObj()->findUser($data['toUserId']); if ('y' == $recipient['pmNotificationEnabled'] && !$this->sendMail($data, $sender, $recipient)) { $form->addError($this->_createMessage(self::MSG_FAILED_TO_SEND_MAIL)); $this->_addFormItem($form); return $this; } $this->_getDataObj()->initFromForm($data); if (false === $this->_getDataObj()->insert()) { $this->_setBackErrors($this->_getDataObj()->getErrors()); } else { $this->_postInsert(); $this->_setBackMessage(static::MSG_ADDED); } $this->_doBack(); return $this; } protected function sendMail(array $mail, $sender, $recipient) { $data = [ 'senderFullName' => $sender['fullName'], 'subject' => htmlspecialchars($mail['subject']), 'message' => nl2br(htmlspecialchars($mail['body'])), 'messagesUrl' => self::getPage('url'), 'senderPageUrl' => MemberDirectoryView::getPage('url') . '/' . $sender['alias'], ]; $prefix = 'webMailNotification'; $subject = App_Settings_Obj::get($prefix . 'Subject'); $subject = Qs_String::fill($subject, $data); $from = App_Settings_Obj::getEmailFrom($prefix . 'Form'); $body = App_Settings_Obj::getEmailFrom($prefix . 'Body'); $body = Qs_String::fill($body, $data); $to = [$recipient['email']]; return $this->_sendMail(compact('from', 'subject', 'body', 'to')); } protected function _doSummary() { $this->_addLinksItem(); $item = [ 'list' => $this->_getDataObj()->getSummary(), 'tpl' => $this->getTemplate('summary.tpl'), ]; $this->_addItem($item); return $this; } protected function _getListOptions() { $options = parent::_getListOptions(); $options['receiverColumn'] = $this->getListType() == Entity::LABEL_SENT; $options['senderColumn'] = $this->getListType() == Entity::LABEL_INBOX; $options['url'] = self::getPage('url') . '/' . $this->getListType(); return $options; } protected function _getListItem() { $item = parent::_getListItem(); $item['url'] = MemberDirectoryView::getPage('url') . '/{alias}'; $item['attribs']['class'] = 'table table-striped table-bordered app-user-webmail mailbox-' . $this->getListType(); unset($item['attribs']['id']); if ($this->getListType() == Entity::LABEL_INBOX) { $item['rowInitCallback'] = function (&$attribs, $row) { if ('n' === $row['read']) { $attribs['class'] = 'not-read'; } }; } return $item; } public function getListType() { return $this->listType; } public function setListType($listType) { $this->listType = $listType; return $this; } protected function _prepareViewItem(array &$item) { if ('n' === $item['read'] && $this->_doc->getAuthData('id') == $item['toUserId']) { $this->_getDataObj()->update(['read' => 'y']); } return parent::_prepareViewItem($item); } }