BxNtfsModule.php 26 KB

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