Inbox.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AdminNotification\Model\ResourceModel;
  7. /**
  8. * Inbox resource model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Inbox extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  14. {
  15. /**
  16. * AdminNotification Resource initialization
  17. *
  18. * @return void
  19. */
  20. protected function _construct()
  21. {
  22. $this->_init('adminnotification_inbox', 'notification_id');
  23. }
  24. /**
  25. * Load latest notice
  26. *
  27. * @param \Magento\AdminNotification\Model\Inbox $object
  28. * @return $this
  29. */
  30. public function loadLatestNotice(\Magento\AdminNotification\Model\Inbox $object)
  31. {
  32. $connection = $this->getConnection();
  33. $select = $connection->select()->from(
  34. $this->getMainTable()
  35. )->order(
  36. $this->getIdFieldName() . ' DESC'
  37. )->where(
  38. 'is_read != 1'
  39. )->where(
  40. 'is_remove != 1'
  41. )->limit(
  42. 1
  43. );
  44. $data = $connection->fetchRow($select);
  45. if ($data) {
  46. $object->setData($data);
  47. }
  48. $this->_afterLoad($object);
  49. return $this;
  50. }
  51. /**
  52. * Get notifications grouped by severity
  53. *
  54. * @param \Magento\AdminNotification\Model\Inbox $object
  55. * @return array
  56. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  57. */
  58. public function getNoticeStatus(\Magento\AdminNotification\Model\Inbox $object)
  59. {
  60. $connection = $this->getConnection();
  61. $select = $connection->select()->from(
  62. $this->getMainTable(),
  63. [
  64. 'severity' => 'severity',
  65. 'count_notice' => new \Zend_Db_Expr('COUNT(' . $this->getIdFieldName() . ')')
  66. ]
  67. )->group(
  68. 'severity'
  69. )->where(
  70. 'is_remove=?',
  71. 0
  72. )->where(
  73. 'is_read=?',
  74. 0
  75. );
  76. return $connection->fetchPairs($select);
  77. }
  78. /**
  79. * Save notifications (if not exists)
  80. *
  81. * @param \Magento\AdminNotification\Model\Inbox $object
  82. * @param array $data
  83. * @return void
  84. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  85. */
  86. public function parse(\Magento\AdminNotification\Model\Inbox $object, array $data)
  87. {
  88. $connection = $this->getConnection();
  89. foreach ($data as $item) {
  90. $select = $connection->select()->from($this->getMainTable())->where('title = ?', $item['title']);
  91. if (empty($item['url'])) {
  92. $select->where('url IS NULL');
  93. } else {
  94. $select->where('url = ?', $item['url']);
  95. }
  96. if (isset($item['internal'])) {
  97. $row = false;
  98. unset($item['internal']);
  99. } else {
  100. $row = $connection->fetchRow($select);
  101. }
  102. if (!$row) {
  103. $connection->insert($this->getMainTable(), $item);
  104. }
  105. }
  106. }
  107. }