BxCnvModule.php 8.0 KB

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