BxDolInformer.php 3.3 KB

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