MassDelete.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Controller\Adminhtml\Configuration\Location;
  6. use Magento\Backend\App\Action;
  7. use Magento\Backend\App\Action\Context;
  8. use Magento\Framework\Exception\CouldNotDeleteException;
  9. use Temando\Shipping\Model\ResourceModel\Repository\LocationRepositoryInterface;
  10. use Temando\Shipping\Ui\Component\MassAction\Filter;
  11. /**
  12. * Temando Mass Delete Location Action
  13. *
  14. * @package Temando\Shipping\Controller
  15. * @author Benjamin Heuer <benjamin.heuer@netresearch.de>
  16. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  17. * @link https://www.temando.com/
  18. */
  19. class MassDelete extends Action
  20. {
  21. const ADMIN_RESOURCE = 'Temando_Shipping::locations';
  22. /**
  23. * @var LocationRepositoryInterface
  24. */
  25. private $locationRepository;
  26. /**
  27. * @var Filter
  28. */
  29. private $filter;
  30. /**
  31. * Temando Location Mass Delete Action constructor.
  32. *
  33. * @param Context $context
  34. * @param LocationRepositoryInterface $locationRepository
  35. * @param Filter $filter
  36. */
  37. public function __construct(
  38. Context $context,
  39. LocationRepositoryInterface $locationRepository,
  40. Filter $filter
  41. ) {
  42. $this->locationRepository = $locationRepository;
  43. $this->filter = $filter;
  44. parent::__construct($context);
  45. }
  46. /**
  47. * Execute action.
  48. *
  49. * @return \Magento\Framework\Controller\Result\Redirect
  50. */
  51. public function execute()
  52. {
  53. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  54. $resultRedirect = $this->resultRedirectFactory->create();
  55. $resultRedirect->setPath('*/*/index');
  56. $selected = $this->getRequest()->getParam(\Magento\Ui\Component\MassAction\Filter::SELECTED_PARAM, []);
  57. $excluded = $this->getRequest()->getParam(\Magento\Ui\Component\MassAction\Filter::EXCLUDED_PARAM, []);
  58. if ($excluded === 'false') {
  59. $excluded = [];
  60. }
  61. $locationIds = $this->filter->getLocationIds($this->locationRepository, $selected, $excluded);
  62. $requestedItemsCount = count($locationIds);
  63. $deletedItemsCount = 0;
  64. foreach ($locationIds as $locationId) {
  65. try {
  66. $this->locationRepository->delete($locationId);
  67. $deletedItemsCount++;
  68. } catch (CouldNotDeleteException $e) {
  69. $message = __('Location %1 cannot be deleted: %2', $locationId, $e->getMessage());
  70. $this->messageManager->addExceptionMessage($e, $message);
  71. }
  72. }
  73. $resultMessage = __('A total of %1 record(s) have been deleted.', $deletedItemsCount);
  74. if ($requestedItemsCount !== $deletedItemsCount) {
  75. $this->messageManager->addWarningMessage($resultMessage);
  76. $errorMessage = 'An error occurred while deleting locations.';
  77. $errorMessage.= ' Please see the log files for more detailed information.';
  78. $this->messageManager->addErrorMessage(__($errorMessage));
  79. } else {
  80. $this->messageManager->addSuccessMessage($resultMessage);
  81. }
  82. return $resultRedirect;
  83. }
  84. }