1
0

BxBaseModNotificationsModule.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. * DTYPE - Delivery Type:
  20. * 1. by Onsite notification,
  21. * 2. by Email message,
  22. * 3. by Push notification.
  23. */
  24. define('BX_BASE_MOD_NTFS_DTYPE_SITE', 'site');
  25. define('BX_BASE_MOD_NTFS_DTYPE_EMAIL', 'email');
  26. define('BX_BASE_MOD_NTFS_DTYPE_PUSH', 'push');
  27. /**
  28. * Base module class.
  29. */
  30. class BxBaseModNotificationsModule extends BxBaseModGeneralModule
  31. {
  32. public $_iOwnerId;
  33. function __construct(&$aModule)
  34. {
  35. parent::__construct($aModule);
  36. $this->_oConfig->init($this->_oDb);
  37. $this->_iOwnerId = 0;
  38. }
  39. /**
  40. * @page service Service Calls
  41. * @section bx_base_notifications Base Notifications
  42. * @subsection bx_base_notifications-other Other
  43. * @subsubsection bx_base_notifications-add_handlers add_handlers
  44. *
  45. * @code bx_srv('bx_notifications', 'add_handlers', [...]); @endcode
  46. *
  47. * Register handlers for specified module.
  48. *
  49. * @param $sModuleUri string with module URI.
  50. *
  51. * @see BxBaseModNotificationsModule::serviceAddHandlers
  52. */
  53. /**
  54. * @ref bx_base_notifications-add_handlers "add_handlers"
  55. */
  56. public function serviceAddHandlers($sModuleUri)
  57. {
  58. $this->_updateModuleData('add_handlers', $sModuleUri);
  59. }
  60. /**
  61. * @page service Service Calls
  62. * @section bx_base_notifications Base Notifications
  63. * @subsection bx_base_notifications-other Other
  64. * @subsubsection bx_base_notifications-delete_handlers delete_handlers
  65. *
  66. * @code bx_srv('bx_notifications', 'delete_handlers', [...]); @endcode
  67. *
  68. * Unregister handlers for specified module.
  69. *
  70. * @param $sModuleUri string with module URI.
  71. *
  72. * @see BxBaseModNotificationsModule::serviceDeleteHandlers
  73. */
  74. /**
  75. * @ref bx_base_notifications-delete_handlers "delete_handlers"
  76. */
  77. public function serviceDeleteHandlers($sModuleUri)
  78. {
  79. $this->_updateModuleData('delete_handlers', $sModuleUri);
  80. }
  81. /**
  82. * @page service Service Calls
  83. * @section bx_base_notifications Base Notifications
  84. * @subsection bx_base_notifications-other Other
  85. * @subsubsection bx_base_notifications-delete_module_events delete_module_events
  86. *
  87. * @code bx_srv('bx_notifications', 'delete_module_events', [...]); @endcode
  88. *
  89. * Delete all events for specified module.
  90. *
  91. * @param $sModuleUri string with module URI.
  92. *
  93. * @see BxBaseModNotificationsModule::serviceDeleteModuleEvents
  94. */
  95. /**
  96. * @ref bx_base_notifications-delete_module_events "delete_module_events"
  97. */
  98. public function serviceDeleteModuleEvents($sModuleUri)
  99. {
  100. $this->_updateModuleData('delete_module_events', $sModuleUri);
  101. }
  102. /**
  103. * @page service Service Calls
  104. * @section bx_base_notifications Base Notifications
  105. * @subsection bx_base_notifications-other Other
  106. * @subsubsection bx_base_notifications-get_actions_checklist get_actions_checklist
  107. *
  108. * @code bx_srv('bx_notifications', 'get_actions_checklist', [...]); @endcode
  109. *
  110. * Get available actions for module settings in Studio.
  111. *
  112. * @return an array with available actions represented as key => value pairs.
  113. *
  114. * @see BxBaseModNotificationsModule::serviceGetActionsChecklist
  115. */
  116. /**
  117. * @ref bx_base_notifications-get_actions_checklist "get_actions_checklist"
  118. */
  119. function serviceGetActionsChecklist()
  120. {
  121. $sLangPrefix = $this->_oConfig->getPrefix('language');
  122. $aHandlers = $this->_oConfig->getHandlers();
  123. $aResults = array();
  124. foreach($aHandlers as $aHandler) {
  125. if($aHandler['type'] != BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT)
  126. continue;
  127. $sUnit = _t($this->_oConfig->getHandlersUnitTitle($aHandler['alert_unit']));
  128. $sAction = '';
  129. if(!empty($aHandler['alert_action']))
  130. $sAction = ' (' . _t($this->_oConfig->getHandlersActionTitle($aHandler['alert_unit'], $aHandler['alert_action'])) . ')';
  131. $aResults[$aHandler['id']] = $sUnit . $sAction;
  132. }
  133. asort($aResults);
  134. return $aResults;
  135. }
  136. public function isAllowedView($aEvent, $bPerform = false)
  137. {
  138. return true;
  139. }
  140. public function getOwnerId()
  141. {
  142. return $this->_iOwnerId;
  143. }
  144. /*
  145. * Retrieve the Privacy from an array (Alert's Extras or Content field from DB)
  146. */
  147. public function getObjectPrivacyView($aData)
  148. {
  149. return is_array($aData) && isset($aData['privacy_view']) ? (int)$aData['privacy_view'] : $this->_oConfig->getPrivacyViewDefault('object');
  150. }
  151. protected function _updateModuleData($sAction, $sModuleUri)
  152. {
  153. $sMethod = $this->_oConfig->getHandlersMethod();
  154. $aModule = $this->_oDb->getModuleByUri($sModuleUri);
  155. if(!BxDolRequest::serviceExists($aModule, $sMethod))
  156. return;
  157. $aData = BxDolService::call($aModule['name'], $sMethod);
  158. if(empty($aData) || !is_array($aData))
  159. return;
  160. switch($sAction) {
  161. case 'add_handlers':
  162. $this->_oDb->insertData($aData);
  163. BxDolAlerts::cacheInvalidate();
  164. $this->_oDb->activateModuleEvents($aData, true);
  165. break;
  166. case 'delete_handlers':
  167. $this->_oDb->deleteData($aData);
  168. BxDolAlerts::cacheInvalidate();
  169. $this->_oDb->activateModuleEvents($aData, false);
  170. break;
  171. case 'delete_module_events':
  172. $this->_oDb->deleteModuleEvents($aData);
  173. break;
  174. }
  175. }
  176. }
  177. /** @} */