BxBaseModNotificationsModule.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup BaseNotifications Base classes for Notifications like modules
  7. * @ingroup DolphinModules
  8. *
  9. * @{
  10. */
  11. bx_import ('BxDolModule');
  12. define('BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT', 'insert');
  13. define('BX_BASE_MOD_NTFS_HANDLER_TYPE_UPDATE', 'update');
  14. define('BX_BASE_MOD_NTFS_HANDLER_TYPE_DELETE', 'delete');
  15. define('BX_BASE_MOD_NTFS_TYPE_OWNER', 'owner');
  16. define('BX_BASE_MOD_NTFS_TYPE_CONNECTIONS', 'connections');
  17. /**
  18. * Base module class.
  19. */
  20. class BxBaseModNotificationsModule extends BxDolModule
  21. {
  22. public $_iOwnerId;
  23. function __construct(&$aModule)
  24. {
  25. parent::__construct($aModule);
  26. $this->_oConfig->init($this->_oDb);
  27. $this->_iOwnerId = 0;
  28. }
  29. public function serviceAddHandlers($sModuleUri)
  30. {
  31. $this->_updateModuleData('add_handlers', $sModuleUri);
  32. }
  33. public function serviceDeleteHandlers($sModuleUri)
  34. {
  35. $this->_updateModuleData('delete_handlers', $sModuleUri);
  36. }
  37. public function serviceDeleteModuleEvents($sModuleUri)
  38. {
  39. $this->_updateModuleData('delete_module_events', $sModuleUri);
  40. }
  41. function serviceGetActionsChecklist()
  42. {
  43. $sLangPrefix = $this->_oConfig->getPrefix('language');
  44. $aHandlers = $this->_oConfig->getHandlers();
  45. $aResults = array();
  46. foreach($aHandlers as $aHandler) {
  47. if($aHandler['type'] != BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT)
  48. continue;
  49. $aModule = array();
  50. if(!empty($aHandler['module_name']))
  51. $aModule = $this->_oDb->getModuleByName($aHandler['module_name']);
  52. if(empty($aModule))
  53. $aModule['title'] = _t($sLangPrefix . '_alert_module_' . $aHandler['alert_unit']);
  54. $sAction = !empty($aHandler['alert_action']) ? ' (' . _t($sLangPrefix . '_alert_action_' . $aHandler['alert_action']) . ')' : '';
  55. $aResults[$aHandler['id']] = $aModule['title'] . $sAction;
  56. }
  57. asort($aResults);
  58. return $aResults;
  59. }
  60. public function getUserId()
  61. {
  62. return isLogged() ? bx_get_logged_profile_id() : 0;
  63. }
  64. public function getUserIp()
  65. {
  66. return getVisitorIP();
  67. }
  68. public function getUserInfo($iUserId = 0)
  69. {
  70. bx_import('BxDolProfile');
  71. $oProfile = BxDolProfile::getInstance($iUserId);
  72. if (!$oProfile) {
  73. bx_import('BxDolProfileUndefined');
  74. $oProfile = BxDolProfileUndefined::getInstance();
  75. }
  76. return array(
  77. $oProfile->getDisplayName(),
  78. $oProfile->getUrl(),
  79. $oProfile->getThumb(),
  80. $oProfile->getUnit()
  81. );
  82. }
  83. protected function _updateModuleData($sAction, $sModuleUri)
  84. {
  85. $sMethod = $this->_oConfig->getHandlersMethod();
  86. $aModule = $this->_oDb->getModuleByUri($sModuleUri);
  87. if(!BxDolRequest::serviceExists($aModule, $sMethod))
  88. return;
  89. $aData = BxDolService::call($aModule['name'], $sMethod);
  90. if(empty($aData) || !is_array($aData))
  91. return;
  92. switch($sAction) {
  93. case 'add_handlers':
  94. $this->_oDb->insertData($aData);
  95. BxDolAlerts::cacheInvalidate();
  96. $this->_oDb->activateModuleEvents($aData, true);
  97. break;
  98. case 'delete_handlers':
  99. $this->_oDb->deleteData($aData);
  100. BxDolAlerts::cacheInvalidate();
  101. $this->_oDb->activateModuleEvents($aData, false);
  102. break;
  103. case 'delete_module_events':
  104. $this->_oDb->deleteModuleEvents($aData);
  105. break;
  106. }
  107. }
  108. protected function _echoResultJson($a, $isAutoWrapForFormFileSubmit = false)
  109. {
  110. header('Content-type: text/html; charset=utf-8');
  111. $s = json_encode($a);
  112. if ($isAutoWrapForFormFileSubmit && !empty($_FILES))
  113. $s = '<textarea>' . $s . '</textarea>';
  114. echo $s;
  115. }
  116. }
  117. /** @} */