Delete.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace Smartwave\Dailydeals\Controller\Adminhtml\Dailydeal;
  3. class Delete extends \Smartwave\Dailydeals\Controller\Adminhtml\Dailydeal
  4. {
  5. /**
  6. * execute action
  7. *
  8. * @return \Magento\Backend\Model\View\Result\Redirect
  9. */
  10. public function execute()
  11. {
  12. $resultRedirect = $this->resultRedirectFactory->create();
  13. $id = $this->getRequest()->getParam('dailydeal_id');
  14. if ($id) {
  15. $sw_product_sku = "";
  16. try {
  17. /** @var \Smartwave\Dailydeals\Model\Dailydeal $dailydeal */
  18. $dailydeal = $this->dailydealFactory->create();
  19. $dailydeal->load($id);
  20. $sw_product_sku = $dailydeal->getSw_product_sku();
  21. $dailydeal->delete();
  22. $this->messageManager->addSuccess(__('The Dailydeal has been deleted.'));
  23. $this->_eventManager->dispatch(
  24. 'adminhtml_sw_dailydeals_dailydeal_on_delete',
  25. ['sw_product_sku' => $sw_product_sku, 'status' => 'success']
  26. );
  27. $resultRedirect->setPath('sw_dailydeals/*/');
  28. return $resultRedirect;
  29. } catch (\Exception $e) {
  30. $this->_eventManager->dispatch(
  31. 'adminhtml_sw_dailydeals_dailydeal_on_delete',
  32. ['sw_product_sku' => $sw_product_sku, 'status' => 'fail']
  33. );
  34. // display error message
  35. $this->messageManager->addError($e->getMessage());
  36. // go back to edit form
  37. $resultRedirect->setPath('sw_dailydeals/*/edit', ['dailydeal_id' => $id]);
  38. return $resultRedirect;
  39. }
  40. }
  41. // display error message
  42. $this->messageManager->addError(__('Dailydeal to delete was not found.'));
  43. // go to grid
  44. $resultRedirect->setPath('sw_dailydeals/*/');
  45. return $resultRedirect;
  46. }
  47. }