MassMarkAsRead.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\AdminNotification\Controller\Adminhtml\Notification;
  8. class MassMarkAsRead extends \Magento\AdminNotification\Controller\Adminhtml\Notification
  9. {
  10. /**
  11. * Authorization level of a basic admin session
  12. *
  13. * @see _isAllowed()
  14. */
  15. const ADMIN_RESOURCE = 'Magento_AdminNotification::mark_as_read';
  16. /**
  17. * @return void
  18. */
  19. public function execute()
  20. {
  21. $ids = $this->getRequest()->getParam('notification');
  22. if (!is_array($ids)) {
  23. $this->messageManager->addErrorMessage(__('Please select messages.'));
  24. } else {
  25. try {
  26. foreach ($ids as $id) {
  27. $model = $this->_objectManager->create(\Magento\AdminNotification\Model\Inbox::class)->load($id);
  28. if ($model->getId()) {
  29. $model->setIsRead(1)->save();
  30. }
  31. }
  32. $this->messageManager->addSuccessMessage(
  33. __('A total of %1 record(s) have been marked as Read.', count($ids))
  34. );
  35. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  36. $this->messageManager->addErrorMessage($e->getMessage());
  37. } catch (\Exception $e) {
  38. $this->messageManager->addExceptionMessage(
  39. $e,
  40. __("We couldn't mark the notification as Read because of an error.")
  41. );
  42. }
  43. }
  44. $this->_redirect('adminhtml/*/');
  45. }
  46. }