BxDolInformer.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. define('BX_INFORMER_ALERT', 1);
  10. define('BX_INFORMER_INFO', 2);
  11. define('BX_INFORMER_ERROR', 3);
  12. /**
  13. * Informer.
  14. *
  15. * It displays alerts or information messages in clearly visible area on the page to let user know important information.
  16. *
  17. * @section example Example of usage
  18. *
  19. * Adding message to informer:
  20. *
  21. * @code
  22. * $oInformer = BxDolInformer::getInstance(); // get object instance
  23. * if ($oInformer) // check if Informer is available for using
  24. * echo $oInformer->add ('my_id', 'Some important information here', BX_INFORMER_ALERT); // add an alert message
  25. * @endcode
  26. *
  27. */
  28. class BxDolInformer extends BxDolFactory implements iBxDolSingleton
  29. {
  30. protected $_bEnabled = true;
  31. protected $_aMessages = array();
  32. /**
  33. * Constructor
  34. */
  35. protected function __construct()
  36. {
  37. if (isset($GLOBALS['bxDolClasses']['BxDolInformer']))
  38. trigger_error ('Multiple instances are not allowed for the BxDolInformer class.', E_USER_ERROR);
  39. parent::__construct();
  40. }
  41. /**
  42. * Prevent cloning the instance
  43. */
  44. public function __clone()
  45. {
  46. if (isset($GLOBALS['bxDolClasses']['BxDolInformer']))
  47. trigger_error('Clone is not allowed for the BxDolInformer class.', E_USER_ERROR);
  48. }
  49. /**
  50. * Get Informer object instance
  51. * @return object instance or false on error
  52. */
  53. public static function getInstance($oTemplate = false)
  54. {
  55. if (isset($GLOBALS['bxDolClasses']['BxDolInformer']))
  56. return $GLOBALS['bxDolClasses']['BxDolInformer'];
  57. $o = new BxTemplInformer($oTemplate);
  58. return ($GLOBALS['bxDolClasses']['BxDolInformer'] = $o);
  59. }
  60. public function setEnabled($bEnabled)
  61. {
  62. $this->_bEnabled = $bEnabled;
  63. }
  64. /**
  65. * Add message to informer.
  66. * @param $sId - message id
  67. * @param $sMsg - message text
  68. * @param $iType - message type: BX_INFORMER_ALERT, BX_INFORMER_INFO or BX_INFORMER_ERROR
  69. */
  70. public function add ($sId, $sMsg, $iType = BX_INFORMER_INFO)
  71. {
  72. $this->_aMessages[$sId] = array (
  73. 'id' => $sId,
  74. 'msg' => $sMsg,
  75. 'type' => $iType,
  76. );
  77. }
  78. /**
  79. * Remove message from informer.
  80. * @param $sId - message id
  81. */
  82. public function remove ($sId)
  83. {
  84. unset($this->_aMessages[$sId]);
  85. }
  86. /**
  87. * Get message from informer.
  88. * @param $sId - message id
  89. */
  90. public function get ($sId)
  91. {
  92. return $this->_aMessages[$sId];
  93. }
  94. /**
  95. * Add permanent messages which are displayed on every page.
  96. */
  97. protected function _addPermanentMessages ()
  98. {
  99. // add account & profile related permament messages
  100. if (isLogged()) {
  101. $oAccount = BxDolAccount::getInstance();
  102. if ($oAccount)
  103. $oAccount->addInformerPermanentMessages($this);
  104. $oProfile = BxDolProfile::getInstance();
  105. if ($oProfile)
  106. $oProfile->addInformerPermanentMessages($this);
  107. }
  108. // add permament messages from modules
  109. bx_alert('system', 'informer_permament_messages', 0, false, array('informer' => $this));
  110. }
  111. }
  112. /** @} */