BxDolAlerts.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * @package Dolphin Core
  4. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  5. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  6. */
  7. defined('BX_DOL') or die('hack attempt');
  8. /**
  9. * Alert/Handler engine.
  10. *
  11. * Is needed to fire some alert(event) in one place and caught it with a handler somewhere else.
  12. *
  13. * Related classes:
  14. * BxDolAlertsResponse - abstract class for all response classes.
  15. * BxDolAlertsResponseUser - response class to process standard profile related alerts.
  16. *
  17. * Example of usage:
  18. * 1. Fire an alert
  19. *
  20. * @code
  21. * $oZ = new BxDolAlerts('unit_name', 'action', 'object_id', 'sender_id', 'extra_params');
  22. * $oZ->alert();
  23. * @endcode
  24. *
  25. * 2. Add handler and caught alert(s) @see BxDolAlertsResponseUser
  26. * a. Create Response class extending BxDolAlertsResponse class. It should process all necessary alerts which are passed to it.
  27. * b. Register your handler in the database by adding it in `sys_alerts_handlers` table.
  28. * c. Associate necessary alerts with the handler by adding them in the `sys_alerts` table.
  29. *
  30. *
  31. * Memberships/ACL:
  32. * Doesn't depend on user's membership.
  33. *
  34. *
  35. * Alerts:
  36. * no alerts available
  37. *
  38. */
  39. class BxDolAlerts extends BxDol {
  40. var $sUnit;
  41. var $sAction;
  42. var $iObject;
  43. var $iSender;
  44. var $aExtras;
  45. var $_aAlerts;
  46. var $_aHandlers;
  47. /**
  48. * Constructor
  49. * @param string $sType - system type
  50. * @param string $sAction - system action
  51. * @param int $iObjectId - object id
  52. * @param int $iSenderId - sender (action's author) profile id, if it is false - then currectly logged in profile id is used
  53. */
  54. public function BxDolAlerts($sUnit, $sAction, $iObjectId, $iSender = false, $aExtras = array()) {
  55. parent::BxDol();
  56. $oDb = BxDolDb::getInstance();
  57. $oCache = $oDb->getDbCacheObject();
  58. $aData = $oCache->getData($oDb->genDbCacheKey('sys_alerts'));
  59. if (null === $aData)
  60. $aData = BxDolAlerts::cache();
  61. $this->_aAlerts = $aData['alerts'];
  62. $this->_aHandlers = $aData['handlers'];
  63. $this->sUnit = $sUnit;
  64. $this->sAction = $sAction;
  65. $this->iObject = (int)$iObjectId;
  66. $this->aExtras = $aExtras;
  67. if (false === $iSender) {
  68. bx_import('BxDolProfile');
  69. $oProfile = BxDolProfile::getInstance();
  70. $this->iSender = $oProfile ? $oProfile->id() : 0;
  71. } else {
  72. $this->iSender = (int)$iSender;
  73. }
  74. }
  75. /**
  76. * Notifies the necessary handlers about the alert.
  77. */
  78. public function alert() {
  79. if (isset($this->_aAlerts[$this->sUnit]) && isset($this->_aAlerts[$this->sUnit][$this->sAction]))
  80. foreach($this->_aAlerts[$this->sUnit][$this->sAction] as $iHandlerId) {
  81. $aHandler = $this->_aHandlers[$iHandlerId];
  82. if (!empty($aHandler['file']) && !empty($aHandler['class']) && file_exists(BX_DIRECTORY_PATH_ROOT . $aHandler['file'])) {
  83. if(!class_exists($aHandler['class']))
  84. require_once(BX_DIRECTORY_PATH_ROOT . $aHandler['file']);
  85. $oHandler = new $aHandler['class']();
  86. $oHandler->response($this);
  87. } else if (!empty($aHandler['eval'])) {
  88. eval($aHandler['eval']);
  89. }
  90. }
  91. }
  92. /**
  93. * Cache alerts and handlers.
  94. *
  95. * @return an array with all alerts and handlers.
  96. */
  97. public static function cache() {
  98. $oDb = BxDolDb::getInstance();
  99. $aResult = array('alerts' => array(), 'handlers' => array());
  100. $aAlerts = $oDb->getAll("SELECT `unit`, `action`, `handler_id` FROM `sys_alerts` ORDER BY `id` ASC");
  101. foreach ($aAlerts as $aAlert)
  102. $aResult['alerts'][$aAlert['unit']][$aAlert['action']][] = $aAlert['handler_id'];
  103. $aHandlers = $oDb->getAll("SELECT `id`, `class`, `file`, `eval` FROM `sys_alerts_handlers` ORDER BY `id` ASC");
  104. foreach ($aHandlers as $aHandler)
  105. $aResult['handlers'][$aHandler['id']] = array('class' => $aHandler['class'], 'file' => $aHandler['file'], 'eval' => $aHandler['eval']);
  106. $oCache = $oDb->getDbCacheObject();
  107. $oCache->setData ($oDb->genDbCacheKey('sys_alerts'), $aResult);
  108. return $aResult;
  109. }
  110. }
  111. class BxDolAlertsResponse extends BxDol
  112. {
  113. public function __construct() {
  114. parent::__construct();
  115. }
  116. public function response($oAlert) {
  117. }
  118. }