Inbox.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AdminNotification\Model;
  7. use Magento\Framework\Notification\MessageInterface;
  8. use Magento\Framework\Notification\NotifierInterface;
  9. use Magento\AdminNotification\Model\InboxInterface;
  10. /**
  11. * AdminNotification Inbox model
  12. *
  13. * @method int getSeverity()
  14. * @method \Magento\AdminNotification\Model\Inbox setSeverity(int $value)
  15. * @method string getDateAdded()
  16. * @method \Magento\AdminNotification\Model\Inbox setDateAdded(string $value)
  17. * @method string getTitle()
  18. * @method \Magento\AdminNotification\Model\Inbox setTitle(string $value)
  19. * @method string getDescription()
  20. * @method \Magento\AdminNotification\Model\Inbox setDescription(string $value)
  21. * @method string getUrl()
  22. * @method \Magento\AdminNotification\Model\Inbox setUrl(string $value)
  23. * @method int getIsRead()
  24. * @method \Magento\AdminNotification\Model\Inbox setIsRead(int $value)
  25. * @method int getIsRemove()
  26. * @method \Magento\AdminNotification\Model\Inbox setIsRemove(int $value)
  27. *
  28. * @api
  29. * @since 100.0.2
  30. */
  31. class Inbox extends \Magento\Framework\Model\AbstractModel implements NotifierInterface, InboxInterface
  32. {
  33. /**
  34. * @return void
  35. */
  36. protected function _construct()
  37. {
  38. $this->_init(\Magento\AdminNotification\Model\ResourceModel\Inbox::class);
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getSeverities($severity = null)
  44. {
  45. $severities = [
  46. MessageInterface::SEVERITY_CRITICAL => __('critical'),
  47. MessageInterface::SEVERITY_MAJOR => __('major'),
  48. MessageInterface::SEVERITY_MINOR => __('minor'),
  49. MessageInterface::SEVERITY_NOTICE => __('notice'),
  50. ];
  51. if ($severity !== null) {
  52. if (isset($severities[$severity])) {
  53. return $severities[$severity];
  54. }
  55. return null;
  56. }
  57. return $severities;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function loadLatestNotice()
  63. {
  64. $this->setData([]);
  65. $this->getResource()->loadLatestNotice($this);
  66. return $this;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function getNoticeStatus()
  72. {
  73. return $this->getResource()->getNoticeStatus($this);
  74. }
  75. /**
  76. * Parse and save new data
  77. *
  78. * @param array $data
  79. * @return $this
  80. */
  81. public function parse(array $data)
  82. {
  83. $this->getResource()->parse($this, $data);
  84. return $this;
  85. }
  86. /**
  87. * Add new message
  88. *
  89. * @param int $severity
  90. * @param string $title
  91. * @param string|string[] $description
  92. * @param string $url
  93. * @param bool $isInternal
  94. * @throws \Magento\Framework\Exception\LocalizedException
  95. * @return $this
  96. */
  97. public function add($severity, $title, $description, $url = '', $isInternal = true)
  98. {
  99. if (!$this->getSeverities($severity)) {
  100. throw new \Magento\Framework\Exception\LocalizedException(__('Wrong message type'));
  101. }
  102. if (is_array($description)) {
  103. $description = '<ul><li>' . implode('</li><li>', $description) . '</li></ul>';
  104. }
  105. $date = date('Y-m-d H:i:s');
  106. $this->parse(
  107. [
  108. [
  109. 'severity' => $severity,
  110. 'date_added' => $date,
  111. 'title' => $title,
  112. 'description' => $description,
  113. 'url' => $url,
  114. 'internal' => $isInternal,
  115. ],
  116. ]
  117. );
  118. return $this;
  119. }
  120. /**
  121. * Add critical severity message
  122. *
  123. * @param string $title
  124. * @param string|string[] $description
  125. * @param string $url
  126. * @param bool $isInternal
  127. * @return $this
  128. */
  129. public function addCritical($title, $description, $url = '', $isInternal = true)
  130. {
  131. $this->add(MessageInterface::SEVERITY_CRITICAL, $title, $description, $url, $isInternal);
  132. return $this;
  133. }
  134. /**
  135. * Add major severity message
  136. *
  137. * @param string $title
  138. * @param string|string[] $description
  139. * @param string $url
  140. * @param bool $isInternal
  141. * @return $this
  142. */
  143. public function addMajor($title, $description, $url = '', $isInternal = true)
  144. {
  145. $this->add(MessageInterface::SEVERITY_MAJOR, $title, $description, $url, $isInternal);
  146. return $this;
  147. }
  148. /**
  149. * Add minor severity message
  150. *
  151. * @param string $title
  152. * @param string|string[] $description
  153. * @param string $url
  154. * @param bool $isInternal
  155. * @return $this
  156. */
  157. public function addMinor($title, $description, $url = '', $isInternal = true)
  158. {
  159. $this->add(MessageInterface::SEVERITY_MINOR, $title, $description, $url, $isInternal);
  160. return $this;
  161. }
  162. /**
  163. * Add notice
  164. *
  165. * @param string $title
  166. * @param string|string[] $description
  167. * @param string $url
  168. * @param bool $isInternal
  169. * @return $this
  170. */
  171. public function addNotice($title, $description, $url = '', $isInternal = true)
  172. {
  173. $this->add(MessageInterface::SEVERITY_NOTICE, $title, $description, $url, $isInternal);
  174. return $this;
  175. }
  176. }