BxReputationAlertsResponse.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 Reputation Reputation
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxReputationAlertsResponse extends BxBaseModNotificationsResponse
  12. {
  13. public function __construct()
  14. {
  15. $this->_sModule = 'bx_reputation';
  16. parent::__construct();
  17. }
  18. public function response($oAlert)
  19. {
  20. parent::response($oAlert);
  21. $sMethod = '_process' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  22. if(method_exists($this, $sMethod))
  23. return $this->$sMethod($oAlert);
  24. /**
  25. * @hooks
  26. * @hookdef hook-bx_reputation-before_register_alert 'bx_reputation', 'before_register_alert' - hook to override alert (hook) before processing
  27. * - $unit_name - equals `bx_reputation`
  28. * - $action - equals `before_register_alert`
  29. * - $object_id - not used
  30. * - $sender_id - not used
  31. * - $extra_params - array of additional params with the following array keys:
  32. * - `unit` - [string] alert (hook) unit
  33. * - `action` - [string] alert (hook) action
  34. * - `alert` - [array] by ref, an instance of alert (hook), @see BxDolAlerts, can be overridden in hook processing
  35. * @hook @ref hook-bx_reputation-before_register_alert
  36. */
  37. bx_alert($this->_oModule->getName(), 'before_register_alert', 0, 0, [
  38. 'unit' => $oAlert->sUnit,
  39. 'action' => $oAlert->sAction,
  40. 'alert' => &$oAlert,
  41. ]);
  42. if(($sMethod = '_checkByAction' . bx_gen_method_name($oAlert->sAction)) && method_exists($this, $sMethod) && !$this->$sMethod($oAlert))
  43. return;
  44. $aHandler = $this->_oModule->_oConfig->getHandlers($oAlert->sUnit . '_' . $oAlert->sAction);
  45. if(empty($aHandler) || !is_array($aHandler))
  46. return;
  47. $iOwnerId = $oAlert->iSender;
  48. $iObjectOwnerId = abs($this->_getObjectOwnerId($oAlert->aExtras));
  49. $iObjectPrivacyView = $this->_getObjectPrivacyView($oAlert->aExtras);
  50. $bInContext = $iObjectPrivacyView < 0;
  51. $iContextId = $bInContext ? abs($iObjectPrivacyView) : 0;
  52. $aEvent = [
  53. 'owner_id' => 0,
  54. 'context_id' => $iContextId,
  55. 'type' => $oAlert->sUnit,
  56. 'action' => $oAlert->sAction,
  57. 'object_id' => $oAlert->iObject,
  58. 'object_owner_id' => $iObjectOwnerId,
  59. 'points' => 0,
  60. 'date' => time()
  61. ];
  62. if(($iPoints = (int)$aHandler['points_active']) != 0) {
  63. $this->_oModule->_oDb->insertEvent(array_merge($aEvent, [
  64. 'owner_id' => $iOwnerId,
  65. 'points' => $iPoints
  66. ]));
  67. $this->_oModule->assignPoints($iOwnerId, 0, $iPoints);
  68. if($bInContext)
  69. $this->_oModule->assignPoints($iOwnerId, $iContextId, $iPoints);
  70. }
  71. if(($iPoints = (int)$aHandler['points_passive']) != 0 && $iOwnerId != $iObjectOwnerId) {
  72. $this->_oModule->_oDb->insertEvent(array_merge($aEvent, [
  73. 'owner_id' => $iObjectOwnerId,
  74. 'points' => $iPoints
  75. ]));
  76. $this->_oModule->assignPoints($iObjectOwnerId, 0, $iPoints);
  77. if($bInContext)
  78. $this->_oModule->assignPoints($iObjectOwnerId, $iContextId, $iPoints);
  79. }
  80. }
  81. protected function _checkByActionConnectionAdded($oAlert)
  82. {
  83. if(($sType = $oAlert->aExtras['object']->getType()) && ($sType == 'one-way' || ($sType == 'mutual' && $oAlert->aExtras['mutual'])))
  84. return true;
  85. return false;
  86. }
  87. protected function _processProfileDelete($oAlert)
  88. {
  89. $this->_oModule->_oDb->deleteProfile($oAlert->iObject);
  90. }
  91. }
  92. /** @} */