132 lines
4.7 KiB
PHP
Executable File
132 lines
4.7 KiB
PHP
Executable File
<?php
|
|
|
|
namespace IpSupply\CustomBanner\Block;
|
|
|
|
use Magento\Framework\View\Element\Template;
|
|
use Magento\Store\Model\StoreManagerInterface;
|
|
use Magento\Framework\UrlInterface;
|
|
use Magento\Framework\View\Element\Template\Context;
|
|
use IpSupply\CustomBanner\Model\BannerFactory;
|
|
use Magento\MediaStorage\Model\File\UploaderFactory;
|
|
use Magento\Framework\App\Filesystem\DirectoryList;
|
|
use Magento\Framework\App\ObjectManager;
|
|
|
|
class CustomBanner extends Template {
|
|
protected $_storeManager;
|
|
protected $_bannerFactory;
|
|
protected $_fileUploader;
|
|
protected $_objectManager;
|
|
|
|
public function __construct(StoreManagerInterface $_storeManager, BannerFactory $_bannerFactory, Context $context,UploaderFactory $_fileUploader, array $data = []) {
|
|
$this->_storeManager = $_storeManager;
|
|
$this->_bannerFactory = $_bannerFactory;
|
|
$this->_fileUploader = $_fileUploader;
|
|
$this->_objectManager = ObjectManager::getInstance();
|
|
parent::__construct($context, $data);
|
|
}
|
|
|
|
protected function _prepareLayout() {
|
|
return parent::_prepareLayout();
|
|
}
|
|
|
|
public function getImagePath() {
|
|
$imagePath = $this->_storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
|
|
return $imagePath;
|
|
}
|
|
|
|
public function getBannerData() {
|
|
$banner = $this->_bannerFactory->create();
|
|
$bannerData = $banner->getCollection()->getData();
|
|
|
|
return $bannerData;
|
|
}
|
|
|
|
public function insertData($data, $files) {
|
|
try {
|
|
$title = $data["title"];
|
|
$description = $data["description"];
|
|
$url_redirect = $data["url_redirect"] != "" ? $data["url_redirect"] : "#";
|
|
$status = array_key_exists("status", $data) ? "active" : "inactive";
|
|
$image_url = null;
|
|
// Check image is uploaded
|
|
if ($files["image"]["error"] == 0) {
|
|
$uploader = $this->_fileUploader->create(["fileId" => "image"]);
|
|
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
|
|
$uploader->setAllowRenameFiles(false);
|
|
$uploader->setFilesDispersion(false);
|
|
$path = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('image_upload/');
|
|
$uploader->save($path);
|
|
$image_url = 'image_upload/'.$uploader->getUploadedFileName();
|
|
}
|
|
|
|
$banner = $this->_objectManager->create("IpSupply\CustomBanner\Model\Banner");
|
|
$response = $banner->addData([
|
|
"title" => $title,
|
|
"image_url" => $image_url,
|
|
"description" => $description,
|
|
"url_redirect" => $url_redirect,
|
|
"status" => $status
|
|
])->save();
|
|
return True;
|
|
}
|
|
catch (Exception $e) {
|
|
return False;
|
|
}
|
|
}
|
|
|
|
public function getBannerByID($idBanner) {
|
|
$banner = $this->_bannerFactory->create();
|
|
$bannerData = $banner->getCollection()->addFieldToFilter("id", ["equal" => $idBanner])->getData();
|
|
|
|
return count($bannerData) == 0 ? null : $bannerData[0];
|
|
}
|
|
|
|
public function updateBanner($idBanner, $dataUpdate, $files) {
|
|
try {
|
|
$title = $dataUpdate["title"];
|
|
$description = $dataUpdate["description"];
|
|
$url_redirect = $dataUpdate["url_redirect"] != "" ? $dataUpdate["url_redirect"] : "#";
|
|
$status = array_key_exists("status", $dataUpdate) ? "active" : "inactive";
|
|
|
|
// Get url of current image
|
|
$bannerCollection = $this->_bannerFactory->create();
|
|
$image_url = $bannerCollection->getCollection()->addFieldToSelect('image_url')
|
|
->addFieldToFilter("id", ["equal" => $idBanner])->getData()[0]["image_url"];
|
|
// Check if file is updated then upload file to server and $image_url
|
|
if ($files["image"]["error"] == 0) {
|
|
$uploader = $this->_fileUploader->create(["fileId" => "image"]);
|
|
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
|
|
$uploader->setAllowRenameFiles(false);
|
|
$uploader->setFilesDispersion(false);
|
|
$path = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('image_upload/');
|
|
$uploader->save($path);
|
|
$image_url = 'image_upload/'.$uploader->getUploadedFileName();
|
|
}
|
|
|
|
$banner = $this->_objectManager->create("IpSupply\CustomBanner\Model\Banner");
|
|
$bannerData = $banner->load($idBanner);
|
|
$response = $bannerData->setTitle($title)
|
|
->setDescription($description)
|
|
->setUrlRedirect($url_redirect)
|
|
->setStatus($status)
|
|
->setImageUrl($image_url)
|
|
->save();
|
|
return True;
|
|
}
|
|
catch (Exception $e) {
|
|
return False;
|
|
}
|
|
}
|
|
|
|
public function deleteBanner($idBanner) {
|
|
try {
|
|
$banner = $this->_objectManager->create("IpSupply\CustomBanner\Model\Banner");
|
|
$bannerData = $banner->load($idBanner);
|
|
$bannerData->delete();
|
|
return True;
|
|
}
|
|
catch (Exception $e) {
|
|
return False;
|
|
}
|
|
}
|
|
} |