BxNtfsModule.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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 Notifications Notifications
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxBaseModNotificationsModule');
  12. define('BX_NTFS_TYPE_OBJECT_OWNER_AND_CONNECTIONS', 'obj_own_and_con');
  13. define('BX_NTFS_TYPE_DEFAULT', BX_NTFS_TYPE_OBJECT_OWNER_AND_CONNECTIONS);
  14. /**
  15. * STYPE - Setting Type:
  16. * 1. personal - related to you;
  17. * 2. follow_member - related to members you follow;
  18. * 3. follow_context - related to contexts you follow;
  19. * 4. other - others.
  20. */
  21. define('BX_NTFS_STYPE_PERSONAL', 'personal');
  22. define('BX_NTFS_STYPE_FOLLOW_MEMBER', 'follow_member');
  23. define('BX_NTFS_STYPE_FOLLOW_CONTEXT', 'follow_context');
  24. define('BX_NTFS_STYPE_OTHER', 'other');
  25. /**
  26. * SLTMODE - Silent mode:
  27. * It is needed for alert sending module to tell that the alert should be ignored
  28. * with Notifications module completely or partially. Available values:
  29. * 1. disabled (global, value = 0) - all notifications are available;
  30. * 2. absolute (global, value = 1) - alert isn't registered which means that there is no notifications at all;
  31. * 3. absolute (for Notifications only, value = 11) - the same as global absolute.
  32. * 3. on-site only (value = 12) - alert is registered. It means that on-site notification is available while the others are disabled.
  33. * 4. on-site + email (value = 13) - alert is registered (on-site) and notification via 'email' is enabled.
  34. * 5. on-site + push (value = 14) - alert is registered (on-site) and notification via 'push' is enabled.
  35. *
  36. * @see BxNtfsResponse::response - 'silent_mode' parameter in Alerts Extras array.
  37. */
  38. define('BX_NTFS_SLTMODE_ABSOLUTE', 11);
  39. define('BX_NTFS_SLTMODE_SITE', 12);
  40. define('BX_NTFS_SLTMODE_SITE_EMAIL', 13);
  41. define('BX_NTFS_SLTMODE_SITE_PUSH', 14);
  42. class BxNtfsModule extends BxBaseModNotificationsModule
  43. {
  44. /**
  45. * Constructor
  46. */
  47. function __construct($aModule)
  48. {
  49. parent::__construct($aModule);
  50. $this->_iOwnerId = $this->getUserId();
  51. }
  52. /**
  53. * ACTION METHODS
  54. */
  55. function actionMarkAsClicked()
  56. {
  57. $iId = bx_process_input(bx_get('id'), BX_DATA_INT);
  58. $aEvent = $this->_oDb->getEvents(['browse' => 'id', 'value' => $iId]);
  59. if(empty($aEvent) || !is_array($aEvent))
  60. return echoJson(['code' => 1]);
  61. $aParams = $this->_prepareParamsGet();
  62. if($this->_oDb->markAsClicked($aParams['viewer_id'], $iId) === false)
  63. return echoJson(['code' => 2]);
  64. echoJson([
  65. 'code' => 0,
  66. 'id' => $iId
  67. ]);
  68. }
  69. function actionGetPosts()
  70. {
  71. $aParams = $this->_prepareParamsGet();
  72. if(empty($aParams['owner_id']) || (int)$aParams['owner_id'] != $this->_iOwnerId)
  73. return echoJson([]);
  74. echoJson([
  75. 'events' => $this->_oTemplate->getPosts($aParams)
  76. ]);
  77. }
  78. /**
  79. * SERVICE METHODS
  80. */
  81. public function serviceGetSafeServices()
  82. {
  83. return [
  84. 'GetBlockView' => '',
  85. 'GetData' => '',
  86. 'GetUnreadNotificationsNum' => '',
  87. 'GetUnreadNotificationsNumEx' => '',
  88. 'MarkAsRead' => '',
  89. ];
  90. }
  91. /**
  92. * @page service Service Calls
  93. * @section bx_notifications Invitations
  94. * @subsection bx_notifications-other Other
  95. * @subsubsection bx_notifications-get_include get_include
  96. *
  97. * @code bx_srv('bx_notifications', 'get_include', [...]); @endcode
  98. *
  99. * Get all necessary CSS and JS files to include in a page.
  100. *
  101. * @return string with all necessary CSS and JS files.
  102. *
  103. * @see BxInvModule::serviceGetInclude
  104. */
  105. /**
  106. * @ref bx_notifications-get_include "get_include"
  107. */
  108. public function serviceGetInclude($bIncludeCss = true, $mixedIncludeJs = false)
  109. {
  110. return $this->_oTemplate->getInclude($bIncludeCss, $mixedIncludeJs);
  111. }
  112. /**
  113. * @page service Service Calls
  114. * @section bx_notifications Notifications
  115. * @subsection bx_notifications-page_blocks Page Blocks
  116. * @subsubsection bx_notifications-get_block_settings get_block_settings
  117. *
  118. * @code bx_srv('bx_notifications', 'get_block_settings', [...]); @endcode
  119. *
  120. * Get Settings block for a separate page.
  121. *
  122. * @return a string with block content to display on the site. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  123. *
  124. * @see BxNtfsModule::serviceGetBlockSettings
  125. */
  126. /**
  127. * @ref bx_notifications-get_block_settings "get_block_settings"
  128. */
  129. public function serviceGetBlockSettings($sDeliveryType = '', $aParams = array())
  130. {
  131. if(!isLogged())
  132. return '';
  133. if(empty($sDeliveryType) && bx_get('delivery') !== false)
  134. $sDeliveryType = bx_process_input(bx_get('delivery'));
  135. if(empty($sDeliveryType))
  136. $sDeliveryType = BX_BASE_MOD_NTFS_DTYPE_SITE;
  137. $iUserId = bx_get_logged_profile_id();
  138. if(!empty($aParams['user_id']))
  139. $iUserId = (int)$aParams['user_id'];
  140. else
  141. $aParams['user_id'] = $iUserId;
  142. $this->_oDb->initSettingUser($iUserId);
  143. return array(
  144. 'content' => $this->getBlockSettings($sDeliveryType, $aParams),
  145. );
  146. }
  147. /**
  148. * @page service Service Calls
  149. * @section bx_notifications Notifications
  150. * @subsection bx_notifications-page_blocks Page Blocks
  151. * @subsubsection bx_notifications-get_data get_data
  152. *
  153. * @code bx_srv('bx_notifications', 'get_data', [...]); @endcode
  154. *
  155. * Get data for app
  156. *
  157. * @return an array .
  158. *
  159. * @see BxNtfsModule::serviceGetData
  160. */
  161. /**
  162. * @ref bx_notifications-get_data "get_data"
  163. * @api @ref bx_notifications-get_data "get_data"
  164. */
  165. public function serviceGetData($aParams)
  166. {
  167. if(is_string($aParams)){
  168. $aParams = json_decode($aParams, true);
  169. }
  170. return $this->serviceGetBlockView($aParams['params']['type'], $aParams['params']['start'], $aParams['params']['per_page'], $aParams['params']['modules']);
  171. }
  172. /**
  173. * @page service Service Calls
  174. * @section bx_notifications Notifications
  175. * @subsection bx_notifications-page_blocks Page Blocks
  176. * @subsubsection bx_notifications-get_block_view get_block_view
  177. *
  178. * @code bx_srv('bx_notifications', 'get_block_view', [...]); @endcode
  179. *
  180. * Get View block for a separate page. Will return a block with "Empty" message if nothing found.
  181. *
  182. * @return an array describing a block to display on the site. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  183. *
  184. * @see BxNtfsModule::serviceGetBlockView
  185. */
  186. /**
  187. * @ref bx_notifications-get_block_view "get_block_view"
  188. */
  189. public function serviceGetBlockView($sType = '', $iStart = -1, $iPerPage = -1, $aModules = [])
  190. {
  191. $aBrowseTypes = [BX_NTFS_TYPE_OBJECT_OWNER_AND_CONNECTIONS, BX_BASE_MOD_NTFS_TYPE_CONNECTIONS, BX_BASE_MOD_NTFS_TYPE_OBJECT_OWNER];
  192. if(empty($sType)) {
  193. $mixedType = bx_get('type');
  194. if($mixedType !== false && in_array($mixedType, $aBrowseTypes))
  195. $sType = $mixedType;
  196. if(empty($sType))
  197. $sType = BX_NTFS_TYPE_DEFAULT;
  198. }
  199. $iOwnerId = $this->getUserId();
  200. if(!$iOwnerId)
  201. return ['content' => MsgBox(_t('_bx_ntfs_txt_msg_no_results'))];
  202. $aParams = $this->_prepareParams($sType, $iOwnerId, $iStart, $iPerPage, $aModules);
  203. if(bx_is_api()) {
  204. $aParams['start_from_item'] = true;
  205. $aContent = defined('BX_API_PAGE') ? [] : $this->_oTemplate->getPosts($aParams);
  206. if(!empty($aContent) && is_array($aContent)) {
  207. $iContent = count($aContent);
  208. if($iContent > $aParams['per_page'])
  209. $aContent = array_slice($aContent, 0, -($iContent - $aParams['per_page']));
  210. $aParams['start'] = end($aContent)['id'];
  211. }
  212. if($aParams['start'] == 0)
  213. $this->markAsRead($iOwnerId, $aParams);
  214. return [bx_api_get_block('browse', [
  215. 'unit' => 'notifications',
  216. 'request_url' => '/api.php?r=bx_notifications/get_data/&params[]=',
  217. 'params' => $aParams,
  218. 'id' => $iOwnerId,
  219. 'data' => $aContent]),
  220. ];
  221. }
  222. $sContent = $this->_oTemplate->getViewBlock($aParams);
  223. $this->markAsRead($iOwnerId, $aParams);
  224. return ['content' => $sContent];
  225. }
  226. /**
  227. * @page service Service Calls
  228. * @section bx_notifications Notifications
  229. * @subsection bx_notifications-browsing Browsing
  230. * @subsubsection bx_notifications-get_event_by_id get_event_by_id
  231. *
  232. * @code bx_srv('bx_notifications', 'get_event_by_id', [...]); @endcode
  233. *
  234. * Get event's data by event ID.
  235. *
  236. * @return an array with event's data.
  237. *
  238. * @see BxNtfsModule::serviceGetEventById
  239. */
  240. /**
  241. * @ref bx_notifications-get_event_by_id "get_event_by_id"
  242. */
  243. public function serviceGetEventById($iId)
  244. {
  245. $aParams = array(
  246. 'browse' => 'id',
  247. 'value' => $iId,
  248. );
  249. return $this->_oDb->getEvents($aParams);
  250. }
  251. /**
  252. * @page service Service Calls
  253. * @section bx_notifications Notifications
  254. * @subsection bx_notifications-other Other
  255. * @subsubsection bx_notifications-get_notifications get_notifications
  256. *
  257. * @code bx_srv('bx_notifications', 'get_notifications', [...]); @endcode
  258. *
  259. * Get notifications by params.
  260. *
  261. * @return array with notifications.
  262. *
  263. * @see BxNtfsModule::serviceGetNotifications
  264. */
  265. /**
  266. * @ref bx_notifications-get_notifications "get_notifications"
  267. */
  268. public function serviceGetNotifications($iOwnerId = 0, $aBrowseParams = array())
  269. {
  270. if(!$iOwnerId)
  271. $iOwnerId = $this->getUserId();
  272. if(!$iOwnerId)
  273. return 0;
  274. $aParams = $this->_prepareParams(BX_NTFS_TYPE_DEFAULT, $iOwnerId);
  275. if(!empty($aBrowseParams) && is_array($aBrowseParams))
  276. $aParams = array_merge($aParams, $aBrowseParams);
  277. $aEvents = $this->_oDb->getEvents($aParams);
  278. if($this->_oConfig->isEventsGrouped())
  279. $this->groupEvents($aEvents);
  280. // returns parsed content for React Jot
  281. if (!is_array($aBrowseParams) && $aBrowseParams === 'return_parsed_content')
  282. foreach ($aEvents as &$aContent)
  283. $this->_oTemplate->getPost($aContent, array('return_parsed_content'));
  284. return $aEvents;
  285. }
  286. /**
  287. * @page service Service Calls
  288. * @section bx_notifications Notifications
  289. * @subsection bx_notifications-other Other
  290. * @subsubsection bx_notifications-get_unread_notifications get_unread_notifications
  291. *
  292. * @code bx_srv('bx_notifications', 'get_unread_notifications', [...]); @endcode
  293. *
  294. * Get unread notifications.
  295. *
  296. * @return array with unread notifications.
  297. *
  298. * @see BxNtfsModule::serviceGetUnreadNotifications
  299. */
  300. /**
  301. * @ref bx_notifications-get_unread_notifications "get_unread_notifications"
  302. */
  303. public function serviceGetUnreadNotifications($iOwnerId = 0)
  304. {
  305. return $this->serviceGetNotifications($iOwnerId, array('new' => 1));
  306. }
  307. /**
  308. * @page service Service Calls
  309. * @section bx_notifications Notifications
  310. * @subsection bx_notifications-other Other
  311. * @subsubsection bx_notifications-get_unread_notifications_num get_unread_notifications_num
  312. *
  313. * @code bx_srv('bx_notifications', 'get_unread_notifications_num', [...]); @endcode
  314. *
  315. * Get number of unread notifications.
  316. *
  317. * @return integer value with number of unread notifications.
  318. *
  319. * @see BxNtfsModule::serviceGetUnreadNotificationsNum
  320. */
  321. /**
  322. * @ref bx_notifications-get_unread_notifications_num "get_unread_notifications_num"
  323. */
  324. public function serviceGetUnreadNotificationsNum($iOwnerId = 0)
  325. {
  326. if(!$iOwnerId)
  327. $iOwnerId = $this->getUserId();
  328. if(!$iOwnerId)
  329. return 0;
  330. $sModule = $this->_oConfig->getName();
  331. $aBrowseParams = $this->_prepareParams(BX_NTFS_TYPE_DEFAULT, $iOwnerId, 0, PHP_INT_MAX);
  332. $sParamCheck = 'perform_privacy_check';
  333. $sParamCheckFor = 'perform_privacy_check_for';
  334. $iEvents = 0;
  335. if($this->_oConfig->isEventsGrouped()) {
  336. $aEvents = $this->_oDb->getEvents($aBrowseParams);
  337. $this->groupEvents($aEvents);
  338. $iCount = 0;
  339. $iLastRead = $this->_oDb->getLastRead((int)$aBrowseParams['owner_id']);
  340. foreach($aEvents as $aEvent)
  341. if($aEvent['id'] > $iLastRead)
  342. $iCount++;
  343. $aEvents = array_slice($aEvents, 0, $iCount);
  344. }
  345. else {
  346. $aBrowseParams = array_merge($aBrowseParams, [
  347. 'new' => 1,
  348. ]);
  349. $aEvents = $this->_oDb->getEvents($aBrowseParams);
  350. }
  351. foreach($aEvents as $aEvent) {
  352. if(isset($aBrowseParams[$sParamCheck]) && $aBrowseParams[$sParamCheck] !== true)
  353. continue;
  354. $iViewerId = !empty($aBrowseParams[$sParamCheckFor]) ? (int)$aBrowseParams[$sParamCheckFor] : 0;
  355. $oPrivacyInt = BxDolPrivacy::getObjectInstance($this->_oConfig->getObject('privacy_view'));
  356. if(!$oPrivacyInt->check($aEvent['id'], $iViewerId))
  357. continue;
  358. $oPrivacyExt = $this->_oConfig->getPrivacyObject($aEvent['type'] . '_' . $aEvent['action']);
  359. if($oPrivacyExt !== false && !$oPrivacyExt->check($aEvent['id'], $iViewerId))
  360. continue;
  361. $sSrvModule = $this->_oConfig->getContentModule($aEvent);
  362. $sSrvMethod = 'check_allowed_with_content_for_profile';
  363. if($sSrvModule && BxDolRequest::serviceExists($sSrvModule, $sSrvMethod) && BxDolService::call($sSrvModule, $sSrvMethod, array('view', $this->_oConfig->getContentObjectId($aEvent), $iViewerId)) !== CHECK_ACTION_RESULT_ALLOWED)
  364. continue;
  365. $bEventCanceled = false;
  366. bx_alert($sModule, 'is_notification', 0, 0, [
  367. 'event' => &$aEvent,
  368. 'event_canceled' => &$bEventCanceled,
  369. 'browse_params' => $aBrowseParams
  370. ]);
  371. if($bEventCanceled)
  372. continue;
  373. $iEvents++;
  374. }
  375. return $iEvents;
  376. }
  377. public function serviceGetUnreadNotificationsNumEx($iOwnerId, $iCount)
  378. {
  379. header('Content-Type: text/event-stream');
  380. header('Cache-Control: no-cache');
  381. $iTmp = $this->serviceGetUnreadNotificationsNum($iOwnerId);
  382. if ($iCount != $iTmp){
  383. $time = date('r');
  384. echo "id: " . time() . "\n";
  385. echo "data: $iTmp\n\n";
  386. flush();
  387. }
  388. exit();
  389. }
  390. /**
  391. * @page service Service Calls
  392. * @section bx_notifications Notifications
  393. * @subsection bx_notifications-other Other
  394. * @subsubsection bx_notifications-get_live_updates get_live_updates
  395. *
  396. * @code bx_srv('bx_notifications', 'get_live_updates', [...]); @endcode
  397. *
  398. * Get data for Live Updates system.
  399. *
  400. * @return an array with special format.
  401. *
  402. * @see BxNtfsModule::serviceGetLiveUpdates
  403. */
  404. /**
  405. * @ref bx_notifications-get_live_updates "get_live_updates"
  406. */
  407. public function serviceGetLiveUpdates($aMenuItemParent, $aMenuItemChild, $iCount = 0)
  408. {
  409. $iOwnerId = $this->getUserId();
  410. $iCountNew = $this->serviceGetUnreadNotificationsNum($iOwnerId);
  411. if($iCountNew == $iCount)
  412. return false;
  413. return array(
  414. 'count' => $iCountNew, // required
  415. 'method' => 'bx_menu_show_live_update(oData)', // required
  416. 'data' => array(
  417. 'code' => BxDolTemplate::getInstance()->parseHtmlByTemplateName('menu_item_addon', array(
  418. 'content' => '{count}'
  419. )),
  420. 'mi_parent' => $aMenuItemParent,
  421. 'mi_child' => $aMenuItemChild
  422. ), // optional, may have some additional data to be passed in JS method provided using 'method' param above.
  423. );
  424. }
  425. /*
  426. * COMMON METHODS
  427. */
  428. public function getBlockSettings($sDeliveryType, $aParams = array())
  429. {
  430. return $this->_oTemplate->getSettingsBlock($sDeliveryType, $aParams);
  431. }
  432. public function setSubmenu($sSelected)
  433. {
  434. $oSubmenuSystem = BxDolMenu::getObjectInstance('sys_site_submenu');
  435. if(!$oSubmenuSystem)
  436. return;
  437. $CNF = &$this->_oConfig->CNF;
  438. $oSubmenuSystem->setObjectSubmenu($CNF['OBJECT_MENU_SUBMENU'], array (
  439. 'title' => _t('_bx_ntfs'),
  440. 'link' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME'])),
  441. 'icon' => '',
  442. ));
  443. $oSubmenuModule = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SUBMENU']);
  444. if($oSubmenuModule)
  445. $oSubmenuModule->setSelected($this->_oConfig->getName(), $sSelected);
  446. }
  447. public function onPost($iId)
  448. {
  449. //--- Event -> Post for Alerts Engine ---//
  450. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'post', $iId);
  451. $oAlert->alert();
  452. //--- Event -> Post for Alerts Engine ---//
  453. }
  454. public function enableSettingsLike($iId, $bValue, $bAdministration = false)
  455. {
  456. $aSetting = $this->_oDb->getSetting(array(
  457. 'by' => $bAdministration ? 'id' : 'tsu_id',
  458. 'id' => (int)$iId
  459. ));
  460. if(empty($aSetting) || !is_array($aSetting))
  461. return false;
  462. $aSettingsIds = $this->_oDb->getSetting(array(
  463. 'by' => 'group_type_delivery',
  464. 'group' => $aSetting['group'],
  465. 'delivery' => $aSetting['delivery'],
  466. 'type' => $aSetting['type'],
  467. 'active' => !$bAdministration
  468. ));
  469. if(empty($aSettingsIds) || !is_array($aSettingsIds))
  470. return false;
  471. $iUserId = bx_get_logged_profile_id();
  472. $mixedResult = false;
  473. if($bAdministration)
  474. $mixedResult = $this->_oDb->activateSettingById($bValue, $aSettingsIds);
  475. else
  476. $mixedResult = $this->_oDb->activateSettingByIdUser($bValue, $iUserId, $aSettingsIds);
  477. return $mixedResult;
  478. }
  479. public function _changeSettingsValueLike($iId, $sField, $mixedValue, $bAdministration = false)
  480. {
  481. $aSetting = $this->_oDb->getSetting(array(
  482. 'by' => $bAdministration ? 'id' : 'tsu_id',
  483. 'id' => (int)$iId
  484. ));
  485. if(empty($aSetting) || !is_array($aSetting))
  486. return false;
  487. $aSettingsIds = $this->_oDb->getSetting(array(
  488. 'by' => 'group_type_delivery',
  489. 'group' => $aSetting['group'],
  490. 'delivery' => $aSetting['delivery'],
  491. 'type' => $aSetting['type'],
  492. 'active' => !$bAdministration
  493. ));
  494. if(empty($aSettingsIds) || !is_array($aSettingsIds))
  495. return false;
  496. $iUserId = bx_get_logged_profile_id();
  497. $mixedResult = false;
  498. if($bAdministration)
  499. $mixedResult = $this->_oDb->changeSettingById($sField, $mixedValue, $aSettingsIds);
  500. else
  501. $mixedResult = $this->_oDb->changeSettingByIdUser($sField, $mixedValue, $iUserId, $aSettingsIds);
  502. return $mixedResult;
  503. }
  504. public function sendNotificationEmail($iProfile, $aNotification)
  505. {
  506. if(!$iProfile)
  507. return false;
  508. $aSettings = &$aNotification['settings'];
  509. $sTemplate = !empty($aSettings['template']) ? $aSettings['template'] : 'bx_notifications_new_event';
  510. $aTemplateMarkers = ['subject' => $aNotification['subject'], 'content' => $aNotification['content']];
  511. if(!empty($aSettings['markers']) && is_array($aSettings['markers']))
  512. $aTemplateMarkers = array_merge($aTemplateMarkers, $aSettings['markers']);
  513. $aTemplate = null;
  514. bx_alert($this->_aModule['name'], 'before_parse_email_template', 0, 0, array('profile_id' => $iProfile, 'template' => $sTemplate, 'markers' => $aTemplateMarkers, 'notification' => $aNotification, 'override_result' => &$aTemplate));
  515. if(is_null($aTemplate))
  516. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate($sTemplate, $aTemplateMarkers, 0, $iProfile);
  517. if(!$aTemplate)
  518. return false;
  519. $sEmail = BxDolProfileQuery::getInstance()->getEmailById($iProfile);
  520. $sSubject = !empty($aSettings['subject']) ? $aSettings['subject'] : $aTemplate['Subject'];
  521. return sendMail($sEmail, $sSubject, $aTemplate['Body'], 0, array(), BX_EMAIL_NOTIFY, 'html', false, array(), true);
  522. }
  523. public function sendNotificationPush($iProfile, $aNotification)
  524. {
  525. if(!$iProfile)
  526. return false;
  527. $aContent = &$aNotification['content'];
  528. $aSettings = &$aNotification['settings'];
  529. $sSubject = !empty($aSettings['subject']) ? $aSettings['subject'] : _t('_bx_ntfs_push_new_event_subject', getParam('site_title'));
  530. bx_alert($this->_aModule['name'], 'before_send_notification_push', 0, 0, array('profile_id' => $iProfile, 'content' => &$aContent, 'setting' => &$aSettings, 'subject' => &$sSubject));
  531. return BxDolPush::getInstance()->send($iProfile, array(
  532. 'contents' => array(
  533. 'en' => $aContent['message']
  534. ),
  535. 'headings' => array(
  536. 'en' => $sSubject
  537. ),
  538. 'url' => $aContent['url'],
  539. 'icon' => $aContent['icon']
  540. ), true);
  541. }
  542. public function groupEvents(&$aEvents)
  543. {
  544. //--- Check for Visual Grouping
  545. $aGroups = [];
  546. foreach($aEvents as $iIndex => $aEvent) {
  547. if(empty($aEvent['source']))
  548. continue;
  549. $sSource = $aEvent['source'];
  550. if(!isset($aGroups[$sSource]))
  551. $aGroups[$sSource] = [];
  552. $aGroups[$sSource][$iIndex] = $aEvent['priority'];
  553. }
  554. //--- Perform Visual Grouping
  555. foreach($aGroups as $sSource => $aGroup) {
  556. if(!is_array($aGroup) || count($aGroup) < 2)
  557. continue;
  558. arsort($aGroup);
  559. $iPriorityMax = reset($aGroup);
  560. foreach($aGroup as $iIndex => $iPriority)
  561. if($iPriority != $iPriorityMax)
  562. unset($aEvents[$iIndex]);
  563. }
  564. }
  565. public function serviceMarkAsRead()
  566. {
  567. $iOwnerId = bx_get_logged_profile_id();
  568. $aParams = $this->_prepareParams(BX_NTFS_TYPE_OBJECT_OWNER_AND_CONNECTIONS, $iOwnerId);
  569. $this->markAsRead($iOwnerId, $aParams);
  570. }
  571. public function markAsRead($iOwnerId, $aParams)
  572. {
  573. $aParams['browse'] = 'first';
  574. $aEvent = $this->_oDb->getEvents($aParams);
  575. if(empty($aEvent) || !is_array($aEvent))
  576. return false;
  577. return $this->_oDb->markAsRead($iOwnerId, $aEvent['id']) !== false;
  578. }
  579. /*
  580. * INTERNAL METHODS
  581. */
  582. protected function _prepareParams($sType = '', $iOwnerId = 0, $iStart = -1, $iPerPage = -1, $aModules = [])
  583. {
  584. $iUserId = $this->getUserId();
  585. $iOwnerId = (int)$iOwnerId != 0 ? $iOwnerId : $iUserId;
  586. $aParams = [
  587. 'browse' => 'list',
  588. 'type' => !empty($sType) ? $sType : BX_NTFS_TYPE_DEFAULT,
  589. 'owner_id' => $iOwnerId,
  590. 'start' => (int)$iStart > 0 ? $iStart : 0,
  591. 'start_from_item' => false,
  592. 'per_page' => (int)$iPerPage > 0 ? $iPerPage : $this->_oConfig->getPerPage(),
  593. 'modules' => is_array($aModules) && !empty($aModules) ? $aModules : [],
  594. 'last_read' => $this->_oDb->getLastRead($iOwnerId),
  595. 'viewer_id' => $iUserId,
  596. 'active' => 1
  597. ];
  598. return $aParams;
  599. }
  600. protected function _prepareParamsGet()
  601. {
  602. $aParams = [];
  603. $aParams['browse'] = 'list';
  604. $sType = bx_get('type');
  605. $aParams['type'] = $sType !== false ? bx_process_input($sType, BX_DATA_TEXT) : BX_NTFS_TYPE_DEFAULT;
  606. $aParams['owner_id'] = $sType !== false ? bx_process_input(bx_get('owner_id'), BX_DATA_INT) : $this->getUserId();
  607. $iStart = bx_get('start');
  608. $aParams['start'] = $iStart !== false ? bx_process_input($iStart, BX_DATA_INT) : 0;
  609. $aParams['start_from_item'] = false;
  610. $iPerPage = bx_get('per_page');
  611. $aParams['per_page'] = $iPerPage !== false ? bx_process_input($iPerPage, BX_DATA_INT) : $this->_oConfig->getPerPage();
  612. $aModules = bx_get('modules');
  613. $aParams['modules'] = $aModules !== false ? bx_process_input($aModules, BX_DATA_TEXT) : [];
  614. $iViewerId = $this->getUserId();
  615. if(($aParams['viewer_id'] = bx_get('viewer_id')) !== false)
  616. $aParams['viewer_id'] = bx_process_input($aParams['viewer_id'], BX_DATA_INT);
  617. if(!$aParams['viewer_id'] || $aParams['viewer_id'] != $iViewerId)
  618. $aParams['viewer_id'] = $iViewerId;
  619. $aParams['active'] = 1;
  620. return $aParams;
  621. }
  622. }
  623. /** @} */