BxCnvModule.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup Convos Convos
  7. * @ingroup TridentModules
  8. *
  9. * @{
  10. */
  11. bx_import ('BxBaseModTextModule');
  12. define('BX_CNV_FOLDER_INBOX', 1);
  13. define('BX_CNV_FOLDER_DRAFTS', 2);
  14. define('BX_CNV_FOLDER_SPAM', 3);
  15. define('BX_CNV_FOLDER_TRASH', 4);
  16. /**
  17. * Conversations module
  18. */
  19. class BxCnvModule extends BxBaseModTextModule
  20. {
  21. function __construct(&$aModule)
  22. {
  23. parent::__construct($aModule);
  24. }
  25. public function sortCollaborators ($aCollaborators, $iProfileIdLastComment, $iProfileIdAuthor, $iProfileIdCurrent = 0)
  26. {
  27. if (!$iProfileIdCurrent)
  28. $iProfileIdCurrent = bx_get_logged_profile_id();
  29. $aMoveUp = array($iProfileIdCurrent, $iProfileIdLastComment, $iProfileIdAuthor);
  30. asort($aCollaborators, SORT_NUMERIC);
  31. foreach ($aMoveUp as $iProfileId) {
  32. if (!isset($aCollaborators[$iProfileId]))
  33. continue;
  34. $a = array($iProfileId => $aCollaborators[$iProfileId]);
  35. unset($aCollaborators[$iProfileId]);
  36. $aCollaborators = $a + $aCollaborators;
  37. }
  38. return $aCollaborators;
  39. }
  40. public function setModuleSubmenu ($iCurrentFolderId = 0)
  41. {
  42. $CNF = &$this->_oConfig->CNF;
  43. $aMapFolderId2MenuName = array (
  44. BX_CNV_FOLDER_INBOX => 'convos-folder-inbox',
  45. BX_CNV_FOLDER_DRAFTS => 'convos-drafts',
  46. BX_CNV_FOLDER_SPAM => 'convos-spam',
  47. BX_CNV_FOLDER_TRASH => 'convos-trash',
  48. );
  49. bx_import('BxDolMenu');
  50. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  51. if (!$oMenuSubmenu)
  52. return;
  53. $oMenuSubmenu->setObjectSubmenu($CNF['OBJECT_MENU_SUBMENU'], array (
  54. 'title' => _t('_bx_cnv'),
  55. 'link' => BX_DOL_URL_ROOT . $CNF['URL_HOME'],
  56. 'icon' => '',
  57. ));
  58. $oMenuModule = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SUBMENU']);
  59. if ($oMenuModule && isset($aMapFolderId2MenuName[$iCurrentFolderId]))
  60. $oMenuModule->setSelected($this->_aModule['name'], $aMapFolderId2MenuName[$iCurrentFolderId]);
  61. }
  62. /**
  63. * Mark conversation as unread for the current user
  64. * @return error string on error, or empty string on success
  65. */
  66. public function markUnread ($iContentId)
  67. {
  68. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  69. if (!$aContentInfo)
  70. return _t('_sys_request_page_not_found_cpt');
  71. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->checkAllowedView($aContentInfo)))
  72. return $sMsg;
  73. if (!$this->_oDb->updateReadComments(bx_get_logged_profile_id(), (int)$iContentId, -1))
  74. return _t('_error occured');
  75. return '';
  76. }
  77. /**
  78. * Delete conversation for current user by content id
  79. * @return error string on error, or empty string on success
  80. */
  81. public function deleteConvo ($iContentId)
  82. {
  83. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  84. if (!$aContentInfo)
  85. return _t('_sys_request_page_not_found_cpt');
  86. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->checkAllowedDelete($aContentInfo)))
  87. return $sMsg;
  88. if (!$this->_oDb->moveConvo((int)$iContentId, bx_get_logged_profile_id(), BX_CNV_FOLDER_TRASH))
  89. return _t('_error occured');
  90. return '';
  91. }
  92. public function actionMarkUnread($iContentId)
  93. {
  94. header('Content-Type:text/plain; charset=utf-8');
  95. if ($s = $this->markUnread ($iContentId)) {
  96. echo $s;
  97. exit;
  98. }
  99. echo BX_DOL_URL_ROOT . $this->_oConfig->CNF['URL_HOME'];
  100. exit;
  101. }
  102. public function actionDelete($iContentId)
  103. {
  104. header('Content-Type:text/plain; charset=utf-8');
  105. if ($s = $this->deleteConvo ($iContentId)) {
  106. echo $s;
  107. exit;
  108. }
  109. echo BX_DOL_URL_ROOT . $this->_oConfig->CNF['URL_HOME'];
  110. exit;
  111. }
  112. /**
  113. * Display convos in folder
  114. */
  115. public function actionFolder ($iFolderId)
  116. {
  117. $oTemplate = BxDolTemplate::getInstance();
  118. bx_import('BxDolPage');
  119. $aFolder = $this->_oDb->getFolder((int)$iFolderId);
  120. $oPage = BxDolPage::getObjectInstance('bx_convos_home');
  121. if (!$aFolder || !$oPage) {
  122. $this->_oTemplate->displayPageNotFound();
  123. exit;
  124. }
  125. // add replaceable markers
  126. $oPage->addMarkers(array(
  127. 'folder_id' => (int)$iFolderId,
  128. 'folder' => _t($aFolder['name']),
  129. ));
  130. $s = $oPage->getCode();
  131. $this->_oTemplate = BxDolTemplate::getInstance();
  132. $this->_oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
  133. $this->_oTemplate->setPageContent ('page_main_code', $s);
  134. $this->_oTemplate->getPageCode();
  135. }
  136. /**
  137. * Get possible recipients for start conversation form
  138. */
  139. public function actionAjaxGetRecipients ()
  140. {
  141. $sTerm = bx_get('term');
  142. $a = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
  143. header('Content-Type:text/javascript; charset=utf-8');
  144. echo(json_encode($a));
  145. }
  146. public function serviceConversationsInFolder ($iFolderId = BX_CNV_FOLDER_INBOX)
  147. {
  148. bx_import('BxDolGrid');
  149. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID']);
  150. if (!$oGrid)
  151. return false;
  152. $aFolder = $this->_oDb->getFolder((int)$iFolderId);
  153. if (!$aFolder)
  154. return false;
  155. $this->setModuleSubmenu ((int)$iFolderId);
  156. // TODO: incorporate markers into custom class, so replace will work in search and so on
  157. $oGrid->addMarkers(array(
  158. 'folder_id' => (int)$iFolderId,
  159. 'profile_id' => bx_get_logged_profile_id(),
  160. ));
  161. return $oGrid->getCode();
  162. }
  163. public function serviceMessagesPreviews ($iProfileId = 0)
  164. {
  165. if (!$iProfileId)
  166. $iProfileId = bx_get_logged_profile_id();
  167. $a = $this->_oDb->getMessagesPreviews($iProfileId, 0, (int)getParam('bx_convos_preview_messages_num'));
  168. return $this->_oTemplate->getMessagesPreviews($a);
  169. }
  170. /**
  171. * Get number of unread messages for spme profile
  172. * @param $iProfileId - profile to get unread messages for, if omitted then currently logged is profile is used
  173. * @return integer
  174. */
  175. public function serviceGetUnreadMessagesNum ($iProfileId = 0)
  176. {
  177. if (!$iProfileId)
  178. $iProfileId = bx_get_logged_profile_id();
  179. return $this->_oDb->getUnreadMessagesNum((int)$iProfileId);
  180. }
  181. /**
  182. * Update last comment time and author
  183. */
  184. public function serviceTriggerCommentPost ($iContentId, $iProfileId, $iCommentId, $iTimestamp = 0)
  185. {
  186. if (!(int)$iContentId)
  187. return false;
  188. $aContentInfo = $this->_oDb->getContentInfoById((int)$iContentId);
  189. if (!$aContentInfo)
  190. return false;
  191. if (!$iTimestamp)
  192. $iTimestamp = time();
  193. if ($iProfileId == bx_get_logged_profile_id())
  194. $this->_oDb->updateReadComments($iProfileId, $aContentInfo[$this->_oConfig->CNF['FIELD_ID']], $aContentInfo[$this->_oConfig->CNF['FIELD_COMMENTS']]);
  195. return $this->_oDb->updateLastCommentTimeProfile((int)$iContentId, (int)$iProfileId, (int)$iCommentId, $iTimestamp);
  196. }
  197. /**
  198. * Entry collaborators block
  199. */
  200. public function serviceEntityCollaborators ($iContentId = 0)
  201. {
  202. if (!$iContentId)
  203. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  204. if (!$iContentId)
  205. return false;
  206. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  207. if (!$aContentInfo)
  208. return false;
  209. return $this->_oTemplate->entryCollaborators ($aContentInfo, 5, 'right');
  210. }
  211. /**
  212. * No moderators for personal convos
  213. */
  214. protected function _isModerator ($isPerformAction = false)
  215. {
  216. return false;
  217. }
  218. /**
  219. * No thumbs for convos
  220. */
  221. public function checkAllowedSetThumb ()
  222. {
  223. return _t('_sys_txt_access_denied');
  224. }
  225. /**
  226. * Only collaborators can view convo
  227. */
  228. public function checkAllowedView ($aDataEntry, $isPerformAction = false)
  229. {
  230. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = parent::checkAllowedView ($aDataEntry, $isPerformAction)))
  231. return $sMsg;
  232. $aCollaborators = $this->_oDb->getCollaborators($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]);
  233. return isset($aCollaborators[bx_get_logged_profile_id()]) ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_access_denied');
  234. }
  235. }
  236. /** @} */