MassDelete.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  8. use Magento\Backend\App\Action\Context;
  9. use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
  10. use Magento\Eav\Model\Entity\Collection\AbstractCollection;
  11. use Magento\Ui\Component\MassAction\Filter;
  12. use Magento\Customer\Api\CustomerRepositoryInterface;
  13. use Magento\Framework\Controller\ResultFactory;
  14. /**
  15. * Class MassDelete
  16. */
  17. class MassDelete extends AbstractMassAction implements HttpPostActionInterface
  18. {
  19. /**
  20. * Authorization level of a basic admin session
  21. *
  22. * @see _isAllowed()
  23. */
  24. const ADMIN_RESOURCE = 'Magento_Customer::delete';
  25. /**
  26. * @var CustomerRepositoryInterface
  27. */
  28. protected $customerRepository;
  29. /**
  30. * @param Context $context
  31. * @param Filter $filter
  32. * @param CollectionFactory $collectionFactory
  33. * @param CustomerRepositoryInterface $customerRepository
  34. */
  35. public function __construct(
  36. Context $context,
  37. Filter $filter,
  38. CollectionFactory $collectionFactory,
  39. CustomerRepositoryInterface $customerRepository
  40. ) {
  41. parent::__construct($context, $filter, $collectionFactory);
  42. $this->customerRepository = $customerRepository;
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. protected function massAction(AbstractCollection $collection)
  48. {
  49. $customersDeleted = 0;
  50. foreach ($collection->getAllIds() as $customerId) {
  51. $this->customerRepository->deleteById($customerId);
  52. $customersDeleted++;
  53. }
  54. if ($customersDeleted) {
  55. $this->messageManager->addSuccess(__('A total of %1 record(s) were deleted.', $customersDeleted));
  56. }
  57. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  58. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  59. $resultRedirect->setPath($this->getComponentRefererUrl());
  60. return $resultRedirect;
  61. }
  62. }