BxCnvModule.php 10.0 KB

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