BxNtfsModule.php 30 KB

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