Edit.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Smartwave\Dailydeals\Controller\Adminhtml\Dailydeal;
  3. class Edit extends \Smartwave\Dailydeals\Controller\Adminhtml\Dailydeal
  4. {
  5. /**
  6. * Backend session
  7. *
  8. * @var \Magento\Backend\Model\Session
  9. */
  10. protected $backendSession;
  11. /**
  12. * Page factory
  13. *
  14. * @var \Magento\Framework\View\Result\PageFactory
  15. */
  16. protected $resultPageFactory;
  17. /**
  18. * Result JSON factory
  19. *
  20. * @var \Magento\Framework\Controller\Result\JsonFactory
  21. */
  22. protected $resultJsonFactory;
  23. /**
  24. * constructor
  25. *
  26. * @param \Magento\Backend\Model\Session $backendSession
  27. * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
  28. * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
  29. * @param \Smartwave\Dailydeals\Model\DailydealFactory $dailydealFactory
  30. * @param \Magento\Framework\Registry $registry
  31. * @param \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory
  32. * @param \Magento\Backend\App\Action\Context $context
  33. */
  34. public function __construct(
  35. \Magento\Framework\View\Result\PageFactory $resultPageFactory,
  36. \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
  37. \Smartwave\Dailydeals\Model\DailydealFactory $dailydealFactory,
  38. \Magento\Framework\Registry $registry,
  39. \Magento\Backend\App\Action\Context $context
  40. ) {
  41. $this->backendSession = $context->getSession();
  42. $this->resultPageFactory = $resultPageFactory;
  43. $this->resultJsonFactory = $resultJsonFactory;
  44. parent::__construct($dailydealFactory, $registry, $context);
  45. }
  46. /**
  47. * is action allowed
  48. *
  49. * @return bool
  50. */
  51. protected function _isAllowed()
  52. {
  53. return $this->_authorization->isAllowed('Smartwave_Dailydeals::dailydeal');
  54. }
  55. /**
  56. * @return \Magento\Backend\Model\View\Result\Page|\Magento\Backend\Model\View\Result\Redirect|\Magento\Framework\View\Result\Page
  57. */
  58. public function execute()
  59. {
  60. $id = $this->getRequest()->getParam('dailydeal_id');
  61. /** @var \Smartwave\Dailydeals\Model\Dailydeal $dailydeal */
  62. $dailydeal = $this->initDailydeal();
  63. /** @var \Magento\Backend\Model\View\Result\Page|\Magento\Framework\View\Result\Page $resultPage */
  64. $resultPage = $this->resultPageFactory->create();
  65. $resultPage->setActiveMenu('Smartwave_Dailydeals::dailydeal');
  66. $resultPage->getConfig()->getTitle()->set(__('Dailydeals'));
  67. if ($id) {
  68. $dailydeal->load($id);
  69. if (!$dailydeal->getId()) {
  70. $this->messageManager->addError(__('This Dailydeal no longer exists.'));
  71. $resultRedirect = $this->resultRedirectFactory->create();
  72. $resultRedirect->setPath(
  73. 'sw_dailydeals/*/edit',
  74. [
  75. 'dailydeal_id' => $dailydeal->getId(),
  76. '_current' => true
  77. ]
  78. );
  79. return $resultRedirect;
  80. }
  81. }
  82. $title = $dailydeal->getId() ? $dailydeal->getSw_product_sku() : __('New Dailydeal');
  83. $resultPage->getConfig()->getTitle()->prepend($title);
  84. $data = $this->backendSession->getData('sw_dailydeals_dailydeal_data', true);
  85. if (!empty($data)) {
  86. $dailydeal->setData($data);
  87. }
  88. return $resultPage;
  89. }
  90. }