| Current Path : /var/www/html/app/code/FlipsDigital/TextbookList/Controller/Adminhtml/TextbookList/ |
| Current File : /var/www/html/app/code/FlipsDigital/TextbookList/Controller/Adminhtml/TextbookList/Save.php |
<?php
/**
* Save
*
* @copyright Copyright © 2021 FlipsDigital. All rights reserved.
* @author calvin.so@flipsdigital.com
*/
namespace FlipsDigital\TextbookList\Controller\Adminhtml\TextbookList;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use FlipsDigital\TextbookList\Model\TextbookListFactory;
class Save extends Action
{
/** @var TextbookListFactory $objectFactory */
protected $objectFactory;
/**
* @param Context $context
* @param TextbookListFactory $objectFactory
*/
public function __construct(
Context $context,
TextbookListFactory $objectFactory
) {
$this->objectFactory = $objectFactory;
parent::__construct($context);
}
/**
* {@inheritdoc}
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('FlipsDigital_TextbookList::textbooklist');
}
/**
* Save action
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
$storeId = (int)$this->getRequest()->getParam('store_id');
$data = $this->getRequest()->getParams();
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
if ($data) {
$params = [];
$objectInstance = $this->objectFactory->create();
$objectInstance->setStoreId($storeId);
$params['store'] = $storeId;
if (empty($data['entity_id'])) {
$data['entity_id'] = null;
} else {
$objectInstance->load($data['entity_id']);
$params['entity_id'] = $data['entity_id'];
}
$objectInstance->addData($data);
$this->_eventManager->dispatch(
'flipsdigital_textbooklist_textbooklist_prepare_save',
['object' => $this->objectFactory, 'request' => $this->getRequest()]
);
try {
if($objectInstance->getData('identifier') == null)
$objectInstance->setIdentifier($this->generateRandomString());
$objectInstance->save();
$this->messageManager->addSuccessMessage(__('You saved this record.'));
$this->_getSession()->setFormData(false);
if ($this->getRequest()->getParam('back')) {
$params['entity_id'] = $objectInstance->getId();
$params['_current'] = true;
return $resultRedirect->setPath('*/*/edit', $params);
}
return $resultRedirect->setPath('*/*/');
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the record.'));
}
$this->_getSession()->setFormData($this->getRequest()->getPostValue());
return $resultRedirect->setPath('*/*/edit', $params);
}
return $resultRedirect->setPath('*/*/');
}
}