MassDelete.php 3.1 KB

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