BxNtfsModule.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. class BxNtfsModule extends BxBaseModNotificationsModule
  16. {
  17. /**
  18. * Constructor
  19. */
  20. function __construct($aModule)
  21. {
  22. parent::__construct($aModule);
  23. $this->_iOwnerId = $this->getUserId();
  24. }
  25. /**
  26. * ACTION METHODS
  27. */
  28. function actionGetPosts()
  29. {
  30. $aParams = $this->_prepareParamsGet();
  31. $sEvents = $this->_oTemplate->getPosts($aParams);
  32. echoJson(array('events' => $sEvents));
  33. }
  34. /**
  35. * SERVICE METHODS
  36. */
  37. /**
  38. * @page service Service Calls
  39. * @section bx_notifications Notifications
  40. * @subsection bx_notifications-page_blocks Page Blocks
  41. * @subsubsection bx_notifications-get_block_view get_block_view
  42. *
  43. * @code bx_srv('bx_notifications', 'get_block_view', [...]); @endcode
  44. *
  45. * Get View block for a separate page. Will return a block with "Empty" message if nothing found.
  46. *
  47. * @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.
  48. *
  49. * @see BxNtfsModule::serviceGetBlockView
  50. */
  51. /**
  52. * @ref bx_notifications-get_block_view "get_block_view"
  53. */
  54. public function serviceGetBlockView($sType = '', $iStart = -1, $iPerPage = -1, $aModules = array())
  55. {
  56. $aBrowseTypes = array(BX_NTFS_TYPE_OBJECT_OWNER_AND_CONNECTIONS, BX_BASE_MOD_NTFS_TYPE_CONNECTIONS, BX_BASE_MOD_NTFS_TYPE_OBJECT_OWNER);
  57. if(empty($sType)) {
  58. $mixedType = bx_get('type');
  59. if($mixedType !== false && in_array($mixedType, $aBrowseTypes))
  60. $sType = $mixedType;
  61. if(empty($sType))
  62. $sType = BX_NTFS_TYPE_DEFAULT;
  63. }
  64. $iOwnerId = $this->getUserId();
  65. if(!$iOwnerId)
  66. return array('content' => MsgBox(_t('_bx_ntfs_txt_msg_no_results')));
  67. $aParams = $this->_prepareParams($sType, $iOwnerId, $iStart, $iPerPage, $aModules);
  68. $sContent = $this->_oTemplate->getViewBlock($aParams);
  69. $aParams['browse'] = 'first';
  70. $aEvent = $this->_oDb->getEvents($aParams);
  71. if(!empty($aEvent))
  72. $this->_oDb->markAsRead($iOwnerId, $aEvent['id']);
  73. $sModule = $this->_oConfig->getName();
  74. $sJsObject = $this->_oConfig->getJsObject('view');
  75. return array('content' => $sContent);
  76. }
  77. /**
  78. * @page service Service Calls
  79. * @section bx_notifications Notifications
  80. * @subsection bx_notifications-browsing Browsing
  81. * @subsubsection bx_notifications-get_event_by_id get_event_by_id
  82. *
  83. * @code bx_srv('bx_notifications', 'get_event_by_id', [...]); @endcode
  84. *
  85. * Get event's data by event ID.
  86. *
  87. * @return an array with event's data.
  88. *
  89. * @see BxNtfsModule::serviceGetEventById
  90. */
  91. /**
  92. * @ref bx_notifications-get_event_by_id "get_event_by_id"
  93. */
  94. public function serviceGetEventById($iId)
  95. {
  96. $aParams = array(
  97. 'browse' => 'id',
  98. 'value' => $iId,
  99. );
  100. return $this->_oDb->getEvents($aParams);
  101. }
  102. /**
  103. * @page service Service Calls
  104. * @section bx_notifications Notifications
  105. * @subsection bx_notifications-other Other
  106. * @subsubsection bx_notifications-get_unread_notifications_num get_unread_notifications_num
  107. *
  108. * @code bx_srv('bx_notifications', 'get_unread_notifications_num', [...]); @endcode
  109. *
  110. * Get number of unread notifications.
  111. *
  112. * @return integer value with number of unread notifications.
  113. *
  114. * @see BxNtfsModule::serviceGetUnreadNotificationsNum
  115. */
  116. /**
  117. * @ref bx_notifications-get_unread_notifications_num "get_unread_notifications_num"
  118. */
  119. public function serviceGetUnreadNotificationsNum($iOwnerId = 0)
  120. {
  121. if(!$iOwnerId)
  122. $iOwnerId = $this->getUserId();
  123. if(!$iOwnerId)
  124. return 0;
  125. $aParams = $this->_prepareParams(BX_NTFS_TYPE_DEFAULT, $iOwnerId);
  126. $aParams['new'] = 1;
  127. list($aEvent, $iCount) = $this->_oDb->getEvents($aParams, true);
  128. return $iCount;
  129. }
  130. /**
  131. * @page service Service Calls
  132. * @section bx_notifications Notifications
  133. * @subsection bx_notifications-other Other
  134. * @subsubsection bx_notifications-get_live_updates get_live_updates
  135. *
  136. * @code bx_srv('bx_notifications', 'get_live_updates', [...]); @endcode
  137. *
  138. * Get data for Live Updates system.
  139. *
  140. * @return an array with special format.
  141. *
  142. * @see BxNtfsModule::serviceGetLiveUpdates
  143. */
  144. /**
  145. * @ref bx_notifications-get_live_updates "get_live_updates"
  146. */
  147. public function serviceGetLiveUpdates($aMenuItemParent, $aMenuItemChild, $iCount = 0)
  148. {
  149. $iOwnerId = $this->getUserId();
  150. $iCountNew = $this->serviceGetUnreadNotificationsNum($iOwnerId);
  151. if($iCountNew == $iCount)
  152. return false;
  153. return array(
  154. 'count' => $iCountNew, // required
  155. 'method' => 'bx_menu_show_live_update(oData)', // required
  156. 'data' => array(
  157. 'code' => BxDolTemplate::getInstance()->parseHtmlByTemplateName('menu_item_addon', array(
  158. 'content' => '{count}'
  159. )),
  160. 'mi_parent' => $aMenuItemParent,
  161. 'mi_child' => $aMenuItemChild
  162. ), // optional, may have some additional data to be passed in JS method provided using 'method' param above.
  163. );
  164. }
  165. /*
  166. * COMMON METHODS
  167. */
  168. public function setSubmenu($sSelected)
  169. {
  170. $oSubmenuSystem = BxDolMenu::getObjectInstance('sys_site_submenu');
  171. if(!$oSubmenuSystem)
  172. return;
  173. $CNF = &$this->_oConfig->CNF;
  174. $oSubmenuSystem->setObjectSubmenu($CNF['OBJECT_MENU_SUBMENU'], array (
  175. 'title' => _t('_bx_ntfs'),
  176. 'link' => BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME']),
  177. 'icon' => '',
  178. ));
  179. $oSubmenuModule = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SUBMENU']);
  180. if($oSubmenuModule)
  181. $oSubmenuModule->setSelected($this->_oConfig->getName(), $sSelected);
  182. }
  183. public function onPost($iId)
  184. {
  185. //--- Event -> Post for Alerts Engine ---//
  186. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'post', $iId);
  187. $oAlert->alert();
  188. //--- Event -> Post for Alerts Engine ---//
  189. }
  190. protected function _prepareParams($sType = '', $iOwnerId = 0, $iStart = -1, $iPerPage = -1, $aModules = array())
  191. {
  192. $aParams = array();
  193. $aParams['browse'] = 'list';
  194. $aParams['type'] = !empty($sType) ? $sType : BX_NTFS_TYPE_DEFAULT;
  195. $aParams['owner_id'] = (int)$iOwnerId != 0 ? $iOwnerId : $this->getUserId();
  196. $aParams['start'] = (int)$iStart > 0 ? $iStart : 0;
  197. $aParams['per_page'] = (int)$iPerPage > 0 ? $iPerPage : $this->_oConfig->getPerPage();
  198. $aParams['modules'] = is_array($aModules) && !empty($aModules) ? $aModules : array();
  199. $aParams['active'] = 1;
  200. return $aParams;
  201. }
  202. protected function _prepareParamsGet()
  203. {
  204. $aParams = array();
  205. $aParams['browse'] = 'list';
  206. $sType = bx_get('type');
  207. $aParams['type'] = $sType !== false ? bx_process_input($sType, BX_DATA_TEXT) : BX_NTFS_TYPE_DEFAULT;
  208. $aParams['owner_id'] = $sType !== false ? bx_process_input(bx_get('owner_id'), BX_DATA_INT) : $this->getUserId();
  209. $iStart = bx_get('start');
  210. $aParams['start'] = $iStart !== false ? bx_process_input($iStart, BX_DATA_INT) : 0;
  211. $iPerPage = bx_get('per_page');
  212. $aParams['per_page'] = $iPerPage !== false ? bx_process_input($iPerPage, BX_DATA_INT) : $this->_oConfig->getPerPage();
  213. $aModules = bx_get('modules');
  214. $aParams['modules'] = $aModules !== false ? bx_process_input($aModules, BX_DATA_TEXT) : array();
  215. $aParams['active'] = 1;
  216. return $aParams;
  217. }
  218. }
  219. /** @} */