BxNtfsModule.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. class BxNtfsModule extends BxBaseModNotificationsModule
  27. {
  28. /**
  29. * Constructor
  30. */
  31. function __construct($aModule)
  32. {
  33. parent::__construct($aModule);
  34. $this->_iOwnerId = $this->getUserId();
  35. }
  36. /**
  37. * ACTION METHODS
  38. */
  39. function actionGetPosts()
  40. {
  41. $aParams = $this->_prepareParamsGet();
  42. $sEvents = $this->_oTemplate->getPosts($aParams);
  43. echoJson(array('events' => $sEvents));
  44. }
  45. /**
  46. * SERVICE METHODS
  47. */
  48. /**
  49. * @page service Service Calls
  50. * @section bx_notifications Invitations
  51. * @subsection bx_notifications-other Other
  52. * @subsubsection bx_notifications-get_include get_include
  53. *
  54. * @code bx_srv('bx_notifications', 'get_include', [...]); @endcode
  55. *
  56. * Get all necessary CSS and JS files to include in a page.
  57. *
  58. * @return string with all necessary CSS and JS files.
  59. *
  60. * @see BxInvModule::serviceGetInclude
  61. */
  62. /**
  63. * @ref bx_notifications-get_include "get_include"
  64. */
  65. public function serviceGetInclude($bIncludeCss = true, $mixedIncludeJs = false)
  66. {
  67. return $this->_oTemplate->getInclude($bIncludeCss, $mixedIncludeJs);
  68. }
  69. /**
  70. * @page service Service Calls
  71. * @section bx_notifications Notifications
  72. * @subsection bx_notifications-page_blocks Page Blocks
  73. * @subsubsection bx_notifications-get_block_settings get_block_settings
  74. *
  75. * @code bx_srv('bx_notifications', 'get_block_settings', [...]); @endcode
  76. *
  77. * Get Settings block for a separate page.
  78. *
  79. * @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.
  80. *
  81. * @see BxNtfsModule::serviceGetBlockSettings
  82. */
  83. /**
  84. * @ref bx_notifications-get_block_settings "get_block_settings"
  85. */
  86. public function serviceGetBlockSettings($sDeliveryType = '', $aParams = array())
  87. {
  88. if(!isLogged())
  89. return '';
  90. if(empty($sDeliveryType) && bx_get('delivery') !== false)
  91. $sDeliveryType = bx_process_input(bx_get('delivery'));
  92. if(empty($sDeliveryType))
  93. $sDeliveryType = BX_BASE_MOD_NTFS_DTYPE_SITE;
  94. $this->_oDb->initSettingUser(bx_get_logged_profile_id());
  95. return array(
  96. 'content' => $this->getBlockSettings($sDeliveryType, $aParams),
  97. );
  98. }
  99. /**
  100. * @page service Service Calls
  101. * @section bx_notifications Notifications
  102. * @subsection bx_notifications-page_blocks Page Blocks
  103. * @subsubsection bx_notifications-get_block_view get_block_view
  104. *
  105. * @code bx_srv('bx_notifications', 'get_block_view', [...]); @endcode
  106. *
  107. * Get View block for a separate page. Will return a block with "Empty" message if nothing found.
  108. *
  109. * @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.
  110. *
  111. * @see BxNtfsModule::serviceGetBlockView
  112. */
  113. /**
  114. * @ref bx_notifications-get_block_view "get_block_view"
  115. */
  116. public function serviceGetBlockView($sType = '', $iStart = -1, $iPerPage = -1, $aModules = array())
  117. {
  118. $aBrowseTypes = array(BX_NTFS_TYPE_OBJECT_OWNER_AND_CONNECTIONS, BX_BASE_MOD_NTFS_TYPE_CONNECTIONS, BX_BASE_MOD_NTFS_TYPE_OBJECT_OWNER);
  119. if(empty($sType)) {
  120. $mixedType = bx_get('type');
  121. if($mixedType !== false && in_array($mixedType, $aBrowseTypes))
  122. $sType = $mixedType;
  123. if(empty($sType))
  124. $sType = BX_NTFS_TYPE_DEFAULT;
  125. }
  126. $iOwnerId = $this->getUserId();
  127. if(!$iOwnerId)
  128. return array('content' => MsgBox(_t('_bx_ntfs_txt_msg_no_results')));
  129. $aParams = $this->_prepareParams($sType, $iOwnerId, $iStart, $iPerPage, $aModules);
  130. $sContent = $this->_oTemplate->getViewBlock($aParams);
  131. $aParams['browse'] = 'first';
  132. $aEvent = $this->_oDb->getEvents($aParams);
  133. if(!empty($aEvent))
  134. $this->_oDb->markAsRead($iOwnerId, $aEvent['id']);
  135. $sModule = $this->_oConfig->getName();
  136. $sJsObject = $this->_oConfig->getJsObject('view');
  137. return array('content' => $sContent);
  138. }
  139. /**
  140. * @page service Service Calls
  141. * @section bx_notifications Notifications
  142. * @subsection bx_notifications-browsing Browsing
  143. * @subsubsection bx_notifications-get_event_by_id get_event_by_id
  144. *
  145. * @code bx_srv('bx_notifications', 'get_event_by_id', [...]); @endcode
  146. *
  147. * Get event's data by event ID.
  148. *
  149. * @return an array with event's data.
  150. *
  151. * @see BxNtfsModule::serviceGetEventById
  152. */
  153. /**
  154. * @ref bx_notifications-get_event_by_id "get_event_by_id"
  155. */
  156. public function serviceGetEventById($iId)
  157. {
  158. $aParams = array(
  159. 'browse' => 'id',
  160. 'value' => $iId,
  161. );
  162. return $this->_oDb->getEvents($aParams);
  163. }
  164. /**
  165. * @page service Service Calls
  166. * @section bx_notifications Notifications
  167. * @subsection bx_notifications-other Other
  168. * @subsubsection bx_notifications-get_notifications get_notifications
  169. *
  170. * @code bx_srv('bx_notifications', 'get_notifications', [...]); @endcode
  171. *
  172. * Get notifications by params.
  173. *
  174. * @return array with notifications.
  175. *
  176. * @see BxNtfsModule::serviceGetNotifications
  177. */
  178. /**
  179. * @ref bx_notifications-get_notifications "get_notifications"
  180. */
  181. public function serviceGetNotifications($iOwnerId = 0, $aBrowseParams = array())
  182. {
  183. if(!$iOwnerId)
  184. $iOwnerId = $this->getUserId();
  185. if(!$iOwnerId)
  186. return 0;
  187. $aParams = $this->_prepareParams(BX_NTFS_TYPE_DEFAULT, $iOwnerId);
  188. if(!empty($aBrowseParams) && is_array($aBrowseParams))
  189. $aParams = array_merge($aParams, $aBrowseParams);
  190. list($aEvent) = $this->_oDb->getEvents($aParams, true);
  191. return $aEvent;
  192. }
  193. /**
  194. * @page service Service Calls
  195. * @section bx_notifications Notifications
  196. * @subsection bx_notifications-other Other
  197. * @subsubsection bx_notifications-get_unread_notifications get_unread_notifications
  198. *
  199. * @code bx_srv('bx_notifications', 'get_unread_notifications', [...]); @endcode
  200. *
  201. * Get unread notifications.
  202. *
  203. * @return array with unread notifications.
  204. *
  205. * @see BxNtfsModule::serviceGetUnreadNotifications
  206. */
  207. /**
  208. * @ref bx_notifications-get_unread_notifications "get_unread_notifications"
  209. */
  210. public function serviceGetUnreadNotifications($iOwnerId = 0)
  211. {
  212. return $this->serviceGetNotifications($iOwnerId, array('new' => 1));
  213. }
  214. /**
  215. * @page service Service Calls
  216. * @section bx_notifications Notifications
  217. * @subsection bx_notifications-other Other
  218. * @subsubsection bx_notifications-get_unread_notifications_num get_unread_notifications_num
  219. *
  220. * @code bx_srv('bx_notifications', 'get_unread_notifications_num', [...]); @endcode
  221. *
  222. * Get number of unread notifications.
  223. *
  224. * @return integer value with number of unread notifications.
  225. *
  226. * @see BxNtfsModule::serviceGetUnreadNotificationsNum
  227. */
  228. /**
  229. * @ref bx_notifications-get_unread_notifications_num "get_unread_notifications_num"
  230. */
  231. public function serviceGetUnreadNotificationsNum($iOwnerId = 0)
  232. {
  233. if(!$iOwnerId)
  234. $iOwnerId = $this->getUserId();
  235. if(!$iOwnerId)
  236. return 0;
  237. $aParams = $this->_prepareParams(BX_NTFS_TYPE_DEFAULT, $iOwnerId);
  238. $aParams['new'] = 1;
  239. list($aEvent, $iCount) = $this->_oDb->getEvents($aParams, true);
  240. return $iCount;
  241. }
  242. /**
  243. * @page service Service Calls
  244. * @section bx_notifications Notifications
  245. * @subsection bx_notifications-other Other
  246. * @subsubsection bx_notifications-get_live_updates get_live_updates
  247. *
  248. * @code bx_srv('bx_notifications', 'get_live_updates', [...]); @endcode
  249. *
  250. * Get data for Live Updates system.
  251. *
  252. * @return an array with special format.
  253. *
  254. * @see BxNtfsModule::serviceGetLiveUpdates
  255. */
  256. /**
  257. * @ref bx_notifications-get_live_updates "get_live_updates"
  258. */
  259. public function serviceGetLiveUpdates($aMenuItemParent, $aMenuItemChild, $iCount = 0)
  260. {
  261. $iOwnerId = $this->getUserId();
  262. $iCountNew = $this->serviceGetUnreadNotificationsNum($iOwnerId);
  263. if($iCountNew == $iCount)
  264. return false;
  265. return array(
  266. 'count' => $iCountNew, // required
  267. 'method' => 'bx_menu_show_live_update(oData)', // required
  268. 'data' => array(
  269. 'code' => BxDolTemplate::getInstance()->parseHtmlByTemplateName('menu_item_addon', array(
  270. 'content' => '{count}'
  271. )),
  272. 'mi_parent' => $aMenuItemParent,
  273. 'mi_child' => $aMenuItemChild
  274. ), // optional, may have some additional data to be passed in JS method provided using 'method' param above.
  275. );
  276. }
  277. /*
  278. * COMMON METHODS
  279. */
  280. public function getBlockSettings($sDeliveryType, $aParams = array())
  281. {
  282. return $this->_oTemplate->getSettingsBlock($sDeliveryType, $aParams);
  283. }
  284. public function setSubmenu($sSelected)
  285. {
  286. $oSubmenuSystem = BxDolMenu::getObjectInstance('sys_site_submenu');
  287. if(!$oSubmenuSystem)
  288. return;
  289. $CNF = &$this->_oConfig->CNF;
  290. $oSubmenuSystem->setObjectSubmenu($CNF['OBJECT_MENU_SUBMENU'], array (
  291. 'title' => _t('_bx_ntfs'),
  292. 'link' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME']),
  293. 'icon' => '',
  294. ));
  295. $oSubmenuModule = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SUBMENU']);
  296. if($oSubmenuModule)
  297. $oSubmenuModule->setSelected($this->_oConfig->getName(), $sSelected);
  298. }
  299. public function onPost($iId)
  300. {
  301. //--- Event -> Post for Alerts Engine ---//
  302. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'post', $iId);
  303. $oAlert->alert();
  304. //--- Event -> Post for Alerts Engine ---//
  305. }
  306. public function enableSettingsLike($iId, $bValue, $bAdministration = false)
  307. {
  308. $aSetting = $this->_oDb->getSetting(array(
  309. 'by' => $bAdministration ? 'id' : 'tsu_id',
  310. 'id' => (int)$iId
  311. ));
  312. if(empty($aSetting) || !is_array($aSetting))
  313. return false;
  314. $aSettingsIds = $this->_oDb->getSetting(array(
  315. 'by' => 'group_type_delivery',
  316. 'group' => $aSetting['group'],
  317. 'delivery' => $aSetting['delivery'],
  318. 'type' => $aSetting['type'],
  319. 'active' => !$bAdministration
  320. ));
  321. if(empty($aSettingsIds) || !is_array($aSettingsIds))
  322. return false;
  323. $iUserId = bx_get_logged_profile_id();
  324. $mixedResult = false;
  325. if($bAdministration)
  326. $mixedResult = $this->_oDb->activateSettingById($bValue, $aSettingsIds);
  327. else
  328. $mixedResult = $this->_oDb->activateSettingByIdUser($bValue, $iUserId, $aSettingsIds);
  329. return $mixedResult;
  330. }
  331. protected function _prepareParams($sType = '', $iOwnerId = 0, $iStart = -1, $iPerPage = -1, $aModules = array())
  332. {
  333. $aParams = array();
  334. $aParams['browse'] = 'list';
  335. $aParams['type'] = !empty($sType) ? $sType : BX_NTFS_TYPE_DEFAULT;
  336. $aParams['owner_id'] = (int)$iOwnerId != 0 ? $iOwnerId : $this->getUserId();
  337. $aParams['start'] = (int)$iStart > 0 ? $iStart : 0;
  338. $aParams['per_page'] = (int)$iPerPage > 0 ? $iPerPage : $this->_oConfig->getPerPage();
  339. $aParams['modules'] = is_array($aModules) && !empty($aModules) ? $aModules : array();
  340. $aParams['last_read'] = $this->_oDb->getLastRead($aParams['owner_id']);
  341. $aParams['active'] = 1;
  342. return $aParams;
  343. }
  344. protected function _prepareParamsGet()
  345. {
  346. $aParams = array();
  347. $aParams['browse'] = 'list';
  348. $sType = bx_get('type');
  349. $aParams['type'] = $sType !== false ? bx_process_input($sType, BX_DATA_TEXT) : BX_NTFS_TYPE_DEFAULT;
  350. $aParams['owner_id'] = $sType !== false ? bx_process_input(bx_get('owner_id'), BX_DATA_INT) : $this->getUserId();
  351. $iStart = bx_get('start');
  352. $aParams['start'] = $iStart !== false ? bx_process_input($iStart, BX_DATA_INT) : 0;
  353. $iPerPage = bx_get('per_page');
  354. $aParams['per_page'] = $iPerPage !== false ? bx_process_input($iPerPage, BX_DATA_INT) : $this->_oConfig->getPerPage();
  355. $aModules = bx_get('modules');
  356. $aParams['modules'] = $aModules !== false ? bx_process_input($aModules, BX_DATA_TEXT) : array();
  357. $aParams['active'] = 1;
  358. return $aParams;
  359. }
  360. }
  361. /** @} */