BxNtfsModule.php 31 KB

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