BxNtfsModule.php 8.3 KB

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