BxNtfsModule.php 30 KB

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