BxDolInformer.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. $this->_addPermanentMessages();
  41. }
  42. /**
  43. * Prevent cloning the instance
  44. */
  45. public function __clone()
  46. {
  47. if (isset($GLOBALS['bxDolClasses']['BxDolInformer']))
  48. trigger_error('Clone is not allowed for the BxDolInformer class.', E_USER_ERROR);
  49. }
  50. /**
  51. * Get Informer object instance
  52. * @return object instance or false on error
  53. */
  54. public static function getInstance($oTemplate = false)
  55. {
  56. if (isset($GLOBALS['bxDolClasses']['BxDolInformer']))
  57. return $GLOBALS['bxDolClasses']['BxDolInformer'];
  58. $o = new BxTemplInformer($oTemplate);
  59. return ($GLOBALS['bxDolClasses']['BxDolInformer'] = $o);
  60. }
  61. public function setEnabled($bEnabled)
  62. {
  63. $this->_bEnabled = $bEnabled;
  64. }
  65. /**
  66. * Add message to informer.
  67. * @param $sId - message id
  68. * @param $sMsg - message text
  69. * @param $iType - message type: BX_INFORMER_ALERT, BX_INFORMER_INFO or BX_INFORMER_ERROR
  70. */
  71. public function add ($sId, $sMsg, $iType = BX_INFORMER_INFO)
  72. {
  73. if(!$this->_bEnabled)
  74. return;
  75. $this->_addJsCss();
  76. $this->_aMessages[$sId] = [
  77. 'id' => $sId,
  78. 'msg' => $sMsg,
  79. 'type' => $iType,
  80. ];
  81. }
  82. /**
  83. * Remove message from informer.
  84. * @param $sId - message id
  85. */
  86. public function remove ($sId)
  87. {
  88. unset($this->_aMessages[$sId]);
  89. }
  90. /**
  91. * Get message from informer.
  92. * @param $sId - message id
  93. */
  94. public function get ($sId)
  95. {
  96. return $this->_aMessages[$sId];
  97. }
  98. /**
  99. * Add permanent messages which are displayed on every page.
  100. */
  101. protected function _addPermanentMessages ()
  102. {
  103. // add account & profile related permament messages
  104. if (isLogged()) {
  105. $oAccount = BxDolAccount::getInstance();
  106. if ($oAccount)
  107. $oAccount->addInformerPermanentMessages($this);
  108. $oProfile = BxDolProfile::getInstance();
  109. if ($oProfile)
  110. $oProfile->addInformerPermanentMessages($this);
  111. }
  112. /**
  113. * @hooks
  114. * @hookdef hook-system-informer_permament_messages 'system', 'informer_permament_messages' - hook on informer
  115. * - $unit_name - equals `system`
  116. * - $action - equals `informer_permament_messages`
  117. * - $object_id - label id
  118. * - $sender_id - not used
  119. * - $extra_params - array of additional params with the following array keys:
  120. * - `informer` - [object] object with informer
  121. * @hook @ref hook-system-informer_permament_messages
  122. */
  123. bx_alert('system', 'informer_permament_messages', 0, false, array('informer' => $this));
  124. }
  125. }
  126. /** @} */