BxBaseModNotificationsModule.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. define('BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT', 'insert');
  12. define('BX_BASE_MOD_NTFS_HANDLER_TYPE_UPDATE', 'update');
  13. define('BX_BASE_MOD_NTFS_HANDLER_TYPE_DELETE', 'delete');
  14. define('BX_BASE_MOD_NTFS_TYPE_OWNER', 'owner');
  15. define('BX_BASE_MOD_NTFS_TYPE_OBJECT_OWNER', 'object_owner');
  16. define('BX_BASE_MOD_NTFS_TYPE_CONNECTIONS', 'connections');
  17. define('BX_BASE_MOD_NTFS_TYPE_PUBLIC', 'public');
  18. /**
  19. * Base module class.
  20. */
  21. class BxBaseModNotificationsModule extends BxBaseModGeneralModule
  22. {
  23. public $_iOwnerId;
  24. function __construct(&$aModule)
  25. {
  26. parent::__construct($aModule);
  27. $this->_oConfig->init($this->_oDb);
  28. $this->_iOwnerId = 0;
  29. }
  30. /**
  31. * @page service Service Calls
  32. * @section bx_base_notifications Base Notifications
  33. * @subsection bx_base_notifications-other Other
  34. * @subsubsection bx_base_notifications-add_handlers add_handlers
  35. *
  36. * @code bx_srv('bx_notifications', 'add_handlers', [...]); @endcode
  37. *
  38. * Register handlers for specified module.
  39. *
  40. * @param $sModuleUri string with module URI.
  41. *
  42. * @see BxBaseModNotificationsModule::serviceAddHandlers
  43. */
  44. /**
  45. * @ref bx_base_notifications-add_handlers "add_handlers"
  46. */
  47. public function serviceAddHandlers($sModuleUri)
  48. {
  49. $this->_updateModuleData('add_handlers', $sModuleUri);
  50. }
  51. /**
  52. * @page service Service Calls
  53. * @section bx_base_notifications Base Notifications
  54. * @subsection bx_base_notifications-other Other
  55. * @subsubsection bx_base_notifications-delete_handlers delete_handlers
  56. *
  57. * @code bx_srv('bx_notifications', 'delete_handlers', [...]); @endcode
  58. *
  59. * Unregister handlers for specified module.
  60. *
  61. * @param $sModuleUri string with module URI.
  62. *
  63. * @see BxBaseModNotificationsModule::serviceDeleteHandlers
  64. */
  65. /**
  66. * @ref bx_base_notifications-delete_handlers "delete_handlers"
  67. */
  68. public function serviceDeleteHandlers($sModuleUri)
  69. {
  70. $this->_updateModuleData('delete_handlers', $sModuleUri);
  71. }
  72. /**
  73. * @page service Service Calls
  74. * @section bx_base_notifications Base Notifications
  75. * @subsection bx_base_notifications-other Other
  76. * @subsubsection bx_base_notifications-delete_module_events delete_module_events
  77. *
  78. * @code bx_srv('bx_notifications', 'delete_module_events', [...]); @endcode
  79. *
  80. * Delete all events for specified module.
  81. *
  82. * @param $sModuleUri string with module URI.
  83. *
  84. * @see BxBaseModNotificationsModule::serviceDeleteModuleEvents
  85. */
  86. /**
  87. * @ref bx_base_notifications-delete_module_events "delete_module_events"
  88. */
  89. public function serviceDeleteModuleEvents($sModuleUri)
  90. {
  91. $this->_updateModuleData('delete_module_events', $sModuleUri);
  92. }
  93. /**
  94. * @page service Service Calls
  95. * @section bx_base_notifications Base Notifications
  96. * @subsection bx_base_notifications-other Other
  97. * @subsubsection bx_base_notifications-get_actions_checklist get_actions_checklist
  98. *
  99. * @code bx_srv('bx_notifications', 'get_actions_checklist', [...]); @endcode
  100. *
  101. * Get available actions for module settings in Studio.
  102. *
  103. * @return an array with available actions represented as key => value pairs.
  104. *
  105. * @see BxBaseModNotificationsModule::serviceGetActionsChecklist
  106. */
  107. /**
  108. * @ref bx_base_notifications-get_actions_checklist "get_actions_checklist"
  109. */
  110. function serviceGetActionsChecklist()
  111. {
  112. $sLangPrefix = $this->_oConfig->getPrefix('language');
  113. $aHandlers = $this->_oConfig->getHandlers();
  114. $aResults = array();
  115. foreach($aHandlers as $aHandler) {
  116. if($aHandler['type'] != BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT)
  117. continue;
  118. $_sUnit = '_' . $aHandler['alert_unit'];
  119. $sUnit = _t($_sUnit);
  120. if(strcmp($_sUnit, $sUnit) === 0)
  121. $sUnit = _t($sLangPrefix . '_alert_module_' . $aHandler['alert_unit']);
  122. $sAction = '';
  123. if(!empty($aHandler['alert_action'])) {
  124. $_sAction = '_' . $aHandler['alert_unit'] . '_alert_action_' . $aHandler['alert_action'];
  125. $sAction = _t($_sAction);
  126. if(strcmp($_sAction, $sAction) === 0)
  127. $sAction = _t($sLangPrefix . '_alert_action_' . $aHandler['alert_action']);
  128. $sAction = ' (' . $sAction . ')';
  129. }
  130. $aResults[$aHandler['id']] = $sUnit . $sAction;
  131. }
  132. asort($aResults);
  133. return $aResults;
  134. }
  135. public function isAllowedView($aEvent, $bPerform = false)
  136. {
  137. return true;
  138. }
  139. public function getOwnerId()
  140. {
  141. return $this->_iOwnerId;
  142. }
  143. protected function _updateModuleData($sAction, $sModuleUri)
  144. {
  145. $sMethod = $this->_oConfig->getHandlersMethod();
  146. $aModule = $this->_oDb->getModuleByUri($sModuleUri);
  147. if(!BxDolRequest::serviceExists($aModule, $sMethod))
  148. return;
  149. $aData = BxDolService::call($aModule['name'], $sMethod);
  150. if(empty($aData) || !is_array($aData))
  151. return;
  152. switch($sAction) {
  153. case 'add_handlers':
  154. $this->_oDb->insertData($aData);
  155. BxDolAlerts::cacheInvalidate();
  156. $this->_oDb->activateModuleEvents($aData, true);
  157. break;
  158. case 'delete_handlers':
  159. $this->_oDb->deleteData($aData);
  160. BxDolAlerts::cacheInvalidate();
  161. $this->_oDb->activateModuleEvents($aData, false);
  162. break;
  163. case 'delete_module_events':
  164. $this->_oDb->deleteModuleEvents($aData);
  165. break;
  166. }
  167. }
  168. }
  169. /** @} */