init
This commit is contained in:
+47
@@ -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\Post;
|
||||
|
||||
use Magento\Framework\App\Action\Action;
|
||||
use Magento\Framework\App\ResponseInterface;
|
||||
use Magento\Framework\Controller\Result\Redirect;
|
||||
use Magento\Framework\Controller\ResultInterface;
|
||||
use Magento\Framework\View\Result\Page;
|
||||
|
||||
/**
|
||||
* Class AddPost
|
||||
* @package Mageplaza\Blog\Controller\Author
|
||||
*/
|
||||
class AddPost extends Action
|
||||
{
|
||||
/**
|
||||
* @return ResponseInterface|Redirect|ResultInterface|Page
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$resultRedirect = $this->resultRedirectFactory->create();
|
||||
|
||||
$resultRedirect->setPath('mpblog/*/editpost');
|
||||
|
||||
return $resultRedirect;
|
||||
}
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
<?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\Post;
|
||||
|
||||
use Exception;
|
||||
use Magento\Framework\App\Action\Action;
|
||||
use Magento\Framework\App\Action\Context;
|
||||
use Magento\Framework\App\ResponseInterface;
|
||||
use Magento\Framework\Controller\ResultInterface;
|
||||
use Magento\Framework\Stdlib\DateTime\DateTime;
|
||||
use Mageplaza\Blog\Helper\Data;
|
||||
use Mageplaza\Blog\Model\PostFactory;
|
||||
|
||||
/**
|
||||
* Class Manage
|
||||
* @package Mageplaza\Blog\Controller\Post
|
||||
*/
|
||||
class DeletePost extends Action
|
||||
{
|
||||
/**
|
||||
* @var PostFactory
|
||||
*/
|
||||
protected $postFactory;
|
||||
|
||||
/**
|
||||
* @var DateTime
|
||||
*/
|
||||
protected $date;
|
||||
|
||||
/**
|
||||
* @var Data
|
||||
*/
|
||||
protected $_helperBlog;
|
||||
|
||||
/**
|
||||
* DeletePost constructor.
|
||||
*
|
||||
* @param Context $context
|
||||
* @param PostFactory $postFactory
|
||||
* @param Data $helperData
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
PostFactory $postFactory,
|
||||
Data $helperData
|
||||
) {
|
||||
$this->_helperBlog = $helperData;
|
||||
$this->postFactory = $postFactory;
|
||||
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ResponseInterface|ResultInterface|null
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$postId = $this->getRequest()->getParam('post_id');
|
||||
$this->_helperBlog->setCustomerContextId();
|
||||
$author = $this->_helperBlog->getCurrentAuthor();
|
||||
$post = $this->postFactory->create();
|
||||
|
||||
if (!$author || !$postId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$post->load($postId)->delete();
|
||||
$this->messageManager->addSuccessMessage(__('The post has been deleted.'));
|
||||
|
||||
return $this->getResponse()->representJson(Data::jsonEncode([
|
||||
'status' => 1,
|
||||
'post_id' => $postId
|
||||
]));
|
||||
} catch (Exception $exception) {
|
||||
$this->messageManager->addErrorMessage($exception->getMessage());
|
||||
|
||||
return $this->getResponse()->representJson(Data::jsonEncode([
|
||||
'status' => 0
|
||||
]));
|
||||
}
|
||||
}
|
||||
}
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
<?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\Post;
|
||||
|
||||
use Magento\Customer\Model\Session;
|
||||
use Magento\Framework\App\Action\Action;
|
||||
use Magento\Framework\App\Action\Context;
|
||||
use Magento\Framework\App\ResponseInterface;
|
||||
use Magento\Framework\Controller\Result\ForwardFactory;
|
||||
use Magento\Framework\Controller\Result\Redirect;
|
||||
use Magento\Framework\Controller\ResultInterface;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\Framework\View\Result\Page;
|
||||
use Magento\Framework\View\Result\PageFactory;
|
||||
use Mageplaza\Blog\Helper\Data;
|
||||
use Mageplaza\Blog\Model\ResourceModel\Author\Collection as AuthorCollection;
|
||||
|
||||
/**
|
||||
* Class EditPost
|
||||
* @package Mageplaza\Blog\Controller\Author
|
||||
*/
|
||||
class EditPost extends Action
|
||||
{
|
||||
/**
|
||||
* @var PageFactory
|
||||
*/
|
||||
public $resultPageFactory;
|
||||
|
||||
/**
|
||||
* @var ForwardFactory
|
||||
*/
|
||||
protected $resultForwardFactory;
|
||||
|
||||
/**
|
||||
* @var Data
|
||||
*/
|
||||
protected $_helperBlog;
|
||||
|
||||
/**
|
||||
* @var AuthorCollection
|
||||
*/
|
||||
protected $authorCollection;
|
||||
|
||||
/**
|
||||
* @var Session
|
||||
*/
|
||||
protected $customerSession;
|
||||
|
||||
/**
|
||||
* @var Registry
|
||||
*/
|
||||
protected $coreRegistry;
|
||||
|
||||
/**
|
||||
* View constructor.
|
||||
*
|
||||
* @param Context $context
|
||||
* @param PageFactory $resultPageFactory
|
||||
* @param ForwardFactory $resultForwardFactory
|
||||
* @param AuthorCollection $authorCollection
|
||||
* @param Session $customerSession
|
||||
* @param Registry $coreRegistry
|
||||
* @param Data $helperData
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
PageFactory $resultPageFactory,
|
||||
ForwardFactory $resultForwardFactory,
|
||||
AuthorCollection $authorCollection,
|
||||
Session $customerSession,
|
||||
Registry $coreRegistry,
|
||||
Data $helperData
|
||||
) {
|
||||
$this->_helperBlog = $helperData;
|
||||
$this->resultPageFactory = $resultPageFactory;
|
||||
$this->resultForwardFactory = $resultForwardFactory;
|
||||
$this->authorCollection = $authorCollection;
|
||||
$this->customerSession = $customerSession;
|
||||
$this->coreRegistry = $coreRegistry;
|
||||
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ResponseInterface|Redirect|ResultInterface|Page
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$resultRedirect = $this->resultRedirectFactory->create();
|
||||
$this->_helperBlog->setCustomerContextId();
|
||||
|
||||
if (!$this->_helperBlog->isAuthor()
|
||||
&& $this->_helperBlog->isLogin()
|
||||
&& $this->getRequest()->isAjax()
|
||||
) {
|
||||
$page = $this->resultPageFactory->create();
|
||||
if ($this->getRequest()->getParam('postId')) {
|
||||
$author = $this->_helperBlog->getCurrentAuthor();
|
||||
|
||||
$this->coreRegistry->register('mp_author', $author);
|
||||
|
||||
$page->getConfig()->getTitle()->set($author->getName());
|
||||
}
|
||||
$page->getConfig()->setPageLayout('1column');
|
||||
$this->_view->loadLayout();
|
||||
$layout = $this->_view->getLayout();
|
||||
$this->getResponse()->setBody($layout->renderElement('main.content'));
|
||||
|
||||
return $page;
|
||||
} else {
|
||||
$resultRedirect->setPath('customer/account');
|
||||
|
||||
return $resultRedirect;
|
||||
}
|
||||
}
|
||||
}
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
<?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\Post;
|
||||
|
||||
use Magento\Customer\Model\Session;
|
||||
use Magento\Framework\App\Action\Action;
|
||||
use Magento\Framework\App\Action\Context;
|
||||
use Magento\Framework\App\ResponseInterface;
|
||||
use Magento\Framework\Controller\Result\ForwardFactory;
|
||||
use Magento\Framework\Controller\Result\Redirect;
|
||||
use Magento\Framework\Controller\ResultInterface;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\Framework\View\Result\Page;
|
||||
use Magento\Framework\View\Result\PageFactory;
|
||||
use Mageplaza\Blog\Helper\Data;
|
||||
use Mageplaza\Blog\Model\Config\Source\SideBarLR;
|
||||
use Mageplaza\Blog\Model\ResourceModel\Author\Collection as AuthorCollection;
|
||||
|
||||
/**
|
||||
* Class View
|
||||
* @package Mageplaza\Blog\Controller\Author
|
||||
*/
|
||||
class History extends Action
|
||||
{
|
||||
/**
|
||||
* @var PageFactory
|
||||
*/
|
||||
public $resultPageFactory;
|
||||
|
||||
/**
|
||||
* @var ForwardFactory
|
||||
*/
|
||||
protected $resultForwardFactory;
|
||||
|
||||
/**
|
||||
* @var Data
|
||||
*/
|
||||
protected $_helperBlog;
|
||||
|
||||
/**
|
||||
* @var AuthorCollection
|
||||
*/
|
||||
protected $authorCollection;
|
||||
|
||||
/**
|
||||
* @var Session
|
||||
*/
|
||||
protected $customerSession;
|
||||
|
||||
/**
|
||||
* @var Registry
|
||||
*/
|
||||
protected $coreRegistry;
|
||||
|
||||
/**
|
||||
* View constructor.
|
||||
*
|
||||
* @param Context $context
|
||||
* @param PageFactory $resultPageFactory
|
||||
* @param ForwardFactory $resultForwardFactory
|
||||
* @param AuthorCollection $authorCollection
|
||||
* @param Session $customerSession
|
||||
* @param Registry $coreRegistry
|
||||
* @param Data $helperData
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
PageFactory $resultPageFactory,
|
||||
ForwardFactory $resultForwardFactory,
|
||||
AuthorCollection $authorCollection,
|
||||
Session $customerSession,
|
||||
Registry $coreRegistry,
|
||||
Data $helperData
|
||||
) {
|
||||
$this->_helperBlog = $helperData;
|
||||
$this->resultPageFactory = $resultPageFactory;
|
||||
$this->resultForwardFactory = $resultForwardFactory;
|
||||
$this->authorCollection = $authorCollection;
|
||||
$this->customerSession = $customerSession;
|
||||
$this->coreRegistry = $coreRegistry;
|
||||
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ResponseInterface|Redirect|ResultInterface|Page
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$resultRedirect = $this->resultRedirectFactory->create();
|
||||
$this->_helperBlog->setCustomerContextId();
|
||||
|
||||
if (!$this->_helperBlog->isEnabledAuthor()) {
|
||||
$resultRedirect->setPath('customer/account');
|
||||
|
||||
return $resultRedirect;
|
||||
}
|
||||
|
||||
if (!$this->_helperBlog->isAuthor()) {
|
||||
$author = $this->_helperBlog->getCurrentAuthor();
|
||||
|
||||
$this->coreRegistry->register('mp_author', $author);
|
||||
|
||||
$page = $this->resultPageFactory->create();
|
||||
$page->getConfig()->setPageLayout(SideBarLR::LEFT);
|
||||
$page->getConfig()->getTitle()->set($author->getName());
|
||||
|
||||
return $page;
|
||||
}
|
||||
|
||||
if ($this->_helperBlog->isLogin()) {
|
||||
$resultRedirect->setPath('mpblog/*/signup');
|
||||
} else {
|
||||
$resultRedirect->setPath('customer/account');
|
||||
}
|
||||
|
||||
return $resultRedirect;
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
<?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\Post;
|
||||
|
||||
use Magento\Framework\App\Action\Action;
|
||||
use Magento\Framework\App\Action\Context;
|
||||
use Magento\Framework\View\Result\Page;
|
||||
use Magento\Framework\View\Result\PageFactory;
|
||||
use Mageplaza\Blog\Helper\Data;
|
||||
|
||||
/**
|
||||
* Class Index
|
||||
* @package Mageplaza\Blog\Controller\Post
|
||||
*/
|
||||
class Index extends Action
|
||||
{
|
||||
/**
|
||||
* @var PageFactory
|
||||
*/
|
||||
public $resultPageFactory;
|
||||
|
||||
/**
|
||||
* @var Data
|
||||
*/
|
||||
protected $_helperBlog;
|
||||
|
||||
/**
|
||||
* Index constructor.
|
||||
*
|
||||
* @param Context $context
|
||||
* @param PageFactory $resultPageFactory
|
||||
* @param Data $helperData
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
PageFactory $resultPageFactory,
|
||||
Data $helperData
|
||||
) {
|
||||
$this->_helperBlog = $helperData;
|
||||
$this->resultPageFactory = $resultPageFactory;
|
||||
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Page
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$page = $this->resultPageFactory->create();
|
||||
$page->getConfig()->setPageLayout($this->_helperBlog->getSidebarLayout());
|
||||
|
||||
return $page;
|
||||
}
|
||||
}
|
||||
+218
@@ -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\Post;
|
||||
|
||||
use Exception;
|
||||
use Magento\Customer\Model\Session;
|
||||
use Magento\Framework\App\Action\Action;
|
||||
use Magento\Framework\App\Action\Context;
|
||||
use Magento\Framework\App\ResponseInterface;
|
||||
use Magento\Framework\Controller\Result\ForwardFactory;
|
||||
use Magento\Framework\Controller\ResultInterface;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\Framework\Stdlib\DateTime\DateTime;
|
||||
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
|
||||
use Magento\Framework\View\Result\PageFactory;
|
||||
use Mageplaza\Blog\Helper\Data;
|
||||
use Mageplaza\Blog\Helper\Image;
|
||||
use Mageplaza\Blog\Model\PostFactory;
|
||||
use Mageplaza\Blog\Model\ResourceModel\Author\Collection as AuthorCollection;
|
||||
|
||||
/**
|
||||
* Class Manage
|
||||
* @package Mageplaza\Blog\Controller\Post
|
||||
*/
|
||||
class Manage extends Action
|
||||
{
|
||||
/**
|
||||
* @var PageFactory
|
||||
*/
|
||||
public $resultPageFactory;
|
||||
|
||||
/**
|
||||
* @var ForwardFactory
|
||||
*/
|
||||
protected $resultForwardFactory;
|
||||
|
||||
/**
|
||||
* @var Data
|
||||
*/
|
||||
protected $_helperBlog;
|
||||
|
||||
/**
|
||||
* @var AuthorCollection
|
||||
*/
|
||||
protected $authorCollection;
|
||||
|
||||
/**
|
||||
* @var Session
|
||||
*/
|
||||
protected $customerSession;
|
||||
|
||||
/**
|
||||
* @var Registry
|
||||
*/
|
||||
protected $coreRegistry;
|
||||
|
||||
/**
|
||||
* @var PostFactory
|
||||
*/
|
||||
protected $postFactory;
|
||||
|
||||
/**
|
||||
* @var DateTime
|
||||
*/
|
||||
protected $date;
|
||||
|
||||
/**
|
||||
* @var TimezoneInterface
|
||||
*/
|
||||
protected $timezone;
|
||||
|
||||
/**
|
||||
* @var Image
|
||||
*/
|
||||
protected $imageHelper;
|
||||
|
||||
/**
|
||||
* View constructor.
|
||||
*
|
||||
* @param Context $context
|
||||
* @param PageFactory $resultPageFactory
|
||||
* @param ForwardFactory $resultForwardFactory
|
||||
* @param PostFactory $postFactory
|
||||
* @param AuthorCollection $authorCollection
|
||||
* @param Session $customerSession
|
||||
* @param Registry $coreRegistry
|
||||
* @param DateTime $date
|
||||
* @param Image $imageHelper
|
||||
* @param Data $helperData
|
||||
* @param TimezoneInterface $timezone
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
PageFactory $resultPageFactory,
|
||||
ForwardFactory $resultForwardFactory,
|
||||
PostFactory $postFactory,
|
||||
AuthorCollection $authorCollection,
|
||||
Session $customerSession,
|
||||
Registry $coreRegistry,
|
||||
DateTime $date,
|
||||
Image $imageHelper,
|
||||
Data $helperData,
|
||||
TimezoneInterface $timezone
|
||||
) {
|
||||
$this->_helperBlog = $helperData;
|
||||
$this->resultPageFactory = $resultPageFactory;
|
||||
$this->resultForwardFactory = $resultForwardFactory;
|
||||
$this->authorCollection = $authorCollection;
|
||||
$this->customerSession = $customerSession;
|
||||
$this->coreRegistry = $coreRegistry;
|
||||
$this->postFactory = $postFactory;
|
||||
$this->date = $date;
|
||||
$this->imageHelper = $imageHelper;
|
||||
$this->timezone = $timezone;
|
||||
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ResponseInterface|ResultInterface|null
|
||||
* @throws NoSuchEntityException
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$data = $this->getRequest()->getParams();
|
||||
$this->_helperBlog->setCustomerContextId();
|
||||
$author = $this->_helperBlog->getCurrentAuthor();
|
||||
$post = $this->postFactory->create();
|
||||
|
||||
if (!$author) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->getRequest()->getFiles('image')['size'] > 0) {
|
||||
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'] : '';
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($data['image']['delete']) || (isset($data['image']) && $data['image'] === 'null')) {
|
||||
$data['image'] = '';
|
||||
}
|
||||
|
||||
$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'] ?? ''
|
||||
) : [];
|
||||
|
||||
$data['author_id'] = $author->getId();
|
||||
$data['store_ids'] = $this->_helperBlog->getCurrentStoreId();
|
||||
$data['enabled'] = $this->_helperBlog->getConfigGeneral('auto_post') ? 1 : 0;
|
||||
$data['in_rss'] = '0';
|
||||
$data['meta_robots'] = '{"0":"INDEX,FOLLOW","' . $data['store_ids'] . '": "INDEX,FOLLOW"}';
|
||||
$data['layout'] = 'empty';
|
||||
/** 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());
|
||||
}
|
||||
|
||||
if ($data['post_id']) {
|
||||
$post->load($data['post_id']);
|
||||
if ($post->getId()) {
|
||||
$post->setData($data);
|
||||
}
|
||||
$data['updated_at'] = $this->date->date();
|
||||
} else {
|
||||
unset($data['post_id']);
|
||||
$data['created_at'] = $this->date->date();
|
||||
$post->setData($data);
|
||||
}
|
||||
|
||||
try {
|
||||
$post->save();
|
||||
$this->messageManager->addSuccessMessage(__('The post has been saved.'));
|
||||
|
||||
return $this->getResponse()->representJson(Data::jsonEncode([
|
||||
'status' => 1
|
||||
]));
|
||||
} catch (Exception $exception) {
|
||||
$this->messageManager->addErrorMessage($exception->getMessage());
|
||||
|
||||
return $this->getResponse()->representJson(Data::jsonEncode([
|
||||
'status' => 0
|
||||
]));
|
||||
}
|
||||
}
|
||||
}
|
||||
+392
@@ -0,0 +1,392 @@
|
||||
<?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\Post;
|
||||
|
||||
use Exception;
|
||||
use Magento\Customer\Api\AccountManagementInterface;
|
||||
use Magento\Customer\Api\Data\CustomerInterface;
|
||||
use Magento\Customer\Model\Session;
|
||||
use Magento\Customer\Model\Url as CustomerUrl;
|
||||
use Magento\Framework\App\Action\Action;
|
||||
use Magento\Framework\App\Action\Context;
|
||||
use Magento\Framework\App\ResponseInterface;
|
||||
use Magento\Framework\Controller\Result\ForwardFactory;
|
||||
use Magento\Framework\Controller\ResultInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\Json\Helper\Data as JsonData;
|
||||
use Magento\Framework\Stdlib\DateTime\DateTime;
|
||||
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
|
||||
use Magento\Framework\View\Result\Page;
|
||||
use Magento\Framework\View\Result\PageFactory;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Mageplaza\Blog\Helper\Data;
|
||||
use Mageplaza\Blog\Helper\Data as HelperBlog;
|
||||
use Mageplaza\Blog\Model\Author;
|
||||
use Mageplaza\Blog\Model\Category;
|
||||
use Mageplaza\Blog\Model\Comment;
|
||||
use Mageplaza\Blog\Model\CommentFactory;
|
||||
use Mageplaza\Blog\Model\Config\Source\Comments\Status;
|
||||
use Mageplaza\Blog\Model\Like;
|
||||
use Mageplaza\Blog\Model\LikeFactory;
|
||||
use Mageplaza\Blog\Model\Post;
|
||||
use Mageplaza\Blog\Model\PostFactory;
|
||||
use Mageplaza\Blog\Model\Tag;
|
||||
use Mageplaza\Blog\Model\Topic;
|
||||
use Mageplaza\Blog\Model\TrafficFactory;
|
||||
|
||||
/**
|
||||
* Class Preview
|
||||
* @package Mageplaza\Blog\Controller\Post
|
||||
*/
|
||||
class Preview extends Action
|
||||
{
|
||||
const COMMENT = 1;
|
||||
const LIKE = 2;
|
||||
|
||||
/**
|
||||
* @var TrafficFactory
|
||||
*/
|
||||
protected $trafficFactory;
|
||||
|
||||
/**
|
||||
* @var PageFactory
|
||||
*/
|
||||
protected $resultPageFactory;
|
||||
|
||||
/**
|
||||
* @var HelperBlog
|
||||
*/
|
||||
protected $helperBlog;
|
||||
|
||||
/**
|
||||
* @var AccountManagementInterface
|
||||
*/
|
||||
protected $accountManagement;
|
||||
|
||||
/**
|
||||
* @var CustomerUrl
|
||||
*/
|
||||
protected $customerUrl;
|
||||
|
||||
/**
|
||||
* @var Session
|
||||
*/
|
||||
protected $session;
|
||||
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
protected $storeManager;
|
||||
|
||||
/**
|
||||
* @var JsonData
|
||||
*/
|
||||
protected $jsonHelper;
|
||||
|
||||
/**
|
||||
* @var CommentFactory
|
||||
*/
|
||||
protected $cmtFactory;
|
||||
|
||||
/**
|
||||
* @var LikeFactory
|
||||
*/
|
||||
protected $likeFactory;
|
||||
|
||||
/**
|
||||
* @var DateTime
|
||||
*/
|
||||
protected $dateTime;
|
||||
|
||||
/**
|
||||
* @var TimezoneInterface
|
||||
*/
|
||||
protected $timeZone;
|
||||
|
||||
/**
|
||||
* @var ForwardFactory
|
||||
*/
|
||||
protected $resultForwardFactory;
|
||||
|
||||
/**
|
||||
* @var PostFactory
|
||||
*/
|
||||
protected $postFactory;
|
||||
|
||||
/**
|
||||
* Preview constructor.
|
||||
*
|
||||
* @param Context $context
|
||||
* @param ForwardFactory $resultForwardFactory
|
||||
* @param StoreManagerInterface $storeManager
|
||||
* @param JsonData $jsonHelper
|
||||
* @param CommentFactory $commentFactory
|
||||
* @param LikeFactory $likeFactory
|
||||
* @param DateTime $dateTime
|
||||
* @param TimezoneInterface $timezone
|
||||
* @param HelperBlog $helperBlog
|
||||
* @param PageFactory $resultPageFactory
|
||||
* @param AccountManagementInterface $accountManagement
|
||||
* @param CustomerUrl $customerUrl
|
||||
* @param Session $customerSession
|
||||
* @param TrafficFactory $trafficFactory
|
||||
* @param PostFactory $postFactory
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
ForwardFactory $resultForwardFactory,
|
||||
StoreManagerInterface $storeManager,
|
||||
JsonData $jsonHelper,
|
||||
CommentFactory $commentFactory,
|
||||
LikeFactory $likeFactory,
|
||||
DateTime $dateTime,
|
||||
TimezoneInterface $timezone,
|
||||
HelperBlog $helperBlog,
|
||||
PageFactory $resultPageFactory,
|
||||
AccountManagementInterface $accountManagement,
|
||||
CustomerUrl $customerUrl,
|
||||
Session $customerSession,
|
||||
TrafficFactory $trafficFactory,
|
||||
PostFactory $postFactory
|
||||
) {
|
||||
$this->storeManager = $storeManager;
|
||||
$this->helperBlog = $helperBlog;
|
||||
$this->resultPageFactory = $resultPageFactory;
|
||||
$this->accountManagement = $accountManagement;
|
||||
$this->customerUrl = $customerUrl;
|
||||
$this->session = $customerSession;
|
||||
$this->timeZone = $timezone;
|
||||
$this->trafficFactory = $trafficFactory;
|
||||
$this->resultForwardFactory = $resultForwardFactory;
|
||||
$this->jsonHelper = $jsonHelper;
|
||||
$this->cmtFactory = $commentFactory;
|
||||
$this->likeFactory = $likeFactory;
|
||||
$this->dateTime = $dateTime;
|
||||
$this->postFactory = $postFactory;
|
||||
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ResponseInterface|ResultInterface|Page
|
||||
* @throws LocalizedException
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$id = $this->getRequest()->getParam('id');
|
||||
$historyId = $this->getRequest()->getParam('historyId');
|
||||
$history = $this->helperBlog->getFactoryByType(Data::TYPE_HISTORY)->create()->load($historyId);
|
||||
$post = $this->helperBlog->getFactoryByType(Data::TYPE_POST)->create()->load($history->getPostId());
|
||||
$this->helperBlog->setCustomerContextId();
|
||||
|
||||
$data = $this->prepareData($history);
|
||||
$post->addData($data);
|
||||
|
||||
$page = $this->resultPageFactory->create();
|
||||
$pageLayout = ($post->getLayout() === 'empty') ? $this->helperBlog->getSidebarLayout() : $post->getLayout();
|
||||
$page->getConfig()->setPageLayout($pageLayout);
|
||||
|
||||
if (!$post->getEnabled() || !$this->helperBlog->checkStore($post)) {
|
||||
return $this->_redirect('noroute');
|
||||
}
|
||||
|
||||
if ($this->getRequest()->isAjax()) {
|
||||
$params = $this->getRequest()->getParams();
|
||||
$customerData = $this->session->getCustomerData();
|
||||
$result = [];
|
||||
if (isset($params['cmt_text'])) {
|
||||
$cmt_text = $params['cmt_text'];
|
||||
$content = htmlentities($cmt_text, ENT_COMPAT, 'UTF-8') . "<br />";
|
||||
$htmlEntity = htmlentities($content, ENT_COMPAT, 'UTF-8');
|
||||
// phpcs:disable Magento2.Functions.DiscouragedFunction
|
||||
$content = html_entity_decode($htmlEntity);
|
||||
|
||||
$cmtText = $content;
|
||||
$isReply = isset($params['isReply']) ? $params['isReply'] : 0;
|
||||
$replyId = isset($params['replyId']) ? $params['replyId'] : 0;
|
||||
if ($this->session->isLoggedIn()) {
|
||||
$commentData = [
|
||||
'post_id' => $id,
|
||||
'',
|
||||
'entity_id' => $customerData->getId(),
|
||||
'is_reply' => $isReply,
|
||||
'reply_id' => $replyId,
|
||||
'content' => $cmtText,
|
||||
'created_at' => $this->dateTime->date(),
|
||||
'status' => $this->helperBlog->getBlogConfig('comment/need_approve')
|
||||
? Status::PENDING : Status::APPROVED,
|
||||
'store_ids' => $this->storeManager->getStore()->getId()
|
||||
];
|
||||
} else {
|
||||
$commentData = [
|
||||
'post_id' => $id,
|
||||
'',
|
||||
'entity_id' => 0,
|
||||
'is_reply' => $isReply,
|
||||
'reply_id' => $replyId,
|
||||
'content' => $cmtText,
|
||||
'user_name' => $params['guestName'],
|
||||
'user_email' => $params['guestEmail'],
|
||||
'created_at' => $this->dateTime->date(),
|
||||
'status' => $this->helperBlog->getBlogConfig('comment/need_approve')
|
||||
? Status::PENDING : Status::APPROVED,
|
||||
'store_ids' => $this->storeManager->getStore()->getId()
|
||||
];
|
||||
}
|
||||
|
||||
$commentModel = $this->cmtFactory->create();
|
||||
$result = $this->commentActions(self::COMMENT, $customerData, $commentData, $commentModel);
|
||||
}
|
||||
|
||||
if (isset($params['cmtId'])) {
|
||||
$cmtId = $params['cmtId'];
|
||||
$likeData = [
|
||||
'comment_id' => $cmtId,
|
||||
'entity_id' => $customerData->getId()
|
||||
];
|
||||
|
||||
$likeModel = $this->likeFactory->create();
|
||||
$result = $this->commentActions(self::LIKE, $customerData, $likeData, $likeModel, $cmtId);
|
||||
}
|
||||
|
||||
return $this->getResponse()->representJson($this->jsonHelper->jsonEncode($result));
|
||||
}
|
||||
|
||||
return $this->resultPageFactory->create();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Author|Category|Post|Tag|Topic $history
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function prepareData($history)
|
||||
{
|
||||
$data = $history->getData();
|
||||
unset(
|
||||
$data['post_id'],
|
||||
$data['history_id'],
|
||||
$data['category_ids'],
|
||||
$data['tag_ids'],
|
||||
$data['topic_ids'],
|
||||
$data['categories_ids'],
|
||||
$data['tags_ids'],
|
||||
$data['topics_ids'],
|
||||
$data['product_ids']
|
||||
);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $action
|
||||
* @param CustomerInterface $user
|
||||
* @param array $data
|
||||
* @param Comment|Like $model
|
||||
* @param null $cmtId
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function commentActions($action, $user, $data, $model, $cmtId = null)
|
||||
{
|
||||
try {
|
||||
switch ($action) {
|
||||
/** Comment action */
|
||||
case self::COMMENT:
|
||||
$model->addData($data)->save();
|
||||
$cmtHasReply = $model->getCollection()
|
||||
->addFieldToFilter('comment_id', $data['reply_id'])
|
||||
->getFirstItem();
|
||||
if ($cmtHasReply->getId()) {
|
||||
$cmtHasReply->setHasReply(1)->save();
|
||||
}
|
||||
|
||||
$lastCmt = $model->getCollection()->setOrder('comment_id', 'desc')->getFirstItem();
|
||||
$lastCmtId = $lastCmt !== null ? $lastCmt->getId() : 1;
|
||||
$users = $user ? $user->getFirstname() . ' ' . $user->getLastname() : $data['user_name'];
|
||||
|
||||
$result = [
|
||||
'cmt_id' => $lastCmtId,
|
||||
'cmt_text' => $data['content'],
|
||||
'user_cmt' => $users,
|
||||
'is_reply' => $data['is_reply'],
|
||||
'reply_cmt' => $data['reply_id'],
|
||||
'created_at' => __('Just now'),
|
||||
'status' => $data['status']
|
||||
];
|
||||
break;
|
||||
/** Like action */
|
||||
case self::LIKE:
|
||||
$checkLike = $this->isLikedComment($cmtId, $user->getId(), $model);
|
||||
if (!$checkLike) {
|
||||
$model->addData($data)->save();
|
||||
}
|
||||
$likes = $model->getCollection()->addFieldToFilter('comment_id', $cmtId);
|
||||
$countLikes = $likes->getSize() ?: '';
|
||||
$isLiked = $checkLike ? 'yes' : 'no';
|
||||
$result = [
|
||||
'liked' => $isLiked,
|
||||
'comment_id' => $cmtId,
|
||||
'count_like' => $countLikes,
|
||||
'status' => 'ok'
|
||||
];
|
||||
break;
|
||||
default:
|
||||
$result = ['status' => 'error', 'error' => __('Action not found.')];
|
||||
break;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$result = ['status' => 'error', 'error' => $e->getMessage()];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* check if user like a comment
|
||||
*
|
||||
* @param int $cmtId
|
||||
* @param int $userId
|
||||
* @param Like $model
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isLikedComment($cmtId, $userId, $model)
|
||||
{
|
||||
$liked = $model->getCollection()->addFieldToFilter('comment_id', $cmtId);
|
||||
foreach ($liked as $item) {
|
||||
if ($item->getEntityId() === $userId) {
|
||||
try {
|
||||
$item->delete();
|
||||
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+162
@@ -0,0 +1,162 @@
|
||||
<?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\Post;
|
||||
|
||||
use Exception;
|
||||
use Magento\Framework\App\Action\Action;
|
||||
use Magento\Framework\App\Action\Context;
|
||||
use Magento\Framework\App\ResponseInterface;
|
||||
use Magento\Framework\Controller\ResultInterface;
|
||||
use Mageplaza\Blog\Helper\Data;
|
||||
use Mageplaza\Blog\Model\PostFactory;
|
||||
use Mageplaza\Blog\Model\PostLikeFactory;
|
||||
use Mageplaza\Blog\Model\ResourceModel\PostLike\Collection;
|
||||
|
||||
/**
|
||||
* Class Review
|
||||
* @package Mageplaza\Blog\Controller\Post
|
||||
*/
|
||||
class Review extends Action
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Data
|
||||
*/
|
||||
protected $_helperBlog;
|
||||
|
||||
/**
|
||||
* @var PostFactory
|
||||
*/
|
||||
protected $postFactory;
|
||||
|
||||
/**
|
||||
* @var PostLike
|
||||
*/
|
||||
protected $_postLike;
|
||||
|
||||
/**
|
||||
* @var Collection
|
||||
*/
|
||||
protected $_postLikeCollection;
|
||||
|
||||
/**
|
||||
* Review constructor.
|
||||
*
|
||||
* @param Context $context
|
||||
* @param PostFactory $postFactory
|
||||
* @param Collection $postLikeCollection
|
||||
* @param PostLikeFactory $postLikeFactory
|
||||
* @param Data $helperData
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
PostFactory $postFactory,
|
||||
Collection $postLikeCollection,
|
||||
PostLikeFactory $postLikeFactory,
|
||||
Data $helperData
|
||||
) {
|
||||
$this->_helperBlog = $helperData;
|
||||
$this->_postLikeCollection = $postLikeCollection;
|
||||
$this->_postLike = $postLikeFactory;
|
||||
$this->postFactory = $postFactory;
|
||||
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ResponseInterface|ResultInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$id = $this->getRequest()->getParam('post_id');
|
||||
$action = $this->getRequest()->getParam('action');
|
||||
$mode = $this->getRequest()->getParam('mode');
|
||||
$likeId = $this->getRequest()->getParam('likeId');
|
||||
$customerId = $this->_helperBlog->getCurrentUser() ?: 0;
|
||||
$post = $this->postFactory->create()->load($id);
|
||||
|
||||
if ($mode === '1') {
|
||||
$like = $this->_postLikeCollection->addFieldToFilter('entity_id', $customerId)
|
||||
->addFieldToFilter('post_id', $post->getId());
|
||||
$likeId = $like->getFirstItem()->getId();
|
||||
|
||||
if ($action === '3') {
|
||||
return $this->getResponse()->representJson(Data::jsonEncode([
|
||||
'status' => $like->count() > 0 ? 0 : 1,
|
||||
'action' => $like->getFirstItem()->getAction(),
|
||||
'type' => $action
|
||||
]));
|
||||
}
|
||||
|
||||
if (!$customerId || !$post) {
|
||||
if ($action === '1') {
|
||||
$this->messageManager->addErrorMessage(__('Can\'t Like Post.'));
|
||||
} else {
|
||||
$this->messageManager->addErrorMessage(__('Can\'t Dislike Post.'));
|
||||
}
|
||||
|
||||
return $this->getResponse()->representJson(Data::jsonEncode([
|
||||
'status' => 0,
|
||||
'type' => $action
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$postLike = $this->_postLike->create()->load($likeId);
|
||||
|
||||
if ($postLike->getId() && $postLike->getAction() === $action) {
|
||||
$postLike->delete();
|
||||
$postLike->setId(0);
|
||||
} else {
|
||||
$postLike->addData(
|
||||
[
|
||||
'post_id' => $post->getId(),
|
||||
'action' => $action,
|
||||
'entity_id' => $customerId
|
||||
]
|
||||
)->save();
|
||||
}
|
||||
|
||||
$sumLike = $this->_postLike->create()->getCollection()->addFieldToFilter('action', '1')
|
||||
->addFieldToFilter('post_id', $id);
|
||||
$sumDislike = $this->_postLike->create()->getCollection()->addFieldToFilter('action', '0')
|
||||
->addFieldToFilter('post_id', $id);
|
||||
|
||||
return $this->getResponse()->representJson(Data::jsonEncode([
|
||||
'status' => 1,
|
||||
'type' => $action,
|
||||
'sumLike' => $sumLike->count(),
|
||||
'sumDislike' => $sumDislike->count(),
|
||||
'postLike' => $postLike->getId()
|
||||
]));
|
||||
} catch (Exception $exception) {
|
||||
$this->messageManager->addErrorMessage($exception->getMessage());
|
||||
|
||||
return $this->getResponse()->representJson(Data::jsonEncode([
|
||||
'status' => 0,
|
||||
'type' => $action
|
||||
]));
|
||||
}
|
||||
}
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
<?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\Post;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Magento\Framework\App\ResponseInterface;
|
||||
use Magento\Framework\Controller\ResultInterface;
|
||||
use Magento\Framework\Exception\InputException;
|
||||
use Magento\Framework\Exception\NotFoundException;
|
||||
use Magento\Framework\Exception\RuntimeException;
|
||||
use Magento\Rss\Controller\Feed;
|
||||
|
||||
/**
|
||||
* Class Rss
|
||||
* @package Mageplaza\Blog\Controller\Post
|
||||
*/
|
||||
class Rss extends Feed
|
||||
{
|
||||
/**
|
||||
* @return ResponseInterface|ResultInterface|void
|
||||
* @throws NotFoundException
|
||||
* @throws InputException
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$type = 'blog_posts';
|
||||
try {
|
||||
$provider = $this->rssManager->getProvider($type);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
throw new NotFoundException(__($e->getMessage()));
|
||||
}
|
||||
|
||||
if ($provider->isAuthRequired() && !$this->auth()) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var $rss \Magento\Rss\Model\Rss */
|
||||
$rss = $this->rssFactory->create();
|
||||
$rss->setDataProvider($provider);
|
||||
|
||||
$this->getResponse()->setHeader('Content-type', 'text/xml; charset=UTF-8');
|
||||
$this->getResponse()->setBody($rss->createRssXml());
|
||||
}
|
||||
}
|
||||
+355
@@ -0,0 +1,355 @@
|
||||
<?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\Post;
|
||||
|
||||
use Exception;
|
||||
use Magento\Customer\Api\AccountManagementInterface;
|
||||
use Magento\Customer\Api\Data\CustomerInterface;
|
||||
use Magento\Customer\Model\Data\Customer;
|
||||
use Magento\Customer\Model\Session;
|
||||
use Magento\Customer\Model\Url as CustomerUrl;
|
||||
use Magento\Framework\App\Action\Action;
|
||||
use Magento\Framework\App\Action\Context;
|
||||
use Magento\Framework\App\ResponseInterface;
|
||||
use Magento\Framework\Controller\Result\ForwardFactory;
|
||||
use Magento\Framework\Controller\ResultInterface;
|
||||
use Magento\Framework\Json\Helper\Data as JsonData;
|
||||
use Magento\Framework\Stdlib\DateTime\DateTime;
|
||||
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
|
||||
use Magento\Framework\View\Result\Page;
|
||||
use Magento\Framework\View\Result\PageFactory;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Mageplaza\Blog\Helper\Data;
|
||||
use Mageplaza\Blog\Helper\Data as HelperBlog;
|
||||
use Mageplaza\Blog\Model\Comment;
|
||||
use Mageplaza\Blog\Model\CommentFactory;
|
||||
use Mageplaza\Blog\Model\Config\Source\Comments\Status;
|
||||
use Mageplaza\Blog\Model\Like;
|
||||
use Mageplaza\Blog\Model\LikeFactory;
|
||||
use Mageplaza\Blog\Model\PostFactory;
|
||||
use Mageplaza\Blog\Model\TrafficFactory;
|
||||
|
||||
/**
|
||||
* Class View
|
||||
* @package Mageplaza\Blog\Controller\Post
|
||||
*/
|
||||
class View extends Action
|
||||
{
|
||||
const COMMENT = 1;
|
||||
const LIKE = 2;
|
||||
|
||||
/**
|
||||
* @var TrafficFactory
|
||||
*/
|
||||
protected $trafficFactory;
|
||||
|
||||
/**
|
||||
* @var PageFactory
|
||||
*/
|
||||
protected $resultPageFactory;
|
||||
|
||||
/**
|
||||
* @var HelperBlog
|
||||
*/
|
||||
protected $helperBlog;
|
||||
|
||||
/**
|
||||
* @var AccountManagementInterface
|
||||
*/
|
||||
protected $accountManagement;
|
||||
|
||||
/**
|
||||
* @var CustomerUrl
|
||||
*/
|
||||
protected $customerUrl;
|
||||
|
||||
/**
|
||||
* @var Session
|
||||
*/
|
||||
protected $session;
|
||||
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
protected $storeManager;
|
||||
|
||||
/**
|
||||
* @var JsonData
|
||||
*/
|
||||
protected $jsonHelper;
|
||||
|
||||
/**
|
||||
* @var CommentFactory
|
||||
*/
|
||||
protected $cmtFactory;
|
||||
|
||||
/**
|
||||
* @var LikeFactory
|
||||
*/
|
||||
protected $likeFactory;
|
||||
|
||||
/**
|
||||
* @var DateTime
|
||||
*/
|
||||
protected $dateTime;
|
||||
|
||||
/**
|
||||
* @var TimezoneInterface
|
||||
*/
|
||||
protected $timeZone;
|
||||
|
||||
/**
|
||||
* @var ForwardFactory
|
||||
*/
|
||||
protected $resultForwardFactory;
|
||||
|
||||
/**
|
||||
* @var PostFactory
|
||||
*/
|
||||
protected $postFactory;
|
||||
|
||||
/**
|
||||
* View constructor.
|
||||
*
|
||||
* @param Context $context
|
||||
* @param ForwardFactory $resultForwardFactory
|
||||
* @param StoreManagerInterface $storeManager
|
||||
* @param JsonData $jsonHelper
|
||||
* @param CommentFactory $commentFactory
|
||||
* @param LikeFactory $likeFactory
|
||||
* @param DateTime $dateTime
|
||||
* @param TimezoneInterface $timezone
|
||||
* @param HelperBlog $helperBlog
|
||||
* @param PageFactory $resultPageFactory
|
||||
* @param AccountManagementInterface $accountManagement
|
||||
* @param CustomerUrl $customerUrl
|
||||
* @param Session $customerSession
|
||||
* @param TrafficFactory $trafficFactory
|
||||
* @param PostFactory $postFactory
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
ForwardFactory $resultForwardFactory,
|
||||
StoreManagerInterface $storeManager,
|
||||
JsonData $jsonHelper,
|
||||
CommentFactory $commentFactory,
|
||||
LikeFactory $likeFactory,
|
||||
DateTime $dateTime,
|
||||
TimezoneInterface $timezone,
|
||||
HelperBlog $helperBlog,
|
||||
PageFactory $resultPageFactory,
|
||||
AccountManagementInterface $accountManagement,
|
||||
CustomerUrl $customerUrl,
|
||||
Session $customerSession,
|
||||
TrafficFactory $trafficFactory,
|
||||
PostFactory $postFactory
|
||||
) {
|
||||
$this->storeManager = $storeManager;
|
||||
$this->helperBlog = $helperBlog;
|
||||
$this->resultPageFactory = $resultPageFactory;
|
||||
$this->accountManagement = $accountManagement;
|
||||
$this->customerUrl = $customerUrl;
|
||||
$this->session = $customerSession;
|
||||
$this->timeZone = $timezone;
|
||||
$this->trafficFactory = $trafficFactory;
|
||||
$this->resultForwardFactory = $resultForwardFactory;
|
||||
$this->jsonHelper = $jsonHelper;
|
||||
$this->cmtFactory = $commentFactory;
|
||||
$this->likeFactory = $likeFactory;
|
||||
$this->dateTime = $dateTime;
|
||||
$this->postFactory = $postFactory;
|
||||
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this|ResponseInterface|ResultInterface|Page
|
||||
* @throws Exception
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$id = $this->getRequest()->getParam('id');
|
||||
$post = $this->helperBlog->getFactoryByType(Data::TYPE_POST)->create()->load($id);
|
||||
$this->helperBlog->setCustomerContextId();
|
||||
|
||||
$page = $this->resultPageFactory->create();
|
||||
$pageLayout = ($post->getLayout() === 'empty') ? $this->helperBlog->getSidebarLayout() : $post->getLayout();
|
||||
$page->getConfig()->setPageLayout($pageLayout);
|
||||
|
||||
if ($post->getEnabled() !== '1' || !$this->helperBlog->checkStore($post)) {
|
||||
return $this->_redirect('noroute');
|
||||
}
|
||||
|
||||
$trafficModel = $this->trafficFactory->create()->load($id, 'post_id');
|
||||
if ($trafficModel->getId()) {
|
||||
$trafficModel->setNumbersView($trafficModel->getNumbersView() + 1);
|
||||
$trafficModel->save();
|
||||
} else {
|
||||
$traffic = $this->trafficFactory->create();
|
||||
$traffic->addData(['post_id' => $id, 'numbers_view' => 1])->save();
|
||||
}
|
||||
|
||||
if ($this->getRequest()->isAjax()) {
|
||||
$params = $this->getRequest()->getParams();
|
||||
$customerData = $this->session->getCustomerData();
|
||||
$result = [];
|
||||
if (isset($params['cmt_text'])) {
|
||||
$cmt_text = $params['cmt_text'];
|
||||
$content = htmlentities($cmt_text, ENT_COMPAT, 'UTF-8');
|
||||
$htmlEntity = htmlentities($content, ENT_COMPAT, 'UTF-8');
|
||||
$content = html_entity_decode($htmlEntity);
|
||||
|
||||
$cmtText = $content;
|
||||
$isReply = isset($params['isReply']) ? $params['isReply'] : 0;
|
||||
$replyId = isset($params['replyId']) ? $params['replyId'] : 0;
|
||||
|
||||
$userName = $this->session->isLoggedIn()
|
||||
? $customerData->getFirstname() . ' ' . $customerData->getLastname()
|
||||
: $params['guestName'] . ' (Guest)';
|
||||
$commentData = [
|
||||
'post_id' => $id,
|
||||
'',
|
||||
'entity_id' => $this->session->isLoggedIn() ? $customerData->getId() : 0,
|
||||
'is_reply' => $isReply,
|
||||
'reply_id' => $replyId,
|
||||
'content' => $cmtText,
|
||||
'user_name' => $userName,
|
||||
'user_email' => $this->session->isLoggedIn() ? $customerData->getEmail() : $params['guestEmail'],
|
||||
'created_at' => $this->dateTime->date(),
|
||||
'status' => $this->helperBlog->getBlogConfig('comment/need_approve')
|
||||
? Status::PENDING : Status::APPROVED,
|
||||
'store_ids' => $this->storeManager->getStore()->getId()
|
||||
];
|
||||
|
||||
$commentModel = $this->cmtFactory->create();
|
||||
$result = $this->commentActions(self::COMMENT, $customerData, $commentData, $commentModel);
|
||||
}
|
||||
|
||||
if (isset($params['cmtId'])) {
|
||||
$cmtId = $params['cmtId'];
|
||||
$likeData = [
|
||||
'comment_id' => $cmtId,
|
||||
'entity_id' => $customerData->getId()
|
||||
];
|
||||
|
||||
$likeModel = $this->likeFactory->create();
|
||||
$result = $this->commentActions(self::LIKE, $customerData, $likeData, $likeModel, $cmtId);
|
||||
}
|
||||
|
||||
return $this->getResponse()->representJson($this->jsonHelper->jsonEncode($result));
|
||||
}
|
||||
|
||||
return $this->resultPageFactory->create();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $action
|
||||
* @param CustomerInterface|Customer|null $user
|
||||
* @param array $data
|
||||
* @param Like|Comment $model
|
||||
* @param null $cmtId
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function commentActions($action, $user, $data, $model, $cmtId = null)
|
||||
{
|
||||
try {
|
||||
switch ($action) {
|
||||
/** Comment action */
|
||||
case self::COMMENT:
|
||||
$model->addData($data)->save();
|
||||
$cmtHasReply = $model->getCollection()
|
||||
->addFieldToFilter('comment_id', $data['reply_id'])
|
||||
->getFirstItem();
|
||||
if ($cmtHasReply->getId()) {
|
||||
$cmtHasReply->setHasReply(1)->save();
|
||||
}
|
||||
$this->_eventManager->dispatch('blog_post_comment', ['comment_data' => $data]);
|
||||
|
||||
$lastCmt = $model->getCollection()->setOrder('comment_id', 'desc')->getFirstItem();
|
||||
$lastCmtId = $lastCmt !== null ? $lastCmt->getId() : 1;
|
||||
$users = $user ? $user->getFirstname() . ' ' . $user->getLastname() : $data['user_name'];
|
||||
|
||||
$result = [
|
||||
'cmt_id' => $lastCmtId,
|
||||
'cmt_text' => $data['content'],
|
||||
'user_cmt' => $users,
|
||||
'is_reply' => $data['is_reply'],
|
||||
'reply_cmt' => $data['reply_id'],
|
||||
'created_at' => __('Just now'),
|
||||
'status' => $data['status']
|
||||
];
|
||||
break;
|
||||
/** Like action */
|
||||
case self::LIKE:
|
||||
$checkLike = $this->isLikedComment($cmtId, $user->getId(), $model);
|
||||
if (!$checkLike) {
|
||||
$model->addData($data)->save();
|
||||
}
|
||||
$likes = $model->getCollection()->addFieldToFilter('comment_id', $cmtId);
|
||||
$countLikes = $likes->getSize() ?: '';
|
||||
$isLiked = $checkLike ? 'yes' : 'no';
|
||||
$result = [
|
||||
'liked' => $isLiked,
|
||||
'comment_id' => $cmtId,
|
||||
'count_like' => $countLikes,
|
||||
'status' => 'ok'
|
||||
];
|
||||
break;
|
||||
default:
|
||||
$result = ['status' => 'error', 'error' => __('Action not found.')];
|
||||
break;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$result = ['status' => 'error', 'error' => $e->getMessage()];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user like a comment
|
||||
*
|
||||
* @param int $cmtId
|
||||
* @param int $userId
|
||||
* @param Like $model
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isLikedComment($cmtId, $userId, $model)
|
||||
{
|
||||
$liked = $model->getCollection()->addFieldToFilter('comment_id', $cmtId);
|
||||
foreach ($liked as $item) {
|
||||
if ($item->getEntityId() == $userId) {
|
||||
try {
|
||||
$item->delete();
|
||||
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user