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. public function serviceGetSafeServices()
  51. {
  52. return array();
  53. }
  54. /**
  55. * @page service Service Calls
  56. * @section bx_base_notifications Base Notifications
  57. * @subsection bx_base_notifications-other Other
  58. * @subsubsection bx_base_notifications-add_handlers add_handlers
  59. *
  60. * @code bx_srv('bx_notifications', 'add_handlers', [...]); @endcode
  61. *
  62. * Register handlers for specified module.
  63. *
  64. * @param $sModuleUri string with module URI.
  65. *
  66. * @see BxBaseModNotificationsModule::serviceAddHandlers
  67. */
  68. /**
  69. * @ref bx_base_notifications-add_handlers "add_handlers"
  70. */
  71. public function serviceAddHandlers($sModuleUri)
  72. {
  73. $this->_updateModuleData('add_handlers', $sModuleUri);
  74. }
  75. /**
  76. * @page service Service Calls
  77. * @section bx_base_notifications Base Notifications
  78. * @subsection bx_base_notifications-other Other
  79. * @subsubsection bx_base_notifications-delete_handlers delete_handlers
  80. *
  81. * @code bx_srv('bx_notifications', 'delete_handlers', [...]); @endcode
  82. *
  83. * Unregister handlers for specified module.
  84. *
  85. * @param $sModuleUri string with module URI.
  86. *
  87. * @see BxBaseModNotificationsModule::serviceDeleteHandlers
  88. */
  89. /**
  90. * @ref bx_base_notifications-delete_handlers "delete_handlers"
  91. */
  92. public function serviceDeleteHandlers($sModuleUri)
  93. {
  94. $this->_updateModuleData('delete_handlers', $sModuleUri);
  95. }
  96. /**
  97. * @page service Service Calls
  98. * @section bx_base_notifications Base Notifications
  99. * @subsection bx_base_notifications-other Other
  100. * @subsubsection bx_base_notifications-delete_module_events delete_module_events
  101. *
  102. * @code bx_srv('bx_notifications', 'delete_module_events', [...]); @endcode
  103. *
  104. * Delete all events for specified module.
  105. *
  106. * @param $sModuleUri string with module URI.
  107. *
  108. * @see BxBaseModNotificationsModule::serviceDeleteModuleEvents
  109. */
  110. /**
  111. * @ref bx_base_notifications-delete_module_events "delete_module_events"
  112. */
  113. public function serviceDeleteModuleEvents($sModuleUri)
  114. {
  115. $this->_updateModuleData('delete_module_events', $sModuleUri);
  116. }
  117. /**
  118. * @page service Service Calls
  119. * @section bx_base_notifications Base Notifications
  120. * @subsection bx_base_notifications-other Other
  121. * @subsubsection bx_base_notifications-get_actions_checklist get_actions_checklist
  122. *
  123. * @code bx_srv('bx_notifications', 'get_actions_checklist', [...]); @endcode
  124. *
  125. * Get available actions for module settings in Studio.
  126. *
  127. * @return an array with available actions represented as key => value pairs.
  128. *
  129. * @see BxBaseModNotificationsModule::serviceGetActionsChecklist
  130. */
  131. /**
  132. * @ref bx_base_notifications-get_actions_checklist "get_actions_checklist"
  133. */
  134. function serviceGetActionsChecklist()
  135. {
  136. $sLangPrefix = $this->_oConfig->getPrefix('language');
  137. $aHandlers = $this->_oConfig->getHandlers();
  138. $aResults = array();
  139. foreach($aHandlers as $aHandler) {
  140. if($aHandler['type'] != BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT)
  141. continue;
  142. $sUnit = _t($this->_oConfig->getHandlersUnitTitle($aHandler['alert_unit']));
  143. $sAction = '';
  144. if(!empty($aHandler['alert_action']))
  145. $sAction = ' (' . _t($this->_oConfig->getHandlersActionTitle($aHandler['alert_unit'], $aHandler['alert_action'])) . ')';
  146. $aResults[$aHandler['id']] = $sUnit . $sAction;
  147. }
  148. asort($aResults);
  149. return $aResults;
  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. /** @} */