BxNtfsModule.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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. 'EnableSetting' => '',
  96. 'ChangeSetting' => '',
  97. ];
  98. }
  99. /**
  100. * @page service Service Calls
  101. * @section bx_notifications Invitations
  102. * @subsection bx_notifications-other Other
  103. * @subsubsection bx_notifications-get_include get_include
  104. *
  105. * @code bx_srv('bx_notifications', 'get_include', [...]); @endcode
  106. *
  107. * Get all necessary CSS and JS files to include in a page.
  108. *
  109. * @return string with all necessary CSS and JS files.
  110. *
  111. * @see BxInvModule::serviceGetInclude
  112. */
  113. /**
  114. * @ref bx_notifications-get_include "get_include"
  115. */
  116. public function serviceGetInclude($bIncludeCss = true, $mixedIncludeJs = false)
  117. {
  118. return $this->_oTemplate->getInclude($bIncludeCss, $mixedIncludeJs);
  119. }
  120. /**
  121. * @page service Service Calls
  122. * @section bx_notifications Notifications
  123. * @subsection bx_notifications-page_blocks Page Blocks
  124. * @subsubsection bx_notifications-get_block_settings get_block_settings
  125. *
  126. * @code bx_srv('bx_notifications', 'get_block_settings', [...]); @endcode
  127. *
  128. * Get Settings block for a separate page.
  129. *
  130. * @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.
  131. *
  132. * @see BxNtfsModule::serviceGetBlockSettings
  133. */
  134. /**
  135. * @ref bx_notifications-get_block_settings "get_block_settings"
  136. */
  137. public function serviceGetBlockSettings($sDeliveryType = '', $aParams = array())
  138. {
  139. if(!isLogged())
  140. return '';
  141. if(empty($sDeliveryType) && bx_get('delivery') !== false)
  142. $sDeliveryType = bx_process_input(bx_get('delivery'));
  143. if(empty($sDeliveryType))
  144. $sDeliveryType = BX_BASE_MOD_NTFS_DTYPE_SITE;
  145. $iUserId = bx_get_logged_profile_id();
  146. if(!empty($aParams['user_id']))
  147. $iUserId = (int)$aParams['user_id'];
  148. else
  149. $aParams['user_id'] = $iUserId;
  150. $this->_oDb->initSettingUser($iUserId);
  151. return array(
  152. 'content' => $this->getBlockSettings($sDeliveryType, $aParams),
  153. );
  154. }
  155. /**
  156. * @page service Service Calls
  157. * @section bx_notifications Notifications
  158. * @subsection bx_notifications-page_blocks Page Blocks
  159. * @subsubsection bx_notifications-get_data get_data
  160. *
  161. * @code bx_srv('bx_notifications', 'get_data', [...]); @endcode
  162. *
  163. * Get data for app
  164. *
  165. * @return an array .
  166. *
  167. * @see BxNtfsModule::serviceGetData
  168. */
  169. /**
  170. * @ref bx_notifications-get_data "get_data"
  171. * @api @ref bx_notifications-get_data "get_data"
  172. */
  173. public function serviceGetData($aParams)
  174. {
  175. if(is_string($aParams))
  176. $aParams = bx_api_get_browse_params($aParams, true);
  177. return call_user_func_array([$this, 'serviceGetBlockView'], [
  178. isset($aParams['type']) ? $aParams['type'] : '',
  179. isset($aParams['start']) ? (int)$aParams['start'] : -1,
  180. isset($aParams['per_page']) ? (int)$aParams['per_page'] : -1,
  181. isset($aParams['modules']) ? $aParams['modules'] : [],
  182. ]);
  183. }
  184. /**
  185. * @page service Service Calls
  186. * @section bx_notifications Notifications
  187. * @subsection bx_notifications-page_blocks Page Blocks
  188. * @subsubsection bx_notifications-get_block_view get_block_view
  189. *
  190. * @code bx_srv('bx_notifications', 'get_block_view', [...]); @endcode
  191. *
  192. * Get View block for a separate page. Will return a block with "Empty" message if nothing found.
  193. *
  194. * @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.
  195. *
  196. * @see BxNtfsModule::serviceGetBlockView
  197. */
  198. /**
  199. * @ref bx_notifications-get_block_view "get_block_view"
  200. */
  201. public function serviceGetBlockView($sType = '', $iStart = -1, $iPerPage = -1, $aModules = [])
  202. {
  203. $aBrowseTypes = [BX_NTFS_TYPE_OBJECT_OWNER_AND_CONNECTIONS, BX_BASE_MOD_NTFS_TYPE_CONNECTIONS, BX_BASE_MOD_NTFS_TYPE_OBJECT_OWNER];
  204. if(empty($sType)) {
  205. $mixedType = bx_get('type');
  206. if($mixedType !== false && in_array($mixedType, $aBrowseTypes))
  207. $sType = $mixedType;
  208. if(empty($sType))
  209. $sType = BX_NTFS_TYPE_DEFAULT;
  210. }
  211. $iOwnerId = $this->getUserId();
  212. if(!$iOwnerId) {
  213. $sResult = _t('_bx_ntfs_txt_msg_no_results');
  214. return $this->_bIsApi ? [bx_api_get_msg($sResult)] : ['content' => MsgBox($sResult)];
  215. }
  216. $aParams = $this->_prepareParams($sType, $iOwnerId, $iStart, $iPerPage, $aModules);
  217. if($this->_bIsApi) {
  218. $aParams['start_from_item'] = true;
  219. $aContent = defined('BX_API_PAGE') ? [] : $this->_oTemplate->getPosts($aParams);
  220. if(!empty($aContent) && is_array($aContent)) {
  221. $iContent = count($aContent);
  222. if($iContent > $aParams['per_page'])
  223. $aContent = array_slice($aContent, 0, -($iContent - $aParams['per_page']));
  224. $aParams['start'] = end($aContent)['id'];
  225. }
  226. if($aParams['start'] == 0)
  227. $this->markAsRead($iOwnerId, $aParams);
  228. return [bx_api_get_block('browse', [
  229. 'unit' => 'notifications',
  230. 'request_url' => '/api.php?r=bx_notifications/get_data/&params[]=',
  231. 'params' => $aParams,
  232. 'id' => $iOwnerId,
  233. 'data' => $aContent]),
  234. ];
  235. }
  236. $sContent = $this->_oTemplate->getViewBlock($aParams);
  237. $this->markAsRead($iOwnerId, $aParams);
  238. return ['content' => $sContent];
  239. }
  240. /**
  241. * @page service Service Calls
  242. * @section bx_notifications Notifications
  243. * @subsection bx_notifications-browsing Browsing
  244. * @subsubsection bx_notifications-get_event_by_id get_event_by_id
  245. *
  246. * @code bx_srv('bx_notifications', 'get_event_by_id', [...]); @endcode
  247. *
  248. * Get event's data by event ID.
  249. *
  250. * @return an array with event's data.
  251. *
  252. * @see BxNtfsModule::serviceGetEventById
  253. */
  254. /**
  255. * @ref bx_notifications-get_event_by_id "get_event_by_id"
  256. */
  257. public function serviceGetEventById($iId)
  258. {
  259. $aParams = array(
  260. 'browse' => 'id',
  261. 'value' => $iId,
  262. );
  263. return $this->_oDb->getEvents($aParams);
  264. }
  265. /**
  266. * @page service Service Calls
  267. * @section bx_notifications Notifications
  268. * @subsection bx_notifications-other Other
  269. * @subsubsection bx_notifications-get_notifications get_notifications
  270. *
  271. * @code bx_srv('bx_notifications', 'get_notifications', [...]); @endcode
  272. *
  273. * Get notifications by params.
  274. *
  275. * @return array with notifications.
  276. *
  277. * @see BxNtfsModule::serviceGetNotifications
  278. */
  279. /**
  280. * @ref bx_notifications-get_notifications "get_notifications"
  281. */
  282. public function serviceGetNotifications($iOwnerId = 0, $aBrowseParams = array())
  283. {
  284. if(!$iOwnerId)
  285. $iOwnerId = $this->getUserId();
  286. if(!$iOwnerId)
  287. return 0;
  288. $aParams = $this->_prepareParams(BX_NTFS_TYPE_DEFAULT, $iOwnerId);
  289. if(!empty($aBrowseParams) && is_array($aBrowseParams))
  290. $aParams = array_merge($aParams, $aBrowseParams);
  291. $aEvents = $this->_oDb->getEvents($aParams);
  292. if($this->_oConfig->isEventsGrouped())
  293. $this->groupEvents($aEvents);
  294. // returns parsed content for React Jot
  295. if (!is_array($aBrowseParams) && $aBrowseParams === 'return_parsed_content')
  296. foreach ($aEvents as &$aContent)
  297. $this->_oTemplate->getPost($aContent, array('return_parsed_content'));
  298. return $aEvents;
  299. }
  300. /**
  301. * @page service Service Calls
  302. * @section bx_notifications Notifications
  303. * @subsection bx_notifications-other Other
  304. * @subsubsection bx_notifications-get_unread_notifications get_unread_notifications
  305. *
  306. * @code bx_srv('bx_notifications', 'get_unread_notifications', [...]); @endcode
  307. *
  308. * Get unread notifications.
  309. *
  310. * @return array with unread notifications.
  311. *
  312. * @see BxNtfsModule::serviceGetUnreadNotifications
  313. */
  314. /**
  315. * @ref bx_notifications-get_unread_notifications "get_unread_notifications"
  316. */
  317. public function serviceGetUnreadNotifications($iOwnerId = 0)
  318. {
  319. return $this->serviceGetNotifications($iOwnerId, array('new' => 1));
  320. }
  321. /**
  322. * @page service Service Calls
  323. * @section bx_notifications Notifications
  324. * @subsection bx_notifications-other Other
  325. * @subsubsection bx_notifications-get_unread_notifications_num get_unread_notifications_num
  326. *
  327. * @code bx_srv('bx_notifications', 'get_unread_notifications_num', [...]); @endcode
  328. *
  329. * Get number of unread notifications.
  330. *
  331. * @return integer value with number of unread notifications.
  332. *
  333. * @see BxNtfsModule::serviceGetUnreadNotificationsNum
  334. */
  335. /**
  336. * @ref bx_notifications-get_unread_notifications_num "get_unread_notifications_num"
  337. */
  338. public function serviceGetUnreadNotificationsNum($iOwnerId = 0)
  339. {
  340. if(!$iOwnerId)
  341. $iOwnerId = $this->getUserId();
  342. if(!$iOwnerId)
  343. return 0;
  344. $sModule = $this->_oConfig->getName();
  345. $aBrowseParams = $this->_prepareParams(BX_NTFS_TYPE_DEFAULT, $iOwnerId, 0, PHP_INT_MAX);
  346. $sParamCheck = 'perform_privacy_check';
  347. $sParamCheckFor = 'perform_privacy_check_for';
  348. $iEvents = 0;
  349. if($this->_oConfig->isEventsGrouped()) {
  350. $aEvents = $this->_oDb->getEvents($aBrowseParams);
  351. $this->groupEvents($aEvents);
  352. $iCount = 0;
  353. $iLastRead = $this->_oDb->getLastRead((int)$aBrowseParams['owner_id']);
  354. foreach($aEvents as $aEvent)
  355. if($aEvent['id'] > $iLastRead)
  356. $iCount++;
  357. $aEvents = array_slice($aEvents, 0, $iCount);
  358. }
  359. else {
  360. $aBrowseParams = array_merge($aBrowseParams, [
  361. 'new' => 1,
  362. ]);
  363. $aEvents = $this->_oDb->getEvents($aBrowseParams);
  364. }
  365. foreach($aEvents as $aEvent) {
  366. if(isset($aBrowseParams[$sParamCheck]) && $aBrowseParams[$sParamCheck] !== true)
  367. continue;
  368. $iViewerId = !empty($aBrowseParams[$sParamCheckFor]) ? (int)$aBrowseParams[$sParamCheckFor] : 0;
  369. $oPrivacyInt = BxDolPrivacy::getObjectInstance($this->_oConfig->getObject('privacy_view'));
  370. if(!$oPrivacyInt->check($aEvent['id'], $iViewerId))
  371. continue;
  372. $oPrivacyExt = $this->_oConfig->getPrivacyObject($aEvent['type'] . '_' . $aEvent['action']);
  373. if($oPrivacyExt !== false && !$oPrivacyExt->check($aEvent['id'], $iViewerId))
  374. continue;
  375. $iContentObjectId = $this->_oConfig->getContentObjectId($aEvent);
  376. if($iContentObjectId === false) {
  377. $this->_oTemplate->getPost($aEvent, $aBrowseParams);
  378. $iContentObjectId = $this->_oConfig->getContentObjectId($aEvent);
  379. }
  380. $sSrvModule = $this->_oConfig->getContentModule($aEvent);
  381. $sSrvMethod = 'check_allowed_with_content_for_profile';
  382. if($sSrvModule && bx_is_srv($sSrvModule, $sSrvMethod) && bx_srv($sSrvModule, $sSrvMethod, ['view', $iContentObjectId, $iViewerId]) !== CHECK_ACTION_RESULT_ALLOWED)
  383. continue;
  384. $bEventCanceled = false;
  385. /**
  386. * @hooks
  387. * @hookdef hook-bx_notifications-is_notification 'bx_notifications', 'is_notification' - hook to override checking if an event is available or not
  388. * - $unit_name - equals `bx_notifications`
  389. * - $action - equals `is_notification`
  390. * - $object_id - not used
  391. * - $sender_id - not used
  392. * - $extra_params - array of additional params with the following array keys:
  393. * - `browse_params` - [array] browse params array as key&value pairs
  394. * - `event` - [array] by ref, array with event data as key&value pairs, can be overridden in hook processing
  395. * - `event_canceled` - [boolean] by ref, if event is canceled (not available) or not, can be overridden in hook processing
  396. * @hook @ref hook-bx_notifications-is_notification
  397. */
  398. bx_alert($sModule, 'is_notification', 0, 0, [
  399. 'browse_params' => $aBrowseParams,
  400. 'event' => &$aEvent,
  401. 'event_canceled' => &$bEventCanceled
  402. ]);
  403. if($bEventCanceled)
  404. continue;
  405. $iEvents++;
  406. }
  407. return $iEvents;
  408. }
  409. public function serviceGetUnreadNotificationsNumEx($iOwnerId, $iCount)
  410. {
  411. header('Content-Type: text/event-stream');
  412. header('Cache-Control: no-cache');
  413. $iTmp = $this->serviceGetUnreadNotificationsNum($iOwnerId);
  414. if ($iCount != $iTmp){
  415. $time = date('r');
  416. echo "id: " . time() . "\n";
  417. echo "data: $iTmp\n\n";
  418. flush();
  419. }
  420. exit();
  421. }
  422. /**
  423. * @page service Service Calls
  424. * @section bx_notifications Notifications
  425. * @subsection bx_notifications-other Other
  426. * @subsubsection bx_notifications-get_live_updates get_live_updates
  427. *
  428. * @code bx_srv('bx_notifications', 'get_live_updates', [...]); @endcode
  429. *
  430. * Get data for Live Updates system.
  431. *
  432. * @return an array with special format.
  433. *
  434. * @see BxNtfsModule::serviceGetLiveUpdates
  435. */
  436. /**
  437. * @ref bx_notifications-get_live_updates "get_live_updates"
  438. */
  439. public function serviceGetLiveUpdates($aMenuItemParent, $aMenuItemChild, $iCount = 0)
  440. {
  441. $iOwnerId = $this->getUserId();
  442. $iCountNew = $this->serviceGetUnreadNotificationsNum($iOwnerId);
  443. if($iCountNew == $iCount)
  444. return false;
  445. return array(
  446. 'count' => $iCountNew, // required
  447. 'method' => 'bx_menu_show_live_update(oData)', // required
  448. 'data' => array(
  449. 'code' => BxDolTemplate::getInstance()->parseHtmlByTemplateName('menu_item_addon', array(
  450. 'content' => '{count}'
  451. )),
  452. 'mi_parent' => $aMenuItemParent,
  453. 'mi_child' => $aMenuItemChild
  454. ), // optional, may have some additional data to be passed in JS method provided using 'method' param above.
  455. );
  456. }
  457. /*
  458. * COMMON METHODS
  459. */
  460. public function getBlockSettings($sDeliveryType, $aParams = array())
  461. {
  462. return $this->_oTemplate->getSettingsBlock($sDeliveryType, $aParams);
  463. }
  464. public function setSubmenu($sSelected)
  465. {
  466. $oSubmenuSystem = BxDolMenu::getObjectInstance('sys_site_submenu');
  467. if(!$oSubmenuSystem)
  468. return;
  469. $CNF = &$this->_oConfig->CNF;
  470. $oSubmenuSystem->setObjectSubmenu($CNF['OBJECT_MENU_SUBMENU'], array (
  471. 'title' => _t('_bx_ntfs'),
  472. 'link' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME'])),
  473. 'icon' => '',
  474. ));
  475. $oSubmenuModule = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SUBMENU']);
  476. if($oSubmenuModule)
  477. $oSubmenuModule->setSelected($this->_oConfig->getName(), $sSelected);
  478. }
  479. public function onPost($iId)
  480. {
  481. //--- Event -> Post for Alerts Engine ---//
  482. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'post', $iId);
  483. $oAlert->alert();
  484. //--- Event -> Post for Alerts Engine ---//
  485. }
  486. public function enableSettingsLike($iId, $bValue, $bAdministration = false)
  487. {
  488. $aSetting = $this->_oDb->getSetting(array(
  489. 'by' => $bAdministration ? 'id' : 'tsu_id',
  490. 'id' => (int)$iId
  491. ));
  492. if(empty($aSetting) || !is_array($aSetting))
  493. return false;
  494. $aSettingsIds = $this->_oDb->getSetting(array(
  495. 'by' => 'group_type_delivery',
  496. 'group' => $aSetting['group'],
  497. 'delivery' => $aSetting['delivery'],
  498. 'type' => $aSetting['type'],
  499. 'active' => !$bAdministration
  500. ));
  501. if(empty($aSettingsIds) || !is_array($aSettingsIds))
  502. return false;
  503. $iUserId = bx_get_logged_profile_id();
  504. $mixedResult = false;
  505. if($bAdministration)
  506. $mixedResult = $this->_oDb->activateSettingById($bValue, $aSettingsIds);
  507. else
  508. $mixedResult = $this->_oDb->activateSettingByIdUser($bValue, $iUserId, $aSettingsIds);
  509. return $mixedResult;
  510. }
  511. public function changeSettingsValueLike($iId, $sField, $mixedValue, $bAdministration = false)
  512. {
  513. $aSetting = $this->_oDb->getSetting(array(
  514. 'by' => $bAdministration ? 'id' : 'tsu_id',
  515. 'id' => (int)$iId
  516. ));
  517. if(empty($aSetting) || !is_array($aSetting))
  518. return false;
  519. $aSettingsIds = $this->_oDb->getSetting(array(
  520. 'by' => 'group_type_delivery',
  521. 'group' => $aSetting['group'],
  522. 'delivery' => $aSetting['delivery'],
  523. 'type' => $aSetting['type'],
  524. 'active' => !$bAdministration
  525. ));
  526. if(empty($aSettingsIds) || !is_array($aSettingsIds))
  527. return false;
  528. $iUserId = bx_get_logged_profile_id();
  529. $mixedResult = false;
  530. if($bAdministration)
  531. $mixedResult = $this->_oDb->changeSettingById($sField, $mixedValue, $aSettingsIds);
  532. else
  533. $mixedResult = $this->_oDb->changeSettingByIdUser($sField, $mixedValue, $iUserId, $aSettingsIds);
  534. return $mixedResult;
  535. }
  536. public function sendNotificationEmail($iProfile, $aNotification)
  537. {
  538. if(!$iProfile)
  539. return false;
  540. $aSettings = &$aNotification['settings'];
  541. $sTemplate = !empty($aSettings['template']) ? $aSettings['template'] : 'bx_notifications_new_event';
  542. $aTemplateMarkers = ['subject' => $aNotification['subject'], 'content' => $aNotification['content']];
  543. if(!empty($aSettings['markers']) && is_array($aSettings['markers']))
  544. $aTemplateMarkers = array_merge($aTemplateMarkers, $aSettings['markers']);
  545. $aTemplate = null;
  546. /**
  547. * @hooks
  548. * @hookdef hook-bx_notifications-before_parse_email_template 'bx_notifications', 'before_parse_email_template' - hook to override email template
  549. * - $unit_name - equals `bx_notifications`
  550. * - $action - equals `before_parse_email_template`
  551. * - $object_id - not used
  552. * - $sender_id - not used
  553. * - $extra_params - array of additional params with the following array keys:
  554. * - `profile_id` - [int] recipient profile id
  555. * - `template` - [string] email template name
  556. * - `markers` - [array] parsable variables array as key&value pairs
  557. * - `notification` - [array] notification data array as key&value pairs
  558. * - `override_result` - [array] by ref, email template (subject and body), if null, then default one will be used, can be overridden in hook processing
  559. * @hook @ref hook-bx_notifications-before_parse_email_template
  560. */
  561. bx_alert($this->_aModule['name'], 'before_parse_email_template', 0, 0, [
  562. 'profile_id' => $iProfile,
  563. 'template' => $sTemplate,
  564. 'markers' => $aTemplateMarkers,
  565. 'notification' => $aNotification,
  566. 'override_result' => &$aTemplate
  567. ]);
  568. if(is_null($aTemplate))
  569. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate($sTemplate, $aTemplateMarkers, 0, $iProfile);
  570. if(!$aTemplate)
  571. return false;
  572. $sEmail = BxDolProfileQuery::getInstance()->getEmailById($iProfile);
  573. $sSubject = !empty($aSettings['subject']) ? $aSettings['subject'] : $aTemplate['Subject'];
  574. return sendMail($sEmail, $sSubject, $aTemplate['Body'], 0, array(), BX_EMAIL_NOTIFY, 'html', false, array(), true);
  575. }
  576. public function sendNotificationPush($iProfile, $aNotification)
  577. {
  578. if(!$iProfile)
  579. return false;
  580. $aContent = &$aNotification['content'];
  581. $aSettings = &$aNotification['settings'];
  582. /**
  583. * @hooks
  584. * @hookdef hook-bx_notifications-before_send_notification_push 'bx_notifications', 'before_send_notification_push' - hook to override push message
  585. * - $unit_name - equals `bx_notifications`
  586. * - $action - equals `before_send_notification_push`
  587. * - $object_id - not used
  588. * - $sender_id - not used
  589. * - $extra_params - array of additional params with the following array keys:
  590. * - `profile_id` - [int] recipient profile id
  591. * - `content` - [array] by ref, push message params array as key&value pairs, can be overridden in hook processing
  592. * - `setting` - [array] by ref, push settings array as key&value pairs, can be overridden in hook processing
  593. * - `subject` - [string] by ref, push message subject, can be overridden in hook processing
  594. * @hook @ref hook-bx_notifications-before_send_notification_push
  595. */
  596. bx_alert($this->_aModule['name'], 'before_send_notification_push', 0, 0, [
  597. 'profile_id' => $iProfile,
  598. 'content' => &$aContent,
  599. 'setting' => &$aSettings,
  600. 'subject' => &$sSubject
  601. ]);
  602. if (!$aContent)
  603. return false;
  604. $sSubject = !empty($aSettings['subject']) ? $aSettings['subject'] : _t('_bx_ntfs_push_new_event_subject', getParam('site_title'));
  605. return BxDolPush::getInstance()->send($iProfile, array(
  606. 'contents' => array(
  607. 'en' => $aContent['message']
  608. ),
  609. 'headings' => array(
  610. 'en' => $sSubject
  611. ),
  612. 'url' => $aContent['url'],
  613. 'icon' => $aContent['icon']
  614. ), true);
  615. }
  616. public function groupEvents(&$aEvents)
  617. {
  618. //--- Check for Visual Grouping
  619. $aGroups = [];
  620. foreach($aEvents as $iIndex => $aEvent) {
  621. if(empty($aEvent['source']))
  622. continue;
  623. $sSource = $aEvent['source'];
  624. if(!isset($aGroups[$sSource]))
  625. $aGroups[$sSource] = [];
  626. $aGroups[$sSource][$iIndex] = $aEvent['priority'];
  627. }
  628. //--- Perform Visual Grouping
  629. foreach($aGroups as $sSource => $aGroup) {
  630. if(!is_array($aGroup) || count($aGroup) < 2)
  631. continue;
  632. arsort($aGroup);
  633. $iPriorityMax = reset($aGroup);
  634. foreach($aGroup as $iIndex => $iPriority)
  635. if($iPriority != $iPriorityMax)
  636. unset($aEvents[$iIndex]);
  637. }
  638. }
  639. public function serviceMarkAsRead()
  640. {
  641. $iOwnerId = bx_get_logged_profile_id();
  642. $aParams = $this->_prepareParams(BX_NTFS_TYPE_OBJECT_OWNER_AND_CONNECTIONS, $iOwnerId);
  643. $this->markAsRead($iOwnerId, $aParams);
  644. }
  645. public function markAsRead($iOwnerId, $aParams)
  646. {
  647. $aParams['browse'] = 'first';
  648. $aEvent = $this->_oDb->getEvents($aParams);
  649. if(empty($aEvent) || !is_array($aEvent))
  650. return false;
  651. return $this->_oDb->markAsRead($iOwnerId, $aEvent['id']) !== false;
  652. }
  653. public function serviceEnableSetting($mixedParams)
  654. {
  655. if(!is_array($mixedParams))
  656. $mixedParams = json_decode($mixedParams, true);
  657. if(!isset($mixedParams['id'], $mixedParams['value']))
  658. return false;
  659. $iId = (int)$mixedParams['id'];
  660. $iValue = (int)$mixedParams['value'];
  661. $bAdministration = isset($mixedParams['admin']) && $mixedParams['admin'];
  662. if(!$this->_oConfig->isSettingsGrouped())
  663. $mixedResult = $this->_oDb->updateSetting(['active' => $iValue], ['id' => $iId]);
  664. else
  665. $mixedResult = $this->enableSettingsLike($iId, $iValue, $bAdministration);
  666. return $mixedResult !== false;
  667. }
  668. public function serviceChangeSetting($mixedParams)
  669. {
  670. if(!is_array($mixedParams))
  671. $mixedParams = json_decode($mixedParams, true);
  672. if(!isset($mixedParams['id'], $mixedParams['value']))
  673. return false;
  674. $iId = (int)$mixedParams['id'];
  675. $iValue = (int)$mixedParams['value'];
  676. if(!$this->_oConfig->isSettingsGrouped())
  677. $mixedResult = $this->_oDb->updateSetting(['value' => $iValue], ['id' => $iId]);
  678. else
  679. $mixedResult = $this->changeSettingsValueLike($iId, 'value', $iValue, true);
  680. return $mixedResult !== false;
  681. }
  682. /*
  683. * INTERNAL METHODS
  684. */
  685. protected function _prepareParams($sType = '', $iOwnerId = 0, $iStart = -1, $iPerPage = -1, $aModules = [])
  686. {
  687. $iUserId = $this->getUserId();
  688. $iOwnerId = (int)$iOwnerId != 0 ? $iOwnerId : $iUserId;
  689. $aParams = [
  690. 'browse' => 'list',
  691. 'type' => !empty($sType) ? $sType : BX_NTFS_TYPE_DEFAULT,
  692. 'owner_id' => $iOwnerId,
  693. 'start' => (int)$iStart > 0 ? $iStart : 0,
  694. 'start_from_item' => false,
  695. 'per_page' => (int)$iPerPage > 0 ? $iPerPage : $this->_oConfig->getPerPage(),
  696. 'modules' => is_array($aModules) && !empty($aModules) ? $aModules : [],
  697. 'last_read' => $this->_oDb->getLastRead($iOwnerId),
  698. 'viewer_id' => $iUserId,
  699. 'active' => 1
  700. ];
  701. return $aParams;
  702. }
  703. protected function _prepareParamsGet()
  704. {
  705. $aParams = [];
  706. $aParams['browse'] = 'list';
  707. $sType = bx_get('type');
  708. $aParams['type'] = $sType !== false ? bx_process_input($sType, BX_DATA_TEXT) : BX_NTFS_TYPE_DEFAULT;
  709. $aParams['owner_id'] = $sType !== false ? bx_process_input(bx_get('owner_id'), BX_DATA_INT) : $this->getUserId();
  710. $iStart = bx_get('start');
  711. $aParams['start'] = $iStart !== false ? bx_process_input($iStart, BX_DATA_INT) : 0;
  712. $aParams['start_from_item'] = false;
  713. $iPerPage = bx_get('per_page');
  714. $aParams['per_page'] = $iPerPage !== false ? bx_process_input($iPerPage, BX_DATA_INT) : $this->_oConfig->getPerPage();
  715. $aModules = bx_get('modules');
  716. $aParams['modules'] = $aModules !== false ? bx_process_input($aModules, BX_DATA_TEXT) : [];
  717. $iViewerId = $this->getUserId();
  718. if(($aParams['viewer_id'] = bx_get('viewer_id')) !== false)
  719. $aParams['viewer_id'] = bx_process_input($aParams['viewer_id'], BX_DATA_INT);
  720. if(!$aParams['viewer_id'] || $aParams['viewer_id'] != $iViewerId)
  721. $aParams['viewer_id'] = $iViewerId;
  722. $aParams['active'] = 1;
  723. return $aParams;
  724. }
  725. }
  726. /** @} */