BxCnvModule.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Convos Convos
  7. * @ingroup UnaModules
  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. return $this->_oDb->deleteConvo((int)$iContentId);
  97. }
  98. public function actionMarkUnread($iContentId)
  99. {
  100. header('Content-Type:text/plain; charset=utf-8');
  101. if ($s = $this->markUnread ($iContentId)) {
  102. echo $s;
  103. exit;
  104. }
  105. echo BX_DOL_URL_ROOT . $this->_oConfig->CNF['URL_HOME'];
  106. exit;
  107. }
  108. public function actionDelete($iContentId)
  109. {
  110. header('Content-Type:text/plain; charset=utf-8');
  111. if ($s = $this->deleteConvo ($iContentId)) {
  112. echo $s;
  113. exit;
  114. }
  115. echo BX_DOL_URL_ROOT . $this->_oConfig->CNF['URL_HOME'];
  116. exit;
  117. }
  118. /**
  119. * Display convos in folder
  120. */
  121. public function actionFolder ($iFolderId)
  122. {
  123. $oTemplate = BxDolTemplate::getInstance();
  124. $aFolder = $this->_oDb->getFolder((int)$iFolderId);
  125. $oPage = BxDolPage::getObjectInstance('bx_convos_home');
  126. if (!$aFolder || !$oPage) {
  127. $this->_oTemplate->displayPageNotFound();
  128. exit;
  129. }
  130. // add replaceable markers
  131. $oPage->addMarkers(array(
  132. 'folder_id' => (int)$iFolderId,
  133. 'folder' => _t($aFolder['name']),
  134. ));
  135. $s = $oPage->getCode();
  136. $this->_oTemplate = BxDolTemplate::getInstance();
  137. $this->_oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
  138. $this->_oTemplate->setPageContent ('page_main_code', $s);
  139. $_GET['i']='convos-folder'; //#1148
  140. $this->_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 serviceGetSafeServices()
  153. {
  154. return array (
  155. // other
  156. 'ModuleIcon' => '',
  157. 'GetLink' => '',
  158. // forms
  159. 'GetCreatePostForm' => '',
  160. 'EntityEdit' => '',
  161. 'EntityDelete' => '',
  162. // page blocks
  163. 'EntityTextBlock' => '',
  164. 'EntityInfo' => '',
  165. 'EntityComments' => '',
  166. 'EntityAttachments' => '',
  167. // menu
  168. 'EntityAllActions' => '',
  169. 'EntityActions' => '',
  170. 'MyEntriesActions' => '',
  171. // own methods
  172. 'ConversationsInFolder' => '',
  173. );
  174. }
  175. public function serviceIsAllowedContact($iProfileId)
  176. {
  177. return $this->checkAllowedContact($iProfileId) === CHECK_ACTION_RESULT_ALLOWED;
  178. }
  179. /**
  180. * @page service Service Calls
  181. * @section bx_convos Conversations
  182. * @subsection bx_convos-page_blocks Page Blocks
  183. * @subsubsection bx_convos-conversations_in_folder conversations_in_folder
  184. *
  185. * @code bx_srv('bx_convos', 'conversations_in_folder', [...]); @endcode
  186. *
  187. * Display conversations in particulr folder for currently logged-in user
  188. * @param $iFolderId folder ID
  189. *
  190. * @see BxCnvModule::serviceConversationsInFolder
  191. */
  192. /**
  193. * @ref bx_convos-conversations_in_folder "conversations_in_folder"
  194. */
  195. public function serviceConversationsInFolder ($iFolderId = BX_CNV_FOLDER_INBOX)
  196. {
  197. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID']);
  198. if (!$oGrid)
  199. return false;
  200. $aFolder = $this->_oDb->getFolder((int)$iFolderId);
  201. if (!$aFolder)
  202. return false;
  203. $this->setModuleSubmenu ((int)$iFolderId);
  204. // TODO: incorporate markers into custom class, so replace will work in search and so on
  205. $oGrid->addMarkers(array(
  206. 'folder_id' => (int)$iFolderId,
  207. 'profile_id' => bx_get_logged_profile_id(),
  208. ));
  209. $this->_oTemplate->addCss(array('manage_tools.css'));
  210. $this->_oTemplate->addJs(array('manage_tools.js'));
  211. $this->_oTemplate->addJsTranslation(array('_sys_grid_search'));
  212. return $this->_oTemplate->getJsCode('manage_tools', array('sObjNameGrid' => $this->_oConfig->CNF['OBJECT_GRID'])) . $oGrid->getCode();
  213. }
  214. public function serviceMessagesPreviews ($iProfileId = 0, $bEmptyMessage = true)
  215. {
  216. if (!$iProfileId)
  217. $iProfileId = bx_get_logged_profile_id();
  218. $a = $this->_oDb->getMessagesPreviews($iProfileId, 0, (int)getParam('bx_convos_preview_messages_num'));
  219. if((empty($a) || !is_array($a)) && !$bEmptyMessage)
  220. return '';
  221. return $this->_oTemplate->getMessagesPreviews($a);
  222. }
  223. /**
  224. * Returns preview of profile's conversations with statistic for React Jot
  225. * @param int $iProfileId
  226. * @param int $iStart
  227. * @param int $iPerPage
  228. * @param int $iFolderId
  229. * @return mixed
  230. */
  231. public function serviceGetMessagesPreviews ($iProfileId = 0, $iStart = 0, $iPerPage = 10, $iFolderId = BX_CNV_FOLDER_INBOX)
  232. {
  233. if (!$iProfileId)
  234. $iProfileId = bx_get_logged_profile_id();
  235. return $this->_oDb->getMessagesPreviews($iProfileId, (int)$iStart, (int)$iPerPage, $iFolderId);
  236. }
  237. /**
  238. * Get number of unread messages for spme profile
  239. * @param $iProfileId - profile to get unread messages for, if omitted then currently logged is profile is used
  240. * @return integer
  241. */
  242. public function serviceGetUnreadMessagesNum ($iProfileId = 0)
  243. {
  244. if (!$iProfileId)
  245. $iProfileId = bx_get_logged_profile_id();
  246. return $this->_oDb->getUnreadMessagesNum((int)$iProfileId);
  247. }
  248. public function serviceGetLiveUpdates($aMenuItemParent, $aMenuItemChild, $iCount = 0)
  249. {
  250. $iProfileId = (int)bx_get_logged_profile_id();
  251. $iCountNew = $this->_oDb->getUnreadMessagesNum($iProfileId);
  252. if($iCountNew == $iCount)
  253. return false;
  254. return array(
  255. 'count' => $iCountNew, // required
  256. 'method' => 'bx_menu_show_live_update(oData)', // required
  257. 'data' => array(
  258. 'code' => BxDolTemplate::getInstance()->parseHtmlByTemplateName('menu_item_addon', array(
  259. 'content' => '{count}'
  260. )),
  261. 'mi_parent' => $aMenuItemParent,
  262. 'mi_child' => $aMenuItemChild
  263. ), // optional, may have some additional data to be passed in JS method provided using 'method' param above.
  264. );
  265. }
  266. /**
  267. * Update last comment time and author
  268. */
  269. public function serviceTriggerCommentPost ($iContentId, $iProfileId, $iCommentId, $iTimestamp = 0, $sCommentText = '')
  270. {
  271. $CNF = &$this->_oConfig->CNF;
  272. if (!(int)$iContentId)
  273. return false;
  274. $aContentInfo = $this->_oDb->getContentInfoById((int)$iContentId);
  275. if (!$aContentInfo)
  276. return false;
  277. // add author to the list of collaborators when somebody replies
  278. // if author is removed from collaborators
  279. // of if authot placed message to trash
  280. $aCollaborators = $this->_oDb->getCollaborators($aContentInfo[$CNF['FIELD_ID']]);
  281. if (!isset($aCollaborators[$aContentInfo[$CNF['FIELD_AUTHOR']]]) || BX_CNV_FOLDER_TRASH == $this->_oDb->getConversationFolder($aContentInfo[$CNF['FIELD_ID']], $aContentInfo[$CNF['FIELD_AUTHOR']])) {
  282. $oFormsHelper = $this->serviceFormsHelper ();
  283. $oForm = $oFormsHelper->getObjectFormEdit();
  284. $oForm->updateParticipants($aContentInfo[$CNF['FIELD_ID']], BX_CNV_FOLDER_INBOX, false, array($aContentInfo[$CNF['FIELD_AUTHOR']]));
  285. }
  286. $oCmts = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $iContentId);
  287. if(!$oCmts)
  288. return false;
  289. if (!$iTimestamp)
  290. $iTimestamp = time();
  291. if ($iProfileId == bx_get_logged_profile_id())
  292. $this->_oDb->updateReadComments($iProfileId, $aContentInfo[$CNF['FIELD_ID']], $aContentInfo[$CNF['FIELD_COMMENTS']]);
  293. if (!$this->_oDb->updateLastCommentTimeProfile((int)$iContentId, (int)$iProfileId, (int)$iCommentId, $iTimestamp))
  294. return false;
  295. // send notification to all collaborators
  296. if ($oProfile = BxDolProfile::getInstance($iProfileId)) {
  297. foreach ($aCollaborators as $iCollaborator => $iReadComments) {
  298. if ($iCollaborator == $iProfileId)
  299. continue;
  300. sendMailTemplate('bx_cnv_new_reply', 0, $iCollaborator, array(
  301. 'SenderDisplayName' => $oProfile->getDisplayName(),
  302. 'SenderUrl' => $oProfile->getUrl(),
  303. 'PageUrl' => $oCmts->getItemUrl($iCommentId),
  304. 'PageTitle' => $oCmts->getObjectTitle(),
  305. 'Message' => $sCommentText,
  306. ), BX_EMAIL_NOTIFY, true);
  307. }
  308. }
  309. return true;
  310. }
  311. /**
  312. * Create entry form
  313. * @return HTML string
  314. */
  315. public function serviceEntityCreate ($sParams = false)
  316. {
  317. $sProfilesIds = bx_get('profiles');
  318. if($sProfilesIds !== false) {
  319. $sDiv = ',';
  320. $aIds = explode($sDiv, $sProfilesIds);
  321. $aIdsAllowed = array();
  322. foreach($aIds as $iId)
  323. if($this->checkAllowedContact($iId) === CHECK_ACTION_RESULT_ALLOWED)
  324. $aIdsAllowed[] = $iId;
  325. if(empty($aIdsAllowed))
  326. return MsgBox(_t('_sys_txt_access_denied'));
  327. bx_set('profiles', implode($sDiv, $aIdsAllowed));
  328. }
  329. return parent::serviceEntityCreate();
  330. }
  331. /**
  332. * Entry collaborators block
  333. */
  334. public function serviceEntityCollaborators ($iContentId = 0)
  335. {
  336. if (!$iContentId)
  337. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  338. if (!$iContentId)
  339. return false;
  340. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  341. if (!$aContentInfo)
  342. return false;
  343. return $this->_oTemplate->entryCollaborators ($aContentInfo, 5, 'right');
  344. }
  345. /**
  346. * No social sharing for private conversations
  347. */
  348. public function serviceEntitySocialSharing ($mixedContent = false, $aParams = array())
  349. {
  350. return '';
  351. }
  352. /**
  353. * Delete content entry
  354. * @param $iContentId content id
  355. * @return error message or empty string on success
  356. */
  357. public function serviceDeleteEntity ($iContentId, $sFuncDelete = 'deleteData')
  358. {
  359. return $this->_oDb->deleteConvo($iContentId);
  360. }
  361. /**
  362. * Delete profile from all conversation (when profile is delete without content)
  363. * @param $iProfileId profile id
  364. * @return number of deleted items
  365. */
  366. public function serviceRemoveCollaboratorFromAllConversations ($iProfileId)
  367. {
  368. return $this->_oDb->removeCollaboratorFromAllConversations((int)$iProfileId);
  369. }
  370. /**
  371. * Delete all content by profile (when profile is delete with content)
  372. * @param $iProfileId profile id
  373. * @return number of deleted items
  374. */
  375. public function serviceDeleteEntitiesByAuthor ($iProfileId)
  376. {
  377. $a = $this->_oDb->getEntriesByAuthor((int)$iProfileId);
  378. if (!$a)
  379. return 0;
  380. $iCount = 0;
  381. foreach ($a as $aContentInfo)
  382. $iCount += ('' == $this->serviceDeleteEntity($aContentInfo[$this->_oConfig->CNF['FIELD_ID']]) ? 1 : 0);
  383. return $iCount;
  384. }
  385. /**
  386. * No moderators for personal convos
  387. */
  388. public function _isModerator ($isPerformAction = false)
  389. {
  390. return false;
  391. }
  392. /**
  393. * No thumbs for convos
  394. */
  395. public function checkAllowedSetThumb ($iContentId = 0)
  396. {
  397. return _t('_sys_txt_access_denied');
  398. }
  399. /**
  400. * There is no 'moderator' access in convos
  401. */
  402. public function checkAllowedEditAnyEntry ($isPerformAction = false)
  403. {
  404. return _t('_sys_txt_access_denied');
  405. }
  406. public function checkAllowedEditAnyEntryForProfile ($isPerformAction = false, $iProfileId = false)
  407. {
  408. return _t('_sys_txt_access_denied');
  409. }
  410. public function checkAllowedDeleteAnyEntryForProfile ($isPerformAction = false, $iProfileId = false)
  411. {
  412. return _t('_sys_txt_access_denied');
  413. }
  414. public function checkAllowedEdit ($aDataEntry, $isPerformAction = false)
  415. {
  416. if ($aDataEntry[$this->_oConfig->CNF['FIELD_ALLOW_EDIT']] && $this->isCollaborator($aDataEntry, bx_get_logged_profile_id()))
  417. return CHECK_ACTION_RESULT_ALLOWED;
  418. return parent::checkAllowedEdit ($aDataEntry, $isPerformAction);
  419. }
  420. /**
  421. * Conversations can be deleted by author and/or collaborators only.
  422. * Admins can't delete conversations, since it's designed for participant only, it's moved to trash actually, also it's private content.
  423. */
  424. public function checkAllowedDelete (&$aDataEntry, $isPerformAction = false)
  425. {
  426. if ($aDataEntry[$this->_oConfig->CNF['FIELD_AUTHOR']] == $this->_iProfileId || $this->isCollaborator($aDataEntry, bx_get_logged_profile_id()))
  427. return CHECK_ACTION_RESULT_ALLOWED;
  428. if ($this->_isModerator($isPerformAction))
  429. return _t('_sys_txt_access_denied');
  430. return parent::checkAllowedDelete ($aDataEntry, $isPerformAction);
  431. }
  432. /**
  433. * Only collaborators can view convo
  434. */
  435. public function checkAllowedView ($aDataEntry, $isPerformAction = false)
  436. {
  437. return $this->serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction);
  438. }
  439. /**
  440. * Checks whether contact is allowed.
  441. * @param integer $iProfileId - recipient profile ID
  442. * @param boolean $isPerformAction - perform or just check the action
  443. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  444. */
  445. public function checkAllowedContact($iProfileId, $isPerformAction = false)
  446. {
  447. $oProfile = BxDolProfile::getInstance($iProfileId);
  448. if(!$oProfile)
  449. return _t('_sys_txt_access_denied');
  450. $mixedResult = $oProfile->checkAllowedProfileContact();
  451. if($mixedResult !== CHECK_ACTION_RESULT_ALLOWED)
  452. return _t('_sys_txt_access_denied');
  453. return $this->checkAllowedAdd($isPerformAction);
  454. }
  455. public function serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction = false, $iProfileId = false)
  456. {
  457. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = parent::serviceCheckAllowedViewForProfile ($aDataEntry, $isPerformAction, $iProfileId)))
  458. return $sMsg;
  459. if (!$iProfileId)
  460. $iProfileId = bx_get_logged_profile_id();
  461. return $this->isCollaborator($aDataEntry, $iProfileId) ? CHECK_ACTION_RESULT_ALLOWED : _t('_sys_txt_access_denied');
  462. }
  463. protected function isCollaborator($aDataEntry, $iProfileId)
  464. {
  465. $aCollaborators = $this->_oDb->getCollaborators($aDataEntry[$this->_oConfig->CNF['FIELD_ID']]);
  466. return isset($aCollaborators[$iProfileId]);
  467. }
  468. public function serviceIsAllowedPostInContext()
  469. {
  470. return false;
  471. }
  472. }
  473. /** @} */