BxCnvModule.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 DolphinModules
  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. // TODO: add to page builder
  118. bx_import('BxDolGrid');
  119. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID']);
  120. if (!$oGrid){
  121. $this->_oTemplate->displayErrorOccured();
  122. exit;
  123. }
  124. $aFolder = $this->_oDb->getFolder((int)$iFolderId);
  125. if (!$aFolder) {
  126. $this->_oTemplate->displayPageNotFound();
  127. exit;
  128. }
  129. $this->setModuleSubmenu ((int)$iFolderId);
  130. // TODO: incorporate markers into custom class, so replace will work in search and so on
  131. $oGrid->addMarkers(array(
  132. 'folder_id' => (int)$iFolderId,
  133. 'profile_id' => bx_get_logged_profile_id(),
  134. ));
  135. // TODO: refactor below
  136. $oTemplate = BxDolTemplate::getInstance();
  137. $oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
  138. $oTemplate->setPageHeader (str_replace('{folder}', _t($aFolder['name']), _t('_bx_cnv_page_title_folder')));
  139. $oTemplate->setPageContent ('page_main_code', $oGrid->getCode());
  140. $oTemplate->getPageCode();
  141. }
  142. /**
  143. * Get possible recipients for start conversation form
  144. */
  145. public function actionAjaxGetRecipients ()
  146. {
  147. $sTerm = bx_get('term');
  148. $a = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
  149. header('Content-Type:text/javascript; charset=utf-8');
  150. echo(json_encode($a));
  151. }
  152. public function serviceMessagesPreviews ($iProfileId = 0)
  153. {
  154. if (!$iProfileId)
  155. $iProfileId = bx_get_logged_profile_id();
  156. $a = $this->_oDb->getMessagesPreviews($iProfileId);
  157. return $this->_oTemplate->getMessagesPreviews($a);
  158. }
  159. /**
  160. * Get number of unread messages for spme profile
  161. * @param $iProfileId - profile to get unread messages for, if omitted then currently logged is profile is used
  162. * @return integer
  163. */
  164. public function serviceGetUnreadMessagesNum ($iProfileId = 0)
  165. {
  166. if (!$iProfileId)
  167. $iProfileId = bx_get_logged_profile_id();
  168. return $this->_oDb->getUnreadMessagesNum((int)$iProfileId);
  169. }
  170. /**
  171. * Update last comment time and author
  172. */
  173. public function serviceTriggerCommentPost ($iContentId, $iProfileId, $iCommentId, $iTimestamp = 0)
  174. {
  175. if (!(int)$iContentId)
  176. return false;
  177. $aContentInfo = $this->_oDb->getContentInfoById((int)$iContentId);
  178. if (!$aContentInfo)
  179. return false;
  180. if (!$iTimestamp)
  181. $iTimestamp = time();
  182. if ($iProfileId == bx_get_logged_profile_id())
  183. $this->_oDb->updateReadComments($iProfileId, $aContentInfo[$this->_oConfig->CNF['FIELD_ID']], $aContentInfo[$this->_oConfig->CNF['FIELD_COMMENTS']]);
  184. return $this->_oDb->updateLastCommentTimeProfile((int)$iContentId, (int)$iProfileId, (int)$iCommentId, $iTimestamp);
  185. }
  186. /**
  187. * Entry collaborators block
  188. */
  189. public function serviceEntityCollaborators ($iContentId = 0)
  190. {
  191. if (!$iContentId)
  192. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  193. if (!$iContentId)
  194. return false;
  195. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  196. if (!$aContentInfo)
  197. return false;
  198. return $this->_oTemplate->entryCollaborators ($aContentInfo, 5, 'right');
  199. }
  200. /**
  201. * No moderators for personal convos
  202. */
  203. protected function _isModerator ($isPerformAction = false)
  204. {
  205. return false;
  206. }
  207. /**
  208. * No thumbs for convos
  209. */
  210. public function checkAllowedSetThumb ()
  211. {
  212. return _t('_sys_txt_access_denied');
  213. }
  214. /**
  215. * Only collaborators can view convo
  216. */
  217. public function checkAllowedView ($aDataEntry, $isPerformAction = false)
  218. {
  219. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = parent::checkAllowedView ($aDataEntry, $isPerformAction)))
  220. return $sMsg;
  221. $aCollaborators = $this->_oDb->getCollaborators($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]);
  222. return isset($aCollaborators[bx_get_logged_profile_id()]) ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_access_denied');
  223. }
  224. }
  225. /** @} */