BxDolInformer.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. * @param $aParams - optional params:
  71. * - `icon` - [string] font icon name to show instead of the default type icon, e.g. `user-plus`
  72. * - `action_url` - [string] URL of the trailing action button
  73. * - `action_title` - [string] title of the trailing action button, the button is shown only when both URL and title are provided
  74. */
  75. public function add ($sId, $sMsg, $iType = BX_INFORMER_INFO, $aParams = [])
  76. {
  77. if(!$this->_bEnabled)
  78. return;
  79. $this->_addJsCss();
  80. $this->_aMessages[$sId] = [
  81. 'id' => $sId,
  82. 'msg' => $sMsg,
  83. 'type' => $iType,
  84. 'icon' => !empty($aParams['icon']) ? $aParams['icon'] : '',
  85. 'action_url' => !empty($aParams['action_url']) ? $aParams['action_url'] : '',
  86. 'action_title' => !empty($aParams['action_title']) ? $aParams['action_title'] : '',
  87. ];
  88. }
  89. /**
  90. * Remove message from informer.
  91. * @param $sId - message id
  92. */
  93. public function remove ($sId)
  94. {
  95. unset($this->_aMessages[$sId]);
  96. }
  97. /**
  98. * Get message from informer.
  99. * @param $sId - message id
  100. */
  101. public function get ($sId)
  102. {
  103. return $this->_aMessages[$sId];
  104. }
  105. /**
  106. * Add permanent messages which are displayed on every page.
  107. */
  108. protected function _addPermanentMessages ()
  109. {
  110. // add account & profile related permament messages
  111. if (isLogged()) {
  112. $oAccount = BxDolAccount::getInstance();
  113. if ($oAccount)
  114. $oAccount->addInformerPermanentMessages($this);
  115. $oProfile = BxDolProfile::getInstance();
  116. if ($oProfile)
  117. $oProfile->addInformerPermanentMessages($this);
  118. }
  119. /**
  120. * @hooks
  121. * @hookdef hook-system-informer_permament_messages 'system', 'informer_permament_messages' - hook on informer
  122. * - $unit_name - equals `system`
  123. * - $action - equals `informer_permament_messages`
  124. * - $object_id - label id
  125. * - $sender_id - not used
  126. * - $extra_params - array of additional params with the following array keys:
  127. * - `informer` - [object] object with informer
  128. * @hook @ref hook-system-informer_permament_messages
  129. */
  130. bx_alert('system', 'informer_permament_messages', 0, false, array('informer' => $this));
  131. }
  132. }
  133. /** @} */