1
0

BxNtfsModule.php 17 KB

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