MassDelete.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Search\Controller\Adminhtml\Synonyms;
  7. use Magento\Framework\App\Action\HttpPostActionInterface;
  8. /**
  9. * Mass-Delete Controller.
  10. *
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class MassDelete extends \Magento\Backend\App\Action implements HttpPostActionInterface
  14. {
  15. /**
  16. * Authorization level of a basic admin session.
  17. *
  18. * @see _isAllowed()
  19. */
  20. const ADMIN_RESOURCE = 'Magento_Search::synonyms';
  21. /**
  22. * @var \Magento\Ui\Component\MassAction\Filter
  23. */
  24. private $filter;
  25. /**
  26. * @var \Magento\Search\Model\ResourceModel\SynonymGroup\CollectionFactory
  27. */
  28. private $collectionFactory;
  29. /**
  30. * @var \Magento\Search\Api\SynonymGroupRepositoryInterface $synGroupRepository
  31. */
  32. private $synGroupRepository;
  33. /**
  34. * MassDelete constructor.
  35. *
  36. * @param \Magento\Backend\App\Action\Context $context
  37. * @param \Magento\Ui\Component\MassAction\Filter $filter
  38. * @param \Magento\Search\Model\ResourceModel\SynonymGroup\CollectionFactory $collectionFactory
  39. * @param \Magento\Search\Api\SynonymGroupRepositoryInterface $synGroupRepository
  40. */
  41. public function __construct(
  42. \Magento\Backend\App\Action\Context $context,
  43. \Magento\Ui\Component\MassAction\Filter $filter,
  44. \Magento\Search\Model\ResourceModel\SynonymGroup\CollectionFactory $collectionFactory,
  45. \Magento\Search\Api\SynonymGroupRepositoryInterface $synGroupRepository
  46. ) {
  47. $this->filter = $filter;
  48. $this->collectionFactory = $collectionFactory;
  49. $this->synGroupRepository = $synGroupRepository;
  50. parent::__construct($context);
  51. }
  52. /**
  53. * Execute action.
  54. *
  55. * @return \Magento\Backend\Model\View\Result\Redirect
  56. * @throws \Magento\Framework\Exception\LocalizedException|\Exception
  57. */
  58. public function execute()
  59. {
  60. $collection = $this->filter->getCollection($this->collectionFactory->create());
  61. $collectionSize = $collection->getSize();
  62. $deletedItems = 0;
  63. foreach ($collection as $synonymGroup) {
  64. try {
  65. $this->synGroupRepository->delete($synonymGroup);
  66. $deletedItems++;
  67. } catch (\Exception $e) {
  68. $this->messageManager->addErrorMessage($e->getMessage());
  69. }
  70. }
  71. if ($deletedItems != 0) {
  72. if ($collectionSize != $deletedItems) {
  73. $this->messageManager->addErrorMessage(
  74. __('Failed to delete %1 synonym group(s).', $collectionSize - $deletedItems)
  75. );
  76. }
  77. $this->messageManager->addSuccessMessage(
  78. __('A total of %1 synonym group(s) have been deleted.', $deletedItems)
  79. );
  80. }
  81. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  82. $resultRedirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
  83. return $resultRedirect->setPath('*/*/');
  84. }
  85. }