예제 #1
0
파일: mailbox.php 프로젝트: vazahat/dudex
 /**
  * Displays mailbox inbox page
  */
 public function inbox()
 {
     $this->contentMenu->getElement('inbox')->setActive(true);
     $language = OW::getLanguage();
     OW::getDocument()->setTitle($language->text('mailbox', 'inbox_meta_tilte'));
     $userId = OW::getUser()->getId();
     if (!OW::getUser()->isAuthenticated() || $userId === null) {
         throw new AuthenticateException();
     }
     $conversationService = MAILBOX_BOL_ConversationService::getInstance();
     if (OW::getRequest()->isPost()) {
         if (!empty($_POST['conversation']) && is_array($_POST['conversation'])) {
             $conversationList = $_POST['conversation'];
             if (isset($_POST['mark_as_read'])) {
                 $count = $conversationService->markRead($conversationList, $userId);
                 OW::getFeedback()->info($language->text('mailbox', 'mark_read_message', array('count' => $count)));
             } else {
                 if (isset($_POST['mark_as_unread'])) {
                     $count = $conversationService->markUnread($conversationList, $userId);
                     OW::getFeedback()->info($language->text('mailbox', 'mark_unread_message', array('count' => $count)));
                 } else {
                     if (isset($_POST['delete'])) {
                         $count = $conversationService->deleteConversation($conversationList, $userId);
                         OW::getFeedback()->info($language->text('mailbox', 'delete_message', array('count' => $count)));
                     }
                 }
             }
         }
         $this->redirect(OW::getRequest()->getRequestUri());
     }
     //paging
     $recordsToPage = $this->recordsToPage;
     $record = array();
     $recordsCount = (int) $conversationService->getInboxConversationCount($userId);
     $pageCount = (int) ceil($recordsCount / $recordsToPage);
     $page = 1;
     if ($recordsCount > $recordsToPage) {
         if (isset($_GET['page'])) {
             if ((int) $_GET['page'] < 1) {
                 $this->redirect(OW::getRouter()->urlForRoute('mailbox_inbox') . '?page=' . 1);
             }
             $page = (int) $_GET['page'];
         }
     }
     if (empty($pageCount) || $pageCount < 1) {
         $pageCount = 1;
     }
     if ($page > $pageCount) {
         $this->redirect(OW::getRouter()->urlForRoute('mailbox_inbox') . '?page=' . $pageCount);
     }
     $paging = new BASE_CMP_Paging($page, $pageCount, $recordsToPage);
     $this->assign('paging', $paging->render());
     $startRecord = (int) (($page - 1) * $recordsToPage);
     $record['new'] = (int) $conversationService->getNewInboxConversationCount($userId);
     $record['total'] = $recordsCount;
     $record['start'] = $startRecord + 1;
     $record['end'] = (int) $page * $recordsToPage <= $recordsCount ? (int) $page * $recordsToPage : $recordsCount;
     $this->assign('record', $record);
     //--
     $conversations = $conversationService->getInboxConversationList($userId, $startRecord, $recordsToPage);
     $conversationList = array();
     $opponentsId = array();
     $conversationsId = array();
     foreach ($conversations as $value) {
         $conversation = array();
         $conversation['conversationId'] = $value['conversationId'];
         $conversation['userId'] = $userId;
         $conversation['recipientRead'] = $value['recipientRead'];
         $conversation['read'] = false;
         $conversation['url'] = $conversationService->getConversationUrl($conversation['conversationId']);
         $conversation['deleteUrl'] = OW::getRouter()->urlFor('MAILBOX_CTRL_Mailbox', 'deleteInbox', array("conversationId" => $conversation['conversationId'], "page" => $page));
         switch ($userId) {
             case $value['initiatorId']:
                 $conversation['opponentId'] = $value['interlocutorId'];
                 $conversation['isOpponentLastMessage'] = false;
                 if ($value['initiatorMessageId'] < $value['interlocutorMessageId']) {
                     $conversation['isOpponentLastMessage'] = true;
                 }
                 if ((int) $value['read'] & MAILBOX_BOL_ConversationDao::READ_INITIATOR) {
                     $conversation['read'] = true;
                 }
                 break;
             case $value['interlocutorId']:
                 $conversation['opponentId'] = $value['initiatorId'];
                 $conversation['isOpponentLastMessage'] = false;
                 if ($value['initiatorMessageId'] > $value['interlocutorMessageId']) {
                     $conversation['isOpponentLastMessage'] = true;
                 }
                 if ((int) $value['read'] & MAILBOX_BOL_ConversationDao::READ_INTERLOCUTOR) {
                     $conversation['read'] = true;
                 }
                 break;
         }
         $conversation['timeStamp'] = UTIL_DateTime::formatDate((int) $value['timeStamp']);
         $conversation['subject'] = $value['subject'];
         $conversation['text'] = '<span class="error">' . OW::getLanguage()->text('mailbox', 'read_permission_denied') . '</span>';
         if (OW::getUser()->isAuthorized('mailbox', 'read_message')) {
             // check credits
             $eventParams = array('pluginKey' => 'mailbox', 'action' => 'read_message');
             $credits = OW::getEventManager()->call('usercredits.check_balance', $eventParams);
             if ($credits === false && !$conversation['recipientRead']) {
                 $creditsMsg = OW::getEventManager()->call('usercredits.error_message', $eventParams);
                 $conversation['text'] = '<span class="error">' . $creditsMsg . '</span>';
             } else {
                 $short = mb_strlen($value['text']) > 100 ? mb_substr($value['text'], 0, 100) . '...' : $value['text'];
                 //TODO:
                 $short = UTIL_HtmlTag::autoLink($short);
                 $event = new OW_Event('mailbox.message_render', array('conversationId' => $conversation['conversationId'], 'messageId' => $value['id'], 'senderId' => $conversation['userId'], 'recipientId' => $conversation['opponentId']), array('short' => $short, 'full' => $value['text']));
                 OW::getEventManager()->trigger($event);
                 $eventData = $event->getData();
                 $conversation['text'] = $eventData['short'];
             }
         }
         $conversationList[] = $conversation;
         $opponentsId[] = $conversation['opponentId'];
         $conversationsId[] = $conversation['conversationId'];
     }
     $opponentsId = array_unique($opponentsId);
     $conversationsId = array_unique($conversationsId);
     $opponentsAvatar = BOL_AvatarService::getInstance()->getDataForUserAvatars($opponentsId);
     $opponentsUrl = BOL_UserService::getInstance()->getUserUrlsForList($opponentsId);
     $opponentsDisplayNames = BOL_UserService::getInstance()->getDisplayNamesForList($opponentsId);
     $attachmentsCount = $conversationService->getAttachmentsCountByConversationList($conversationsId);
     $this->assign('attachments', $attachmentsCount);
     $this->assign('opponentsAvatar', $opponentsAvatar);
     $this->assign('opponentsUrl', $opponentsUrl);
     $this->assign('opponentsDisplayNames', $opponentsDisplayNames);
     $this->assign('conversationList', $conversationList);
     $deleteConfirmMessage = OW::getLanguage()->text('mailbox', 'delete_confirm_message');
     //include js
     $onLoadJs = " \$( document ).ready( function(){\n\t\t\t\t\t\tvar inbox = new mailboxConversationList( " . json_encode(array('responderUrl' => $this->responderUrl, 'deleteConfirmMessage' => $deleteConfirmMessage)) . " );\n\t\t\t\t\t\tinbox.bindFunction();\n\t\t\t\t\t} ); ";
     OW::getDocument()->addOnloadScript($onLoadJs);
     OW::getDocument()->addScript($this->jsDirUrl . "mailbox.js");
 }