<?php
namespace magentotestControllerAdminhtmlIndex;
use MagentoBackendAppActionContext;
use MagentoFrameworkApiDataObjectHelper;
use MagentoFrameworkAppRequestInterface;
use MagentoFrameworkExceptionLocalizedException;
use MagentoFrameworkSerializeSerializerInterface;
use magentotestApiBannersRepositoryInterface;
use magentotestApiDataBannersInterfaceFactory;
use magentotestModelResourceModelBannersCollectionFactory as BannersItemFactory;
class Save extends MagentoBackendAppAction
{
/**
* @var RequestInterface
*/
private $request;
private $dataObjectHelper;
/**
* @var SerializerInterface
*/
private $serializer;
/**
* @var BannersItemFactory
*/
private BannersItemFactory $bannersItem;
/**
* @var BannersRepositoryInterface
*/
private BannersRepositoryInterface $bannersRepository;
/**
* @var BannersInterfaceFactory
*/
private BannersInterfaceFactory $bannersInterfaceFactory;
/**
* @param Context $context
* @param RequestInterface $request
* @param BannersItemFactory $bannersItem
* @param DataObjectHelper $dataObjectHelper
* @param BannersInterfaceFactory $bannersInterfaceFactory
* @param BannersRepositoryInterface $bannersRepository
* @param SerializerInterface $serializer
*/
public function __construct(
Context $context,
RequestInterface $request,
BannersItemFactory $bannersItem,
DataObjectHelper $dataObjectHelper,
BannersInterfaceFactory $bannersInterfaceFactory,
BannersRepositoryInterface $bannersRepository,
SerializerInterface $serializer
) {
$this->request = $request;
$this->bannersItem = $bannersItem;
$this->bannersRepository = $bannersRepository;
$this->bannersInterfaceFactory = $bannersInterfaceFactory;
$this->dataObjectHelper = $dataObjectHelper;
$this->serializer = $serializer;
parent::__construct($context);
}
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$data = $this->request->getPostValue();
try {
if (isset($data['id'])) {
$this->update($data);
$this->messageManager->addSuccess(__('You saved the banner.'));
return $resultRedirect->setPath('*/*/index');
} else {
$dataArray = [
'position' => $data['position'] ?? null,
'name' => $data['name'] ?? null,
'category_id' => $data['category_id'] ?? null,
'custom_link' => $data['custom_link'] ?? null,
'image' => isset($data['image']) ? explode('media/', $data['image'][0]['url'])[1] : null,
'image_two' => isset($data['image_two']) ? explode('media/', $data['image_two'][0]['url'])[1] : null,
'arabic_image_one' => isset($data['arabic_image_one']) ? explode('media/', $data['arabic_image_one'][0]['url'])[1] : null,
];
$bannersSaveData = $this->bannersInterfaceFactory->create();
$this->dataObjectHelper->populateWithArray(
$bannersSaveData,
$dataArray,
magentotestApiDataBannersInterface::class
);
$this->bannersRepository->save($bannersSaveData);
$this->messageManager->addSuccess(__('You have saved the Brands.'));
return $resultRedirect->setPath('*/*/index');
}
} catch (LocalizedException $e) {
$this->messageManager->addError(__($e->getMessage()));
return $resultRedirect->setPath('*/*/index');
}
}
public function update($data)
{
$resultRedirect = $this->resultRedirectFactory->create();
try {
$updateArray = [
'position' => $data['position'] ?? null,
'name' => $data['name'] ?? null,
'category_id' => $data['category_id'] ?? null,
'custom_link' => $data['custom_link'] ?? null,
'image' => isset($data['image']) ? explode('media/', $data['image'][0]['url'])[1] : null,
'image_two' => isset($data['image_two']) ? explode('media/', $data['image_two'][0]['url'])[1] : null,
'arabic_image_one' => isset($data['arabic_image_one']) ? explode('media/', $data['arabic_image_one'][0]['url'])[1] : null,
];
$bannersData = $this->bannersRepository->getById($data['id']);
$this->dataObjectHelper->populateWithArray(
$bannersData,
$updateArray,
magentoTestApiDataBannersInterface::class
);
$this->bannersRepository->save($bannersData);
} catch (LocalizedException $e) {
$this->messageManager->addError(__($e->getMessage()));
return $resultRedirect->setPath('*/*/show');
}
}
}