Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3167 change blog grids 2 #434

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions Controller/Adminhtml/Category/InlineEdit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*
* Glory to Ukraine! Glory to the heroes!
*/
namespace Magefan\Blog\Controller\Adminhtml\Category;

use Magento\Backend\App\Action\Context;
use Magefan\Blog\Api\CategoryRepositoryInterface as CategoryRepository;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\Result\JsonFactory;
use Magefan\Blog\Model\Category;
use Magento\Cms\Controller\Adminhtml\Page\PostDataProcessor;

/**
* Blog Category grid inline edit controller
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class InlineEdit extends \Magento\Backend\App\Action implements HttpPostActionInterface
{
/**
* Authorization level of a basic admin session
*/
const ADMIN_RESOURCE = 'Magefan_Blog::category_save';

/**
* @var PostDataProcessor
*/
protected $dataProcessor;

/**
* @var CategoryRepository
*/
protected $categoryRepository;

/**
* @var \Magento\Framework\Controller\Result\JsonFactory
*/
protected $jsonFactory;

/**
* InlineEdit constructor.
* @param Context $context
* @param PostDataProcessor $dataProcessor
* @param CategoryRepository $categoryRepository
* @param JsonFactory $jsonFactory
*/
public function __construct(
Context $context,
PostDataProcessor $dataProcessor,
CategoryRepository $categoryRepository,
JsonFactory $jsonFactory
) {
parent::__construct($context);
$this->dataProcessor = $dataProcessor;
$this->categoryRepository = $categoryRepository;
$this->jsonFactory = $jsonFactory;
}

/**
* Process the request
*
* @return \Magento\Framework\Controller\ResultInterface
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->jsonFactory->create();
$error = false;
$messages = [];

$categoryItems = $this->getRequest()->getParam('items', []);
if (!($this->getRequest()->getParam('isAjax') && count($categoryItems))) {
return $resultJson->setData(
[
'messages' => [__('Please correct the data sent.')],
'error' => true,
]
);
}

foreach (array_keys($categoryItems) as $categoryId) {
/** @var \Magefan\Blog\Model\Category $category */
$category = $this->categoryRepository->getById($categoryId);
try {
$categoryData = $this->filterPost($categoryItems[$categoryId]);
$this->validatePost($categoryData, $category, $error, $messages);
$extendedCategoryData = $category->getData();
$this->setBlogCategoryData($category, $extendedCategoryData, $categoryData);
$this->categoryRepository->save($category);
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$messages[] = $this->getErrorWithPostId($category, $e->getMessage());
$error = true;
} catch (\RuntimeException $e) {
$messages[] = $this->getErrorWithPostId($category, $e->getMessage());
$error = true;
} catch (\Exception $e) {
$messages[] = $this->getErrorWithPostId(
$category,
__('Something went wrong while saving the post.')
);
$error = true;
}
}

return $resultJson->setData(
[
'messages' => $messages,
'error' => $error
]
);
}

/**
* Filtering POSTed data.
*
* @param array $postData
* @return array
*/
protected function filterPost($postData = [])
{
$blogCategoryData = $this->dataProcessor->filter($postData);
$blogCategoryData['custom_theme'] = isset($blogCategoryData['custom_theme']) ? $blogCategoryData['custom_theme'] : null;
$blogCategoryData['custom_root_template'] = isset($blogCategoryData['custom_root_template'])
? $blogCategoryData['custom_root_template']
: null;
return $blogCategoryData;
}

/**
* Validate POST data
*
* @param array $blogCategoryData
* @param Category $category
* @param bool $error
* @param array $messages
* @return void
*/
protected function validatePost(array $blogCategoryData, Category $category, &$error, array &$messages)
{
if (!$this->dataProcessor->validateRequireEntry($blogCategoryData)) {
$error = true;
foreach ($this->messageManager->getMessages(true)->getItems() as $error) {
$messages[] = $this->getErrorWithPostId($category, $error->getText());
}
}
}

