BxCnvModule.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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, before deletetion it checks user permission to delete convos
  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. /**
  93. * Delete whole conversation for all users by content id
  94. * @return error string on error, or empty string on success
  95. */
  96. public function deleteConvoForever ($iContentId)
  97. {
  98. if (!$this->_oDb->deleteConvo((int)$iContentId))
  99. return _t('_error occured');
  100. return '';
  101. }
  102. public function actionMarkUnread($iContentId)
  103. {
  104. header('Content-Type:text/plain; charset=utf-8');
  105. if ($s = $this->markUnread ($iContentId)) {
  106. echo $s;
  107. exit;
  108. }
  109. echo BX_DOL_URL_ROOT . $this->_oConfig->CNF['URL_HOME'];
  110. exit;
  111. }
  112. public function actionDelete($iContentId)
  113. {
  114. header('Content-Type:text/plain; charset=utf-8');
  115. if ($s = $this->deleteConvo ($iContentId)) {
  116. echo $s;
  117. exit;
  118. }
  119. echo BX_DOL_URL_ROOT . $this->_oConfig->CNF['URL_HOME'];
  120. exit;
  121. }
  122. /**
  123. * Display convos in folder
  124. */
  125. public function actionFolder ($iFolderId)
  126. {
  127. $oTemplate = BxDolTemplate::getInstance();
  128. bx_import('BxDolPage');
  129. $aFolder = $this->_oDb->getFolder((int)$iFolderId);
  130. $oPage = BxDolPage::getObjectInstance('bx_convos_home');
  131. if (!$aFolder || !$oPage) {
  132. $this->_oTemplate->displayPageNotFound();
  133. exit;
  134. }
  135. // add replaceable markers
  136. $oPage->addMarkers(array(
  137. 'folder_id' => (int)$iFolderId,
  138. 'folder' => _t($aFolder['name']),
  139. ));
  140. $s = $oPage->getCode();
  141. $this->_oTemplate = BxDolTemplate::getInstance();
  142. $this->_oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
  143. $this->_oTemplate->setPageContent ('page_main_code', $s);
  144. $this->_oTemplate->getPageCode();
  145. }
  146. /**
  147. * Get possible recipients for start conversation form
  148. */
  149. public function actionAjaxGetRecipients ()
  150. {
  151. $sTerm = bx_get('term');
  152. $a = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
  153. header('Content-Type:text/javascript; charset=utf-8');
  154. echo(json_encode($a));
  155. }
  156. public function serviceConversationsInFolder ($iFolderId = BX_CNV_FOLDER_INBOX)
  157. {
  158. bx_import('BxDolGrid');
  159. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID']);
  160. if (!$oGrid)
  161. return false;
  162. $aFolder = $this->_oDb->getFolder((int)$iFolderId);
  163. if (!$aFolder)
  164. return false;
  165. $this->setModuleSubmenu ((int)$iFolderId);
  166. // TODO: incorporate markers into custom class, so replace will work in search and so on
  167. $oGrid->addMarkers(array(
  168. 'folder_id' => (int)$iFolderId,
  169. 'profile_id' => bx_get_logged_profile_id(),
  170. ));
  171. return $oGrid->getCode();
  172. }
  173. public function serviceMessagesPreviews ($iProfileId = 0)
  174. {
  175. if (!$iProfileId)
  176. $iProfileId = bx_get_logged_profile_id();
  177. $a = $this->_oDb->getMessagesPreviews($iProfileId, 0, (int)getParam('bx_convos_preview_messages_num'));
  178. return $this->_oTemplate->getMessagesPreviews($a);
  179. }
  180. /**
  181. * Get number of unread messages for spme profile
  182. * @param $iProfileId - profile to get unread messages for, if omitted then currently logged is profile is used
  183. * @return integer
  184. */
  185. public function serviceGetUnreadMessagesNum ($iProfileId = 0)
  186. {
  187. if (!$iProfileId)
  188. $iProfileId = bx_get_logged_profile_id();
  189. return $this->_oDb->getUnreadMessagesNum((int)$iProfileId);
  190. }
  191. /**
  192. * Update last comment time and author
  193. */
  194. public function serviceTriggerCommentPost ($iContentId, $iProfileId, $iCommentId, $iTimestamp = 0)
  195. {
  196. if (!(int)$iContentId)
  197. return false;
  198. $aContentInfo = $this->_oDb->getContentInfoById((int)$iContentId);
  199. if (!$aContentInfo)
  200. return false;
  201. if (!$iTimestamp)
  202. $iTimestamp = time();
  203. if ($iProfileId == bx_get_logged_profile_id())
  204. $this->_oDb->updateReadComments($iProfileId, $aContentInfo[$this->_oConfig->CNF['FIELD_ID']], $aContentInfo[$this->_oConfig->CNF['FIELD_COMMENTS']]);
  205. return $this->_oDb->updateLastCommentTimeProfile((int)$iContentId, (int)$iProfileId, (int)$iCommentId, $iTimestamp);
  206. }
  207. /**
  208. * Entry collaborators block
  209. */
  210. public function serviceEntityCollaborators ($iContentId = 0)
  211. {
  212. if (!$iContentId)
  213. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  214. if (!$iContentId)
  215. return false;
  216. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  217. if (!$aContentInfo)
  218. return false;
  219. return $this->_oTemplate->entryCollaborators ($aContentInfo, 5, 'right');
  220. }
  221. /**
  222. * No moderators for personal convos
  223. */
  224. protected function _isModerator ($isPerformAction = false)
  225. {
  226. return false;
  227. }
  228. /**
  229. * No thumbs for convos
  230. */
  231. public function checkAllowedSetThumb ()
  232. {
  233. return _t('_sys_txt_access_denied');
  234. }
  235. /**
  236. * Conversations can be deleted by author and/or collaborators only.
  237. * Admins can't delete conversations, since it's designed for participant only, it's moved to trash actually, also it's private content.
  238. */
  239. public function checkAllowedDelete (&$aDataEntry, $isPerformAction = false)
  240. {
  241. if ($aDataEntry[$this->_oConfig->CNF['FIELD_AUTHOR']] == $this->_iProfileId || $this->isCollaborator($aDataEntry, bx_get_logged_profile_id()))
  242. return CHECK_ACTION_RESULT_ALLOWED;
  243. if ($this->_isModerator($isPerformAction))
  244. return _t('_sys_txt_access_denied');
  245. return parent::checkAllowedDelete ($aDataEntry, $isPerformAction);
  246. }
  247. /**
  248. * Only collaborators can view convo
  249. */
  250. public function checkAllowedView ($aDataEntry, $isPerformAction = false)
  251. {
  252. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = parent::checkAllowedView ($aDataEntry, $isPerformAction)))
  253. return $sMsg;
  254. return $this->isCollaborator($aDataEntry, bx_get_logged_profile_id()) ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_access_denied');
  255. }
  256. protected function isCollaborator($aDataEntry, $iProfileId)
  257. {
  258. $aCollaborators = $this->_oDb->getCollaborators($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]);
  259. return isset($aCollaborators[$iProfileId]);
  260. }
  261. }
  262. /** @} */