Skip to content

Commit

Permalink
Merge pull request #340 from mageplaza/2.4-develop
Browse files Browse the repository at this point in the history
2.4 develop
  • Loading branch information
haitv282 authored Jun 22, 2022
2 parents d56a6d1 + d43a120 commit e6eb186
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 75 deletions.
20 changes: 9 additions & 11 deletions Block/Adminhtml/Category/Edit/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Magento\Framework\Phrase;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\BlockInterface;
use Mageplaza\Blog\Model\Category as BlogCategory;
use Mageplaza\Blog\Model\CategoryFactory as BlogCategoryFactory;
use Mageplaza\Blog\Model\ResourceModel\Category\Tree as BlogResourceTree;

Expand Down Expand Up @@ -86,7 +87,7 @@ public function __construct(
}

/**
* @inheritdoc
* @return AbstractCategory
* @throws LocalizedException
*/
protected function _prepareLayout()
Expand Down Expand Up @@ -116,14 +117,11 @@ protected function _prepareLayout()
]);

// Delete button
if ($categoryId && !in_array($categoryId, $this->getRootIds()) && !$this->getRequest()->getParam('duplicate')) {
if ($categoryId && $categoryId != 1 && !$this->getRequest()->getParam('duplicate')) {
$this->addButton('delete', [
'id' => 'delete',
'label' => __('Delete Category'),
'onclick' => "categoryDelete('" . $this->getUrl(
'mageplaza_blog/*/delete',
['_current' => true]
) . "')",
'onclick' => "categoryDelete('" . $this->getUrl('mageplaza_blog/*/delete', ['_current' => true]) . "')",
'class' => 'delete'
]);
}
Expand Down Expand Up @@ -170,7 +168,7 @@ public function isAjax()
*/
public function getSaveUrl(array $args = [])
{
/** @var \Mageplaza\Blog\Model\Category $category */
/** @var BlogCategory $category */
$category = $this->_coreRegistry->registry('category');
$params = ['_current' => false, '_query' => false];
if ($category->getDuplicate()) {
Expand All @@ -190,8 +188,8 @@ public function getEditUrl()
}

/**
* @param $alias
* @param $config
* @param string $alias
* @param array $config
*
* @return $this
* @throws LocalizedException
Expand Down Expand Up @@ -305,7 +303,7 @@ public function getCategoryId()
}

/**
* @param $buttonId
* @param int $buttonId
* @param array $data
*
* @throws LocalizedException
Expand Down Expand Up @@ -333,7 +331,7 @@ public function hasToolbarBlock()
}

/**
* @param $childId
* @param int $childId
* @param null $blockClassName
*
* @return BlockInterface
Expand Down
29 changes: 22 additions & 7 deletions Controller/Adminhtml/Category/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
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;

/**
Expand All @@ -32,22 +34,35 @@
class Delete extends Category
{
/**
* @return Redirect
* @return ResponseInterface|Redirect|ResultInterface
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
if ($id = $this->getRequest()->getParam('id')) {
try {
$this->categoryFactory->create()
->load($id)
->delete();
$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.'));
$this->messageManager->addSuccessMessage(__('The Blog Category has been deleted.'));

$resultRedirect->setPath('mageplaza_blog/*/');
$resultRedirect->setPath('mageplaza_blog/*/');

return $resultRedirect;
return $resultRedirect;
}
} catch (Exception $e) {
// display error message
$this->messageManager->addErrorMessage($e->getMessage());
Expand Down
24 changes: 15 additions & 9 deletions Model/ResourceModel/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Magento\Framework\Stdlib\DateTime\DateTime;
use Mageplaza\Blog\Helper\Data;
use Mageplaza\Blog\Model\Category as CategoryModel;
use Zend_Db_Expr;

/**
* Class Category
Expand Down Expand Up @@ -77,9 +78,9 @@ public function __construct(
ManagerInterface $eventManager,
Data $helperData
) {
$this->helperData = $helperData;
$this->date = $date;
$this->eventManager = $eventManager;
$this->helperData = $helperData;
$this->date = $date;
$this->eventManager = $eventManager;

parent::__construct($context);

Expand Down Expand Up @@ -357,15 +358,15 @@ public function changeParent(
$childrenCount = (int) $this->getChildrenCount($category->getId()) + 1;
$table = $this->getMainTable();
$adapter = $this->getConnection();
$levelFiled = $adapter->quoteIdentifier('level');
$levelField = $adapter->quoteIdentifier('level');
$pathField = $adapter->quoteIdentifier('path');

/**
* Decrease children count for all old Blog Category parent Categories
*/
$adapter->update(
$table,
['children_count' => 'children_count - ' . $childrenCount],
['children_count' => new Zend_Db_Expr('children_count - ' . $childrenCount)],
['category_id IN(?)' => $category->getParentIds()]
);

Expand All @@ -374,7 +375,7 @@ public function changeParent(
*/
$adapter->update(
$table,
['children_count' => 'children_count + ' . $childrenCount],
['children_count' => new Zend_Db_Expr('children_count + ' . $childrenCount)],
['category_id IN(?)' => $newParent->getPathIds()]
);

Expand All @@ -389,9 +390,14 @@ public function changeParent(
$adapter->update(
$table,
[
'path' => 'REPLACE(' . $pathField . ',' . $adapter->quote($category->getPath() . '/') . ', '
. $adapter->quote($newPath . '/') . ')',
'level' => $levelFiled . ' + ' . $levelDisposition
'path' => new Zend_Db_Expr(
'REPLACE(' . $pathField . ',' . $adapter->quote(
$category->getPath() . '/'
) . ', ' . $adapter->quote(
$newPath . '/'
) . ')'
),
'level' => new Zend_Db_Expr($levelField . ' + ' . $levelDisposition)
],
[$pathField . ' LIKE ?' => $category->getPath() . '/%']
);
Expand Down
95 changes: 49 additions & 46 deletions Model/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,24 @@ class Sitemap extends \Magento\Sitemap\Model\Sitemap
protected $router;

/**
* Initialize resource model
*
* @return void
* @inheritdoc
*/
protected function _construct()
public function _initSitemapItems()
{
parent::_construct();
$this->_sitemapItems[] = new DataObject([
'collection' => $this->getBlogPostsSiteMapCollection(),
]);
$this->_sitemapItems[] = new DataObject([
'collection' => $this->getBlogCategoriesSiteMapCollection(),
]);
$this->_sitemapItems[] = new DataObject([
'collection' => $this->getBlogTagsSiteMapCollection(),
]);
$this->_sitemapItems[] = new DataObject([
'collection' => $this->getBlogTopicsSiteMapCollection(),
]);

$this->blogDataHelper = ObjectManager::getInstance()->get(Data::class);
$this->imageHelper = ObjectManager::getInstance()->get(Image::class);
$this->router = $this->blogDataHelper->getBlogConfig('general/url_prefix');
parent::_initSitemapItems(); // TODO: Change the autogenerated stub
}

/**
Expand All @@ -69,8 +76,9 @@ protected function _construct()
public function getBlogPostsSiteMapCollection()
{
$urlSuffix = $this->blogDataHelper->getUrlSuffix();
$postCollection = $this->blogDataHelper->postFactory->create()->getCollection();
$currentStoreId = $this->getStoreId();
$postCollection = $this->blogDataHelper->postFactory->create()->getCollection()
->addFieldToFilter('enabled', 1);
$postCollection = $this->blogDataHelper->addStoreFilter($postCollection, $currentStoreId);
$postSiteMapCollection = [];

Expand All @@ -79,30 +87,32 @@ public function getBlogPostsSiteMapCollection()
}

foreach ($postCollection as $item) {
$images = null;
if ($item->getEnabled() !== null) {
if ($item->getImage()) {
$imageFile = $this->imageHelper->getMediaPath(
$item->getImage(),
Image::TEMPLATE_MEDIA_TYPE_POST
);
$imagesCollection = null;
$imagesCollection[] = new DataObject([
'url' => $this->imageHelper->getMediaUrl($imageFile),
'caption' => null,
]);
$images = new DataObject([
'collection' => $imagesCollection,
'title' => $item->getName()
]);
}
$postSiteMapCollection[] = new DataObject([
'id' => $item->getId(),
'url' => $this->router . '/post/' . $item->getUrlKey() . $urlSuffix,
'images' => $images,
'updated_at' => $item->getUpdatedAt(),
$images = null;
$publishDate = strtotime($item->getPublishDate());
if ($publishDate > strtotime('now')) {
continue;
}
if ($item->getImage()) {
$imageFile = $this->imageHelper->getMediaPath(
$item->getImage(),
Image::TEMPLATE_MEDIA_TYPE_POST
);
$imagesCollection = null;
$imagesCollection[] = new DataObject([
'url' => $this->imageHelper->getMediaUrl($imageFile),
'caption' => null,
]);
$images = new DataObject([
'collection' => $imagesCollection,
'title' => $item->getName()
]);
}
$postSiteMapCollection[] = new DataObject([
'id' => $item->getId(),
'url' => $this->router . '/post/' . $item->getUrlKey() . $urlSuffix,
'images' => $images,
'updated_at' => $item->getUpdatedAt(),
]);
}

return $postSiteMapCollection;
Expand Down Expand Up @@ -184,23 +194,16 @@ public function getBlogTopicsSiteMapCollection()
}

/**
* @inheritdoc
* Initialize resource model
*
* @return void
*/
public function _initSitemapItems()
protected function _construct()
{
$this->_sitemapItems[] = new DataObject([
'collection' => $this->getBlogPostsSiteMapCollection(),
]);
$this->_sitemapItems[] = new DataObject([
'collection' => $this->getBlogCategoriesSiteMapCollection(),
]);
$this->_sitemapItems[] = new DataObject([
'collection' => $this->getBlogTagsSiteMapCollection(),
]);
$this->_sitemapItems[] = new DataObject([
'collection' => $this->getBlogTopicsSiteMapCollection(),
]);
parent::_construct();

parent::_initSitemapItems(); // TODO: Change the autogenerated stub
$this->blogDataHelper = ObjectManager::getInstance()->get(Data::class);
$this->imageHelper = ObjectManager::getInstance()->get(Image::class);
$this->router = $this->blogDataHelper->getBlogConfig('general/url_prefix');
}
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "mageplaza/magento-2-blog-extension",
"description": "Magento 2 Blog extension",
"require": {
"mageplaza/module-core": "^1.4.5"
"mageplaza/module-core": "^1.4.12"
},
"type": "magento2-module",
"version": "4.1.3",
"version": "4.1.4",
"license": "proprietary",
"keywords": [
"magento 2",
Expand Down

0 comments on commit e6eb186

Please sign in to comment.