Your IP : 216.73.216.220


Current Path : /var/www/www.indacotrentino.com/www/app/code/Ashsmith/Blog/Controller/Adminhtml/Post/
Upload File :
Current File : //var/www/www.indacotrentino.com/www/app/code/Ashsmith/Blog/Controller/Adminhtml/Post/Save.php

<?php
namespace Ashsmith\Blog\Controller\Adminhtml\Post;

use Magento\Backend\App\Action;
use Magento\TestFramework\ErrorLog\Logger;
use Psr\Log\LoggerInterface;
use Ashsmith\Blog\Model\ImageUploader;

class Save extends \Magento\Backend\App\Action
{

    protected $logger;
    protected $imageUploader;

    /**
     * @param Action\Context $context
     */
    public function __construct(
        Action\Context $context,
        LoggerInterface $logger,
        ImageUploader $imageUploader
    )
    {
        $this->logger = $logger;
        $this->imageUploader = $imageUploader;
        parent::__construct($context);
    }

    /**
     * {@inheritdoc}
     */
    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('Ashsmith_Blog::save');
    }

    /**
     * Save action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        $data = $this->getRequest()->getPostValue();
        /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();
        if ($data) {
            /** @var \Ashsmith\Blog\Model\Post $model */
            $model = $this->_objectManager->create('Ashsmith\Blog\Model\Post');

            $id = $this->getRequest()->getParam('post_id');
            if ($id) {
                $model->load($id);
            }

            $model->setData($data);

            $this->_eventManager->dispatch(
                'blog_post_prepare_save',
                ['post' => $model, 'request' => $this->getRequest()]
            );

//            if (isset($data['image'][0]['name']) && isset($data['image'][0]['tmp_name'])) {
            if (isset($data['image'])) {
                try {
//                    $data['image'] = $this->imageUploader->saveImage('image');
                    $model->setData('image', $data['image']);
                } catch (\Exception $e) {
                    $this->messageManager->addError(__('Image could not be saved: ' . $e->getMessage()));
                    $data['image'] = ''; // Imposta a vuoto se fallisce
                }
            } elseif (isset($data['image']['delete']) && $data['image']['delete'] == 1) {
                $data['image'] = '';
                $model->setData('image', '');
            } else {
                unset($data['image']);
            }
            $this->logger->info(print_r($data, true));
            try {
                $model->save();
                $this->messageManager->addSuccess(__('You saved this Post.'));
                $this->_objectManager->get('Magento\Backend\Model\Session')->setFormData(false);
                if ($this->getRequest()->getParam('back')) {
                    return $resultRedirect->setPath('*/*/edit', ['post_id' => $model->getId(), '_current' => true]);
                }
                return $resultRedirect->setPath('*/*/');
            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                $this->messageManager->addError($e->getMessage());
            } catch (\RuntimeException $e) {
                $this->messageManager->addError($e->getMessage());
            } catch (\Exception $e) {
                $this->messageManager->addException($e, __('Something went wrong while saving the post.'));
                echo var_dump($e);
            }

            $this->_getSession()->setFormData($data);
            return $resultRedirect->setPath('*/*/edit', ['post_id' => $this->getRequest()->getParam('post_id')]);
        }
        return $resultRedirect->setPath('*/*/');
    }
}