This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
+92
View File
@@ -0,0 +1,92 @@
<?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;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Mageplaza\Blog\Model\AuthorFactory;
/**
* Class Author
* @package Mageplaza\Blog\Controller\Adminhtml
*/
abstract class Author extends Action
{
/** Authorization level of a basic admin session */
const ADMIN_RESOURCE = 'Mageplaza_Blog::author';
/**
* @var Registry
*/
public $coreRegistry;
/**
* @var AuthorFactory
*/
public $authorFactory;
/**
* Author constructor.
*
* @param Context $context
* @param Registry $coreRegistry
* @param AuthorFactory $authorFactory
*/
public function __construct(
Context $context,
Registry $coreRegistry,
AuthorFactory $authorFactory
) {
$this->authorFactory = $authorFactory;
$this->coreRegistry = $coreRegistry;
parent::__construct($context);
}
/**
* @param bool $register
*
* @return bool|\Mageplaza\Blog\Model\Author
*/
public function initAuthor($register = false)
{
$authorId = (int)$this->getRequest()->getParam('id');
/** @var \Mageplaza\Blog\Model\Author $author */
$author = $this->authorFactory->create();
if ($authorId) {
$author->load($authorId);
if (!$author->getId()) {
$this->messageManager->addErrorMessage(__('This author no longer exists.'));
return false;
}
}
if ($register) {
$this->coreRegistry->register('mageplaza_blog_author', $author);
}
return $author;
}
}
@@ -0,0 +1,79 @@
<?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\Author;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\Raw;
use Magento\Framework\Controller\Result\RawFactory;
use Magento\Framework\View\LayoutFactory;
/**
* Class CustomerGrid
* @package Mageplaza\Blog\Controller\Adminhtml\Author
*/
class CustomerGrid extends Action
{
/**
* @var RawFactory
*/
protected $resultRawFactory;
/**
* @var LayoutFactory
*/
protected $layoutFactory;
/**
* @param Context $context
* @param RawFactory $resultRawFactory
* @param LayoutFactory $layoutFactory
*/
public function __construct(
Context $context,
RawFactory $resultRawFactory,
LayoutFactory $layoutFactory
) {
parent::__construct($context);
$this->resultRawFactory = $resultRawFactory;
$this->layoutFactory = $layoutFactory;
}
/**
* Grid Action
*
* @return Raw
*/
public function execute()
{
/** @var Raw $resultRaw */
$resultRaw = $this->resultRawFactory->create();
return $resultRaw->setContents(
$this->layoutFactory->create()->createBlock(
\Mageplaza\Blog\Block\Adminhtml\Author\CustomerGrid::class,
'blog.customer.grid'
)->toHtml()
);
}
}
@@ -0,0 +1,99 @@
<?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\Author;
use Exception;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Registry;
use Mageplaza\Blog\Controller\Adminhtml\Author;
use Mageplaza\Blog\Model\AuthorFactory;
use Mageplaza\Blog\Model\PostFactory;
/**
* Class Delete
* @package Mageplaza\Blog\Controller\Adminhtml\Post
*/
class Delete extends Author
{
/**
* @var PostFactory
*/
protected $_postFactory;
/**
* Delete constructor.
*
* @param Context $context
* @param Registry $coreRegistry
* @param AuthorFactory $authorFactory
* @param PostFactory $postFactory
*/
public function __construct(
Context $context,
Registry $coreRegistry,
AuthorFactory $authorFactory,
PostFactory $postFactory
) {
$this->_postFactory = $postFactory;
parent::__construct($context, $coreRegistry, $authorFactory);
}
/**
* @return Redirect
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
if ($id = $this->getRequest()->getParam('id')) {
$post = $this->_postFactory->create();
$postCollectionSize = $post->getCollection()->addFieldToFilter('author_id', ['eq' => $id])->getSize();
if ($postCollectionSize > 0) {
$this->messageManager->addErrorMessage(__('You can not delete this author.'
. ' This is the author of %1 post(s)', $postCollectionSize));
$resultRedirect->setPath('mageplaza_blog/*/edit', ['id' => $id]);
return $resultRedirect;
}
try {
$this->authorFactory->create()
->load($id)
->delete();
$this->messageManager->addSuccessMessage(__('The Author has been deleted.'));
} catch (Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$resultRedirect->setPath('mageplaza_blog/*/edit', ['id' => $id]);
return $resultRedirect;
}
} else {
$this->messageManager->addErrorMessage(__('Author to delete was not found.'));
}
$resultRedirect->setPath('mageplaza_blog/*/');
return $resultRedirect;
}
}
@@ -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\Author;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
use Mageplaza\Blog\Controller\Adminhtml\Author;
use Mageplaza\Blog\Model\AuthorFactory;
/**
* Class Edit
* @package Mageplaza\Blog\Controller\Adminhtml\Author
*/
class Edit extends Author
{
/**
* @var PageFactory
*/
public $resultPageFactory;
/**
* Edit constructor.
*
* @param Context $context
* @param Registry $registry
* @param AuthorFactory $authorFactory
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
Registry $registry,
AuthorFactory $authorFactory,
PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context, $registry, $authorFactory);
}
/**
* @return \Magento\Backend\Model\View\Result\Page|ResponseInterface|ResultInterface|Page
*/
public function execute()
{
/** @var \Mageplaza\Blog\Model\Author $author */
$author = $this->initAuthor();
if (!$author) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('*');
return $resultRedirect;
}
/** Set entered data if was error when we do save */
$data = $this->_session->getData('mageplaza_blog_author_data', true);
if (!empty($data)) {
$author->addData($data);
}
$this->coreRegistry->register('mageplaza_blog_author', $author);
/** @var \Magento\Backend\Model\View\Result\Page|Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu('Mageplaza_Blog::author');
$resultPage->getConfig()->getTitle()->set(__('Author Management'));
$resultPage->getConfig()->getTitle()->prepend($author->getId() ? $author->getName() : __('New Author'));
return $resultPage;
}
}
@@ -0,0 +1,65 @@
<?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\Author;
use Magento\Backend\App\Action;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
/**
* Class Index
* @package Mageplaza\Blog\Controller\Adminhtml\Author
*/
class Index extends Action
{
/**
* @var PageFactory
*/
public $_resultPageFactory;
/**
* Index constructor.
*
* @param PageFactory $resultPageFactory
* @param Action\Context $context
*/
public function __construct(
PageFactory $resultPageFactory,
Action\Context $context
) {
$this->_resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
/**
* @return ResponseInterface|ResultInterface|Page
*/
public function execute()
{
$resultPage = $this->_resultPageFactory->create();
$resultPage->getConfig()->getTitle()->prepend(__('Authors'));
return $resultPage;
}
}
@@ -0,0 +1,129 @@
<?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\Author;
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\Author;
use Mageplaza\Blog\Model\AuthorFactory;
use Mageplaza\Blog\Model\Post;
use RuntimeException;
/**
* Class InlineEdit
* @package Mageplaza\Blog\Controller\Adminhtml\Author
*/
class InlineEdit extends Action
{
/**
* JSON Factory
*
* @var JsonFactory
*/
public $jsonFactory;
/**
* Author Factory
*
* @var AuthorFactory
*/
public $authorFactory;
/**
* InlineEdit constructor.
*
* @param Context $context
* @param JsonFactory $jsonFactory
* @param AuthorFactory $postFactory
*/
public function __construct(
Context $context,
JsonFactory $jsonFactory,
AuthorFactory $postFactory
) {
$this->jsonFactory = $jsonFactory;
$this->authorFactory = $postFactory;
parent::__construct($context);
}
/**
* @return ResultInterface
*/
public function execute()
{
/** @var Json $resultJson */
$resultJson = $this->jsonFactory->create();
$error = false;
$messages = [];
$authorItems = $this->getRequest()->getParam('items', []);
if (!(!empty($authorItems) && $this->getRequest()->getParam('isAjax'))) {
return $resultJson->setData([
'messages' => [__('Please correct the data sent.')],
'error' => true,
]);
}
$key = array_keys($authorItems);
$authorId = !empty($key) ? (int)$key[0] : '';
/** @var Post $post */
$author = $this->authorFactory->create()->load($authorId);
try {
$authorData = $authorItems[$authorId];
$author->addData($authorData);
$author->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(
$author,
__('Something went wrong while saving the Post.')
);
$error = true;
}
return $resultJson->setData([
'messages' => $messages,
'error' => $error
]);
}
/**
* @param Author $author
* @param $errorText
*
* @return string
*/
public function getErrorWithPostId(Author $author, $errorText)
{
return '[Post ID: ' . $author->getId() . '] ' . $errorText;
}
}
@@ -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\Author;
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\Author\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 Context $context
* @param Filter $filter
* @param CollectionFactory $collectionFactory
*/
public function __construct(
Context $context,
Filter $filter,
CollectionFactory $collectionFactory
) {
$this->filter = $filter;
$this->collectionFactory = $collectionFactory;
parent::__construct($context);
}
/**
* @return Redirect|ResponseInterface|ResultInterface
* @throws LocalizedException
*/
public function execute()
{
$collection = $this->filter->getCollection($this->collectionFactory->create());
$authorRemove = 0;
foreach ($collection->getItems() as $author) {
if ($author->hasPost()) {
$this->messageManager->addWarningMessage(__('One of the authors has post. You can not delete this one.'));
} else {
try {
$author->delete();
$authorRemove++;
} catch (Exception $e) {
$this->_getSession()->addException(
$e,
__('Something went wrong while updating status for %1.', $author->getName())
);
}
}
}
if ($authorRemove) {
$this->messageManager->addSuccessMessage(__('A total of %1 record(s) have been removed.', $authorRemove));
}
/** @var Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath('*/*/');
}
}
@@ -0,0 +1,108 @@
<?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\Author;
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\Author\CollectionFactory;
/**
* Class MassStatus
* @package Mageplaza\Blog\Controller\Adminhtml\Author
*/
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');
$authorUpdated = 0;
foreach ($collection as $post) {
try {
$post->setStatus($status)
->save();
$authorUpdated++;
} 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 ($authorUpdated) {
$this->messageManager->addSuccessMessage(__('A total of %1 record(s) have been updated.', $authorUpdated));
}
/** @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\Author;
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,70 @@
<?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\Author;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\Layout;
use Magento\Framework\View\Result\LayoutFactory;
use Mageplaza\Blog\Controller\Adminhtml\Author;
use Mageplaza\Blog\Model\AuthorFactory;
/**
* Class Posts
* @package Mageplaza\Blog\Controller\Adminhtml\Tag
*/
class Posts extends Author
{
/**
* @var LayoutFactory
*/
public $resultLayoutFactory;
/**
* Posts constructor.
*
* @param Context $context
* @param Registry $coreRegistry
* @param LayoutFactory $resultLayoutFactory
* @param AuthorFactory $authorFactory
*/
public function __construct(
Context $context,
Registry $coreRegistry,
LayoutFactory $resultLayoutFactory,
AuthorFactory $authorFactory
) {
$this->resultLayoutFactory = $resultLayoutFactory;
parent::__construct($context, $coreRegistry, $authorFactory);
}
/**
* @return Layout
*/
public function execute()
{
$this->initAuthor(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\Author;
/**
* Class PostsGrid
* @package Mageplaza\Blog\Controller\Adminhtml\Tag
*/
class PostsGrid extends Posts
{
}
@@ -0,0 +1,158 @@
<?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\Author;
use Exception;
use Magento\Backend\App\Action\Context;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
use Mageplaza\Blog\Controller\Adminhtml\Author;
use Mageplaza\Blog\Helper\Image;
use Mageplaza\Blog\Model\AuthorFactory;
use RuntimeException;
/**
* Class Save
* @package Mageplaza\Blog\Controller\Adminhtml\Author
*/
class Save extends Author
{
/**
* @var Image
*/
protected $imageHelper;
/**
* @var CustomerRepositoryInterface
*/
protected $customerRepository;
/**
* Save constructor.
*
* @param Context $context
* @param Registry $registry
* @param AuthorFactory $authorFactory
* @param CustomerRepositoryInterface $customerRepository
* @param Image $imageHelper
*/
public function __construct(
Context $context,
Registry $registry,
AuthorFactory $authorFactory,
CustomerRepositoryInterface $customerRepository,
Image $imageHelper
) {
$this->imageHelper = $imageHelper;
$this->customerRepository = $customerRepository;
parent::__construct($context, $registry, $authorFactory);
}
/**
* @return ResponseInterface|Redirect|ResultInterface
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
if ($data = $this->getRequest()->getPost('author')) {
/** @var \Mageplaza\Blog\Model\Author $author */
$author = $this->initAuthor();
$this->prepareData($author, $data);
$this->_eventManager->dispatch(
'mageplaza_blog_author_prepare_save',
['author' => $author, 'request' => $this->getRequest()]
);
try {
$author->save();
$this->messageManager->addSuccessMessage(__('The Author has been saved.'));
$this->_getSession()->setData('mageplaza_blog_author_data', false);
if ($this->getRequest()->getParam('back')) {
$resultRedirect->setPath('mageplaza_blog/*/edit', ['id' => $author->getId(), '_current' => true]);
} else {
$resultRedirect->setPath('mageplaza_blog/*/');
}
return $resultRedirect;
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (RuntimeException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (Exception $e) {
$this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the Author.'));
}
$this->_getSession()->setData('mageplaza_blog_author_data', $data);
$resultRedirect->setPath('mageplaza_blog/*/edit', ['id' => $author->getId(), '_current' => true]);
return $resultRedirect;
}
$resultRedirect->setPath('mageplaza_blog/*/');
return $resultRedirect;
}
/**
* @param $author
* @param $data
*
* @return $this
*/
public function prepareData($author, $data)
{
// upload image
if (!$this->getRequest()->getParam('image')) {
try {
$this->imageHelper->uploadImage($data, 'image', Image::TEMPLATE_MEDIA_TYPE_AUTH, $author->getImage());
} catch (Exception $exception) {
$data['image'] = isset($data['image']['value']) ? $data['image']['value'] : '';
}
}
if ($this->getRequest()->getParam('image') && isset($this->getRequest()->getParam('image')['delete'])) {
$data['image'] = '';
}
// set data
if (!empty($data)) {
if (!empty($data['customer_id'])) {
try {
$data['email'] = $this->customerRepository->getById($data['customer_id'])->getEmail();
} catch (Exception $e) {
$data['email'] = '';
}
}
$author->addData($data);
}
return $this;
}
}
+104
View File
@@ -0,0 +1,104 @@
<?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;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Mageplaza\Blog\Model\CategoryFactory;
/**
* Class Category
* @package Mageplaza\Blog\Controller\Adminhtml
*/
abstract class Category extends Action
{
/** Authorization level of a basic admin session */
const ADMIN_RESOURCE = 'Mageplaza_Blog::category';
/**
* Blog Category Factory
*
* @var CategoryFactory
*/
public $categoryFactory;
/**
* Core registry
*
* @var Registry
*/
public $coreRegistry;
/**
* Category constructor.
*
* @param Context $context
* @param Registry $coreRegistry
* @param CategoryFactory $categoryFactory
*/
public function __construct(
Context $context,
Registry $coreRegistry,
CategoryFactory $categoryFactory
) {
$this->categoryFactory = $categoryFactory;
$this->coreRegistry = $coreRegistry;
parent::__construct($context);
}
/**
* @param bool $register
* @param bool $isSave
*
* @return bool|\Mageplaza\Blog\Model\Category
*/
public function initCategory($register = false, $isSave = false)
{
$categoryId = null;
if ($this->getRequest()->getParam('id')) {
$categoryId = (int)$this->getRequest()->getParam('id');
} elseif ($this->getRequest()->getParam('category_id')) {
$categoryId = (int)$this->getRequest()->getParam('category_id');
}
/** @var \Mageplaza\Blog\Model\Post $post */
$category = $this->categoryFactory->create();
if (!$this->getRequest()->getParam('duplicate') || !$isSave) {
if ($categoryId) {
$category->load($categoryId);
if (!$category->getId()) {
$this->messageManager->addErrorMessage(__('This category no longer exists.'));
return false;
}
}
}
if ($register) {
$this->coreRegistry->register('category', $category);
}
return $category;
}
}
@@ -0,0 +1,71 @@
<?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\Category;
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 Add
* @package Mageplaza\Blog\Controller\Adminhtml\Category
*/
class Add extends Action
{
/**
* Redirect result factory
*
* @var ForwardFactory
*/
public $resultForwardFactory;
/**
* NewAction constructor.
*
* @param Context $context
* @param ForwardFactory $resultForwardFactory
*/
public function __construct(
Context $context,
ForwardFactory $resultForwardFactory
) {
$this->resultForwardFactory = $resultForwardFactory;
parent::__construct($context);
}
/**
* forward to edit
*
* @return Forward
*/
public function execute()
{
$this->_getSession()->unsMageplazaBlogCategoryActiveTabId();
$resultForward = $this->resultForwardFactory->create();
$resultForward->forward('edit');
return $resultForward;
}
}
@@ -0,0 +1,111 @@
<?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\Category;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\Auth\Session;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\LayoutFactory;
use Mageplaza\Blog\Block\Adminhtml\Category\Tree;
use Mageplaza\Blog\Controller\Adminhtml\Category;
use Mageplaza\Blog\Model\CategoryFactory;
/**
* Class CategoriesJson
* @package Mageplaza\Blog\Controller\Adminhtml\Category
*/
class CategoriesJson extends Category
{
/**
* JSON Result Factory
*
* @var JsonFactory
*/
public $resultJsonFactory;
/**
* Layout Factory
*
* @var LayoutFactory
*/
public $layoutFactory;
/**
* CategoriesJson constructor.
*
* @param Context $context
* @param Registry $coreRegistry
* @param CategoryFactory $categoryFactory
* @param JsonFactory $resultJsonFactory
* @param LayoutFactory $layoutFactory
*/
public function __construct(
Context $context,
Registry $coreRegistry,
CategoryFactory $categoryFactory,
JsonFactory $resultJsonFactory,
LayoutFactory $layoutFactory
) {
$this->resultJsonFactory = $resultJsonFactory;
$this->layoutFactory = $layoutFactory;
parent::__construct($context, $coreRegistry, $categoryFactory);
}
/**
* Get tree node (Ajax version)
*
* @return ResultInterface
*/
public function execute()
{
$this->_objectManager->get(Session::class)->setIsTreeWasExpanded(
(boolean) $this->getRequest()->getParam('expand_all')
);
$resultJson = $this->resultJsonFactory->create();
if ($categoryId = (int) $this->getRequest()->getPost('id')) {
$this->getRequest()->setParam('id', $categoryId);
$category = $this->initCategory(true);
if (!$category) {
/** @var Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('mageplaza_blog/*/', ['_current' => true]);
}
$treeJson = $this->layoutFactory->create()
->createBlock(Tree::class)
->getTreeJson($category);
/** @var Json $resultJson */
return $resultJson->setJsonData($treeJson);
}
return $resultJson->setJsonData('[]');
}
}
@@ -0,0 +1,83 @@
<?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\Category;
use Exception;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultInterface;
use Mageplaza\Blog\Controller\Adminhtml\Category;
/**
* Class Delete
* @package Mageplaza\Blog\Controller\Adminhtml\Category
*/
class Delete extends Category
{
/**
* @return ResponseInterface|Redirect|ResultInterface
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
if ($id = $this->getRequest()->getParam('id')) {
try {
$categoryFactory = $this->categoryFactory->create();
if ($id !== 1) {
$parentCategoryCollection = $categoryFactory->getCollection()
->addFieldToFilter('category_id', ['eq' => $id])
->addFieldToFilter('parent_id', ['eq' => 1]);
if ($parentCategoryCollection->getSize()) {
$pathCategory = $categoryFactory->load($id)->getPath();
$collections = $categoryFactory->getCollection()
->addFieldToFilter('path', ['like' => $pathCategory . '%']);
foreach ($collections as $collection) {
$collection->delete();
}
} else {
$categoryFactory->load($id)->delete();
}
$this->messageManager->addSuccessMessage(__('The Blog Category has been deleted.'));
$resultRedirect->setPath('mageplaza_blog/*/');
return $resultRedirect;
}
} catch (Exception $e) {
// display error message
$this->messageManager->addErrorMessage($e->getMessage());
// go back to edit form
$resultRedirect->setPath('mageplaza_blog/*/edit', ['id' => $id]);
return $resultRedirect;
}
}
// display error message
$this->messageManager->addErrorMessage(__('Blog Category to delete was not found.'));
// go to grid
$resultRedirect->setPath('mageplaza_blog/*/');
return $resultRedirect;
}
}
@@ -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\Category;
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 Duplicate
* @package Mageplaza\Blog\Controller\Adminhtml\Category
*/
class Duplicate extends Action
{
/**
* Redirect result factory
*
* @var ForwardFactory
*/
public $resultForwardFactory;
/**
* NewAction constructor.
*
* @param Context $context
* @param ForwardFactory $resultForwardFactory
*/
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,186 @@
<?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\Category;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\Auth\Session;
use Magento\Backend\Model\View\Result\Page;
use Magento\Catalog\Model\Category as CategoryModel;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\DataObject;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\PageFactory;
use Mageplaza\Blog\Controller\Adminhtml\Category;
use Mageplaza\Blog\Model\CategoryFactory;
/**
* Class Edit
* @package Mageplaza\Blog\Controller\Adminhtml\Category
*/
class Edit extends Category
{
/**
* Page factory
*
* @var PageFactory
*/
public $resultPageFactory;
/**
* Result JSON factory
*
* @var JsonFactory
*/
public $resultJsonFactory;
/**
* @var DataObject
*/
public $dataObject;
/**
* Edit constructor.
*
* @param DataObject $dataObject
* @param PageFactory $resultPageFactory
* @param JsonFactory $resultJsonFactory
* @param CategoryFactory $categoryFactory
* @param Registry $registry
* @param Context $context
*/
public function __construct(
Context $context,
Registry $registry,
CategoryFactory $categoryFactory,
DataObject $dataObject,
PageFactory $resultPageFactory,
JsonFactory $resultJsonFactory
) {
$this->dataObject = $dataObject;
$this->resultPageFactory = $resultPageFactory;
$this->resultJsonFactory = $resultJsonFactory;
parent::__construct($context, $registry, $categoryFactory);
}
/**
* Edit Blog category page
*
* @return ResultInterface
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute()
{
$categoryId = (int) $this->getRequest()->getParam('id');
$duplicate = $this->getRequest()->getParam('duplicate');
$category = $this->initCategory();
if ($duplicate) {
$category->setId(null);
$category->setData('duplicate', true);
$categoryId = null;
}
if (!$category) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('*');
return $resultRedirect;
}
/**
* Check if we have data in session (if during Blog category save was exception)
*/
$data = $this->_getSession()->getData('mageplaza_blog_category_data', true);
if (isset($data['category'])) {
$category->addData($data['category']);
}
$this->coreRegistry->register('category', $category);
/** @var Page $resultPage */
$resultPage = $this->resultPageFactory->create();
/** Build response for ajax request */
if ($this->getRequest()->getQuery('isAjax')) {
// prepare breadcrumbs of selected Blog category, if any
$breadcrumbsPath = $category->getPath();
if (empty($breadcrumbsPath)) {
// but if no Blog category, and it is deleted - prepare breadcrumbs from path, saved in session
$breadcrumbsPath = $this->_objectManager->get(Session::class)
->getDeletedPath(true);
if (!empty($breadcrumbsPath)) {
$breadcrumbsPath = explode('/', $breadcrumbsPath);
// no need to get parent breadcrumbs if deleting Blog category level 1
if (count($breadcrumbsPath) <= 1) {
$breadcrumbsPath = '';
} else {
array_pop($breadcrumbsPath);
$breadcrumbsPath = implode('/', $breadcrumbsPath);
}
}
}
$layout = $resultPage->getLayout();
$content = $layout->getBlock('mageplaza.blog.category.edit')->getFormHtml()
. $layout->getBlock('mageplaza.blog.category.tree')
->getBreadcrumbsJavascript($breadcrumbsPath, 'editingCategoryBreadcrumbs');
$eventResponse = $this->dataObject->addData([
'content' => $content,
'messages' => $layout->getMessagesBlock()->getGroupedHtml(),
'toolbar' => $layout->getBlock('page.actions.toolbar')->toHtml()
]);
$this->_eventManager->dispatch(
'mageplaza_blog_category_prepare_ajax_response',
['response' => $eventResponse, 'controller' => $this]
);
/** @var Json $resultJson */
$resultJson = $this->resultJsonFactory->create();
$resultJson->setHeader('Content-type', 'application/json', true);
$resultJson->setData($eventResponse->getData());
return $resultJson;
}
$resultPage->setActiveMenu('Mageplaza_Blog::category');
$resultPage->getConfig()->getTitle()->prepend(__('Categories'));
if ($categoryId) {
$title = __('%1 (ID: %2)', $category->getName(), $categoryId);
} else {
$parentId = (int) $this->getRequest()->getParam('parent');
if ($parentId && $parentId != CategoryModel::TREE_ROOT_ID) {
$title = __('New Child Category');
} else {
$title = __('New Root Category');
}
}
$resultPage->getConfig()->getTitle()->prepend($title);
$resultPage->addBreadcrumb(__('Manage Categories'), __('Manage Categories'));
return $resultPage;
}
}
@@ -0,0 +1,68 @@
<?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\Category;
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 Index
* @package Mageplaza\Blog\Controller\Adminhtml\Category
*/
class Index extends Action
{
/**
* Forward factory
*
* @var ForwardFactory
*/
public $resultForwardFactory;
/**
* Index constructor.
*
* @param Context $context
* @param ForwardFactory $resultForwardFactory
*/
public function __construct(
Context $context,
ForwardFactory $resultForwardFactory
) {
$this->resultForwardFactory = $resultForwardFactory;
parent::__construct($context);
}
/**
* execute action
*
* @return Forward
*/
public function execute()
{
/** @var Forward $resultForward */
$resultForward = $this->resultForwardFactory->create();
return $resultForward->forward('edit');
}
}
@@ -0,0 +1,131 @@
<?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\Category;
use Exception;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Messages;
use Magento\Framework\View\LayoutFactory;
use Mageplaza\Blog\Controller\Adminhtml\Category;
use Mageplaza\Blog\Model\CategoryFactory;
use Psr\Log\LoggerInterface;
/**
* Class Move
* @package Mageplaza\Blog\Controller\Adminhtml\Category
*/
class Move extends Category
{
/**
* JSON Result Factory
*
* @var JsonFactory
*/
public $resultJsonFactory;
/**
* Layout Factory
*
* @var LayoutFactory
*/
public $layoutFactory;
/**
* @var LoggerInterface
*/
public $logger;
/**
* Move constructor.
*
* @param JsonFactory $resultJsonFactory
* @param LayoutFactory $layoutFactory
* @param LoggerInterface $logger
* @param CategoryFactory $categoryFactory
* @param Registry $coreRegistry
* @param Context $context
*/
public function __construct(
Context $context,
Registry $coreRegistry,
CategoryFactory $categoryFactory,
JsonFactory $resultJsonFactory,
LayoutFactory $layoutFactory,
LoggerInterface $logger
) {
$this->resultJsonFactory = $resultJsonFactory;
$this->layoutFactory = $layoutFactory;
$this->logger = $logger;
parent::__construct($context, $coreRegistry, $categoryFactory);
}
/**
* @return Json
*/
public function execute()
{
/** New parent Blog category identifier */
$parentNodeId = $this->getRequest()->getPost('pid', false);
/** Blog category id after which we have put our Blog category */
$prevNodeId = $this->getRequest()->getPost('aid', false);
/** @var $block Messages */
$block = $this->layoutFactory->create()->getMessagesBlock();
$error = false;
try {
$category = $this->initCategory();
if ($category !== false) {
$category->move($parentNodeId, $prevNodeId);
} else {
$error = true;
$this->messageManager->addErrorMessage(__('There was a Blog category move error.'));
}
} catch (LocalizedException $e) {
$error = true;
$this->messageManager->addErrorMessage(__('There was a Blog category move error.'));
} catch (Exception $e) {
$error = true;
$this->messageManager->addErrorMessage(__('There was a Blog category move error.'));
$this->logger->critical($e);
}
if (!$error) {
$this->messageManager->addSuccessMessage(__('You moved the Blog category'));
}
$block->setMessages($this->messageManager->getMessages(true));
$resultJson = $this->resultJsonFactory->create();
$resultJson->setData([
'messages' => $block->getGroupedHtml(),
'error' => $error
]);
return $resultJson;
}
}
@@ -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\Category;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\Layout;
use Magento\Framework\View\Result\LayoutFactory;
use Mageplaza\Blog\Controller\Adminhtml\Category;
use Mageplaza\Blog\Model\CategoryFactory;
/**
* Class Posts
* @package Mageplaza\Blog\Controller\Adminhtml\Category
*/
class Posts extends Category
{
/**
* Result layout factory
*
* @var LayoutFactory
*/
public $resultLayoutFactory;
/**
* Posts constructor.
*
* @param Context $context
* @param Registry $coreRegistry
* @param CategoryFactory $categoryFactory
* @param LayoutFactory $resultLayoutFactory
*/
public function __construct(
Context $context,
Registry $coreRegistry,
CategoryFactory $categoryFactory,
LayoutFactory $resultLayoutFactory
) {
$this->resultLayoutFactory = $resultLayoutFactory;
parent::__construct($context, $coreRegistry, $categoryFactory);
}
/**
* @return Layout
*/
public function execute()
{
$this->initCategory(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\Category;
/**
* Class PostsGrid
* @package Mageplaza\Blog\Controller\Adminhtml\Category
*/
class PostsGrid extends Posts
{
}
@@ -0,0 +1,80 @@
<?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\Category;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Registry;
use Mageplaza\Blog\Controller\Adminhtml\Category;
use Mageplaza\Blog\Model\CategoryFactory;
/**
* Class RefreshPath
* @package Mageplaza\Blog\Controller\Adminhtml\Category
*/
class RefreshPath extends Category
{
/**
* JSON Result Factory
*
* @var JsonFactory
*/
public $resultJsonFactory;
/**
* RefreshPath constructor.
*
* @param JsonFactory $resultJsonFactory
* @param CategoryFactory $categoryFactory
* @param Registry $coreRegistry
* @param Context $context
*/
public function __construct(
Context $context,
Registry $coreRegistry,
CategoryFactory $categoryFactory,
JsonFactory $resultJsonFactory
) {
$this->resultJsonFactory = $resultJsonFactory;
parent::__construct($context, $coreRegistry, $categoryFactory);
}
/**
* Build response for refresh input element 'path' in form
*
* @return Json
*/
public function execute()
{
$categoryId = (int)$this->getRequest()->getParam('id');
if ($categoryId) {
$category = $this->categoryFactory->create()->load($categoryId);
/** @var Json $resultJson */
$resultJson = $this->resultJsonFactory->create();
return $resultJson->setData(['id' => $categoryId, 'path' => $category->getPath()]);
}
}
}
@@ -0,0 +1,229 @@
<?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\Category;
use Exception;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Helper\Js;
use Magento\Catalog\Model\Category as CategoryModel;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\Result\RawFactory;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Message\MessageInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Messages;
use Magento\Framework\View\LayoutFactory;
use Mageplaza\Blog\Controller\Adminhtml\Category;
use Mageplaza\Blog\Model\CategoryFactory;
use Psr\Log\LoggerInterface;
use Mageplaza\Blog\Helper\Data;
/**
* Class Save
* @package Mageplaza\Blog\Controller\Adminhtml\Category
*/
class Save extends Category
{
/**
* Result Raw Factory
*
* @var RawFactory
*/
public $resultRawFactory;
/**
* Result Json Factory
*
* @var JsonFactory
*/
public $resultJsonFactory;
/**
* Layout Factory
*
* @var LayoutFactory
*/
public $layoutFactory;
/**
* JS helper
*
* @var Js
*/
public $jsHelper;
/**
* @var Data
*/
public $_helperData;
/**
* Save constructor.
*
* @param Context $context
* @param Registry $coreRegistry
* @param CategoryFactory $categoryFactory
* @param RawFactory $resultRawFactory
* @param JsonFactory $resultJsonFactory
* @param LayoutFactory $layoutFactory
* @param Js $jsHelper
* @param Data $helperData
*/
public function __construct(
Context $context,
Registry $coreRegistry,
CategoryFactory $categoryFactory,
RawFactory $resultRawFactory,
JsonFactory $resultJsonFactory,
LayoutFactory $layoutFactory,
Js $jsHelper,
Data $helperData
) {
$this->resultRawFactory = $resultRawFactory;
$this->resultJsonFactory = $resultJsonFactory;
$this->layoutFactory = $layoutFactory;
$this->jsHelper = $jsHelper;
$this->_helperData = $helperData;
parent::__construct($context, $coreRegistry, $categoryFactory);
}
/**
* @return Json|Redirect
*/
public function execute()
{
if ($this->getRequest()->getPost('return_session_messages_only')) {
$category = $this->initCategory();
$categoryPostData = $this->getRequest()->getPostValue();
$categoryPostData['store_ids'] = 0;
$categoryPostData['enabled'] = 1;
$this->_helperData->handleSeoValueBeforeSave($categoryPostData);
$category->addData($categoryPostData);
$parentId = $this->getRequest()->getParam('parent');
if (!$parentId) {
$parentId = CategoryModel::TREE_ROOT_ID;
}
$parentCategory = $this->categoryFactory->create()->load($parentId);
$category->setPath($parentCategory->getPath());
$category->setParentId($parentId);
try {
$category->save();
$this->messageManager->addSuccessMessage(__('You saved the category.'));
} catch (AlreadyExistsException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$this->_objectManager->get(LoggerInterface::class)->critical($e);
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$this->_objectManager->get(LoggerInterface::class)->critical($e);
} catch (Exception $e) {
$this->messageManager->addErrorMessage(__('Something went wrong while saving the category.'));
$this->_objectManager->get(LoggerInterface::class)->critical($e);
}
$hasError = (bool) $this->messageManager->getMessages()->getCountByType(
MessageInterface::TYPE_ERROR
);
$category->load($category->getId());
$category->addData([
'entity_id' => $category->getId(),
'is_active' => $category->getEnabled(),
'parent' => $category->getParentId()
]);
// to obtain truncated category name
/** @var $block Messages */
$block = $this->layoutFactory->create()->getMessagesBlock();
$block->setMessages($this->messageManager->getMessages(true));
/** @var Json $resultJson */
$resultJson = $this->resultJsonFactory->create();
return $resultJson->setData(
[
'messages' => $block->getGroupedHtml(),
'error' => $hasError,
'category' => $category->toArray(),
]
);
}
$resultRedirect = $this->resultRedirectFactory->create();
if ($data = $this->getRequest()->getPost('category')) {
$category = $this->initCategory(false, true);
if ($this->getRequest()->getParam('duplicate')) {
unset($data['id']);
}
if (!$category) {
$resultRedirect->setPath('mageplaza_blog/*/', ['_current' => true]);
return $resultRedirect;
}
$this->_helperData->handleSeoValueBeforeSave($data);
$category->addData($data);
if ($posts = $this->getRequest()->getPost('selected_products')) {
$posts = json_decode($posts, true);
$category->setPostsData($posts);
}
if (!$category->getId()) {
$parentId = $this->getRequest()->getParam('parent');
if (!$parentId) {
$parentId = CategoryModel::TREE_ROOT_ID;
}
$parentCategory = $this->categoryFactory->create()->load($parentId);
$category->setPath($parentCategory->getPath());
$category->setParentId($parentId);
}
$this->_eventManager->dispatch(
'mageplaza_blog_category_prepare_save',
['category' => $category, 'request' => $this->getRequest()]
);
try {
$category->save();
$this->messageManager->addSuccessMessage(__('You saved the Blog Category.'));
$this->_getSession()->setData('mageplaza_blog_category_data', false);
} catch (Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$this->_getSession()->setData('mageplaza_blog_category_data', $data);
}
$resultRedirect->setPath('mageplaza_blog/*/edit', ['_current' => true, 'id' => $category->getId()]);
return $resultRedirect;
}
$resultRedirect->setPath('mageplaza_blog/*/edit', ['_current' => true]);
return $resultRedirect;
}
}
@@ -0,0 +1,92 @@
<?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\Category;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Registry;
use Magento\Framework\View\LayoutFactory;
use Mageplaza\Blog\Block\Adminhtml\Category\Tree;
use Mageplaza\Blog\Controller\Adminhtml\Category;
use Mageplaza\Blog\Model\CategoryFactory;
/**
* Class SuggestCategories
* @package Mageplaza\Blog\Controller\Adminhtml\Category
*/
class SuggestCategories extends Category
{
/**
* Json result factory
*
* @var JsonFactory
*/
public $resultJsonFactory;
/**
* Layout factory
*
* @var LayoutFactory
*/
public $layoutFactory;
/**
* SuggestCategories constructor.
*
* @param Context $context
* @param Registry $coreRegistry
* @param CategoryFactory $categoryFactory
* @param JsonFactory $resultJsonFactory
* @param LayoutFactory $layoutFactory
*/
public function __construct(
Context $context,
Registry $coreRegistry,
CategoryFactory $categoryFactory,
JsonFactory $resultJsonFactory,
LayoutFactory $layoutFactory
) {
$this->resultJsonFactory = $resultJsonFactory;
$this->layoutFactory = $layoutFactory;
parent::__construct($context, $coreRegistry, $categoryFactory);
}
/**
* Blog Category list suggestion based on already entered symbols
*
* @return Json
*/
public function execute()
{
/** @var Tree $treeBlock */
$treeBlock = $this->layoutFactory->create()->createBlock(Tree::class);
$data = $treeBlock->getSuggestedCategoriesJson($this->getRequest()->getParam('label_part'));
/** @var Json $resultJson */
$resultJson = $this->resultJsonFactory->create();
$resultJson->setJsonData($data);
return $resultJson;
}
}
+95
View File
@@ -0,0 +1,95 @@
<?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;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Mageplaza\Blog\Model\CommentFactory;
/**
* Class Comment
* @package Mageplaza\Blog\Controller\Adminhtml
*/
abstract class Comment extends Action
{
/** Authorization level of a basic admin session */
const ADMIN_RESOURCE = 'Mageplaza_Blog::comment';
/**
* @var CommentFactory
*/
public $commentFactory;
/**
* Core registry
*
* @var Registry
*/
public $coreRegistry;
/**
* Comment constructor.
*
* @param CommentFactory $commentFactory
* @param Registry $coreRegistry
* @param Context $context
*/
public function __construct(
CommentFactory $commentFactory,
Registry $coreRegistry,
Context $context
) {
$this->commentFactory = $commentFactory;
$this->coreRegistry = $coreRegistry;
parent::__construct($context);
}
/**
* @param bool $register
*
* @return bool|\Mageplaza\Blog\Model\Comment
*/
protected function initComment($register = false)
{
$cmtId = $this->getRequest()->getParam("id");
/** @var \Mageplaza\Blog\Model\Post $post */
$comment = $this->commentFactory->create();
if ($cmtId) {
$comment->load($cmtId);
if (!$comment->getId()) {
$this->messageManager->addErrorMessage(__('This comment no longer exists.'));
return false;
}
}
if ($register) {
$this->coreRegistry->register('mageplaza_blog_comment', $comment);
}
return $comment;
}
}
@@ -0,0 +1,61 @@
<?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\Comment;
use Exception;
use Magento\Framework\Controller\Result\Redirect;
use Mageplaza\Blog\Controller\Adminhtml\Comment;
/**
* Class Delete
* @package Mageplaza\Blog\Controller\Adminhtml\Comment
*/
class Delete extends Comment
{
/**
* @return Redirect
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
if ($id = $this->getRequest()->getParam('id')) {
try {
$this->commentFactory->create()
->load($id)
->delete();
$this->messageManager->addSuccessMessage(__('The Comment has been deleted.'));
} catch (Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$resultRedirect->setPath('mageplaza_blog/*/edit', ['id' => $id]);
return $resultRedirect;
}
} else {
$this->messageManager->addErrorMessage(__('Comment to delete was not found.'));
}
$resultRedirect->setPath('mageplaza_blog/*/');
return $resultRedirect;
}
}
@@ -0,0 +1,96 @@
<?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\Comment;
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\Comment;
use Mageplaza\Blog\Model\CommentFactory;
/**
* Class Edit
* @package Mageplaza\Blog\Controller\Adminhtml\Comment
*/
class Edit extends Comment
{
/**
* Page factory
*
* @var PageFactory
*/
public $resultPageFactory;
/**
* Edit constructor.
*
* @param PageFactory $pageFactory
* @param CommentFactory $commentFactory
* @param Registry $coreRegistry
* @param Context $context
*/
public function __construct(
PageFactory $pageFactory,
CommentFactory $commentFactory,
Registry $coreRegistry,
Context $context
) {
$this->resultPageFactory = $pageFactory;
parent::__construct($commentFactory, $coreRegistry, $context);
}
/**
* @return \Magento\Backend\Model\View\Result\Page|Redirect|Page
*/
public function execute()
{
/** @var \Mageplaza\Blog\Model\Comment $comment */
$comment = $this->initComment();
if (!$comment) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('*');
return $resultRedirect;
}
$data = $this->_session->getData('mageplaza_blog_comment_data', true);
if (!empty($data)) {
$comment->setData($data);
}
$this->coreRegistry->register('mageplaza_blog_comment', $comment);
/** @var \Magento\Backend\Model\View\Result\Page|Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu('Mageplaza_Blog::comment');
$title = __('Edit Comment');
$resultPage->getConfig()->getTitle()->prepend($title);
return $resultPage;
}
}
@@ -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\Comment;
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\Comment
*/
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(__('Comments'));
return $resultPage;
}
}
@@ -0,0 +1,133 @@
<?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\Comment;
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\Comment;
use Mageplaza\Blog\Model\CommentFactory;
use Mageplaza\Blog\Model\Post;
use RuntimeException;
/**
* Class InlineEdit
* @package Mageplaza\Blog\Controller\Adminhtml\Comment
*/
class InlineEdit extends Action
{
/**
* JSON Factory
*
* @var JsonFactory
*/
public $jsonFactory;
/**
* Post Factory
*
* @var CommentFactory
*/
public $commentFactory;
/**
* InlineEdit constructor.
*
* @param Context $context
* @param JsonFactory $jsonFactory
* @param CommentFactory $commentFactory
*/
public function __construct(
Context $context,
JsonFactory $jsonFactory,
CommentFactory $commentFactory
) {
$this->jsonFactory = $jsonFactory;
$this->commentFactory = $commentFactory;
parent::__construct($context);
}
/**
* @return ResultInterface
*/
public function execute()
{
/** @var Json $resultJson */
$resultJson = $this->jsonFactory->create();
$error = false;
$messages = [];
$commentItems = $this->getRequest()->getParam('items', []);
if (!($this->getRequest()->getParam('isAjax') && !empty($commentItems))) {
return $resultJson->setData([
'messages' => [
__('Please correct the data sent.')
],
'error' => true,
]);
}
$key = array_keys($commentItems);
$commentId = !empty($key) ? (int)$key[0] : '';
/** @var Post $post */
$comment = $this->commentFactory->create()->load($commentId);
try {
$commentData = $commentItems[$commentId];
$comment->addData($commentData);
$comment->save();
} catch (LocalizedException $e) {
$messages[] = $this->getErrorWithCommentId($comment, $e->getMessage());
$error = true;
} catch (RuntimeException $e) {
$messages[] = $this->getErrorWithCommentId($comment, $e->getMessage());
$error = true;
} catch (Exception $e) {
$messages[] = $this->getErrorWithCommentId(
$comment,
__('Something went wrong while saving the Comment.')
);
$error = true;
}
return $resultJson->setData([
'messages' => $messages,
'error' => $error
]);
}
/**
* @param Comment $comment
* @param $errorText
*
* @return string
*/
public function getErrorWithCommentId(Comment $comment, $errorText)
{
return '[Comment ID: ' . $comment->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\Comment;
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\Comment\CollectionFactory;
/**
* Class MassDelete
* @package Mageplaza\Blog\Controller\Adminhtml\Comment
*/
class MassDelete extends Action
{
/**
* Mass Action Filter
*
* @var Filter
*/
public $filter;
/**
* @var CollectionFactory
*/
public $collectionFactory;
/**
* MassDelete constructor.
*
* @param Context $context
* @param Filter $filter
* @param CollectionFactory $collectionFactory
*/
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 {
$count = 0;
foreach ($collection as $item) {
$item->delete();
$count++;
}
$this->messageManager->addSuccessMessage(__('Total of %1 comments has been deleted.', $count));
} catch (Exception $e) {
$this->messageManager->addErrorMessage(__('Something wrong when delete Comments.'));
}
/** @var Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath('*/*/');
}
}
@@ -0,0 +1,103 @@
<?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\Comment;
use Exception;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
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\Comment\CollectionFactory;
/**
* Class MassStatus
* @package Mageplaza\Blog\Controller\Adminhtml\Comment
*/
class MassStatus extends Action
{
/**
* Mass Action Filter
*
* @var Filter
*/
public $filter;
/**
* Collection Factory
*
* @var CollectionFactory
*/
public $collectionFactory;
/**
* MassStatus constructor.
*
* @param Context $context
* @param Filter $filter
* @param CollectionFactory $collectionFactory
*/
public function __construct(
Context $context,
Filter $filter,
CollectionFactory $collectionFactory
) {
$this->filter = $filter;
$this->collectionFactory = $collectionFactory;
parent::__construct($context);
}
/**
* @inheritdoc
* @return ResponseInterface|ResultInterface
* @throws LocalizedException
*/
public function execute()
{
$commentUpdated = 0;
$status = (int)$this->getRequest()->getParam('status');
$collection = $this->filter->getCollection($this->collectionFactory->create());
foreach ($collection as $comment) {
try {
$comment->setStatus($status)->save();
$this->_eventManager->dispatch('blog_post_comment', ['comment_data' => $comment]);
$commentUpdated++;
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (Exception $e) {
$this->_getSession()->addException($e, __('Something went wrong while updating status'));
}
}
if ($commentUpdated) {
$this->messageManager->addSuccessMessage(__('A total of %1 record(s) have been updated.', $commentUpdated));
}
$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\Comment;
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,100 @@
<?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\Comment;
use Exception;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Exception\LocalizedException;
use Mageplaza\Blog\Controller\Adminhtml\Comment;
use RuntimeException;
/**
* Class Save
* @package Mageplaza\Blog\Controller\Adminhtml\Comment
*/
class Save extends Comment
{
/**
* @return Redirect
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
if ($data = $this->getRequest()->getPost('comment')) {
/** @var \Mageplaza\Blog\Model\Comment $post */
$comment = $this->initComment();
$this->prepareData($comment, $data);
$this->_eventManager->dispatch(
'mageplaza_blog_comment_prepare_save',
['comment' => $comment, 'request' => $this->getRequest()]
);
try {
$comment->save();
$this->_eventManager->dispatch('blog_post_comment', ['comment_data' => $comment]);
$this->messageManager->addSuccessMessage(__('The comment has been saved.'));
$this->_getSession()->setData('mageplaza_blog_comment_data', false);
if ($this->getRequest()->getParam('back')) {
$resultRedirect->setPath('mageplaza_blog/*/edit', ['id' => $comment->getId(), '_current' => true]);
} else {
$resultRedirect->setPath('mageplaza_blog/*/');
}
return $resultRedirect;
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (RuntimeException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (Exception $e) {
$this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the Comment.'));
}
$this->_getSession()->setData('mageplaza_blog_comment_data', $data);
$resultRedirect->setPath('mageplaza_blog/*/edit', ['id' => $comment->getId(), '_current' => true]);
return $resultRedirect;
}
$resultRedirect->setPath('mageplaza_blog/*/');
return $resultRedirect;
}
/**
* @param $comment
* @param array $data
*
* @return $this
*/
protected function prepareData($comment, $data = [])
{
$comment->addData($data);
return $this;
}
}
+115
View File
@@ -0,0 +1,115 @@
<?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;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Mageplaza\Blog\Model\PostFactory;
use Mageplaza\Blog\Model\PostHistory;
use Mageplaza\Blog\Model\PostHistoryFactory;
/**
* Class Post
* @package Mageplaza\Blog\Controller\Adminhtml
*/
abstract class History extends Action
{
/** Authorization level of a basic admin session */
const ADMIN_RESOURCE = 'Mageplaza_Blog::post';
/**
* Post History Factory
*
* @var PostHistoryFactory
*/
public $postHistoryFactory;
/**
* Core registry
*
* @var Registry
*/
public $coreRegistry;
/**
* @var PostFactory
*/
protected $postFactory;
/**
* @var DateTime
*/
protected $date;
/**
* Post constructor.
*
* @param PostHistoryFactory $postHistoryFactory
* @param PostFactory $postFactory
* @param Registry $coreRegistry
* @param DateTime $date
* @param Context $context
*/
public function __construct(
PostHistoryFactory $postHistoryFactory,
PostFactory $postFactory,
Registry $coreRegistry,
DateTime $date,
Context $context
) {
$this->postHistoryFactory = $postHistoryFactory;
$this->postFactory = $postFactory;
$this->coreRegistry = $coreRegistry;
$this->date = $date;
parent::__construct($context);
}
/**
* @param bool $register
*
* @return bool|PostHistory
*/
protected function initPostHistory($register = false)
{
$historyId = (int)$this->getRequest()->getParam('id');
/** @var \Mageplaza\Blog\Model\Post $post */
$history = $this->postHistoryFactory->create();
if ($historyId) {
$history->load($historyId);
if (!$history->getId()) {
$this->messageManager->addErrorMessage(__('This History no longer exists.'));
return false;
}
}
if ($register) {
$this->coreRegistry->register('mageplaza_blog_post_history', $history);
}
return $history;
}
}
@@ -0,0 +1,65 @@
<?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\History;
use Exception;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultInterface;
use Mageplaza\Blog\Controller\Adminhtml\History;
/**
* Class Delete
* @package Mageplaza\Blog\Controller\Adminhtml\History
*/
class Delete extends History
{
/**
* @return ResponseInterface|Redirect|ResultInterface
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$postId = null;
if ($id = $this->getRequest()->getParam('id')) {
try {
$history = $this->postHistoryFactory->create()
->load($id);
$postId = $history->getPostId();
$history->delete();
$this->messageManager->addSuccessMessage(__('The Post History has been deleted.'));
} catch (Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
} else {
$this->messageManager->addErrorMessage(__('Post History to delete was not found.'));
}
if ($postId) {
$resultRedirect->setPath('mageplaza_blog/post/edit', ['id' => $postId]);
} else {
$resultRedirect->setPath('mageplaza_blog/post/index');
}
return $resultRedirect;
}
}
@@ -0,0 +1,96 @@
<?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\History;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Registry;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
use Mageplaza\Blog\Controller\Adminhtml\History;
use Mageplaza\Blog\Model\PostFactory;
use Mageplaza\Blog\Model\PostHistory;
use Mageplaza\Blog\Model\PostHistoryFactory;
/**
* Class Edit
* @package Mageplaza\Blog\Controller\Adminhtml\History
*/
class Edit extends History
{
/**
* Page factory
*
* @var PageFactory
*/
public $resultPageFactory;
/**
* Edit constructor.
*
* @param PostHistoryFactory $postHistoryFactory
* @param PostFactory $postFactory
* @param Registry $coreRegistry
* @param DateTime $date
* @param PageFactory $resultPageFactory
* @param Context $context
*/
public function __construct(
PostHistoryFactory $postHistoryFactory,
PostFactory $postFactory,
Registry $coreRegistry,
DateTime $date,
PageFactory $resultPageFactory,
Context $context
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($postHistoryFactory, $postFactory, $coreRegistry, $date, $context);
}
/**
* @return \Magento\Backend\Model\View\Result\Page|Redirect|Page
*/
public function execute()
{
/** @var PostHistory $history */
$history = $this->initPostHistory();
if (!$history) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('*');
return $resultRedirect;
}
$this->coreRegistry->register('mageplaza_blog_post', $history);
/** @var \Magento\Backend\Model\View\Result\Page|Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu('Mageplaza_Blog::history');
$resultPage->getConfig()->getTitle()->set(__('Post History'));
$resultPage->getConfig()->getTitle()->prepend(__('Edit Post History'));
return $resultPage;
}
}
@@ -0,0 +1,47 @@
<?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\History;
use Magento\Backend\App\Action;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultInterface;
/**
* Class Index
* @package Mageplaza\Blog\Controller\Adminhtml\History
*/
class Index extends Action
{
/**
* @return ResponseInterface|Redirect|ResultInterface
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('mageplaza_blog/post/');
return $resultRedirect;
}
}
@@ -0,0 +1,95 @@
<?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\History;
use Exception;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultInterface;
use Mageplaza\Blog\Controller\Adminhtml\History;
use Mageplaza\Blog\Helper\Data;
use Mageplaza\Blog\Model\PostHistory;
/**
* Class Restore
* @package Mageplaza\Blog\Controller\Adminhtml\Restore
*/
class Restore extends History
{
/**
* @return ResponseInterface|Redirect|ResultInterface
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$postId = $this->getRequest()->getParam('post_id');
$historyId = $this->getRequest()->getParam('id');
if ($historyId) {
try {
$history = $this->postHistoryFactory->create()
->load($historyId);
$post = $this->postFactory->create()->load($postId);
$data = $this->prepareData($history);
$post->addData($data)->save();
$this->messageManager->addSuccessMessage(__('The Post History has been restore.'));
} catch (Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
} else {
$this->messageManager->addErrorMessage(__('Post History to restore was not found.'));
}
$resultRedirect->setPath('mageplaza_blog/post/edit', ['id' => $postId]);
return $resultRedirect;
}
/**
* @param PostHistory $history
*
* @return array
*/
protected function prepareData($history)
{
$history->setUpdatedAt($this->date->date());
$history->setData('categories_ids', empty($history->getCategoryIds())
? [] : explode(',', $history->getCategoryIds() ?? ''));
$history->setData('tags_ids', empty($history->getTagIds()) ? [] : explode(',', $history->getTagIds() ?? ''));
$history->setData('topics_ids', empty($history->getTopicIds()) ? [] : explode(',', $history->getTopicIds() ?? ''));
$history->setData('products_data', empty($history->getProductIds())
? [] : Data::jsonDecode($history->getProductIds()));
$data = $history->getData();
unset(
$data['post_id'],
$data['history_id'],
$data['category_ids'],
$data['tag_ids'],
$data['topic_ids'],
$data['product_ids']
);
return $data;
}
}
@@ -0,0 +1,207 @@
<?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\History;
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\Exception\LocalizedException;
use Magento\Framework\Registry;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Mageplaza\Blog\Controller\Adminhtml\History;
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\History
*/
class Save extends History
{
/**
* 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 PostHistoryFactory $postHistoryFactory
* @param PostFactory $postFactory
* @param Registry $coreRegistry
* @param DateTime $date
* @param Js $jsHelper
* @param Image $imageHelper
* @param Data $helperData
* @param TimezoneInterface $timezone
* @param Context $context
*/
public function __construct(
PostHistoryFactory $postHistoryFactory,
PostFactory $postFactory,
Registry $coreRegistry,
DateTime $date,
Js $jsHelper,
Image $imageHelper,
Data $helperData,
TimezoneInterface $timezone,
Context $context
) {
$this->jsHelper = $jsHelper;
$this->_helperData = $helperData;
$this->imageHelper = $imageHelper;
$this->_timezone = $timezone;
parent::__construct($postHistoryFactory, $postFactory, $coreRegistry, $date, $context);
}
/**
* @return ResponseInterface|Redirect|ResultInterface
* @throws LocalizedException
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
if ($data = $this->getRequest()->getPost('post')) {
/** @var PostModel $post */
$history = $this->initPostHistory(false);
$this->prepareData($history, $data);
try {
$history->save();
$this->messageManager->addSuccessMessage(__('The post history has been saved.'));
$resultRedirect->setPath(
'mageplaza_blog/post/edit',
['id' => $history->getPostId(), '_current' => true]
);
return $resultRedirect;
} catch (RuntimeException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (Exception $e) {
$this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the Post.'));
}
}
$resultRedirect->setPath('mageplaza_blog/*/edit', [
'id' => $this->getRequest()->getParam('id'),
'post_id' => $this->getRequest()->getParam('post_id'),
'history' => true,
'_current' => true
]);
return $resultRedirect;
}
/**
* @param $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 */
$data['publish_date'] = $this->_timezone->convertConfigTimeToUtc(isset($data['publish_date'])
? $data['publish_date'] : null);
$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();
$post->addData($data);
if ($products = $this->getRequest()->getPost('products', false)) {
$post->setProductsData(
$this->jsHelper->decodeGridSerializedInput($products)
);
} else {
$productData = [];
foreach ($post->getProductsPosition() as $key => $value) {
$productData[$key] = ['position' => $value];
}
$post->setProductsData($productData);
}
return $this;
}
}
@@ -0,0 +1,83 @@
<?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\Import;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\View\Result\Redirect;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
/**
* Class Edit
* @package Mageplaza\Blog\Controller\Adminhtml\Import
*/
class Edit extends Action
{
/**
* @var PageFactory
*/
public $resultPageFactory;
/**
* @var Registry
*/
public $registry;
/**
* Edit constructor.
*
* @param Context $context
* @param PageFactory $resultPageFactory
* @param Registry $registry
*/
public function __construct(
Action\Context $context,
PageFactory $resultPageFactory,
Registry $registry
) {
$this->registry = $registry;
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
/**
* @return \Magento\Backend\Model\View\Result\Page|Redirect|Page
*/
public function execute()
{
$data = $this->_session->getData('mageplaza_blog_import_data');
$this->registry->register('mageplaza_blog_import', $data);
/** @var \Magento\Backend\Model\View\Result\Page|Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu('Mageplaza_Blog::import');
$resultPage->getConfig()->getTitle()->set(__('Import'));
$resultPage->getConfig()->getTitle()->prepend('Import Settings');
return $resultPage;
}
}
@@ -0,0 +1,218 @@
<?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\Import;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Messages;
use Mageplaza\Blog\Helper\Data as BlogHelper;
use Mageplaza\Blog\Model\Import\AheadWorksM1;
use Mageplaza\Blog\Model\Import\MageFanM2;
use Mageplaza\Blog\Model\Import\WordPress;
/**
* Class Import
* @package Mageplaza\Blog\Controller\Adminhtml\Import
*/
class Import extends Action
{
/**
* @var WordPress
*/
protected $_wordpressModel;
/**
* @var AheadWorksM1
*/
protected $_aheadWorksM1Model;
/**
* @var MageFanM2
*/
protected $_mageFanM2Model;
/**
* @var BlogHelper
*/
public $blogHelper;
/**
* @var Registry
*/
public $registry;
/**
* Import constructor.
*
* @param Context $context
* @param WordPress $wordPress
* @param AheadWorksM1 $aheadWorksM1
* @param MageFanM2 $mageFanM2
* @param BlogHelper $blogHelper
* @param Registry $registry
*/
public function __construct(
Action\Context $context,
WordPress $wordPress,
AheadWorksM1 $aheadWorksM1,
MageFanM2 $mageFanM2,
BlogHelper $blogHelper,
Registry $registry
) {
$this->blogHelper = $blogHelper;
$this->_wordpressModel = $wordPress;
$this->_aheadWorksM1Model = $aheadWorksM1;
$this->_mageFanM2Model = $mageFanM2;
$this->registry = $registry;
parent::__construct($context);
}
/**
* @return ResponseInterface|ResultInterface|mixed
*/
public function execute()
{
$data = $this->_getSession()->getData('mageplaza_blog_import_data');
switch ($data['type']) {
case 'wordpress':
$response = $this->processImport($this->_wordpressModel, $data);
break;
case 'aheadworksm1':
$response = $this->processImport($this->_aheadWorksM1Model, $data);
break;
case 'magefan':
$response = $this->processImport($this->_mageFanM2Model, $data);
break;
default:
$response = $this->processImport($this->_wordpressModel, $data);
}
return $response;
}
/**
* @param array $statisticData
* @param Object $messagesBlock
* @param array $data
*
* @return mixed
*/
protected function getStatistic($statisticData, $messagesBlock, $data)
{
switch ($data['behaviour']) {
case 'replace':
$statisticHtml = $messagesBlock
->{'addsuccess'}(__(
'You have replaced and updated %1 %2 successful. Skipped %3 %2.',
$statisticData['success_count'],
$statisticData['type'],
$statisticData['error_count']
))
->toHtml();
break;
case 'delete':
$statisticHtml = $messagesBlock
->{'addsuccess'}(__(
'You have deleted %1 %2 successful. Skipped %3 %2.',
$statisticData['success_count'],
$statisticData['type'],
$statisticData['error_count']
))
->toHtml();
break;
default:
$statisticHtml = $messagesBlock
->{'addsuccess'}(__(
'You have imported %1 %2 successful. Skipped %3 %2.',
$statisticData['success_count'],
$statisticData['type'],
$statisticData['error_count']
))
->toHtml();
}
return $statisticHtml;
}
/**
* @param Object $object
* @param array $data
*
* @return mixed
*/
protected function processImport($object, $data)
{
// phpcs:disable Magento2.Functions.DiscouragedFunction
$statisticHtml = '';
$connection = mysqli_connect($data['host'], $data['user_name'], $data['password'], $data['database']);
$messagesBlock = $this->_view->getLayout()->createBlock(Messages::class);
if ($object->run($data, $connection)) {
$postStatistic = $this->registry->registry('mageplaza_import_post_statistic');
if ($postStatistic && $postStatistic['has_data']) {
$statisticHtml = $this->getStatistic($postStatistic, $messagesBlock, $data);
}
$tagStatistic = $this->registry->registry('mageplaza_import_tag_statistic');
if ($tagStatistic && $tagStatistic['has_data']) {
$statisticHtml = $this->getStatistic($tagStatistic, $messagesBlock, $data);
}
$categoryStatistic = $this->registry->registry('mageplaza_import_category_statistic');
if ($categoryStatistic && $categoryStatistic['has_data']) {
$statisticHtml = $this->getStatistic($categoryStatistic, $messagesBlock, $data);
}
$authorStatistic = $this->registry->registry('mageplaza_import_user_statistic');
if ($authorStatistic && $authorStatistic['has_data']) {
$statisticHtml = $this->getStatistic($authorStatistic, $messagesBlock, $data);
}
$commentStatistic = $this->registry->registry('mageplaza_import_comment_statistic');
if ($commentStatistic && $commentStatistic['has_data']) {
$statisticHtml = $this->getStatistic($commentStatistic, $messagesBlock, $data);
}
if ($statisticHtml == '' && $data['behaviour'] == 'update') {
$statisticHtml = $messagesBlock
->{'addsuccess'}(__('There are no records are updated.'))
->toHtml();
}
$result = ['statistic' => $statisticHtml, 'status' => 'ok'];
mysqli_close($connection);
return $this->getResponse()->representJson(BlogHelper::jsonEncode($result));
} else {
$statisticHtml = $messagesBlock
->{'adderror'}(__('Can not make import, please check your table prefix OR import type and try again.'))
->toHtml();
$result = ['statistic' => $statisticHtml, 'status' => 'ok'];
return $this->getResponse()->representJson(BlogHelper::jsonEncode($result));
}
}
}
@@ -0,0 +1,88 @@
<?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\Import;
use Exception;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\Session;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Mageplaza\Blog\Helper\Data as BlogHelper;
use RuntimeException;
/**
* Class Validate
* @package Mageplaza\Blog\Controller\Adminhtml\Import
*/
class Validate extends Action
{
/**
* @var BlogHelper
*/
public $blogHelper;
/**
* Validate constructor.
*
* @param Context $context
* @param BlogHelper $blogHelper
*/
public function __construct(
Context $context,
BlogHelper $blogHelper
) {
$this->blogHelper = $blogHelper;
parent::__construct($context);
}
/**
* @return ResponseInterface|ResultInterface
*/
public function execute()
{
// phpcs:disable Magento2.Functions.DiscouragedFunction
$data = $this->getRequest()->getParams();
try {
$connect = mysqli_connect($data['host'], $data['user_name'], $data['password'], $data['database']);
$importName = $data['import_name'];
/** @var Session */
$this->_getSession()->setData('mageplaza_blog_import_data', $data);
$result = ['import_name' => $importName, 'status' => 'ok'];
mysqli_close($connect);
return $this->getResponse()->representJson(BlogHelper::jsonEncode($result));
} catch (RuntimeException $e) {
$result = ['import_name' => $data["import_name"], 'status' => 'false'];
return $this->getResponse()->representJson(BlogHelper::jsonEncode($result));
} catch (Exception $e) {
$result = ['import_name' => $data["import_name"], 'status' => 'false'];
return $this->getResponse()->representJson(BlogHelper::jsonEncode($result));
}
}
}
+104
View File
@@ -0,0 +1,104 @@
<?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;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Mageplaza\Blog\Model\PostFactory;
/**
* Class Post
* @package Mageplaza\Blog\Controller\Adminhtml
*/
abstract class Post extends Action
{
/** Authorization level of a basic admin session */
const ADMIN_RESOURCE = 'Mageplaza_Blog::post';
/**
* Post Factory
*
* @var PostFactory
*/
public $postFactory;
/**
* Core registry
*
* @var Registry
*/
public $coreRegistry;
/**
* Post constructor.
*
* @param PostFactory $postFactory
* @param Registry $coreRegistry
* @param Context $context
*/
public function __construct(
PostFactory $postFactory,
Registry $coreRegistry,
Context $context
) {
$this->postFactory = $postFactory;
$this->coreRegistry = $coreRegistry;
parent::__construct($context);
}
/**
* @param bool $register
* @param bool $isSave
*
* @return bool|\Mageplaza\Blog\Model\Post
*/
protected function initPost($register = false, $isSave = false)
{
$postId = (int)$this->getRequest()->getParam('id');
$duplicate = $this->getRequest()->getParam('post')['duplicate'] ?? null;
/** @var \Mageplaza\Blog\Model\Post $post */
$post = $this->postFactory->create();
if ($postId) {
if (!$isSave || !$duplicate) {
$post->load($postId);
if (!$post->getId()) {
$this->messageManager->addErrorMessage(__('This post no longer exists.'));
return false;
}
}
}
if (!$post->getAuthorId()) {
$post->setAuthorId($this->_auth->getUser()->getId());
}
if ($register) {
$this->coreRegistry->register('mageplaza_blog_post', $post);
}
return $post;
}
}
@@ -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;
}
}
+96
View File
@@ -0,0 +1,96 @@
<?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;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Mageplaza\Blog\Model\TagFactory;
/**
* Class Tag
* @package Mageplaza\Blog\Controller\Adminhtml
*/
abstract class Tag extends Action
{
/** Authorization level of a basic admin session */
const ADMIN_RESOURCE = 'Mageplaza_Blog::tag';
/**
* Tag Factory
*
* @var TagFactory
*/
public $tagFactory;
/**
* Core registry
*
* @var Registry
*/
public $coreRegistry;
/**
* Tag constructor.
*
* @param Context $context
* @param Registry $coreRegistry
* @param TagFactory $tagFactory
*/
public function __construct(
Context $context,
Registry $coreRegistry,
TagFactory $tagFactory
) {
$this->tagFactory = $tagFactory;
$this->coreRegistry = $coreRegistry;
parent::__construct($context);
}
/**
* @param bool $register
*
* @return bool|\Mageplaza\Blog\Model\Tag
*/
protected function initTag($register = false)
{
$tagId = (int)$this->getRequest()->getParam('id');
/** @var \Mageplaza\Blog\Model\Tag $tag */
$tag = $this->tagFactory->create();
if ($tagId) {
$tag->load($tagId);
if (!$tag->getId()) {
$this->messageManager->addErrorMessage(__('This tag no longer exists.'));
return false;
}
}
if ($register) {
$this->coreRegistry->register('mageplaza_blog_tag', $tag);
}
return $tag;
}
}
@@ -0,0 +1,60 @@
<?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\Tag;
use Exception;
use Magento\Framework\Controller\Result\Redirect;
use Mageplaza\Blog\Controller\Adminhtml\Tag;
/**
* Class Delete
* @package Mageplaza\Blog\Controller\Adminhtml\Tag
*/
class Delete extends Tag
{
/**
* @return Redirect
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
if ($id = $this->getRequest()->getParam('id')) {
try {
$this->tagFactory->create()
->load($id)
->delete();
$this->messageManager->addSuccessMessage(__('The Tag has been deleted.'));
} catch (Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$resultRedirect->setPath('mageplaza_blog/*/edit', ['id' => $id]);
return $resultRedirect;
}
} else {
$this->messageManager->addErrorMessage(__('Tag to delete was not found.'));
}
$resultRedirect->setPath('mageplaza_blog/*/');
return $resultRedirect;
}
}
+95
View File
@@ -0,0 +1,95 @@
<?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\Tag;
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\Tag;
use Mageplaza\Blog\Model\TagFactory;
/**
* Class Edit
* @package Mageplaza\Blog\Controller\Adminhtml\Tag
*/
class Edit extends Tag
{
/**
* Page factory
*
* @var PageFactory
*/
public $resultPageFactory;
/**
* Edit constructor.
*
* @param Context $context
* @param Registry $registry
* @param TagFactory $tagFactory
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
Registry $registry,
TagFactory $tagFactory,
PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context, $registry, $tagFactory);
}
/**
* @return \Magento\Backend\Model\View\Result\Page|Redirect|Page
*/
public function execute()
{
/** @var \Mageplaza\Blog\Model\Tag $tag */
$tag = $this->initTag();
if (!$tag) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('*');
return $resultRedirect;
}
$data = $this->_session->getData('mageplaza_blog_tag_data', true);
if (!empty($data)) {
$tag->setData($data);
}
$this->coreRegistry->register('mageplaza_blog_tag', $tag);
/** @var \Magento\Backend\Model\View\Result\Page|Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu('Mageplaza_Blog::tag');
$resultPage->getConfig()->getTitle()->set(__('Tags'));
$title = $tag->getId() ? $tag->getName() : __('New Tag');
$resultPage->getConfig()->getTitle()->prepend($title);
return $resultPage;
}
}
+76
View File
@@ -0,0 +1,76 @@
<?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\Tag;
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\Tag
*/
class Index extends Action
{
/**
* Page result factory
*
* @var PageFactory
*/
public $resultPageFactory;
/**
* Page factory
*
* @var \Magento\Backend\Model\View\Result\Page
*/
public $resultPage;
/**
* 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(__('Tags'));
return $resultPage;
}
}
@@ -0,0 +1,122 @@
<?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\Tag;
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\Tag;
use Mageplaza\Blog\Model\TagFactory;
use RuntimeException;
/**
* Class InlineEdit
* @package Mageplaza\Blog\Controller\Adminhtml\Tag
*/
class InlineEdit extends Action
{
/**
* @var JsonFactory
*/
public $jsonFactory;
/**
* @var TagFactory
*/
public $tagFactory;
/**
* InlineEdit constructor.
*
* @param Context $context
* @param JsonFactory $jsonFactory
* @param TagFactory $tagFactory
*/
public function __construct(
Context $context,
JsonFactory $jsonFactory,
TagFactory $tagFactory
) {
$this->jsonFactory = $jsonFactory;
$this->tagFactory = $tagFactory;
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);
$tagId = !empty($key) ? (int)$key[0] : '';
/** @var Tag $tag */
$tag = $this->tagFactory->create()->load($tagId);
try {
$tag->addData($postItems[$tagId])
->save();
} catch (LocalizedException $e) {
$messages[] = $this->getErrorWithTagId($tag, $e->getMessage());
$error = true;
} catch (RuntimeException $e) {
$messages[] = $this->getErrorWithTagId($tag, $e->getMessage());
$error = true;
} catch (Exception $e) {
$messages[] = $this->getErrorWithTagId($tag, __('Something went wrong while saving the Tag.'));
$error = true;
}
return $resultJson->setData([
'messages' => $messages,
'error' => $error
]);
}
/**
* Add Tag id to error message
*
* @param Tag $tag
* @param string $errorText
*
* @return string
*/
public function getErrorWithTagId(Tag $tag, $errorText)
{
return '[Tag ID: ' . $tag->getId() . '] ' . $errorText;
}
}
@@ -0,0 +1,89 @@
<?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\Tag;
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\Tag\CollectionFactory;
/**
* Class MassDelete
* @package Mageplaza\Blog\Controller\Adminhtml\Tag
*/
class MassDelete extends Action
{
/**
* @var Filter
*/
public $filter;
/**
* @var CollectionFactory
*/
public $collectionFactory;
/**
* MassDelete constructor.
*
* @param Context $context
* @param Filter $filter
* @param CollectionFactory $collectionFactory
*/
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(__('Tags has been deleted.'));
} catch (Exception $e) {
$this->messageManager->addSuccessMessage(__('Something wrong when delete Tag.'));
}
/** @var Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath('*/*/');
}
}
@@ -0,0 +1,108 @@
<?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\Tag;
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\Tag\CollectionFactory;
/**
* Class MassStatus
* @package Mageplaza\Blog\Controller\Adminhtml\Tag
*/
class MassStatus extends Action
{
/**
* Mass Action Filter
*
* @var Filter
*/
public $filter;
/**
* Collection Factory
*
* @var \Mageplaza\Blog\Model\ResourceModel\Post\CollectionFactory
*/
public $collectionFactory;
/**
* MassStatus constructor.
*
* @param Context $context
* @param Filter $filter
* @param CollectionFactory $collectionFactory
*/
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');
$tagUpdated = 0;
foreach ($collection as $tag) {
try {
$tag->setEnabled($status)
->save();
$tagUpdated++;
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (Exception $e) {
$this->_getSession()->addException(
$e,
__('Something went wrong while updating status for %1.', $tag->getName())
);
}
}
if ($tagUpdated) {
$this->messageManager->addSuccessMessage(__('A total of %1 record(s) have been updated.', $tagUpdated));
}
/** @var Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath('*/*/');
}
}
@@ -0,0 +1,67 @@
<?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\Tag;
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\Tag
*/
class NewAction extends Action
{
/**
* @var ForwardFactory
*/
public $resultForwardFactory;
/**
* NewAction constructor.
*
* @param ForwardFactory $resultForwardFactory
* @param Context $context
*/
public function __construct(
ForwardFactory $resultForwardFactory,
Context $context
) {
$this->resultForwardFactory = $resultForwardFactory;
parent::__construct($context);
}
/**
* forward to edit
*
* @return Forward
*/
public function execute()
{
$resultForward = $this->resultForwardFactory->create();
$resultForward->forward('edit');
return $resultForward;
}
}
+70
View File
@@ -0,0 +1,70 @@
<?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\Tag;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\Layout;
use Magento\Framework\View\Result\LayoutFactory;
use Mageplaza\Blog\Controller\Adminhtml\Tag;
use Mageplaza\Blog\Model\TagFactory;
/**
* Class Posts
* @package Mageplaza\Blog\Controller\Adminhtml\Tag
*/
class Posts extends Tag
{
/**
* @var LayoutFactory
*/
public $resultLayoutFactory;
/**
* Posts constructor.
*
* @param Context $context
* @param Registry $registry
* @param LayoutFactory $resultLayoutFactory
* @param TagFactory $postFactory
*/
public function __construct(
Context $context,
Registry $registry,
LayoutFactory $resultLayoutFactory,
TagFactory $postFactory
) {
$this->resultLayoutFactory = $resultLayoutFactory;
parent::__construct($context, $registry, $postFactory);
}
/**
* @return Layout
*/
public function execute()
{
$this->initTag(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\Tag;
/**
* Class PostsGrid
* @package Mageplaza\Blog\Controller\Adminhtml\Tag
*/
class PostsGrid extends Posts
{
}
+204
View File
@@ -0,0 +1,204 @@
<?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\Tag;
use Exception;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Helper\Js;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Message\MessageInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Messages;
use Magento\Framework\View\LayoutFactory;
use Mageplaza\Blog\Controller\Adminhtml\Tag;
use Mageplaza\Blog\Model\TagFactory;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Mageplaza\Blog\Helper\Data;
/**
* Class Save
* @package Mageplaza\Blog\Controller\Adminhtml\Tag
*/
class Save extends Tag
{
/**
* @var Js
*/
public $jsHelper;
/**
* Layout Factory
*
* @var LayoutFactory
*/
public $layoutFactory;
/**
* Result Json Factory
*
* @var JsonFactory
*/
public $resultJsonFactory;
/**
* @var Data
*/
public $_helperData;
/**
* Save constructor.
*
* @param Context $context
* @param Registry $registry
* @param Js $jsHelper
* @param LayoutFactory $layoutFactory
* @param JsonFactory $resultJsonFactory
* @param TagFactory $tagFactory
* @param Data $helperData
*/
public function __construct(
Context $context,
Registry $registry,
Js $jsHelper,
LayoutFactory $layoutFactory,
JsonFactory $resultJsonFactory,
TagFactory $tagFactory,
Data $helperData
) {
$this->jsHelper = $jsHelper;
$this->layoutFactory = $layoutFactory;
$this->resultJsonFactory = $resultJsonFactory;
$this->_helperData = $helperData;
parent::__construct($context, $registry, $tagFactory);
}
/**
* @return $this|ResponseInterface|Redirect|ResultInterface
*/
public function execute()
{
if ($this->getRequest()->getPost('return_session_messages_only')) {
$tag = $this->initTag();
$tagPostData = $this->getRequest()->getPostValue();
$tagPostData['store_ids'] = 0;
$tagPostData['enabled'] = 1;
$this->_helperData->handleSeoValueBeforeSave($tagPostData);
$tag->addData($tagPostData);
try {
$tag->save();
$this->messageManager->addSuccessMessage(__('You saved the tag.'));
} catch (AlreadyExistsException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$this->_objectManager->get(LoggerInterface::class)->critical($e);
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$this->_objectManager->get(LoggerInterface::class)->critical($e);
} catch (Exception $e) {
$this->messageManager->addErrorMessage(__('Something went wrong while saving the tag.'));
$this->_objectManager->get(LoggerInterface::class)->critical($e);
}
$hasError = (bool) $this->messageManager->getMessages()->getCountByType(
MessageInterface::TYPE_ERROR
);
$tag->load($tag->getId());
$tag->addData([
'level' => 1,
'entity_id' => $tag->getId(),
'is_active' => $tag->getEnabled(),
'parent' => 0
]);
// to obtain truncated category name
/** @var $block Messages */
$block = $this->layoutFactory->create()->getMessagesBlock();
$block->setMessages($this->messageManager->getMessages(true));
/** @var Json $resultJson */
$resultJson = $this->resultJsonFactory->create();
return $resultJson->setData([
'messages' => $block->getGroupedHtml(),
'error' => $hasError,
'category' => $tag->toArray(),
]);
}
$resultRedirect = $this->resultRedirectFactory->create();
if ($data = $this->getRequest()->getPost('tag')) {
/** @var \Mageplaza\Blog\Model\Tag $tag */
$tag = $this->initTag();
$this->_helperData->handleSeoValueBeforeSave($data);
$tag->addData($data);
if ($posts = $this->getRequest()->getPost('posts', false)) {
$tag->setPostsData($this->jsHelper->decodeGridSerializedInput($posts));
}
$this->_eventManager->dispatch(
'mageplaza_blog_tag_prepare_save',
['tag' => $tag, 'request' => $this->getRequest()]
);
try {
$tag->save();
$this->messageManager->addSuccessMessage(__('The Tag has been saved.'));
$this->_session->setData('mageplaza_blog_tag_data', false);
if ($this->getRequest()->getParam('back')) {
$resultRedirect->setPath('mageplaza_blog/*/edit', ['id' => $tag->getId(), '_current' => true]);
} else {
$resultRedirect->setPath('mageplaza_blog/*/');
}
return $resultRedirect;
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (RuntimeException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (Exception $e) {
$this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the Tag.'));
}
$this->_getSession()->setData('mageplaza_blog_tag_data', $data);
$resultRedirect->setPath('mageplaza_blog/*/edit', ['id' => $tag->getId(), '_current' => true]);
return $resultRedirect;
}
$resultRedirect->setPath('mageplaza_blog/*/');
return $resultRedirect;
}
}
+96
View File
@@ -0,0 +1,96 @@
<?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;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Mageplaza\Blog\Model\TopicFactory;
/**
* Class Topic
* @package Mageplaza\Blog\Controller\Adminhtml
*/
abstract class Topic extends Action
{
/** Authorization level of a basic admin session */
const ADMIN_RESOURCE = 'Mageplaza_Blog::topic';
/**
* Topic Factory
*
* @var TopicFactory
*/
public $topicFactory;
/**
* Core registry
*
* @var Registry
*/
public $coreRegistry;
/**
* Topic constructor.
*
* @param TopicFactory $topicFactory
* @param Registry $coreRegistry
* @param Context $context
*/
public function __construct(
Context $context,
Registry $coreRegistry,
TopicFactory $topicFactory
) {
$this->topicFactory = $topicFactory;
$this->coreRegistry = $coreRegistry;
parent::__construct($context);
}
/**
* @param bool $register
*
* @return bool|\Mageplaza\Blog\Model\Topic
*/
protected function initTopic($register = false)
{
$topicId = (int)$this->getRequest()->getParam('id');
/** @var \Mageplaza\Blog\Model\Topic $topic */
$topic = $this->topicFactory->create();
if ($topicId) {
$topic->load($topicId);
if (!$topic->getId()) {
$this->messageManager->addErrorMessage(__('This topic no longer exists.'));
return false;
}
}
if ($register) {
$this->coreRegistry->register('mageplaza_blog_topic', $topic);
}
return $topic;
}
}
@@ -0,0 +1,61 @@
<?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\Topic;
use Exception;
use Magento\Framework\Controller\Result\Redirect;
use Mageplaza\Blog\Controller\Adminhtml\Topic;
/**
* Class Delete
* @package Mageplaza\Blog\Controller\Adminhtml\Topic
*/
class Delete extends Topic
{
/**
* @return Redirect
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
if ($id = $this->getRequest()->getParam('id')) {
try {
$this->topicFactory->create()
->load($id)
->delete();
$this->messageManager->addSuccessMessage(__('The Topic has been deleted.'));
} catch (Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$resultRedirect->setPath('mageplaza_blog/*/edit', ['id' => $id]);
return $resultRedirect;
}
} else {
$this->messageManager->addErrorMessage(__('Topic to delete was not found.'));
}
$resultRedirect->setPath('mageplaza_blog/*/');
return $resultRedirect;
}
}
@@ -0,0 +1,95 @@
<?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\Topic;
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\Topic;
use Mageplaza\Blog\Model\TopicFactory;
/**
* Class Edit
* @package Mageplaza\Blog\Controller\Adminhtml\Topic
*/
class Edit extends Topic
{
/**
* Page factory
*
* @var PageFactory
*/
public $resultPageFactory;
/**
* Edit constructor.
*
* @param Context $context
* @param Registry $registry
* @param PageFactory $resultPageFactory
* @param TopicFactory $topicFactory
*/
public function __construct(
Context $context,
Registry $registry,
PageFactory $resultPageFactory,
TopicFactory $topicFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context, $registry, $topicFactory);
}
/**
* @return \Magento\Backend\Model\View\Result\Page|Redirect|Page
*/
public function execute()
{
/** @var \Mageplaza\Blog\Model\Topic $topic */
$topic = $this->initTopic();
if (!$topic) {
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('*');
return $resultRedirect;
}
$data = $this->_session->getData('mageplaza_blog_topic_data', true);
if (!empty($data)) {
$topic->setData($data);
}
$this->coreRegistry->register('mageplaza_blog_topic', $topic);
/** @var \Magento\Backend\Model\View\Result\Page|Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu('Mageplaza_Blog::topic');
$resultPage->getConfig()->getTitle()->set(__('Topics'));
$title = $topic->getId() ? $topic->getName() : __('New Topic');
$resultPage->getConfig()->getTitle()->prepend($title);
return $resultPage;
}
}
@@ -0,0 +1,76 @@
<?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\Topic;
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\Topic
*/
class Index extends Action
{
/**
* Page result factory
*
* @var PageFactory
*/
public $resultPageFactory;
/**
* Page factory
*
* @var \Magento\Backend\Model\View\Result\Page
*/
public $resultPage;
/**
* 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((__('Topics')));
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\Topic;
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\Topic;
use Mageplaza\Blog\Model\TopicFactory;
use RuntimeException;
/**
* Class InlineEdit
* @package Mageplaza\Blog\Controller\Adminhtml\Topic
*/
class InlineEdit extends Action
{
/**
* JSON Factory
*
* @var JsonFactory
*/
public $jsonFactory;
/**
* Topic Factory
*
* @var TopicFactory
*/
public $topicFactory;
/**
* InlineEdit constructor.
*
* @param Context $context
* @param JsonFactory $jsonFactory
* @param TopicFactory $topicFactory
*/
public function __construct(
Context $context,
JsonFactory $jsonFactory,
TopicFactory $topicFactory
) {
$this->jsonFactory = $jsonFactory;
$this->topicFactory = $topicFactory;
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);
$topicId = !empty($key) ? (int)$key[0] : '';
/** @var Topic $topic */
$topic = $this->topicFactory->create()->load($topicId);
try {
$topic->addData($postItems[$topicId])
->save();
} catch (LocalizedException $e) {
$messages[] = $this->getErrorWithTopicId($topic, $e->getMessage());
$error = true;
} catch (RuntimeException $e) {
$messages[] = $this->getErrorWithTopicId($topic, $e->getMessage());
$error = true;
} catch (Exception $e) {
$messages[] = $this->getErrorWithTopicId(
$topic,
__('Something went wrong while saving the Topic.')
);
$error = true;
}
return $resultJson->setData([
'messages' => $messages,
'error' => $error
]);
}
/**
* Add Topic id to error message
*
* @param Topic $topic
* @param string $errorText
*
* @return string
*/
public function getErrorWithTopicId(Topic $topic, $errorText)
{
return '[Topic ID: ' . $topic->getId() . '] ' . $errorText;
}
}
@@ -0,0 +1,92 @@
<?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\Topic;
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\Topic\CollectionFactory;
/**
* Class MassDelete
* @package Mageplaza\Blog\Controller\Adminhtml\Topic
*/
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(__('Topics has been deleted.'));
} catch (Exception $e) {
$this->messageManager->addSuccessMessage(__('Something wrong when delete Topics.'));
}
/** @var Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath('*/*/');
}
}
@@ -0,0 +1,108 @@
<?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\Topic;
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\Topic\CollectionFactory;
/**
* Class MassStatus
* @package Mageplaza\Blog\Controller\Adminhtml\Topic
*/
class MassStatus extends Action
{
/**
* Mass Action Filter
*
* @var Filter
*/
public $filter;
/**
* Collection Factory
*
* @var \Mageplaza\Blog\Model\ResourceModel\Post\CollectionFactory
*/
public $collectionFactory;
/**
* MassStatus constructor.
*
* @param Context $context
* @param Filter $filter
* @param CollectionFactory $collectionFactory
*/
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');
$topicUpdated = 0;
foreach ($collection as $topic) {
try {
$topic->setEnabled($status)
->save();
$topicUpdated++;
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (Exception $e) {
$this->_getSession()->addException(
$e,
__('Something went wrong while updating status for %1.', $topic->getName())
);
}
}
if ($topicUpdated) {
$this->messageManager->addSuccessMessage(__('A total of %1 record(s) have been updated.', $topicUpdated));
}
/** @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\Topic;
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\Topic
*/
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,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\Topic;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\Layout;
use Magento\Framework\View\Result\LayoutFactory;
use Mageplaza\Blog\Controller\Adminhtml\Topic;
use Mageplaza\Blog\Model\TopicFactory;
/**
* Class Posts
* @package Mageplaza\Blog\Controller\Adminhtml\Topic
*/
class Posts extends Topic
{
/**
* Result layout factory
*
* @var LayoutFactory
*/
public $resultLayoutFactory;
/**
* Posts constructor.
*
* @param LayoutFactory $resultLayoutFactory
* @param TopicFactory $postFactory
* @param Registry $registry
* @param Context $context
*/
public function __construct(
Context $context,
Registry $registry,
LayoutFactory $resultLayoutFactory,
TopicFactory $postFactory
) {
$this->resultLayoutFactory = $resultLayoutFactory;
parent::__construct($context, $registry, $postFactory);
}
/**
* @return Layout
*/
public function execute()
{
$this->initTopic(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\Topic;
/**
* Class PostsGrid
* @package Mageplaza\Blog\Controller\Adminhtml\Topic
*/
class PostsGrid extends Posts
{
}
+205
View File
@@ -0,0 +1,205 @@
<?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\Topic;
use Exception;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Helper\Js;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Message\MessageInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Messages;
use Magento\Framework\View\LayoutFactory;
use Mageplaza\Blog\Controller\Adminhtml\Topic;
use Mageplaza\Blog\Model\TopicFactory;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Mageplaza\Blog\Helper\Data;
/**
* Class Save
* @package Mageplaza\Blog\Controller\Adminhtml\Topic
*/
class Save extends Topic
{
/**
* JS helper
*
* @var Js
*/
public $jsHelper;
/**
* Layout Factory
*
* @var LayoutFactory
*/
public $layoutFactory;
/**
* Result Json Factory
*
* @var JsonFactory
*/
public $resultJsonFactory;
/**
* @var Data
*/
public $_helperData;
/**
* Save constructor.
*
* @param Context $context
* @param Registry $registry
* @param Js $jsHelper
* @param LayoutFactory $layoutFactory
* @param JsonFactory $resultJsonFactory
* @param TopicFactory $topicFactory
* @param Data $helperData
*/
public function __construct(
Context $context,
Registry $registry,
Js $jsHelper,
LayoutFactory $layoutFactory,
JsonFactory $resultJsonFactory,
TopicFactory $topicFactory,
Data $helperData
) {
$this->jsHelper = $jsHelper;
$this->layoutFactory = $layoutFactory;
$this->resultJsonFactory = $resultJsonFactory;
$this->_helperData = $helperData;
parent::__construct($context, $registry, $topicFactory);
}
/**
* @return $this|ResponseInterface|Redirect|ResultInterface
*/
public function execute()
{
if ($this->getRequest()->getPost('return_session_messages_only')) {
$topic = $this->initTopic();
$topicPostData = $this->getRequest()->getPostValue();
$topicPostData['store_ids'] = 0;
$topicPostData['enabled'] = 1;
$this->_helperData->handleSeoValueBeforeSave($topicPostData);
$topic->addData($topicPostData);
try {
$topic->save();
$this->messageManager->addSuccessMessage(__('You saved the topic.'));
} catch (AlreadyExistsException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$this->_objectManager->get(LoggerInterface::class)->critical($e);
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$this->_objectManager->get(LoggerInterface::class)->critical($e);
} catch (Exception $e) {
$this->messageManager->addErrorMessage(__('Something went wrong while saving the topic.'));
$this->_objectManager->get(LoggerInterface::class)->critical($e);
}
$hasError = (bool) $this->messageManager->getMessages()->getCountByType(
MessageInterface::TYPE_ERROR
);
$topic->load($topic->getId());
$topic->addData([
'level' => 1,
'entity_id' => $topic->getId(),
'is_active' => $topic->getEnabled(),
'parent' => 0
]);
// to obtain truncated category name
/** @var $block Messages */
$block = $this->layoutFactory->create()->getMessagesBlock();
$block->setMessages($this->messageManager->getMessages(true));
/** @var Json $resultJson */
$resultJson = $this->resultJsonFactory->create();
return $resultJson->setData(
[
'messages' => $block->getGroupedHtml(),
'error' => $hasError,
'category' => $topic->toArray(),
]
);
}
$resultRedirect = $this->resultRedirectFactory->create();
if ($data = $this->getRequest()->getPost('topic')) {
/** @var \Mageplaza\Blog\Model\Topic $topic */
$topic = $this->initTopic();
$this->_helperData->handleSeoValueBeforeSave($data);
$topic->setData($data);
if ($posts = $this->getRequest()->getPost('posts', false)) {
$topic->setPostsData($this->jsHelper->decodeGridSerializedInput($posts));
}
try {
$topic->save();
$this->messageManager->addSuccessMessage(__('The Topic has been saved.'));
$this->_getSession()->setData('mageplaza_blog_topic_data', false);
if ($this->getRequest()->getParam('back')) {
$resultRedirect->setPath('mageplaza_blog/*/edit', ['id' => $topic->getId(), '_current' => true]);
} else {
$resultRedirect->setPath('mageplaza_blog/*/');
}
return $resultRedirect;
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (RuntimeException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (Exception $e) {
$this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the Topic.'));
}
$this->_getSession()->setData('mageplaza_blog_topic_data', $data);
$resultRedirect->setPath('mageplaza_blog/*/edit', ['id' => $topic->getId(), '_current' => true]);
return $resultRedirect;
}
$resultRedirect->setPath('mageplaza_blog/*/');
return $resultRedirect;
}
}