Isobar\Bai22\Controller\Adminhtml\Index\Save

 avatar
user_3906099546
plain_text
3 years ago
3.0 kB
2
Indexable
<?php

namespace Isobar\Bai22\Controller\Adminhtml\Index;

use Magento\Backend\App\Action;
use Isobar\Bai22\Model\Staff;
use Magento\Framework\App\Request\DataPersistorInterface;

class Save extends Action
{
    /**
     * Authorization level of a basic admin session
     */
    const ADMIN_RESOURCE = 'Isobar_Bai22::save';


    protected $dataPersistor;
    protected \Isobar\Bai22\Api\StaffRepositoryInterface $staffRepository;
    protected \Isobar\Bai22\Model\StaffFactory $staffFactory;

    /**
     * @param Action\Context $context
     * @param DataPersistorInterface $dataPersistor
     * @param \Isobar\Bai22\Api\StaffRepositoryInterface $staffRepository
     * @param \Isobar\Bai22\Model\StaffFactory $staffFactory
     */
    public function __construct(
        Action\Context $context,
        DataPersistorInterface $dataPersistor,
        \Isobar\Bai22\Api\StaffRepositoryInterface $staffRepository,
        \Isobar\Bai22\Model\StaffFactory $staffFactory
    )
    {
        $this->dataPersistor = $dataPersistor;
        parent::__construct($context);
        $this->staffRepository = $staffRepository;
        $this->staffFactory = $staffFactory;
    }

    public function execute()
    {

        $resultRedirect = $this->resultRedirectFactory->create();
        $data = $this->getRequest()->getPostValue();

        if ($data) {
            // Init model and load by ID if exists
            $model = $this->staffFactory->create();
            $id = $this->getRequest()->getParam('id');
            if ($id) {
                try {
                    $model = $this->staffRepository->getById($id);
                } catch (LocalizedException $e) {
                    $this->messageManager->addErrorMessage(__('This staff no longer exists.'));
                    return $resultRedirect->setPath('*/*/');
                }
            }


            // Validate data

            // Update model
            $model->setData('staff',$data['staff'])
                ->setData('age',$data['age'])
                ->setData('location',$data['location'])
                ->setData('status',$data['status']);

            // Save data to database
            try {
                $this->staffRepository->save($model);
                $this->messageManager->addSuccess(__('You saved the image.'));
                $this->dataPersistor->clear('staff');
                if ($this->getRequest()->getParam('back')) {
                    return $resultRedirect->setPath('*/*/edit', ['id' => $model->getId(), '_current' => true]);
                }
                return $resultRedirect->setPath('*/*/');
            } catch (\Exception $e) {
                $this->messageManager->addException($e, __('Something went wrong while saving the image.'));
            }

            $this->dataPersistor->set('staff', $data);
            return $resultRedirect->setPath('*/*/edit', ['id' => $this->getRequest()->getParam('id')]);
        }

        // Redirect to List page
        return $resultRedirect->setPath('*/*/');
    }
}