BxBaseInformer.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Informer representation.
  11. * @see BxDolInformer
  12. */
  13. class BxBaseInformer extends BxDolInformer
  14. {
  15. protected $_bJsCssAdded = false;
  16. protected $_oTemplate;
  17. protected $_aMapType2Class = [
  18. BX_INFORMER_ALERT => 'bx-informer-msg-alert',
  19. BX_INFORMER_INFO => 'bx-informer-msg-info',
  20. BX_INFORMER_ERROR => 'bx-informer-msg-error',
  21. ];
  22. protected $_aMapType2Icon = [
  23. BX_INFORMER_ALERT => 'exclamation-triangle',
  24. BX_INFORMER_INFO => 'info-circle',
  25. BX_INFORMER_ERROR => 'times-circle',
  26. ];
  27. /**
  28. * Iconset used to render indicator icons as inline SVG regardless of the site's default iconset.
  29. * When it isn't available (or has no such icon) a font icon of the default iconset is used.
  30. */
  31. protected $_sIconset = 'sys_lucide';
  32. public function __construct ($oTemplate)
  33. {
  34. if ($oTemplate)
  35. $this->_oTemplate = $oTemplate;
  36. else
  37. $this->_oTemplate = BxDolTemplate::getInstance();
  38. parent::__construct ();
  39. }
  40. /**
  41. * Display Informer.
  42. */
  43. public function display ()
  44. {
  45. if(!$this->_bEnabled)
  46. return '';
  47. if (!$this->_aMessages)
  48. return '';
  49. $oIconset = BxDolIconset::getObjectInstance($this->_sIconset, $this->_oTemplate);
  50. $aMessages = [];
  51. foreach ($this->_aMessages as $a)
  52. $aMessages[] = array_merge($a, [
  53. 'class' => $this->_aMapType2Class[$a['type']],
  54. 'icon' => !empty($a['icon']) ? $a['icon'] : $this->_aMapType2Icon[$a['type']],
  55. ]);
  56. if (bx_is_api())
  57. return array_map(function($a) use($oIconset) {
  58. // API clients (NEO) render Lucide icons natively, so send the Lucide name.
  59. return array_merge($a, [
  60. 'icon' => $oIconset ? $oIconset->getIcon($a['icon']) : $a['icon'],
  61. ]);
  62. }, $aMessages);
  63. $this->_addJsCss();
  64. return $this->_oTemplate->parseHtmlByName('informer.html', [
  65. 'bx_repeat:messages' => array_map(function($a) use($oIconset) {
  66. $sIconHtml = $oIconset ? $oIconset->getIconHtml($a['icon']) : false;
  67. return array_merge($a, [
  68. 'bx_if:icon_html' => [
  69. 'condition' => $sIconHtml !== false,
  70. 'content' => [
  71. 'icon_html' => $sIconHtml,
  72. ],
  73. ],
  74. 'bx_if:icon_font' => [
  75. 'condition' => $sIconHtml === false,
  76. 'content' => [
  77. 'icon' => $a['icon'],
  78. ],
  79. ],
  80. 'bx_if:action' => [
  81. 'condition' => !empty($a['action_url']) && !empty($a['action_title']),
  82. 'content' => [
  83. 'action_url' => bx_html_attribute($a['action_url']),
  84. 'action_title' => $a['action_title'],
  85. ],
  86. ],
  87. ]);
  88. }, $aMessages),
  89. ]);
  90. }
  91. /**
  92. * Add css/js files which are needed for display and functionality.
  93. */
  94. protected function _addJsCss()
  95. {
  96. if ($this->_bJsCssAdded)
  97. return;
  98. $this->_oTemplate->addCss(['informer.css']);
  99. $this->_bJsCssAdded = true;
  100. }
  101. }
  102. /** @} */