Delete.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. /**
  11. * Temando Delete Location Action
  12. *
  13. * @package Temando\Shipping\Controller
  14. * @author Benjamin Heuer <benjamin.heuer@netresearch.de>
  15. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  16. * @link https://www.temando.com/
  17. */
  18. class Delete extends Action
  19. {
  20. const ADMIN_RESOURCE = 'Temando_Shipping::locations';
  21. /**
  22. * @var LocationRepositoryInterface
  23. */
  24. private $locationRepository;
  25. /**
  26. * Temando Location Delete Action constructor.
  27. *
  28. * @param Context $context
  29. * @param LocationRepositoryInterface $locationRepository
  30. */
  31. public function __construct(Context $context, LocationRepositoryInterface $locationRepository)
  32. {
  33. $this->locationRepository = $locationRepository;
  34. parent::__construct($context);
  35. }
  36. /**
  37. * Execute action.
  38. *
  39. * @return \Magento\Framework\Controller\Result\Redirect
  40. */
  41. public function execute()
  42. {
  43. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  44. $resultRedirect = $this->resultRedirectFactory->create();
  45. $resultRedirect->setPath('*/*/index');
  46. $locationId = $this->getRequest()->getParam('location_id', false);
  47. if (!$locationId) {
  48. $this->messageManager->addErrorMessage(__('Location ID missing.'));
  49. return $resultRedirect;
  50. }
  51. try {
  52. $this->locationRepository->delete($locationId);
  53. $this->messageManager->addSuccessMessage(__('Location was deleted successfully.'));
  54. } catch (CouldNotDeleteException $e) {
  55. $message = __('An error occurred while deleting the location: %1', $e->getMessage());
  56. $this->messageManager->addExceptionMessage($e, $message);
  57. return $resultRedirect;
  58. }
  59. return $resultRedirect;
  60. }
  61. }