88 lines
3.7 KiB
PHP
Executable File
88 lines
3.7 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\Registry;
|
|
use Magento\Framework\Controller\ResultFactory;
|
|
|
|
class Edit extends Action implements HttpGetActionInterface, HttpPostActionInterface {
|
|
protected $resultFactory;
|
|
protected $resultPageFactory;
|
|
protected $_coreRegistry;
|
|
protected $_objectManager;
|
|
|
|
public function __construct(Context $context, Registry $registry,
|
|
PageFactory $resultPageFactory, ResultFactory $resultFactory) {
|
|
$this->resultFactory = $resultFactory;
|
|
$this->resultPageFactory = $resultPageFactory;
|
|
$this->_coreRegistry = $registry;
|
|
$this->_objectManager = ObjectManager::getInstance();
|
|
parent::__construct($context);
|
|
}
|
|
|
|
// This function will check if method request is POST then call block controller to update data
|
|
// If method request is GET then get current data and render into form update
|
|
public function execute() {
|
|
$blockInstance = $this->_objectManager->get("IpSupply\CustomBanner\Block\CustomBanner");
|
|
$postData = $this->getRequest()->getPostValue();
|
|
$idBanner = $this->getRequest()->getParam("id");
|
|
|
|
if ($postData) {
|
|
$files = $this->getRequest()->getFiles();
|
|
$response = $blockInstance->updateBanner($idBanner, $postData, $files); // Value of $response is True or False
|
|
if ($response == True) {
|
|
$this->messageManager->addSuccessMessage(__("Record Update Successfully."));
|
|
}
|
|
else {
|
|
$this->messageManager->addErrorMessage(__("We can't update record, Please try again."));
|
|
}
|
|
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
|
|
$resultRedirect->setPath('*/*/edit/id/'.$idBanner);
|
|
return $resultRedirect;
|
|
}
|
|
|
|
if ($idBanner) {
|
|
$banner = $blockInstance->getBannerByID($idBanner);
|
|
|
|
// If banner does not exist return index page
|
|
if ($banner == null) {
|
|
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
|
|
$resultRedirect->setPath('*/*/');
|
|
return $resultRedirect;
|
|
}
|
|
|
|
$id = $banner["id"];
|
|
$title = $banner["title"];
|
|
$image_url = $banner["image_url"];
|
|
$description = $banner["description"];
|
|
$url_redirect = $banner["url_redirect"];
|
|
$status = $banner["status"];
|
|
|
|
$resultPage = $this->resultPageFactory->create();
|
|
$resultPage->setActiveMenu("IpSupply_CustomBanner::banner");
|
|
$resultPage->getConfig()->getTitle()->prepend(__("Update Banner"));
|
|
|
|
$block = $resultPage->getLayout()->getBlock("custombanner.edit");
|
|
$block->setData("bannerID", $id)
|
|
->setData("bannerTitle", $title)
|
|
->setData("bannerImage", $image_url)
|
|
->setData("bannerDescription", $description)
|
|
->setData("bannerUrlRedirect", $url_redirect)
|
|
->setData("bannerStatus", $status);
|
|
|
|
return $resultPage;
|
|
}
|
|
|
|
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
|
|
$resultRedirect->setPath('*/*/');
|
|
return $resultRedirect;
|
|
}
|
|
} |