/**
* Add post title to error message
*
* @param Category $category
* @param string $errorText
* @return string
*/
protected function getErrorWithPostId(Category $category, $errorText)
{
return '[Category ID: ' . $category->getId() . '] ' . $errorText;
}

/**
* Set blog category data
*
* @param Category $category
* @param array $extendedCategoryData
* @param array $categoryData
* @return $this
*/
public function setBlogCategoryData(Category $category, array $extendedCategoryData, array $categoryData)
{
$category->setData(array_merge($category->getData(), $extendedCategoryData, $categoryData));
return $this;
}
}
72 changes: 72 additions & 0 deletions Controller/Adminhtml/Category/MassDelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*
* Glory to Ukraine! Glory to the heroes!
*/
namespace Magefan\Blog\Controller\Adminhtml\Category;

use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Magefan\Blog\Model\ResourceModel\Category\CollectionFactory;

/**
* Class MassDelete
*/
class MassDelete extends \Magento\Backend\App\Action implements HttpPostActionInterface
{
/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magefan_Blog::category_delete';

/**
* @var Filter
*/
protected $filter;

/**
* @var CollectionFactory
*/
protected $collectionFactory;

/**
* @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);
}

/**
* Execute action
*
* @return \Magento\Backend\Model\View\Result\Redirect
* @throws \Magento\Framework\Exception\LocalizedException|\Exception
*/
public function execute()
{
$collection = $this->filter->getCollection($this->collectionFactory->create());
$collectionSize = $collection->getSize();

foreach ($collection as $item) {
$item->delete();
}
//$collection->walk('delete');
$this->messageManager->addSuccessMessage(__('A total of %1 record(s) have been deleted.', $collectionSize));

/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);

return $resultRedirect->setPath('*/*/');
}
}
73 changes: 73 additions & 0 deletions Controller/Adminhtml/Category/MassDisable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*
* Glory to Ukraine! Glory to the heroes!
*/
namespace Magefan\Blog\Controller\Adminhtml\Category;

use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Magefan\Blog\Model\ResourceModel\Category\CollectionFactory;

/**
* Class MassDisable
*/
class MassDisable extends \Magento\Backend\App\Action implements HttpPostActionInterface
{
/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magefan_Blog::category_save';

/**
* @var Filter
*/
protected $filter;

/**
* @var CollectionFactory
*/
protected $collectionFactory;

/**
* @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);
}

/**
* Execute action
*
* @return \Magento\Backend\Model\View\Result\Redirect
* @throws \Magento\Framework\Exception\LocalizedException|\Exception
*/
public function execute()
{
$collection = $this->filter->getCollection($this->collectionFactory->create());

foreach ($collection as $item) {
$item->setIsActive(false);
$item->save();
}

$this->messageManager->addSuccessMessage(
__('A total of %1 record(s) have been disabled.', $collection->getSize())
);

/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath('*/*/');
}
}
73 changes: 73 additions & 0 deletions Controller/Adminhtml/Category/MassEnable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*
* Glory to Ukraine! Glory to the heroes!
*/
namespace Magefan\Blog\Controller\Adminhtml\Category;

use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Magefan\Blog\Model\ResourceModel\Category\CollectionFactory;

/**
* Class MassEnable
*/
class MassEnable extends \Magento\Backend\App\Action implements HttpPostActionInterface
{
/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magefan_Blog::category_save';

/**
* @var Filter
*/
protected $filter;

/**
* @var CollectionFactory
*/
protected $collectionFactory;

/**
* @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);
}

/**
* Execute action
*
* @return \Magento\Backend\Model\View\Result\Redirect
* @throws \Magento\Framework\Exception\LocalizedException|\Exception
*/
public function execute()
{
$collection = $this->filter->getCollection($this->collectionFactory->create());

foreach ($collection as $item) {
$item->setIsActive(true);
$item->save();
}

$this->messageManager->addSuccessMessage(
__('A total of %1 record(s) have been enabled.', $collection->getSize())
);

/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath('*/*/');
}
}
Loading