BxNtfsModule.php 18 KB

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