ToolbarEntry.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AdminNotification\Block;
  7. /**
  8. * Toolbar entry that shows latest notifications
  9. *
  10. * @api
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. * @since 100.0.2
  13. */
  14. class ToolbarEntry extends \Magento\Backend\Block\Template
  15. {
  16. /**
  17. * Number of notifications showed on expandable window
  18. */
  19. const NOTIFICATIONS_NUMBER = 3;
  20. /**
  21. * Number of notifications showed on icon
  22. */
  23. const NOTIFICATIONS_COUNTER_MAX = 99;
  24. /**
  25. * Length of notification description showed by default
  26. */
  27. const NOTIFICATION_DESCRIPTION_LENGTH = 150;
  28. /**
  29. * Collection of latest unread notifications
  30. *
  31. * @var \Magento\AdminNotification\Model\ResourceModel\Inbox\Collection
  32. */
  33. protected $_notificationList;
  34. /**
  35. * @param \Magento\Backend\Block\Template\Context $context
  36. * @param \Magento\AdminNotification\Model\ResourceModel\Inbox\Collection\Unread $notificationList
  37. * @param array $data
  38. */
  39. public function __construct(
  40. \Magento\Backend\Block\Template\Context $context,
  41. \Magento\AdminNotification\Model\ResourceModel\Inbox\Collection\Unread $notificationList,
  42. array $data = []
  43. ) {
  44. parent::__construct($context, $data);
  45. $this->_notificationList = $notificationList;
  46. }
  47. /**
  48. * Retrieve notification description start length
  49. *
  50. * @return int
  51. */
  52. public function getNotificationDescriptionLength()
  53. {
  54. return self::NOTIFICATION_DESCRIPTION_LENGTH;
  55. }
  56. /**
  57. * Retrieve notification counter max value
  58. *
  59. * @return int
  60. */
  61. public function getNotificationCounterMax()
  62. {
  63. return self::NOTIFICATIONS_COUNTER_MAX;
  64. }
  65. /**
  66. * Retrieve number of unread notifications
  67. *
  68. * @return int
  69. */
  70. public function getUnreadNotificationCount()
  71. {
  72. return $this->_notificationList->getSize();
  73. }
  74. /**
  75. * Retrieve the list of latest unread notifications
  76. *
  77. * @return \Magento\AdminNotification\Model\ResourceModel\Inbox\Collection
  78. */
  79. public function getLatestUnreadNotifications()
  80. {
  81. return $this->_notificationList->setPageSize(self::NOTIFICATIONS_NUMBER);
  82. }
  83. /**
  84. * Format notification date (show only time if notification has been added today)
  85. *
  86. * @param string $dateString
  87. * @return string
  88. */
  89. public function formatNotificationDate($dateString)
  90. {
  91. $date = new \DateTime($dateString);
  92. if ($date == new \DateTime('today')) {
  93. return $this->_localeDate->formatDateTime(
  94. $date,
  95. \IntlDateFormatter::NONE,
  96. \IntlDateFormatter::SHORT
  97. );
  98. }
  99. return $this->_localeDate->formatDateTime(
  100. $date,
  101. \IntlDateFormatter::MEDIUM,
  102. \IntlDateFormatter::MEDIUM
  103. );
  104. }
  105. }