BxBaseInformer.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 = array(
  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. public function __construct ($oTemplate)
  23. {
  24. parent::__construct ();
  25. if ($oTemplate)
  26. $this->_oTemplate = $oTemplate;
  27. else
  28. $this->_oTemplate = BxDolTemplate::getInstance();
  29. }
  30. /**
  31. * Display Informer.
  32. */
  33. public function display ()
  34. {
  35. if(!$this->_bEnabled)
  36. return '';
  37. if (!$this->_aMessages)
  38. return '';
  39. $aTmplVarsMessages = [];
  40. foreach ($this->_aMessages as $sId => $a) {
  41. $a['class'] = $this->_aMapType2Class[$a['type']];
  42. $aTmplVarsMessages[] = $a;
  43. }
  44. if (bx_is_api()){
  45. return $aTmplVarsMessages;
  46. }
  47. $this->_addJsCss();
  48. return $this->_oTemplate->parseHtmlByName('informer.html', [
  49. 'bx_repeat:messages' => $aTmplVarsMessages
  50. ]);
  51. }
  52. /**
  53. * Add css/js files which are needed for display and functionality.
  54. */
  55. protected function _addJsCss()
  56. {
  57. if ($this->_bJsCssAdded)
  58. return;
  59. $this->_oTemplate->addCss(['informer.css']);
  60. $this->_bJsCssAdded = true;
  61. }
  62. }
  63. /** @} */