BxNtfsModule.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. * SLTMODE - Silent mode:
  27. * It is needed for alert sending module to tell that the alert should be ignored
  28. * with Notifications module completely or partially. Available values:
  29. * 1. disabled (global, value = 0) - all notifications are available;
  30. * 2. absolute (global, value = 1) - alert isn't registered which means that there is no notifications at all;
  31. * 3. absolute (for Notifications only, value = 11) - the same as global absolute.
  32. * 3. on-site only (value = 12) - alert is registered. It means that on-site notification is available while the others are disabled.
  33. * 4. on-site + email (value = 13) - alert is registered (on-site) and notification via 'email' is enabled.
  34. * 5. on-site + push (value = 14) - alert is registered (on-site) and notification via 'push' is enabled.
  35. *
  36. * @see BxNtfsResponse::response - 'silent_mode' parameter in Alerts Extras array.
  37. */
  38. define('BX_NTFS_SLTMODE_ABSOLUTE', 11);
  39. define('BX_NTFS_SLTMODE_SITE', 12);
  40. define('BX_NTFS_SLTMODE_SITE_EMAIL', 13);
  41. define('BX_NTFS_SLTMODE_SITE_PUSH', 14);
  42. class BxNtfsModule extends BxBaseModNotificationsModule
  43. {
  44. /**
  45. * Constructor
  46. */
  47. function __construct($aModule)
  48. {
  49. parent::__construct($aModule);
  50. $this->_iOwnerId = $this->getUserId();
  51. }
  52. /**
  53. * ACTION METHODS
  54. */
  55. function actionGetPosts()
  56. {
  57. $aParams = $this->_prepareParamsGet();
  58. if(empty($aParams['owner_id']) || (int)$aParams['owner_id'] != $this->_iOwnerId)
  59. return echoJson([]);
  60. echoJson([
  61. 'events' => $this->_oTemplate->getPosts($aParams)
  62. ]);
  63. }
  64. /**
  65. * SERVICE METHODS
  66. */
  67. public function serviceGetSafeServices()
  68. {
  69. return [
  70. 'GetBlockView' => '',
  71. ];
  72. }
  73. /**
  74. * @page service Service Calls
  75. * @section bx_notifications Invitations
  76. * @subsection bx_notifications-other Other
  77. * @subsubsection bx_notifications-get_include get_include
  78. *
  79. * @code bx_srv('bx_notifications', 'get_include', [...]); @endcode
  80. *
  81. * Get all necessary CSS and JS files to include in a page.
  82. *
  83. * @return string with all necessary CSS and JS files.
  84. *
  85. * @see BxInvModule::serviceGetInclude
  86. */
  87. /**
  88. * @ref bx_notifications-get_include "get_include"
  89. */
  90. public function serviceGetInclude($bIncludeCss = true, $mixedIncludeJs = false)
  91. {
  92. return $this->_oTemplate->getInclude($bIncludeCss, $mixedIncludeJs);
  93. }
  94. /**
  95. * @page service Service Calls
  96. * @section bx_notifications Notifications
  97. * @subsection bx_notifications-page_blocks Page Blocks
  98. * @subsubsection bx_notifications-get_block_settings get_block_settings
  99. *
  100. * @code bx_srv('bx_notifications', 'get_block_settings', [...]); @endcode
  101. *
  102. * Get Settings block for a separate page.
  103. *
  104. * @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.
  105. *
  106. * @see BxNtfsModule::serviceGetBlockSettings
  107. */
  108. /**
  109. * @ref bx_notifications-get_block_settings "get_block_settings"
  110. */
  111. public function serviceGetBlockSettings($sDeliveryType = '', $aParams = array())
  112. {
  113. if(!isLogged())
  114. return '';
  115. if(empty($sDeliveryType) && bx_get('delivery') !== false)
  116. $sDeliveryType = bx_process_input(bx_get('delivery'));
  117. if(empty($sDeliveryType))
  118. $sDeliveryType = BX_BASE_MOD_NTFS_DTYPE_SITE;
  119. $iUserId = bx_get_logged_profile_id();
  120. if(!empty($aParams['user_id']))
  121. $iUserId = (int)$aParams['user_id'];
  122. else
  123. $aParams['user_id'] = $iUserId;
  124. $this->_oDb->initSettingUser($iUserId);
  125. return array(
  126. 'content' => $this->getBlockSettings($sDeliveryType, $aParams),
  127. );
  128. }
  129. /**
  130. * @page service Service Calls
  131. * @section bx_notifications Notifications
  132. * @subsection bx_notifications-page_blocks Page Blocks
  133. * @subsubsection bx_notifications-get_block_view get_block_view
  134. *
  135. * @code bx_srv('bx_notifications', 'get_block_view', [...]); @endcode
  136. *
  137. * Get View block for a separate page. Will return a block with "Empty" message if nothing found.
  138. *
  139. * @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.
  140. *
  141. * @see BxNtfsModule::serviceGetBlockView
  142. */
  143. /**
  144. * @ref bx_notifications-get_block_view "get_block_view"
  145. */
  146. public function serviceGetBlockView($sType = '', $iStart = -1, $iPerPage = -1, $aModules = array())
  147. {
  148. $aBrowseTypes = array(BX_NTFS_TYPE_OBJECT_OWNER_AND_CONNECTIONS, BX_BASE_MOD_NTFS_TYPE_CONNECTIONS, BX_BASE_MOD_NTFS_TYPE_OBJECT_OWNER);
  149. if(empty($sType)) {
  150. $mixedType = bx_get('type');
  151. if($mixedType !== false && in_array($mixedType, $aBrowseTypes))
  152. $sType = $mixedType;
  153. if(empty($sType))
  154. $sType = BX_NTFS_TYPE_DEFAULT;
  155. }
  156. $iOwnerId = $this->getUserId();
  157. if(!$iOwnerId)
  158. return array('content' => MsgBox(_t('_bx_ntfs_txt_msg_no_results')));
  159. $aParams = $this->_prepareParams($sType, $iOwnerId, $iStart, $iPerPage, $aModules);
  160. $sContent = $this->_oTemplate->getViewBlock($aParams);
  161. $aParams['browse'] = 'first';
  162. $aEvent = $this->_oDb->getEvents($aParams);
  163. if(!empty($aEvent))
  164. $this->_oDb->markAsRead($iOwnerId, $aEvent['id']);
  165. $sModule = $this->_oConfig->getName();
  166. $sJsObject = $this->_oConfig->getJsObject('view');
  167. return array('content' => $sContent);
  168. }
  169. /**
  170. * @page service Service Calls
  171. * @section bx_notifications Notifications
  172. * @subsection bx_notifications-browsing Browsing
  173. * @subsubsection bx_notifications-get_event_by_id get_event_by_id
  174. *
  175. * @code bx_srv('bx_notifications', 'get_event_by_id', [...]); @endcode
  176. *
  177. * Get event's data by event ID.
  178. *
  179. * @return an array with event's data.
  180. *
  181. * @see BxNtfsModule::serviceGetEventById
  182. */
  183. /**
  184. * @ref bx_notifications-get_event_by_id "get_event_by_id"
  185. */
  186. public function serviceGetEventById($iId)
  187. {
  188. $aParams = array(
  189. 'browse' => 'id',
  190. 'value' => $iId,
  191. );
  192. return $this->_oDb->getEvents($aParams);
  193. }
  194. /**
  195. * @page service Service Calls
  196. * @section bx_notifications Notifications
  197. * @subsection bx_notifications-other Other
  198. * @subsubsection bx_notifications-get_notifications get_notifications
  199. *
  200. * @code bx_srv('bx_notifications', 'get_notifications', [...]); @endcode
  201. *
  202. * Get notifications by params.
  203. *
  204. * @return array with notifications.
  205. *
  206. * @see BxNtfsModule::serviceGetNotifications
  207. */
  208. /**
  209. * @ref bx_notifications-get_notifications "get_notifications"
  210. */
  211. public function serviceGetNotifications($iOwnerId = 0, $aBrowseParams = array())
  212. {
  213. if(!$iOwnerId)
  214. $iOwnerId = $this->getUserId();
  215. if(!$iOwnerId)
  216. return 0;
  217. $aParams = $this->_prepareParams(BX_NTFS_TYPE_DEFAULT, $iOwnerId);
  218. if(!empty($aBrowseParams) && is_array($aBrowseParams))
  219. $aParams = array_merge($aParams, $aBrowseParams);
  220. $aEvents = $this->_oDb->getEvents($aParams);
  221. // returns parsed content for React Jot
  222. if (!is_array($aBrowseParams) && $aBrowseParams === 'return_parsed_content')
  223. foreach ($aEvents as &$aContent)
  224. $this->_oTemplate->getPost($aContent, array('return_parsed_content'));
  225. return $aEvents;
  226. }
  227. /**
  228. * @page service Service Calls
  229. * @section bx_notifications Notifications
  230. * @subsection bx_notifications-other Other
  231. * @subsubsection bx_notifications-get_unread_notifications get_unread_notifications
  232. *
  233. * @code bx_srv('bx_notifications', 'get_unread_notifications', [...]); @endcode
  234. *
  235. * Get unread notifications.
  236. *
  237. * @return array with unread notifications.
  238. *
  239. * @see BxNtfsModule::serviceGetUnreadNotifications
  240. */
  241. /**
  242. * @ref bx_notifications-get_unread_notifications "get_unread_notifications"
  243. */
  244. public function serviceGetUnreadNotifications($iOwnerId = 0)
  245. {
  246. return $this->serviceGetNotifications($iOwnerId, array('new' => 1));
  247. }
  248. /**
  249. * @page service Service Calls
  250. * @section bx_notifications Notifications
  251. * @subsection bx_notifications-other Other
  252. * @subsubsection bx_notifications-get_unread_notifications_num get_unread_notifications_num
  253. *
  254. * @code bx_srv('bx_notifications', 'get_unread_notifications_num', [...]); @endcode
  255. *
  256. * Get number of unread notifications.
  257. *
  258. * @return integer value with number of unread notifications.
  259. *
  260. * @see BxNtfsModule::serviceGetUnreadNotificationsNum
  261. */
  262. /**
  263. * @ref bx_notifications-get_unread_notifications_num "get_unread_notifications_num"
  264. */
  265. public function serviceGetUnreadNotificationsNum($iOwnerId = 0)
  266. {
  267. if(!$iOwnerId)
  268. $iOwnerId = $this->getUserId();
  269. if(!$iOwnerId)
  270. return 0;
  271. $aParams = $this->_prepareParams(BX_NTFS_TYPE_DEFAULT, $iOwnerId, 0, PHP_INT_MAX);
  272. $aParams = array_merge($aParams, array(
  273. 'new' => 1,
  274. ));
  275. $sParamCheck = 'perform_privacy_check';
  276. $sParamCheckFor = 'perform_privacy_check_for';
  277. $iEvents = 0;
  278. $aEvents = $this->_oDb->getEvents($aParams);
  279. foreach($aEvents as $aEvent) {
  280. if(isset($aBrowseParams[$sParamCheck]) && $aBrowseParams[$sParamCheck] !== true)
  281. continue;
  282. $iViewerId = !empty($aBrowseParams[$sParamCheckFor]) ? (int)$aBrowseParams[$sParamCheckFor] : 0;
  283. $oPrivacyInt = BxDolPrivacy::getObjectInstance($this->_oConfig->getObject('privacy_view'));
  284. if(!$oPrivacyInt->check($aEvent['id'], $iViewerId))
  285. continue;
  286. $oPrivacyExt = $this->_oConfig->getPrivacyObject($aEvent['type'] . '_' . $aEvent['action']);
  287. if($oPrivacyExt !== false && !$oPrivacyExt->check($aEvent['id'], $iViewerId))
  288. continue;
  289. $sSrvModule = $this->_oConfig->getContentModule($aEvent);
  290. $sSrvMethod = 'check_allowed_with_content_for_profile';
  291. if($sSrvModule && BxDolRequest::serviceExists($sSrvModule, $sSrvMethod) && BxDolService::call($sSrvModule, $sSrvMethod, array('view', $this->_oConfig->getContentObjectId($aEvent), $iViewerId)) !== CHECK_ACTION_RESULT_ALLOWED)
  292. continue;
  293. $iEvents++;
  294. }
  295. return $iEvents;
  296. }
  297. /**
  298. * @page service Service Calls
  299. * @section bx_notifications Notifications
  300. * @subsection bx_notifications-other Other
  301. * @subsubsection bx_notifications-get_live_updates get_live_updates
  302. *
  303. * @code bx_srv('bx_notifications', 'get_live_updates', [...]); @endcode
  304. *
  305. * Get data for Live Updates system.
  306. *
  307. * @return an array with special format.
  308. *
  309. * @see BxNtfsModule::serviceGetLiveUpdates
  310. */
  311. /**
  312. * @ref bx_notifications-get_live_updates "get_live_updates"
  313. */
  314. public function serviceGetLiveUpdates($aMenuItemParent, $aMenuItemChild, $iCount = 0)
  315. {
  316. $iOwnerId = $this->getUserId();
  317. $iCountNew = $this->serviceGetUnreadNotificationsNum($iOwnerId);
  318. if($iCountNew == $iCount)
  319. return false;
  320. return array(
  321. 'count' => $iCountNew, // required
  322. 'method' => 'bx_menu_show_live_update(oData)', // required
  323. 'data' => array(
  324. 'code' => BxDolTemplate::getInstance()->parseHtmlByTemplateName('menu_item_addon', array(
  325. 'content' => '{count}'
  326. )),
  327. 'mi_parent' => $aMenuItemParent,
  328. 'mi_child' => $aMenuItemChild
  329. ), // optional, may have some additional data to be passed in JS method provided using 'method' param above.
  330. );
  331. }
  332. /*
  333. * COMMON METHODS
  334. */
  335. public function getBlockSettings($sDeliveryType, $aParams = array())
  336. {
  337. return $this->_oTemplate->getSettingsBlock($sDeliveryType, $aParams);
  338. }
  339. public function setSubmenu($sSelected)
  340. {
  341. $oSubmenuSystem = BxDolMenu::getObjectInstance('sys_site_submenu');
  342. if(!$oSubmenuSystem)
  343. return;
  344. $CNF = &$this->_oConfig->CNF;
  345. $oSubmenuSystem->setObjectSubmenu($CNF['OBJECT_MENU_SUBMENU'], array (
  346. 'title' => _t('_bx_ntfs'),
  347. 'link' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME']),
  348. 'icon' => '',
  349. ));
  350. $oSubmenuModule = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SUBMENU']);
  351. if($oSubmenuModule)
  352. $oSubmenuModule->setSelected($this->_oConfig->getName(), $sSelected);
  353. }
  354. public function onPost($iId)
  355. {
  356. //--- Event -> Post for Alerts Engine ---//
  357. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'post', $iId);
  358. $oAlert->alert();
  359. //--- Event -> Post for Alerts Engine ---//
  360. }
  361. public function enableSettingsLike($iId, $bValue, $bAdministration = false)
  362. {
  363. $aSetting = $this->_oDb->getSetting(array(
  364. 'by' => $bAdministration ? 'id' : 'tsu_id',
  365. 'id' => (int)$iId
  366. ));
  367. if(empty($aSetting) || !is_array($aSetting))
  368. return false;
  369. $aSettingsIds = $this->_oDb->getSetting(array(
  370. 'by' => 'group_type_delivery',
  371. 'group' => $aSetting['group'],
  372. 'delivery' => $aSetting['delivery'],
  373. 'type' => $aSetting['type'],
  374. 'active' => !$bAdministration
  375. ));
  376. if(empty($aSettingsIds) || !is_array($aSettingsIds))
  377. return false;
  378. $iUserId = bx_get_logged_profile_id();
  379. $mixedResult = false;
  380. if($bAdministration)
  381. $mixedResult = $this->_oDb->activateSettingById($bValue, $aSettingsIds);
  382. else
  383. $mixedResult = $this->_oDb->activateSettingByIdUser($bValue, $iUserId, $aSettingsIds);
  384. return $mixedResult;
  385. }
  386. public function _changeSettingsValueLike($iId, $sField, $mixedValue, $bAdministration = false)
  387. {
  388. $aSetting = $this->_oDb->getSetting(array(
  389. 'by' => $bAdministration ? 'id' : 'tsu_id',
  390. 'id' => (int)$iId
  391. ));
  392. if(empty($aSetting) || !is_array($aSetting))
  393. return false;
  394. $aSettingsIds = $this->_oDb->getSetting(array(
  395. 'by' => 'group_type_delivery',
  396. 'group' => $aSetting['group'],
  397. 'delivery' => $aSetting['delivery'],
  398. 'type' => $aSetting['type'],
  399. 'active' => !$bAdministration
  400. ));
  401. if(empty($aSettingsIds) || !is_array($aSettingsIds))
  402. return false;
  403. $iUserId = bx_get_logged_profile_id();
  404. $mixedResult = false;
  405. if($bAdministration)
  406. $mixedResult = $this->_oDb->changeSettingById($sField, $mixedValue, $aSettingsIds);
  407. else
  408. $mixedResult = $this->_oDb->changeSettingByIdUser($sField, $mixedValue, $iUserId, $aSettingsIds);
  409. return $mixedResult;
  410. }
  411. public function sendNotificationEmail($iProfile, $aNotification)
  412. {
  413. if(!$iProfile)
  414. return false;
  415. $aSettings = &$aNotification['settings'];
  416. $sTemplate = !empty($aSettings['template']) ? $aSettings['template'] : 'bx_notifications_new_event';
  417. $aTemplateMarkers = array('content' => $aNotification['content']);
  418. if(!empty($aSettings['markers']) && is_array($aSettings['markers']))
  419. $aTemplateMarkers = array_merge($aTemplateMarkers, $aSettings['markers']);
  420. $aTemplate = null;
  421. bx_alert($this->_aModule['name'], 'before_parse_email_template', 0, 0, array('profile_id' => $iProfile, 'template' => $sTemplate, 'markers' => $aTemplateMarkers, 'notification' => $aNotification, 'override_result' => &$aTemplate));
  422. if(is_null($aTemplate))
  423. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate($sTemplate, $aTemplateMarkers, 0, $iProfile);
  424. if(!$aTemplate)
  425. return false;
  426. $sEmail = BxDolProfileQuery::getInstance()->getEmailById($iProfile);
  427. $sSubject = !empty($aSettings['subject']) ? $aSettings['subject'] : $aTemplate['Subject'];
  428. return sendMail($sEmail, $sSubject, $aTemplate['Body'], 0, array(), BX_EMAIL_NOTIFY, 'html', false, array(), true);
  429. }
  430. public function sendNotificationPush($iProfile, $aNotification)
  431. {
  432. if(!$iProfile)
  433. return false;
  434. $aContent = &$aNotification['content'];
  435. $aSettings = &$aNotification['settings'];
  436. $sSubject = !empty($aSettings['subject']) ? $aSettings['subject'] : _t('_bx_ntfs_push_new_event_subject', getParam('site_title'));
  437. bx_alert($this->_aModule['name'], 'before_send_notification_push', 0, 0, array('profile_id' => $iProfile, 'content' => &$aContent, 'setting' => &$aSettings, 'subject' => &$sSubject));
  438. return BxDolPush::getInstance()->send($iProfile, array(
  439. 'contents' => array(
  440. 'en' => $aContent['message']
  441. ),
  442. 'headings' => array(
  443. 'en' => $sSubject
  444. ),
  445. 'url' => $aContent['url'],
  446. 'icon' => $aContent['icon']
  447. ), true);
  448. }
  449. /*
  450. * INTERNAL METHODS
  451. */
  452. protected function _prepareParams($sType = '', $iOwnerId = 0, $iStart = -1, $iPerPage = -1, $aModules = array())
  453. {
  454. $aParams = array();
  455. $aParams['browse'] = 'list';
  456. $aParams['type'] = !empty($sType) ? $sType : BX_NTFS_TYPE_DEFAULT;
  457. $aParams['owner_id'] = (int)$iOwnerId != 0 ? $iOwnerId : $this->getUserId();
  458. $aParams['start'] = (int)$iStart > 0 ? $iStart : 0;
  459. $aParams['per_page'] = (int)$iPerPage > 0 ? $iPerPage : $this->_oConfig->getPerPage();
  460. $aParams['modules'] = is_array($aModules) && !empty($aModules) ? $aModules : array();
  461. $aParams['last_read'] = $this->_oDb->getLastRead($aParams['owner_id']);
  462. $aParams['active'] = 1;
  463. return $aParams;
  464. }
  465. protected function _prepareParamsGet()
  466. {
  467. $aParams = array();
  468. $aParams['browse'] = 'list';
  469. $sType = bx_get('type');
  470. $aParams['type'] = $sType !== false ? bx_process_input($sType, BX_DATA_TEXT) : BX_NTFS_TYPE_DEFAULT;
  471. $aParams['owner_id'] = $sType !== false ? bx_process_input(bx_get('owner_id'), BX_DATA_INT) : $this->getUserId();
  472. $iStart = bx_get('start');
  473. $aParams['start'] = $iStart !== false ? bx_process_input($iStart, BX_DATA_INT) : 0;
  474. $iPerPage = bx_get('per_page');
  475. $aParams['per_page'] = $iPerPage !== false ? bx_process_input($iPerPage, BX_DATA_INT) : $this->_oConfig->getPerPage();
  476. $aModules = bx_get('modules');
  477. $aParams['modules'] = $aModules !== false ? bx_process_input($aModules, BX_DATA_TEXT) : array();
  478. $aParams['active'] = 1;
  479. return $aParams;
  480. }
  481. }
  482. /** @} */