MassDelete.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Controller\Adminhtml\Address;
  8. use Magento\Backend\App\Action;
  9. use Magento\Framework\App\Action\HttpPostActionInterface;
  10. use Magento\Framework\Controller\Result\Json;
  11. use Magento\Backend\App\Action\Context;
  12. use Magento\Framework\Controller\Result\JsonFactory;
  13. use Magento\Framework\Exception\LocalizedException;
  14. use Magento\Framework\Exception\NoSuchEntityException;
  15. use Magento\Ui\Component\MassAction\Filter;
  16. use Magento\Customer\Model\ResourceModel\Address\CollectionFactory;
  17. use Magento\Customer\Api\AddressRepositoryInterface;
  18. use Psr\Log\LoggerInterface;
  19. /**
  20. * Class to delete selected customer addresses through massaction
  21. */
  22. class MassDelete extends Action implements HttpPostActionInterface
  23. {
  24. /**
  25. * Authorization level of a basic admin session
  26. *
  27. * @see MassDelete::_isAllowed()
  28. */
  29. const ADMIN_RESOURCE = 'Magento_Customer::manage';
  30. /**
  31. * @var Filter
  32. */
  33. private $filter;
  34. /**
  35. * @var CollectionFactory
  36. */
  37. private $collectionFactory;
  38. /**
  39. * @var AddressRepositoryInterface
  40. */
  41. private $addressRepository;
  42. /**
  43. * @var LoggerInterface
  44. */
  45. private $logger;
  46. /**
  47. * @var JsonFactory
  48. */
  49. private $resultJsonFactory;
  50. /**
  51. * @param Context $context
  52. * @param Filter $filter
  53. * @param CollectionFactory $collectionFactory
  54. * @param AddressRepositoryInterface $addressRepository
  55. * @param LoggerInterface $logger
  56. * @param JsonFactory $resultJsonFactory
  57. */
  58. public function __construct(
  59. Context $context,
  60. Filter $filter,
  61. CollectionFactory $collectionFactory,
  62. AddressRepositoryInterface $addressRepository,
  63. LoggerInterface $logger,
  64. JsonFactory $resultJsonFactory
  65. ) {
  66. $this->filter = $filter;
  67. $this->collectionFactory = $collectionFactory;
  68. $this->addressRepository = $addressRepository;
  69. $this->logger = $logger;
  70. $this->resultJsonFactory = $resultJsonFactory;
  71. parent::__construct($context);
  72. }
  73. /**
  74. * Delete specified customer addresses using grid massaction
  75. *
  76. * @return Json
  77. * @throws LocalizedException
  78. */
  79. public function execute(): Json
  80. {
  81. $customerData = $this->_session->getData('customer_data');
  82. /** @var \Magento\Customer\Model\ResourceModel\Address\Collection $collection */
  83. $collection = $this->filter->getCollection($this->collectionFactory->create());
  84. $error = false;
  85. try {
  86. if ($customerData && $customerData['customer_id']) {
  87. $collection->addFieldToFilter('parent_id', $customerData['customer_id']);
  88. } else {
  89. throw new \Exception();
  90. }
  91. $collectionSize = $collection->getSize();
  92. /** @var \Magento\Customer\Model\Address $address */
  93. foreach ($collection as $address) {
  94. $this->addressRepository->deleteById($address->getId());
  95. }
  96. $message = __('A total of %1 record(s) have been deleted.', $collectionSize);
  97. } catch (NoSuchEntityException $e) {
  98. $message = __('There is no such address entity to delete.');
  99. $error = true;
  100. $this->logger->critical($e);
  101. } catch (LocalizedException $e) {
  102. $message = __($e->getMessage());
  103. $error = true;
  104. $this->logger->critical($e);
  105. } catch (\Exception $e) {
  106. $message = __('We can\'t mass delete the addresses right now.');
  107. $error = true;
  108. $this->logger->critical($e);
  109. }
  110. $resultJson = $this->resultJsonFactory->create();
  111. $resultJson->setData(
  112. [
  113. 'message' => $message,
  114. 'error' => $error,
  115. ]
  116. );
  117. return $resultJson;
  118. }
  119. }