magento2-docker/app/code/IpSupply/CustomBanner/Controller/Adminhtml/Banner/NewAction.php

49 lines
2.0 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace IpSupply\CustomBanner\Controller\Adminhtml\Banner;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Controller\ResultFactory;
class NewAction extends Action implements HttpPostActionInterface, HttpGetActionInterface {
protected $resultFactory;
protected $resultPageFactory;
protected $_objectManager;
public function __construct(Context $context, PageFactory $resultPageFactory, ResultFactory $resultFactory) {
$this->resultFactory = $resultFactory;
$this->resultPageFactory = $resultPageFactory;
$this->_objectManager = ObjectManager::getInstance();
parent::__construct($context);
}
public function execute() {
$postData = $this->getRequest()->getPostValue();
if ($postData) {
$files = $this->getRequest()->getFiles();
$blockInstance = $this->_objectManager->get("IpSupply\CustomBanner\Block\CustomBanner");
$response = $blockInstance->insertData($postData, $files); // Value of $response is True or False
if ($response == True) {
$this->messageManager->addSuccessMessage(__("Record Add Successfully."));
}
else {
$this->messageManager->addErrorMessage(__("We can't add record, Please try again."));
}
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setPath('*/*/');
return $resultRedirect;
}
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu("IpSupply_CustomBanner::banner");
$resultPage->getConfig()->getTitle()->prepend(__("Add new"));
return $resultPage;
}
}