1
0

BxBaseModNotificationsModule.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 BaseNotifications Base classes for Notifications like modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * SLTMODE - Silent mode:
  13. * It is needed for alert sending module to tell that the alert should be ignored
  14. * with Notifications module completely or partially. Available values:
  15. * 1. disabled (value = 0) - all notifications are available;
  16. * 2. absolute (value = 1) - alert isn't registered which means that there is no notifications at all;
  17. *
  18. * @see BxBaseModNotificationsResponse::response - 'silent_mode' parameter in Alerts Extras array.
  19. */
  20. define('BX_BASE_MOD_NTFS_SLTMODE_DISABLED', 0);
  21. define('BX_BASE_MOD_NTFS_SLTMODE_ABSOLUTE', 1);
  22. define('BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT', 'insert');
  23. define('BX_BASE_MOD_NTFS_HANDLER_TYPE_UPDATE', 'update');
  24. define('BX_BASE_MOD_NTFS_HANDLER_TYPE_DELETE', 'delete');
  25. define('BX_BASE_MOD_NTFS_TYPE_OWNER', 'owner');
  26. define('BX_BASE_MOD_NTFS_TYPE_OBJECT_OWNER', 'object_owner');
  27. define('BX_BASE_MOD_NTFS_TYPE_CONNECTIONS', 'connections');
  28. define('BX_BASE_MOD_NTFS_TYPE_PUBLIC', 'public');
  29. /**
  30. * DTYPE - Delivery Type:
  31. * 1. by Onsite notification,
  32. * 2. by Email message,
  33. * 3. by Push notification.
  34. */
  35. define('BX_BASE_MOD_NTFS_DTYPE_SITE', 'site');
  36. define('BX_BASE_MOD_NTFS_DTYPE_EMAIL', 'email');
  37. define('BX_BASE_MOD_NTFS_DTYPE_PUSH', 'push');
  38. /**
  39. * Base module class.
  40. */
  41. class BxBaseModNotificationsModule extends BxBaseModGeneralModule
  42. {
  43. public $_iOwnerId;
  44. function __construct(&$aModule)
  45. {
  46. parent::__construct($aModule);
  47. $this->_oConfig->init($this->_oDb);
  48. $this->_iOwnerId = 0;
  49. }
  50. /**
  51. * @page service Service Calls
  52. * @section bx_base_notifications Base Notifications
  53. * @subsection bx_base_notifications-other Other
  54. * @subsubsection bx_base_notifications-add_handlers add_handlers
  55. *
  56. * @code bx_srv('bx_notifications', 'add_handlers', [...]); @endcode
  57. *
  58. * Register handlers for specified module.
  59. *
  60. * @param $sModuleUri string with module URI.
  61. *
  62. * @see BxBaseModNotificationsModule::serviceAddHandlers
  63. */
  64. /**
  65. * @ref bx_base_notifications-add_handlers "add_handlers"
  66. */
  67. public function serviceAddHandlers($sModuleUri)
  68. {
  69. $this->_updateModuleData('add_handlers', $sModuleUri);
  70. }
  71. /**
  72. * @page service Service Calls
  73. * @section bx_base_notifications Base Notifications
  74. * @subsection bx_base_notifications-other Other
  75. * @subsubsection bx_base_notifications-delete_handlers delete_handlers
  76. *
  77. * @code bx_srv('bx_notifications', 'delete_handlers', [...]); @endcode
  78. *
  79. * Unregister handlers for specified module.
  80. *
  81. * @param $sModuleUri string with module URI.
  82. *
  83. * @see BxBaseModNotificationsModule::serviceDeleteHandlers
  84. */
  85. /**
  86. * @ref bx_base_notifications-delete_handlers "delete_handlers"
  87. */
  88. public function serviceDeleteHandlers($sModuleUri)
  89. {
  90. $this->_updateModuleData('delete_handlers', $sModuleUri);
  91. }
  92. /**
  93. * @page service Service Calls
  94. * @section bx_base_notifications Base Notifications
  95. * @subsection bx_base_notifications-other Other
  96. * @subsubsection bx_base_notifications-delete_module_events delete_module_events
  97. *
  98. * @code bx_srv('bx_notifications', 'delete_module_events', [...]); @endcode
  99. *
  100. * Delete all events for specified module.
  101. *
  102. * @param $sModuleUri string with module URI.
  103. *
  104. * @see BxBaseModNotificationsModule::serviceDeleteModuleEvents
  105. */
  106. /**
  107. * @ref bx_base_notifications-delete_module_events "delete_module_events"
  108. */
  109. public function serviceDeleteModuleEvents($sModuleUri)
  110. {
  111. $this->_updateModuleData('delete_module_events', $sModuleUri);
  112. }
  113. /**
  114. * @page service Service Calls
  115. * @section bx_base_notifications Base Notifications
  116. * @subsection bx_base_notifications-other Other
  117. * @subsubsection bx_base_notifications-get_actions_checklist get_actions_checklist
  118. *
  119. * @code bx_srv('bx_notifications', 'get_actions_checklist', [...]); @endcode
  120. *
  121. * Get available actions for module settings in Studio.
  122. *
  123. * @return an array with available actions represented as key => value pairs.
  124. *
  125. * @see BxBaseModNotificationsModule::serviceGetActionsChecklist
  126. */
  127. /**
  128. * @ref bx_base_notifications-get_actions_checklist "get_actions_checklist"
  129. */
  130. function serviceGetActionsChecklist()
  131. {
  132. $sLangPrefix = $this->_oConfig->getPrefix('language');
  133. $aHandlers = $this->_oConfig->getHandlers();
  134. $aResults = array();
  135. foreach($aHandlers as $aHandler) {
  136. if($aHandler['type'] != BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT)
  137. continue;
  138. $sUnit = _t($this->_oConfig->getHandlersUnitTitle($aHandler['alert_unit']));
  139. $sAction = '';
  140. if(!empty($aHandler['alert_action']))
  141. $sAction = ' (' . _t($this->_oConfig->getHandlersActionTitle($aHandler['alert_unit'], $aHandler['alert_action'])) . ')';
  142. $aResults[$aHandler['id']] = $sUnit . $sAction;
  143. }
  144. asort($aResults);
  145. return $aResults;
  146. }
  147. public function serviceCheckAllowedAddInContext($sContextModule)
  148. {
  149. return _t('_sys_txt_access_denied');
  150. }
  151. public function isAllowedView($aEvent, $bPerform = false)
  152. {
  153. return true;
  154. }
  155. public function getOwnerId()
  156. {
  157. return $this->_iOwnerId;
  158. }
  159. public function getSilentMode($aExtras)
  160. {
  161. if(isset($aExtras['silent_mode']))
  162. return (int)$aExtras['silent_mode'];
  163. return BX_BASE_MOD_NTFS_SLTMODE_DISABLED;
  164. }
  165. /*
  166. * Retrieve the Privacy from an array (Alert's Extras or Content field from DB)
  167. */
  168. public function getObjectPrivacyView($aData)
  169. {
  170. return is_array($aData) && isset($aData['privacy_view']) ? $aData['privacy_view'] : $this->_oConfig->getPrivacyViewDefault('object');
  171. }
  172. /*
  173. * Retrieve the Content Filter from an array (Alert's Extras or Content field from DB)
  174. */
  175. public function getObjectCf($aData)
  176. {
  177. return is_array($aData) && isset($aData['cf']) ? $aData['cf'] : $this->_oConfig->getCfDefault('object');
  178. }
  179. protected function _updateModuleData($sAction, $sModuleUri)
  180. {
  181. $sMethod = $this->_oConfig->getHandlersMethod();
  182. $aModule = $this->_oDb->getModuleByUri($sModuleUri);
  183. if(!BxDolRequest::serviceExists($aModule, $sMethod))
  184. return;
  185. $aData = bx_srv_ii($aModule['name'], $sMethod);
  186. if(empty($aData) || !is_array($aData))
  187. return;
  188. switch($sAction) {
  189. case 'add_handlers':
  190. $this->_oDb->insertData($aData);
  191. BxDolAlerts::cacheInvalidate();
  192. $this->_oDb->activateModuleEvents($aData, true);
  193. break;
  194. case 'delete_handlers':
  195. $this->_oDb->deleteData($aData);
  196. BxDolAlerts::cacheInvalidate();
  197. $this->_oDb->activateModuleEvents($aData, false);
  198. break;
  199. case 'delete_module_events':
  200. $this->_oDb->deleteModuleEvents($aData);
  201. break;
  202. }
  203. }
  204. }
  205. /** @} */