AbstractMassAction.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Controller\Adminhtml\Index;
  7. use Magento\Eav\Model\Entity\Collection\AbstractCollection;
  8. use Magento\Framework\Controller\ResultFactory;
  9. use Magento\Framework\App\ResponseInterface;
  10. use Magento\Framework\Controller\ResultInterface;
  11. use Magento\Backend\App\Action\Context;
  12. use Magento\Ui\Component\MassAction\Filter;
  13. use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
  14. /**
  15. * Class AbstractMassStatus
  16. */
  17. abstract class AbstractMassAction extends \Magento\Backend\App\Action
  18. {
  19. /**
  20. * Authorization level of a basic admin session
  21. *
  22. * @see _isAllowed()
  23. */
  24. const ADMIN_RESOURCE = 'Magento_Customer::manage';
  25. /**
  26. * @var string
  27. */
  28. protected $redirectUrl = '*/*/index';
  29. /**
  30. * @var Filter
  31. */
  32. protected $filter;
  33. /**
  34. * @var CollectionFactory
  35. */
  36. protected $collectionFactory;
  37. /**
  38. * @param Context $context
  39. * @param Filter $filter
  40. * @param CollectionFactory $collectionFactory
  41. */
  42. public function __construct(Context $context, Filter $filter, CollectionFactory $collectionFactory)
  43. {
  44. parent::__construct($context);
  45. $this->filter = $filter;
  46. $this->collectionFactory = $collectionFactory;
  47. }
  48. /**
  49. * Execute action
  50. *
  51. * @return \Magento\Backend\Model\View\Result\Redirect
  52. * @throws \Magento\Framework\Exception\LocalizedException|\Exception
  53. */
  54. public function execute()
  55. {
  56. try {
  57. $collection = $this->filter->getCollection($this->collectionFactory->create());
  58. return $this->massAction($collection);
  59. } catch (\Exception $e) {
  60. $this->messageManager->addError($e->getMessage());
  61. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  62. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  63. return $resultRedirect->setPath($this->redirectUrl);
  64. }
  65. }
  66. /**
  67. * Return component referer url
  68. * TODO: Technical dept referer url should be implement as a part of Action configuration in appropriate way
  69. *
  70. * @return null|string
  71. */
  72. protected function getComponentRefererUrl()
  73. {
  74. return $this->filter->getComponentRefererUrl()?: 'customer/*/index';
  75. }
  76. /**
  77. * Execute action to collection items
  78. *
  79. * @param AbstractCollection $collection
  80. * @return ResponseInterface|ResultInterface
  81. */
  82. abstract protected function massAction(AbstractCollection $collection);
  83. }