Window.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Critical notification window
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\AdminNotification\Block;
  9. /**
  10. * Admin notification window block
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Window extends \Magento\Backend\Block\Template
  16. {
  17. /**
  18. * XML path of Severity icons url
  19. */
  20. const XML_SEVERITY_ICONS_URL_PATH = 'system/adminnotification/severity_icons_url';
  21. /**
  22. * Severity icons url
  23. *
  24. * @var string
  25. */
  26. protected $_severityIconsUrl;
  27. /**
  28. * Authentication
  29. *
  30. * @var \Magento\Backend\Model\Auth\Session
  31. */
  32. protected $_authSession;
  33. /**
  34. * Critical messages collection
  35. *
  36. * @var \Magento\AdminNotification\Model\ResourceModel\Inbox\Collection
  37. */
  38. protected $_criticalCollection;
  39. /**
  40. * @var \Magento\AdminNotification\Model\Inbox
  41. */
  42. protected $_latestItem;
  43. /**
  44. * The property is used to define content-scope of block. Can be private or public.
  45. * If it isn't defined then application considers it as false.
  46. *
  47. * @var bool
  48. */
  49. protected $_isScopePrivate;
  50. /**
  51. * @param \Magento\Backend\Block\Template\Context $context
  52. * @param \Magento\Backend\Model\Auth\Session $authSession
  53. * @param \Magento\AdminNotification\Model\ResourceModel\Inbox\Collection\Critical $criticalCollection
  54. * @param array $data
  55. */
  56. public function __construct(
  57. \Magento\Backend\Block\Template\Context $context,
  58. \Magento\Backend\Model\Auth\Session $authSession,
  59. \Magento\AdminNotification\Model\ResourceModel\Inbox\Collection\Critical $criticalCollection,
  60. array $data = []
  61. ) {
  62. parent::__construct($context, $data);
  63. $this->_authSession = $authSession;
  64. $this->_criticalCollection = $criticalCollection;
  65. $this->_isScopePrivate = true;
  66. }
  67. /**
  68. * Render block
  69. *
  70. * @return string
  71. */
  72. protected function _toHtml()
  73. {
  74. if ($this->canShow()) {
  75. $this->setHeaderText($this->escapeHtml(__('Incoming Message')));
  76. $this->setCloseText($this->escapeHtml(__('close')));
  77. $this->setReadDetailsText($this->escapeHtml(__('Read Details')));
  78. $this->setNoticeMessageText($this->escapeHtml($this->_getLatestItem()->getTitle()));
  79. $this->setNoticeMessageUrl($this->escapeUrl($this->_getLatestItem()->getUrl()));
  80. $this->setSeverityText('critical');
  81. return parent::_toHtml();
  82. }
  83. return '';
  84. }
  85. /**
  86. * Retrieve latest critical item
  87. *
  88. * @return bool|\Magento\AdminNotification\Model\Inbox
  89. */
  90. protected function _getLatestItem()
  91. {
  92. if ($this->_latestItem == null) {
  93. $items = array_values($this->_criticalCollection->getItems());
  94. $this->_latestItem = false;
  95. if (count($items)) {
  96. $this->_latestItem = $items[0];
  97. }
  98. }
  99. return $this->_latestItem;
  100. }
  101. /**
  102. * Check whether block should be displayed
  103. *
  104. * @return bool
  105. */
  106. public function canShow()
  107. {
  108. return $this->_authSession->isFirstPageAfterLogin() && $this->_getLatestItem();
  109. }
  110. }