This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,63 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Blog
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/
namespace Mageplaza\Blog\Controller\Adminhtml\Post;
use Exception;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultInterface;
use Mageplaza\Blog\Controller\Adminhtml\Post;
/**
* Class Delete
* @package Mageplaza\Blog\Controller\Adminhtml\Post
*/
class Delete extends Post
{
/**
* @return ResponseInterface|Redirect|ResultInterface
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
if ($id = $this->getRequest()->getParam('id')) {
try {
$this->postFactory->create()
->load($id)
->delete();
$this->messageManager->addSuccessMessage(__('The Post has been deleted.'));
} catch (Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$resultRedirect->setPath('mageplaza_blog/*/edit', ['id' => $id]);
return $resultRedirect;
}
} else {
$this->messageManager->addErrorMessage(__('Post to delete was not found.'));
}
$resultRedirect->setPath('mageplaza_blog/*/');
return $resultRedirect;
}
}
@@ -0,0 +1,75 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Blog
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/
namespace Mageplaza\Blog\Controller\Adminhtml\Post;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\Forward;
use Magento\Backend\Model\View\Result\ForwardFactory;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Registry;
use Mageplaza\Blog\Controller\Adminhtml\Post;
use Mageplaza\Blog\Model\PostFactory;
/**
* Class Edit
* @package Mageplaza\Blog\Controller\Adminhtml\Post
*/
class Duplicate extends Post
{
/**
* Redirect result factory
*
* @var ForwardFactory
*/
public $resultForwardFactory;
/**
* Duplicate constructor.
*
* @param Context $context
* @param Registry $registry
* @param PostFactory $postFactory
* @param ForwardFactory $resultForwardFactory
*/
public function __construct(
Context $context,
Registry $registry,
PostFactory $postFactory,
ForwardFactory $resultForwardFactory
) {
$this->resultForwardFactory = $resultForwardFactory;
parent::__construct($postFactory, $registry, $context);
}
/**
* @return Forward|ResponseInterface|ResultInterface
*/
public function execute()
{
$resultForward = $this->resultForwardFactory->create();
$resultForward->forward('edit');
return $resultForward;
}
}
+97
View File
@@ -0,0 +1,97 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Blog
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/
namespace Mageplaza\Blog\Controller\Adminhtml\Post;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
use Mageplaza\Blog\Controller\Adminhtml\Post;
use Mageplaza\Blog\Model\PostFactory;
/**
* Class Edit
* @package Mageplaza\Blog\Controller\Adminhtml\Post
*/
class Edit extends Post
{
/**
* Page factory
*
* @var PageFactory
*/
public $resultPageFactory;
/**
* Edit constructor.
*
* @param Context $context
* @param Registry $registry
* @param PostFactory $postFactory
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
Registry $registry,
PostFactory $postFactory,
PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($postFactory, $registry, $context);
}
/**
* @return \Magento\Backend\Model\View\Result\Page|Redirect|Page
*/
public function execute()
{
/** @var \Mageplaza\Blog\Model\Post $post */
$post = $this->initPost();
$duplicate = $this->getRequest()->getParam('duplicate');
if (!$post) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('*');
return $resultRedirect;
}
$data = $this->_session->getData('mageplaza_blog_post_data', true);
if (!empty($data)) {
$post->setData($data);
}
$this->coreRegistry->register('mageplaza_blog_post', $post);
/** @var \Magento\Backend\Model\View\Result\Page|Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu('Mageplaza_Blog::post');
$resultPage->getConfig()->getTitle()->set(__('Posts'));
$title = $post->getId() && !$duplicate ? $post->getName() : __('New Post');
$resultPage->getConfig()->getTitle()->prepend($title);
return $resultPage;
}
}
@@ -0,0 +1,72 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Blog
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/
namespace Mageplaza\Blog\Controller\Adminhtml\Post;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\LayoutFactory;
use Mageplaza\Blog\Controller\Adminhtml\Post;
use Mageplaza\Blog\Model\PostFactory;
/**
* Class Products
* @package Mageplaza\Blog\Controller\Adminhtml\Post
*/
class History extends Post
{
/**
* @var LayoutFactory
*/
protected $resultLayoutFactory;
/**
* Products constructor.
*
* @param Context $context
* @param PostFactory $productFactory
* @param Registry $registry
* @param LayoutFactory $resultLayoutFactory
*/
public function __construct(
Context $context,
Registry $registry,
PostFactory $productFactory,
LayoutFactory $resultLayoutFactory
) {
parent::__construct($productFactory, $registry, $context);
$this->resultLayoutFactory = $resultLayoutFactory;
}
/**
* Save action
*
* @return ResultInterface
*/
public function execute()
{
$this->initPost(true);
return $this->resultLayoutFactory->create();
}
}
@@ -0,0 +1,30 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Blog
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/
namespace Mageplaza\Blog\Controller\Adminhtml\Post;
/**
* Class ProductsGrid
* @package Mageplaza\Blog\Controller\Adminhtml\Post
*/
class HistoryGrid extends History
{
}
@@ -0,0 +1,69 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Blog
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/
namespace Mageplaza\Blog\Controller\Adminhtml\Post;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
/**
* Class Index
* @package Mageplaza\Blog\Controller\Adminhtml\Post
*/
class Index extends Action
{
/**
* Page result factory
*
* @var PageFactory
*/
public $resultPageFactory;
/**
* Index constructor.
*
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
/**
* execute the action
*
* @return \Magento\Backend\Model\View\Result\Page|Page
*/
public function execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->prepend(__('Posts'));
return $resultPage;
}
}
@@ -0,0 +1,130 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Blog
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/
namespace Mageplaza\Blog\Controller\Adminhtml\Post;
use Exception;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;
use Mageplaza\Blog\Model\Post;
use Mageplaza\Blog\Model\PostFactory;
use RuntimeException;
/**
* Class InlineEdit
* @package Mageplaza\Blog\Controller\Adminhtml\Post
*/
class InlineEdit extends Action
{
/**
* JSON Factory
*
* @var JsonFactory
*/
public $jsonFactory;
/**
* Post Factory
*
* @var PostFactory
*/
public $postFactory;
/**
* InlineEdit constructor.
*
* @param Context $context
* @param JsonFactory $jsonFactory
* @param PostFactory $postFactory
*/
public function __construct(
Context $context,
JsonFactory $jsonFactory,
PostFactory $postFactory
) {
$this->jsonFactory = $jsonFactory;
$this->postFactory = $postFactory;
parent::__construct($context);
}
/**
* @return ResultInterface
*/
public function execute()
{
/** @var Json $resultJson */
$resultJson = $this->jsonFactory->create();
$error = false;
$messages = [];
$postItems = $this->getRequest()->getParam('items', []);
if (!($this->getRequest()->getParam('isAjax') && !empty($postItems))) {
return $resultJson->setData([
'messages' => [__('Please correct the data sent.')],
'error' => true,
]);
}
$key = array_keys($postItems);
$postId = !empty($key) ? (int)$key[0] : '';
/** @var Post $post */
$post = $this->postFactory->create()->load($postId);
try {
$postData = $postItems[$postId];
$post->addData($postData);
$post->save();
} catch (LocalizedException $e) {
$messages[] = $this->getErrorWithPostId($post, $e->getMessage());
$error = true;
} catch (RuntimeException $e) {
$messages[] = $this->getErrorWithPostId($post, $e->getMessage());
$error = true;
} catch (Exception $e) {
$messages[] = $this->getErrorWithPostId(
$post,
__('Something went wrong while saving the Post.')
);
$error = true;
}
return $resultJson->setData([
'messages' => $messages,
'error' => $error
]);
}
/**
* Add Post id to error message
*
* @param Post $post
* @param string $errorText
*
* @return string
*/
public function getErrorWithPostId(Post $post, $errorText)
{
return '[Post ID: ' . $post->getId() . '] ' . $errorText;
}
}
@@ -0,0 +1,94 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Blog
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/
namespace Mageplaza\Blog\Controller\Adminhtml\Post;
use Exception;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Ui\Component\MassAction\Filter;
use Mageplaza\Blog\Model\ResourceModel\Post\CollectionFactory;
/**
* Class MassDelete
* @package Mageplaza\Blog\Controller\Adminhtml\Post
*/
class MassDelete extends Action
{
/**
* Mass Action Filter
*
* @var Filter
*/
public $filter;
/**
* Collection Factory
*
* @var CollectionFactory
*/
public $collectionFactory;
/**
* constructor
*
* @param Filter $filter
* @param CollectionFactory $collectionFactory
* @param Context $context
*/
public function __construct(
Context $context,
Filter $filter,
CollectionFactory $collectionFactory
) {
$this->filter = $filter;
$this->collectionFactory = $collectionFactory;
parent::__construct($context);
}
/**
* @return $this|ResponseInterface|ResultInterface
* @throws LocalizedException
*/
public function execute()
{
$collection = $this->filter->getCollection($this->collectionFactory->create());
try {
$collection->walk('delete');
$this->messageManager->addSuccessMessage(__('Posts has been deleted.'));
} catch (Exception $e) {
$this->messageManager->addErrorMessage(__('Something wrong when delete Posts.'));
}
/** @var Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath('*/*/');
}
}
@@ -0,0 +1,107 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Blog
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/
namespace Mageplaza\Blog\Controller\Adminhtml\Post;
use Exception;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Ui\Component\MassAction\Filter;
use Mageplaza\Blog\Model\ResourceModel\Post\CollectionFactory;
/**
* Class MassStatus
* @package Mageplaza\Blog\Controller\Adminhtml\Post
*/
class MassStatus extends Action
{
/**
* Mass Action Filter
*
* @var Filter
*/
public $filter;
/**
* Collection Factory
*
* @var CollectionFactory
*/
public $collectionFactory;
/**
* constructor
*
* @param Filter $filter
* @param CollectionFactory $collectionFactory
* @param Context $context
*/
public function __construct(
Context $context,
Filter $filter,
CollectionFactory $collectionFactory
) {
$this->filter = $filter;
$this->collectionFactory = $collectionFactory;
parent::__construct($context);
}
/**
* @return $this|ResponseInterface|ResultInterface
* @throws LocalizedException
*/
public function execute()
{
$collection = $this->filter->getCollection($this->collectionFactory->create());
$status = (int)$this->getRequest()->getParam('status');
$postUpdated = 0;
foreach ($collection as $post) {
try {
$post->setEnabled($status)
->save();
$postUpdated++;
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (Exception $e) {
$this->_getSession()->addException(
$e,
__('Something went wrong while updating status for %1.', $post->getName())
);
}
}
if ($postUpdated) {
$this->messageManager->addSuccessMessage(__('A total of %1 record(s) have been updated.', $postUpdated));
}
/** @var Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath('*/*/');
}
}
@@ -0,0 +1,69 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Blog
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/
namespace Mageplaza\Blog\Controller\Adminhtml\Post;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\Forward;
use Magento\Backend\Model\View\Result\ForwardFactory;
/**
* Class NewAction
* @package Mageplaza\Blog\Controller\Adminhtml\Post
*/
class NewAction extends Action
{
/**
* Redirect result factory
*
* @var ForwardFactory
*/
public $resultForwardFactory;
/**
* constructor
*
* @param ForwardFactory $resultForwardFactory
* @param Context $context
*/
public function __construct(
Context $context,
ForwardFactory $resultForwardFactory
) {
$this->resultForwardFactory = $resultForwardFactory;
parent::__construct($context);
}
/**
* forward to edit
*
* @return Forward
*/
public function execute()
{
$resultForward = $this->resultForwardFactory->create();
$resultForward->forward('edit');
return $resultForward;
}
}
@@ -0,0 +1,86 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Blog
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/
namespace Mageplaza\Blog\Controller\Adminhtml\Post;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\LayoutFactory;
use Mageplaza\Blog\Controller\Adminhtml\Post;
use Mageplaza\Blog\Model\PostFactory;
use Mageplaza\Blog\Model\PostHistoryFactory;
/**
* Class Products
* @package Mageplaza\Blog\Controller\Adminhtml\Post
*/
class Products extends Post
{
/**
* @var LayoutFactory
*/
protected $resultLayoutFactory;
/**
* @var PostHistoryFactory
*/
protected $postHistoryFactory;
/**
* Products constructor.
*
* @param Context $context
* @param Registry $registry
* @param PostFactory $productFactory
* @param PostHistoryFactory $postHistoryFactory
* @param LayoutFactory $resultLayoutFactory
*/
public function __construct(
Context $context,
Registry $registry,
PostFactory $productFactory,
PostHistoryFactory $postHistoryFactory,
LayoutFactory $resultLayoutFactory
) {
parent::__construct($productFactory, $registry, $context);
$this->resultLayoutFactory = $resultLayoutFactory;
$this->postHistoryFactory = $postHistoryFactory;
}
/**
* Save action
*
* @return ResultInterface
*/
public function execute()
{
if ($this->getRequest()->getParam('history')) {
$history = $this->postHistoryFactory->create()->load($this->getRequest()->getParam('id'));
$this->coreRegistry->register('mageplaza_blog_post', $history);
} else {
$this->initPost(true);
}
return $this->resultLayoutFactory->create();
}
}
@@ -0,0 +1,30 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Blog
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/
namespace Mageplaza\Blog\Controller\Adminhtml\Post;
/**
* Class ProductsGrid
* @package Mageplaza\Blog\Controller\Adminhtml\Post
*/
class ProductsGrid extends Products
{
}
+330
View File
@@ -0,0 +1,330 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Blog
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/
namespace Mageplaza\Blog\Controller\Adminhtml\Post;
use Exception;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Helper\Js;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Mageplaza\Blog\Controller\Adminhtml\Post;
use Mageplaza\Blog\Helper\Data;
use Mageplaza\Blog\Helper\Image;
use Mageplaza\Blog\Model\Post as PostModel;
use Mageplaza\Blog\Model\PostFactory;
use Mageplaza\Blog\Model\PostHistoryFactory;
use RuntimeException;
/**
* Class Save
* @package Mageplaza\Blog\Controller\Adminhtml\Post
*/
class Save extends Post
{
/**
* JS helper
*
* @var Js
*/
public $jsHelper;
/**
* @var DateTime
*/
public $date;
/**
* @var Image
*/
protected $imageHelper;
/**
* @var Data
*/
protected $_helperData;
/**
* @var PostHistoryFactory
*/
protected $_postHistory;
/**
* @var TimezoneInterface
*/
protected $timezone;
/**
* Save constructor.
*
* @param Context $context
* @param Registry $registry
* @param PostFactory $postFactory
* @param Js $jsHelper
* @param Image $imageHelper
* @param Data $helperData
* @param PostHistoryFactory $postHistory
* @param DateTime $date
* @param TimezoneInterface $timezone
*/
public function __construct(
Context $context,
Registry $registry,
PostFactory $postFactory,
Js $jsHelper,
Image $imageHelper,
Data $helperData,
PostHistoryFactory $postHistory,
DateTime $date,
TimezoneInterface $timezone
) {
$this->jsHelper = $jsHelper;
$this->_helperData = $helperData;
$this->_postHistory = $postHistory;
$this->imageHelper = $imageHelper;
$this->date = $date;
$this->timezone = $timezone;
parent::__construct($postFactory, $registry, $context);
}
/**
* @return ResponseInterface|Redirect|ResultInterface
* @throws LocalizedException
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$action = $this->getRequest()->getParam('action');
if ($data = $this->getRequest()->getPost('post')) {
/** @var PostModel $post */
$post = $this->initPost(false, true);
$this->prepareData($post, $data);
try {
if (empty($action) || $action === 'add') {
$post->save();
$this->_eventManager->dispatch(
'mageplaza_blog_post_prepare_save',
['post' => $post, 'request' => $this->getRequest()]
);
$this->messageManager->addSuccessMessage(__('The post has been saved.'));
}
$this->addHistory($post, $action);
$this->_getSession()->setData('mageplaza_blog_post_data', false);
if ($this->getRequest()->getParam('back')) {
$resultRedirect->setPath('mageplaza_blog/*/edit', ['id' => $post->getId(), '_current' => true]);
} else {
$resultRedirect->setPath('mageplaza_blog/*/');
}
return $resultRedirect;
} catch (RuntimeException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (Exception $e) {
$this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the Post.'));
}
$this->_getSession()->setData('mageplaza_blog_post_data', $data);
$resultRedirect->setPath('mageplaza_blog/*/edit', ['id' => $post->getId(), '_current' => true]);
return $resultRedirect;
}
$resultRedirect->setPath('mageplaza_blog/*/');
return $resultRedirect;
}
/**
* @param PostModel $post
* @param null $action
*/
protected function addHistory($post, $action = null)
{
if (!empty($action)) {
$history = $this->_postHistory->create();
$historyCount = $history->getSumPostHistory($post->getPostId());
$limitHistory = (int) $this->_helperData->getConfigGeneral('history_limit');
try {
$data = $post->getData();
unset(
$data['is_changed_tag_list'],
$data['is_changed_topic_list'],
$data['is_changed_category_list'],
$data['is_changed_product_list']
);
if ($isSave = $this->checkHistory($data)) {
$this->messageManager->addErrorMessage(__(
'Record Id %1 like the one you want to save.',
$isSave->getId()
));
} else {
if ($historyCount >= $limitHistory) {
$history->removeFirstHistory($post->getPostId());
}
$history->addData($data);
$history->save();
}
} catch (RuntimeException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (Exception $e) {
$this->messageManager->addExceptionMessage(
$e,
__('Something went wrong while saving the Post History.')
);
}
}
}
/**
* @param array $data
*
* @return DataObject|null
*/
protected function checkHistory($data)
{
unset($data['updated_at']);
$historyItems = $this->_postHistory->create()->getCollection()
->addFieldToFilter('post_id', $data['post_id'])->getItems();
if (count($historyItems) < 1) {
return null;
}
$data['category_ids'] = implode(',', $data['categories_ids']);
$data['topic_ids'] = implode(',', $data['topics_ids']);
$data['tag_ids'] = implode(',', $data['tags_ids']);
$data['product_ids'] = Data::jsonEncode($data['products_data']);
$result = null;
foreach ($historyItems as $historyItem) {
$subReturn = false;
foreach ($historyItem->getData() as $key => $value) {
if (array_key_exists($key, $data)) {
if (is_array($data[$key])) {
$data[$key] = trim(implode(',', $data[$key]), ',');
}
if ($data[$key] === null) {
$data[$key] = '';
}
if ($value === null) {
$value = '';
}
if ($data[$key] !== $value) {
$subReturn = true;
break;
}
}
}
if (!$subReturn) {
$result = $historyItem;
break;
}
}
return $result;
}
/**
* @param PostModel $post
* @param array $data
*
* @return $this
* @throws LocalizedException
*/
protected function prepareData($post, $data = [])
{
if (!$this->getRequest()->getParam('image')) {
try {
$this->imageHelper->uploadImage($data, 'image', Image::TEMPLATE_MEDIA_TYPE_POST, $post->getImage());
} catch (Exception $exception) {
$data['image'] = isset($data['image']['value']) ? $data['image']['value'] : '';
}
} else {
$data['image'] = '';
}
/** Set specify field data */
try {
$data['publish_date'] = $this->timezone->convertConfigTimeToUtc($data['publish_date']);
} catch (Exception $e) {
$data['publish_date'] = $this->timezone->convertConfigTimeToUtc($this->date->date());
}
$data['modifier_id'] = $this->_auth->getUser()->getId();
$data['categories_ids'] = (isset($data['categories_ids']) && $data['categories_ids']) ? explode(
',',
$data['categories_ids'] ?? ''
) : [];
$data['tags_ids'] = (isset($data['tags_ids']) && $data['tags_ids'])
? explode(',', $data['tags_ids'] ?? '') : [];
$data['topics_ids'] = (isset($data['topics_ids']) && $data['topics_ids']) ? explode(
',',
$data['topics_ids'] ?? ''
) : [];
if ($post->getCreatedAt() == null) {
$data['created_at'] = $this->date->date();
}
$data['updated_at'] = $this->date->date();
$this->_helperData->handleSeoValueBeforeSave($data);
$post->addData($data);
if ($tags = $this->getRequest()->getPost('tags', false)) {
$post->setTagsData(
$this->jsHelper->decodeGridSerializedInput($tags)
);
}
if ($topics = $this->getRequest()->getPost('topics', false)) {
$post->setTopicsData(
$this->jsHelper->decodeGridSerializedInput($topics)
);
}
$products = $this->getRequest()->getPost('products', false);
if ($products || $products === '') {
$post->setProductsData(
$this->jsHelper->decodeGridSerializedInput($products)
);
} else {
$productData = [];
foreach ($post->getProductsPosition() as $key => $value) {
$productData[$key] = ['position' => $value];
}
$post->setProductsData($productData);
}
return $this;
}
}