BxBaseModNotificationsModule.php 6.1 KB

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