init
This commit is contained in:
+116
@@ -0,0 +1,116 @@
|
||||
<?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\Model\ResourceModel;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Model\AbstractModel;
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
use Magento\Framework\Model\ResourceModel\Db\Context;
|
||||
use Magento\Framework\Stdlib\DateTime\DateTime;
|
||||
use Mageplaza\Blog\Helper\Data;
|
||||
|
||||
/**
|
||||
* Class Author
|
||||
* @package Mageplaza\Blog\Model\ResourceModel
|
||||
*/
|
||||
class Author extends AbstractDb
|
||||
{
|
||||
/**
|
||||
* @var Data
|
||||
*/
|
||||
public $helperData;
|
||||
|
||||
/**
|
||||
* @var DateTime
|
||||
*/
|
||||
public $dateTime;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $postTable;
|
||||
|
||||
/**
|
||||
* Author constructor.
|
||||
*
|
||||
* @param Context $context
|
||||
* @param Data $helperData
|
||||
* @param DateTime $dateTime
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
Data $helperData,
|
||||
DateTime $dateTime
|
||||
) {
|
||||
$this->helperData = $helperData;
|
||||
$this->dateTime = $dateTime;
|
||||
|
||||
parent::__construct($context);
|
||||
$this->postTable = $this->getTable('mageplaza_blog_post');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init('mageplaza_blog_author', 'user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
protected function _beforeSave(AbstractModel $object)
|
||||
{
|
||||
$object->setUrlKey(
|
||||
$this->helperData->generateUrlKey($this, $object, $object->getUrlKey() ?: $object->getName())
|
||||
);
|
||||
|
||||
if (!$object->isObjectNew()) {
|
||||
$timeStamp = $this->dateTime->gmtDate();
|
||||
$object->setUpdatedAt($timeStamp);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Mageplaza\Blog\Model\Author $author
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getPostIds(\Mageplaza\Blog\Model\Author $author)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$select = $adapter->select()->from(
|
||||
$this->postTable,
|
||||
'post_id'
|
||||
)
|
||||
->where(
|
||||
'author_id = ?',
|
||||
(int)$author->getId()
|
||||
);
|
||||
|
||||
return $adapter->fetchCol($select);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?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\Model\ResourceModel\Author;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
use Mageplaza\Blog\Model\Author;
|
||||
use Mageplaza\Blog\Model\ResourceModel\Author as AuthorResourceModel;
|
||||
|
||||
/**
|
||||
* Class Collection
|
||||
* @package Mageplaza\Blog\Model\ResourceModel\Author
|
||||
*/
|
||||
class Collection extends AbstractCollection
|
||||
{
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected $_idFieldName = 'user_id';
|
||||
|
||||
/**
|
||||
* Construct
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(Author::class, AuthorResourceModel::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
<?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\Model\ResourceModel\Author\Grid;
|
||||
|
||||
use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
|
||||
|
||||
/**
|
||||
* Class Collection
|
||||
* @package Mageplaza\Blog\Model\ResourceModel\Author\Grid
|
||||
*/
|
||||
class Collection extends SearchResult
|
||||
{
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
protected function _initSelect(): Collection
|
||||
{
|
||||
parent::_initSelect();
|
||||
|
||||
$this->getSelect()->joinLeft(
|
||||
['post' => $this->getTable('mageplaza_blog_post')],
|
||||
'main_table.user_id = post.author_id',
|
||||
['qty_post' => 'COUNT(post_id)']
|
||||
)->group('main_table.user_id');
|
||||
|
||||
$this->addFilterToMap('name', 'main_table.name');
|
||||
$this->addFilterToMap('url_key', 'main_table.url_key');
|
||||
$this->addExpressionFieldToSelect(
|
||||
'mp_created_at',
|
||||
'main_table.created_at',
|
||||
['mp_created_at' => 'created_at']
|
||||
);
|
||||
$this->addExpressionFieldToSelect(
|
||||
'mp_updated_at',
|
||||
'main_table.updated_at',
|
||||
['mp_created_at' => 'created_at']
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|string $field
|
||||
* @param null $condition
|
||||
*
|
||||
* @return SearchResult
|
||||
*/
|
||||
public function addFieldToFilter($field, $condition = null)
|
||||
{
|
||||
switch ($field) {
|
||||
case 'qty_post':
|
||||
foreach ($condition as $key => $value) {
|
||||
if ($key === 'like') {
|
||||
$this->getSelect()->having('COUNT(post_id) LIKE ?', $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
case 'mp_created_at':
|
||||
$field = 'main_table.created_at';
|
||||
break;
|
||||
case 'mp_updated_at':
|
||||
$field = 'main_table.updated_at';
|
||||
break;
|
||||
}
|
||||
|
||||
return parent::addFieldToFilter($field, $condition);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
* @param string $direction
|
||||
*
|
||||
* @return SearchResult
|
||||
*/
|
||||
public function setOrder($field, $direction = self::SORT_ORDER_DESC)
|
||||
{
|
||||
switch ($field) {
|
||||
case 'customer_name':
|
||||
return parent::setOrder('user_name', $direction);
|
||||
case 'mp_updated_at':
|
||||
$field = 'main_table.updated_at';
|
||||
break;
|
||||
case 'mp_created_at':
|
||||
$field = 'main_table.updated_at';
|
||||
break;
|
||||
}
|
||||
|
||||
return parent::setOrder($field, $direction);
|
||||
}
|
||||
}
|
||||
+600
@@ -0,0 +1,600 @@
|
||||
<?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\Model\ResourceModel;
|
||||
|
||||
use Magento\Framework\DataObject;
|
||||
use Magento\Framework\Event\ManagerInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Model\AbstractModel;
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
use Magento\Framework\Model\ResourceModel\Db\Context;
|
||||
use Magento\Framework\Stdlib\DateTime\DateTime;
|
||||
use Mageplaza\Blog\Helper\Data;
|
||||
use Mageplaza\Blog\Model\Category as CategoryModel;
|
||||
use Zend_Db_Expr;
|
||||
|
||||
/**
|
||||
* Class Category
|
||||
* @package Mageplaza\Blog\Model\ResourceModel
|
||||
*/
|
||||
class Category extends AbstractDb
|
||||
{
|
||||
/**
|
||||
* Date model
|
||||
*
|
||||
* @var DateTime
|
||||
*/
|
||||
public $date;
|
||||
|
||||
/**
|
||||
* Event Manager
|
||||
*
|
||||
* @var ManagerInterface
|
||||
*/
|
||||
public $eventManager;
|
||||
|
||||
/**
|
||||
* Post relation model
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $categoryPostTable;
|
||||
|
||||
/**
|
||||
* @var Data
|
||||
*/
|
||||
public $helperData;
|
||||
|
||||
/**
|
||||
* Category constructor.
|
||||
*
|
||||
* @param Data $helperData
|
||||
* @param DateTime $date
|
||||
* @param ManagerInterface $eventManager
|
||||
* @param Context $context
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
DateTime $date,
|
||||
ManagerInterface $eventManager,
|
||||
Data $helperData
|
||||
) {
|
||||
$this->helperData = $helperData;
|
||||
$this->date = $date;
|
||||
$this->eventManager = $eventManager;
|
||||
|
||||
parent::__construct($context);
|
||||
|
||||
$this->categoryPostTable = $this->getTable('mageplaza_blog_post_category');
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize resource model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init('mageplaza_blog_category', 'category_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves Blog Category Name from DB by passed id.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return string
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function getCategoryNameById($id)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$select = $adapter->select()
|
||||
->from($this->getMainTable(), 'name')
|
||||
->where('category_id = :category_id');
|
||||
$binds = ['category_id' => (int) $id];
|
||||
|
||||
return $adapter->fetchOne($select, $binds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Before save call back
|
||||
*
|
||||
* @param AbstractModel $object
|
||||
*
|
||||
* @return $this
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
protected function _beforeSave(AbstractModel $object)
|
||||
{
|
||||
$object->setUpdatedAt($this->date->date());
|
||||
if ($object->isObjectNew()) {
|
||||
$object->setCreatedAt($this->date->date());
|
||||
}
|
||||
/** @var CategoryModel $object */
|
||||
parent::_beforeSave($object);
|
||||
|
||||
if (!$object->getChildrenCount()) {
|
||||
$object->setChildrenCount(0);
|
||||
}
|
||||
|
||||
if ($object->isObjectNew()) {
|
||||
if ($object->getPosition() === null) {
|
||||
$object->setPosition($this->getMaxPosition($object->getPath()) + 1);
|
||||
}
|
||||
$path = explode('/', $object->getPath());
|
||||
$level = count($path) - ($object->getId() ? 1 : 0);
|
||||
$toUpdateChild = array_diff($path, [$object->getId()]);
|
||||
|
||||
if (!$object->hasPosition()) {
|
||||
$object->setPosition($this->getMaxPosition(implode('/', $toUpdateChild)) + 1);
|
||||
}
|
||||
if (!$object->hasLevel()) {
|
||||
$object->setLevel($level);
|
||||
}
|
||||
if (!$object->hasParentId() && $level) {
|
||||
$object->setParentId($path[$level - 1]);
|
||||
}
|
||||
if (!$object->getId()) {
|
||||
$object->setPath($object->getPath() . '/');
|
||||
}
|
||||
|
||||
$this->getConnection()->update(
|
||||
$this->getMainTable(),
|
||||
['children_count' => 'children_count+1'],
|
||||
['category_id IN(?)' => $toUpdateChild]
|
||||
);
|
||||
}
|
||||
|
||||
if (is_array($object->getStoreIds())) {
|
||||
$object->setStoreIds(implode(',', $object->getStoreIds()));
|
||||
}
|
||||
|
||||
$object->setUrlKey(
|
||||
$this->helperData->generateUrlKey($this, $object, $object->getUrlKey() ?: $object->getName())
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AbstractModel $object
|
||||
*
|
||||
* @return AbstractDb
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
protected function _afterSave(AbstractModel $object)
|
||||
{
|
||||
/** @var CategoryModel $object */
|
||||
if (substr($object->getPath(), -1) === '/') {
|
||||
$object->setPath($object->getPath() . $object->getId());
|
||||
$this->savePath($object);
|
||||
}
|
||||
$this->savePostRelation($object);
|
||||
|
||||
return parent::_afterSave($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
*
|
||||
* @return int|string
|
||||
*/
|
||||
protected function getMaxPosition($path)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$positionField = $adapter->quoteIdentifier('position');
|
||||
$level = count(explode('/', $path));
|
||||
$bind = ['c_level' => $level, 'c_path' => $path . '/%'];
|
||||
$select = $adapter->select()->from(
|
||||
$this->getTable('mageplaza_blog_category'),
|
||||
'MAX(' . $positionField . ')'
|
||||
)->where(
|
||||
$adapter->quoteIdentifier('path') . ' LIKE :c_path'
|
||||
)->where(
|
||||
$adapter->quoteIdentifier('level') . ' = :c_level'
|
||||
);
|
||||
|
||||
$position = $adapter->fetchOne($select, $bind);
|
||||
if (!$position) {
|
||||
$position = 0;
|
||||
}
|
||||
|
||||
return $position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check category url key is exists
|
||||
*
|
||||
* @param string $urlKey
|
||||
*
|
||||
* @return string
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function isDuplicateUrlKey($urlKey)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$select = $adapter->select()
|
||||
->from($this->getMainTable(), 'category_id')
|
||||
->where('url_key = :url_key');
|
||||
$binds = ['url_key' => $urlKey];
|
||||
|
||||
return $adapter->fetchOne($select, $binds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check is imported category
|
||||
*
|
||||
* @param string $importSource
|
||||
* @param int $oldId
|
||||
*
|
||||
* @return string
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function isImported($importSource, $oldId)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$select = $adapter->select()
|
||||
->from($this->getMainTable(), 'category_id')
|
||||
->where('import_source = :import_source');
|
||||
$binds = ['import_source' => $importSource . '-' . $oldId];
|
||||
|
||||
return $adapter->fetchOne($select, $binds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update path field
|
||||
*
|
||||
* @param Object $object
|
||||
*
|
||||
* @return $this
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function savePath($object)
|
||||
{
|
||||
if ($object->getId()) {
|
||||
$this->getConnection()->update(
|
||||
$this->getMainTable(),
|
||||
['path' => $object->getPath()],
|
||||
['category_id = ?' => $object->getId()]
|
||||
);
|
||||
$object->unsetData('path_ids');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AbstractModel $object
|
||||
*
|
||||
* @return $this
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
protected function _beforeDelete(AbstractModel $object)
|
||||
{
|
||||
parent::_beforeDelete($object);
|
||||
|
||||
/**
|
||||
* Update children count for all parent Categories
|
||||
*/
|
||||
$parentIds = $object->getParentIds();
|
||||
if ($parentIds) {
|
||||
$childDecrease = $object->getChildrenCount() + 1;
|
||||
// +1 is itself
|
||||
$data = ['children_count' => 'children_count - ' . $childDecrease];
|
||||
$where = ['category_id IN(?)' => $parentIds];
|
||||
$this->getConnection()->update($this->getMainTable(), $data, $where);
|
||||
}
|
||||
$this->deleteChildren($object);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DataObject $object
|
||||
*
|
||||
* @return $this
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function deleteChildren(DataObject $object)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$pathField = $adapter->quoteIdentifier('path');
|
||||
|
||||
$select = $adapter->select()->from(
|
||||
$this->getMainTable(),
|
||||
['category_id']
|
||||
)->where(
|
||||
$pathField . ' LIKE :c_path'
|
||||
);
|
||||
|
||||
$childrenIds = $adapter->fetchCol($select, ['c_path' => $object->getPath() . '/%']);
|
||||
|
||||
if (!empty($childrenIds)) {
|
||||
$adapter->delete($this->getMainTable(), ['category_id IN (?)' => $childrenIds]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add deleted children ids to object
|
||||
* This data can be used in after delete event
|
||||
*/
|
||||
$object->setDeletedChildrenIds($childrenIds);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CategoryModel $category
|
||||
* @param CategoryModel $newParent
|
||||
* @param null $afterCategoryId
|
||||
*
|
||||
* @return $this
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function changeParent(
|
||||
CategoryModel $category,
|
||||
CategoryModel $newParent,
|
||||
$afterCategoryId = null
|
||||
) {
|
||||
$childrenCount = (int) $this->getChildrenCount($category->getId()) + 1;
|
||||
$table = $this->getMainTable();
|
||||
$adapter = $this->getConnection();
|
||||
$levelField = $adapter->quoteIdentifier('level');
|
||||
$pathField = $adapter->quoteIdentifier('path');
|
||||
|
||||
/**
|
||||
* Decrease children count for all old Blog Category parent Categories
|
||||
*/
|
||||
$adapter->update(
|
||||
$table,
|
||||
['children_count' => new Zend_Db_Expr('children_count - ' . $childrenCount)],
|
||||
['category_id IN(?)' => $category->getParentIds()]
|
||||
);
|
||||
|
||||
/**
|
||||
* Increase children count for new Blog Category parents
|
||||
*/
|
||||
$adapter->update(
|
||||
$table,
|
||||
['children_count' => new Zend_Db_Expr('children_count + ' . $childrenCount)],
|
||||
['category_id IN(?)' => $newParent->getPathIds()]
|
||||
);
|
||||
|
||||
$position = $this->processPositions($category, $newParent, $afterCategoryId);
|
||||
$newPath = sprintf('%s/%s', $newParent->getPath(), $category->getId());
|
||||
$newLevel = $newParent->getLevel() + 1;
|
||||
$levelDisposition = $newLevel - $category->getLevel();
|
||||
|
||||
/**
|
||||
* Update children nodes path
|
||||
*/
|
||||
$adapter->update(
|
||||
$table,
|
||||
[
|
||||
'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() . '/%']
|
||||
);
|
||||
/**
|
||||
* Update moved Blog Category data
|
||||
*/
|
||||
$data = [
|
||||
'path' => $newPath,
|
||||
'level' => $newLevel,
|
||||
'position' => $position,
|
||||
'parent_id' => $newParent->getId(),
|
||||
];
|
||||
$adapter->update($table, $data, ['category_id = ?' => $category->getId()]);
|
||||
|
||||
/** Update Blog Category object to new data */
|
||||
$category->addData($data);
|
||||
$category->unsetData('path_ids');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CategoryModel $category
|
||||
* @param CategoryModel $newParent
|
||||
* @param int|null $afterCategoryId
|
||||
*
|
||||
* @return int|string
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function processPositions(
|
||||
CategoryModel $category,
|
||||
CategoryModel $newParent,
|
||||
$afterCategoryId
|
||||
) {
|
||||
$table = $this->getMainTable();
|
||||
$connect = $this->getConnection();
|
||||
/** Get old category position */
|
||||
$positionOld = $category->getPosition();
|
||||
/** Get new category position */
|
||||
if (empty($afterCategoryId)) {
|
||||
$positionNew = 1;
|
||||
} else {
|
||||
$select = $connect->select()->from($table, 'position')->where('category_id = :category_id');
|
||||
$positionNew = $connect->fetchOne($select, ['category_id' => $afterCategoryId]);
|
||||
}
|
||||
|
||||
/** Update position when the item is moved */
|
||||
/** Move to other category parent */
|
||||
if ($category->getParentId() != $newParent->getId()) {
|
||||
if ($afterCategoryId == 0) {
|
||||
$positionNew = 0;
|
||||
}
|
||||
$positionNew++;
|
||||
// phpcs:disable Magento2.SQL.RawQuery
|
||||
$sql = "UPDATE `" . $table . "` SET `position`= (`position`-1) WHERE `parent_id`= "
|
||||
. $category->getParentId() . " AND `position` >= " . $positionOld;
|
||||
$connect->query($sql);
|
||||
$sql = "UPDATE `" . $table . "` SET `position`= (`position`+1) WHERE `parent_id`= " . $newParent->getId()
|
||||
. " AND `position` >= " . $positionNew;
|
||||
$connect->query($sql);
|
||||
} else {
|
||||
/** Move in the same parent */
|
||||
/** Move down */
|
||||
if ($positionNew > $positionOld) {
|
||||
$sql = "UPDATE `" . $table . "` SET `position`= (`position`-1) WHERE `parent_id`= "
|
||||
. $newParent->getId() . " AND `position` <= " . $positionNew;
|
||||
$connect->query($sql);
|
||||
$sql = "UPDATE `" . $table . "` SET `position`= (`position`+1) WHERE `parent_id`= "
|
||||
. $newParent->getId() . " AND `position` < " . $positionOld;
|
||||
$connect->query($sql);
|
||||
} else {
|
||||
/** Move up */
|
||||
$positionNew++;
|
||||
if (empty($afterCategoryId)) {
|
||||
$positionNew = 1;
|
||||
}
|
||||
$sql = "UPDATE `" . $table . "` SET `position`= (`position`+1) WHERE `parent_id`= "
|
||||
. $newParent->getId() . " AND `position` >= " . $positionNew;
|
||||
$connect->query($sql);
|
||||
$sql = "UPDATE `" . $table . "` SET `position`= (`position`-1) WHERE `parent_id`= "
|
||||
. $newParent->getId() . " AND `position` > " . $positionOld;
|
||||
$connect->query($sql);
|
||||
}
|
||||
}
|
||||
|
||||
return $positionNew;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $categoryId
|
||||
*
|
||||
* @return string
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function getChildrenCount($categoryId)
|
||||
{
|
||||
$select = $this->getConnection()->select()->from(
|
||||
$this->getMainTable(),
|
||||
'children_count'
|
||||
)->where(
|
||||
'category_id = :category_id'
|
||||
);
|
||||
$bind = ['category_id' => $categoryId];
|
||||
|
||||
return $this->getConnection()->fetchOne($select, $bind);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CategoryModel $category
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getPostsPosition(CategoryModel $category)
|
||||
{
|
||||
$select = $this->getConnection()->select()->from(
|
||||
$this->categoryPostTable,
|
||||
['post_id', 'position']
|
||||
)
|
||||
->where(
|
||||
'category_id = :category_id'
|
||||
);
|
||||
$bind = ['category_id' => (int) $category->getId()];
|
||||
|
||||
return $this->getConnection()->fetchPairs($select, $bind);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CategoryModel $category
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function savePostRelation(CategoryModel $category)
|
||||
{
|
||||
$category->setIsChangedPostList(false);
|
||||
$id = $category->getId();
|
||||
$posts = $category->getPostsData();
|
||||
if ($posts === null) {
|
||||
return $this;
|
||||
}
|
||||
$oldPosts = $category->getPostsPosition();
|
||||
$insert = array_diff_key($posts, $oldPosts);
|
||||
$delete = array_diff_key($oldPosts, $posts);
|
||||
$update = array_intersect_key($posts, $oldPosts);
|
||||
$_update = [];
|
||||
foreach ($update as $key => $position) {
|
||||
if (isset($oldPosts[$key]) && $oldPosts[$key] != $position) {
|
||||
$_update[$key] = $position;
|
||||
}
|
||||
}
|
||||
$update = $_update;
|
||||
$adapter = $this->getConnection();
|
||||
if (!empty($delete)) {
|
||||
$condition = ['post_id IN(?)' => array_keys($delete), 'category_id=?' => $id];
|
||||
$adapter->delete($this->categoryPostTable, $condition);
|
||||
}
|
||||
if (!empty($insert)) {
|
||||
$data = [];
|
||||
foreach ($insert as $postId => $position) {
|
||||
$data[] = [
|
||||
'category_id' => (int) $id,
|
||||
'post_id' => (int) $postId,
|
||||
'position' => (int) $position
|
||||
];
|
||||
}
|
||||
$adapter->insertMultiple($this->categoryPostTable, $data);
|
||||
}
|
||||
if (!empty($update)) {
|
||||
foreach ($update as $postId => $position) {
|
||||
$where = ['category_id = ?' => (int) $id, 'post_id = ?' => (int) $postId];
|
||||
$bind = ['position' => (int) $position];
|
||||
$adapter->update($this->categoryPostTable, $bind, $where);
|
||||
}
|
||||
}
|
||||
if (!empty($insert) || !empty($delete)) {
|
||||
$postIds = array_unique(array_merge(array_keys($insert), array_keys($delete)));
|
||||
$this->eventManager->dispatch(
|
||||
'mageplaza_blog_category_change_posts',
|
||||
['category' => $category, 'post_ids' => $postIds]
|
||||
);
|
||||
}
|
||||
if (!empty($insert) || !empty($update) || !empty($delete)) {
|
||||
$category->setIsChangedPostList(true);
|
||||
$postIds = array_keys($insert + $delete + $update);
|
||||
$category->setAffectedPostIds($postIds);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $importType
|
||||
*
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function deleteImportItems($importType)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$adapter->delete($this->getMainTable(), "`import_source` LIKE '" . $importType . "%'");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
<?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\Model\ResourceModel\Category;
|
||||
|
||||
use Magento\Eav\Model\Entity\Attribute;
|
||||
use Magento\Framework\Data\Collection\AbstractDb;
|
||||
use Magento\Framework\DB\Select;
|
||||
use Magento\Sales\Model\ResourceModel\Collection\AbstractCollection;
|
||||
use Mageplaza\Blog\Api\Data\SearchResult\CategorySearchResultInterface;
|
||||
use Mageplaza\Blog\Model\Category;
|
||||
use Mageplaza\Blog\Model\ResourceModel\Category as CategoryResourceModel;
|
||||
|
||||
/**
|
||||
* Class Collection
|
||||
* @package Mageplaza\Blog\Model\ResourceModel\Category
|
||||
*/
|
||||
class Collection extends AbstractCollection implements CategorySearchResultInterface
|
||||
{
|
||||
/**
|
||||
* ID Field Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_idFieldName = 'category_id';
|
||||
|
||||
/**
|
||||
* Event prefix
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_eventPrefix = 'mageplaza_blog_category_collection';
|
||||
|
||||
/**
|
||||
* Event object
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_eventObject = 'category_collection';
|
||||
|
||||
/**
|
||||
* Define resource model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(Category::class, CategoryResourceModel::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Attribute|string $field
|
||||
* @param null $condition
|
||||
*
|
||||
* @return AbstractDb|AbstractCollection
|
||||
*/
|
||||
public function addAttributeToFilter($field, $condition = null)
|
||||
{
|
||||
return $this->addFieldToFilter($field, $condition);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function addFieldToFilter($field, $condition = null)
|
||||
{
|
||||
if ($field === 'entity_id') {
|
||||
$field = 'category_id';
|
||||
}
|
||||
|
||||
return parent::addFieldToFilter($field, $condition);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setProductStoreId()
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setLoadProductCount()
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setStoreId()
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $attribute
|
||||
* @param bool $joinType
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addAttributeToSelect($attribute, $joinType = false)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get SQL for get record count.
|
||||
* Extra GROUP BY strip added.
|
||||
*
|
||||
* @return Select
|
||||
*/
|
||||
public function getSelectCountSql()
|
||||
{
|
||||
$countSelect = parent::getSelectCountSql();
|
||||
$countSelect->reset(Select::GROUP);
|
||||
|
||||
return $countSelect;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $valueField
|
||||
* @param string $labelField
|
||||
* @param array $additional
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function _toOptionArray($valueField = null, $labelField = 'name', $additional = [])
|
||||
{
|
||||
$valueField = 'category_id';
|
||||
|
||||
return parent::_toOptionArray($valueField, $labelField, $additional); // TODO: Change the autogenerated stub
|
||||
}
|
||||
|
||||
/**
|
||||
* Add if filter
|
||||
*
|
||||
* @param array|mixed $categoryIds
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addIdFilter($categoryIds)
|
||||
{
|
||||
$condition = '';
|
||||
|
||||
if (is_array($categoryIds)) {
|
||||
if (!empty($categoryIds)) {
|
||||
$condition = ['in' => $categoryIds];
|
||||
}
|
||||
} elseif (is_numeric($categoryIds)) {
|
||||
$condition = $categoryIds;
|
||||
} elseif (is_string($categoryIds)) {
|
||||
$ids = explode(',', $categoryIds);
|
||||
if (empty($ids)) {
|
||||
$condition = $categoryIds;
|
||||
} else {
|
||||
$condition = ['in' => $ids];
|
||||
}
|
||||
}
|
||||
|
||||
if ($condition) {
|
||||
$this->addFieldToFilter('category_id', $condition);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,540 @@
|
||||
<?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\Model\ResourceModel\Category;
|
||||
|
||||
use Exception;
|
||||
use Magento\Framework\App\CacheInterface;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Data\Tree\Dbp;
|
||||
use Magento\Framework\Data\Tree\Node;
|
||||
use Magento\Framework\DB\Select;
|
||||
use Magento\Framework\Event\ManagerInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Mageplaza\Blog\Model\ResourceModel\Category;
|
||||
use Mageplaza\Blog\Model\Category as CategoryModel;
|
||||
|
||||
/**
|
||||
* Class Tree
|
||||
* @package Mageplaza\Blog\Model\ResourceModel\Category
|
||||
*/
|
||||
class Tree extends Dbp
|
||||
{
|
||||
/**
|
||||
* ID field
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const ID_FIELD = 'id';
|
||||
/**
|
||||
* Path field
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const PATH_FIELD = 'path';
|
||||
/**
|
||||
* Order field
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const ORDER_FIELD = 'order';
|
||||
/**
|
||||
* Level field
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const LEVEL_FIELD = 'level';
|
||||
|
||||
/**
|
||||
* Event manager
|
||||
*
|
||||
* @var ManagerInterface
|
||||
*/
|
||||
public $eventManager;
|
||||
|
||||
/**
|
||||
* Collection factory
|
||||
*
|
||||
* @var CollectionFactory
|
||||
*/
|
||||
public $collectionFactory;
|
||||
|
||||
/**
|
||||
* Blog Category Resource instance
|
||||
*
|
||||
* @var Category
|
||||
*/
|
||||
public $categoryResource;
|
||||
|
||||
/**
|
||||
* Cache instance
|
||||
*
|
||||
* @var CacheInterface
|
||||
*/
|
||||
public $cache;
|
||||
|
||||
/**
|
||||
* Store Manager instance
|
||||
*
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
public $storeManager;
|
||||
|
||||
/**
|
||||
* App resource
|
||||
*
|
||||
* @var ResourceConnection
|
||||
*/
|
||||
public $coreResource;
|
||||
|
||||
/**
|
||||
* Blog Category Collection
|
||||
*
|
||||
* @var Collection
|
||||
*/
|
||||
public $collection;
|
||||
|
||||
/**
|
||||
* Inactive Blog Category Ids
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $inactiveCategoryIds;
|
||||
|
||||
/**
|
||||
* Tree constructor.
|
||||
*
|
||||
* @param ManagerInterface $eventManager
|
||||
* @param CollectionFactory $collectionFactory
|
||||
* @param Category $categoryResource
|
||||
* @param CacheInterface $cache
|
||||
* @param StoreManagerInterface $storeManager
|
||||
* @param ResourceConnection $coreResource
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct(
|
||||
ManagerInterface $eventManager,
|
||||
CollectionFactory $collectionFactory,
|
||||
Category $categoryResource,
|
||||
CacheInterface $cache,
|
||||
StoreManagerInterface $storeManager,
|
||||
ResourceConnection $coreResource
|
||||
) {
|
||||
$this->eventManager = $eventManager;
|
||||
$this->collectionFactory = $collectionFactory;
|
||||
$this->categoryResource = $categoryResource;
|
||||
$this->cache = $cache;
|
||||
$this->storeManager = $storeManager;
|
||||
$this->coreResource = $coreResource;
|
||||
|
||||
parent::__construct(
|
||||
$coreResource->getConnection('mageplaza_blog_write'),
|
||||
$coreResource->getTableName('mageplaza_blog_category'),
|
||||
[
|
||||
Dbp::ID_FIELD => 'category_id',
|
||||
Dbp::PATH_FIELD => 'path',
|
||||
Dbp::ORDER_FIELD => 'position',
|
||||
Dbp::LEVEL_FIELD => 'level'
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add data to collection
|
||||
*
|
||||
* @param null $collection
|
||||
* @param bool $sorted
|
||||
* @param array $exclude
|
||||
* @param bool $toLoad
|
||||
* @param bool $onlyActive
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addCollectionData(
|
||||
$collection = null,
|
||||
$sorted = false,
|
||||
$exclude = [],
|
||||
$toLoad = true,
|
||||
$onlyActive = false
|
||||
) {
|
||||
if ($collection === null) {
|
||||
$collection = $this->getCollection($sorted);
|
||||
} else {
|
||||
$this->setCollection($collection);
|
||||
}
|
||||
|
||||
if (!is_array($exclude)) {
|
||||
$exclude = [$exclude];
|
||||
}
|
||||
|
||||
$nodeIds = [];
|
||||
foreach ($this->getNodes() as $node) {
|
||||
if (!in_array($node->getId(), $exclude)) {
|
||||
$nodeIds[] = $node->getId();
|
||||
}
|
||||
}
|
||||
$collection->addIdFilter($nodeIds);
|
||||
if ($onlyActive) {
|
||||
$disabledIds = $this->getDisabledIds();
|
||||
if ($disabledIds) {
|
||||
$collection->addFieldToFilter('category_id', ['nin' => $disabledIds]);
|
||||
}
|
||||
}
|
||||
|
||||
if ($toLoad) {
|
||||
$collection->load();
|
||||
|
||||
foreach ($collection as $category) {
|
||||
if ($this->getNodeById($category->getId())) {
|
||||
$this->getNodeById($category->getId())->addData($category->getData());
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->getNodes() as $node) {
|
||||
if (!$collection->getItemById($node->getId()) && $node->getParent()) {
|
||||
$this->removeNode($node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add inactive categories ids
|
||||
*
|
||||
* @param mixed $ids
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addInactiveCategoryIds($ids)
|
||||
{
|
||||
if (!is_array($this->inactiveCategoryIds)) {
|
||||
$this->initInactiveCategoryIds();
|
||||
}
|
||||
$this->inactiveCategoryIds = array_merge($ids, $this->inactiveCategoryIds);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve inactive Categories ids
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function initInactiveCategoryIds()
|
||||
{
|
||||
$this->inactiveCategoryIds = [];
|
||||
$this->eventManager->dispatch('mageplaza_blog_category_tree_init_inactive_category_ids', ['tree' => $this]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve inactive Categories ids
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getInactiveCategoryIds()
|
||||
{
|
||||
if (!is_array($this->inactiveCategoryIds)) {
|
||||
$this->initInactiveCategoryIds();
|
||||
}
|
||||
|
||||
return $this->inactiveCategoryIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getDisabledIds()
|
||||
{
|
||||
/* implement this for frontend */
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve inactive Blog Category item ids
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getInactiveItemIds()
|
||||
{
|
||||
/* implement this for frontend */
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check is Blog Category items active
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getItemIsActive()
|
||||
{
|
||||
//implement this for frontend
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Categories collection
|
||||
*
|
||||
* @param boolean $sorted
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getCollection($sorted = false)
|
||||
{
|
||||
if ($this->collection === null) {
|
||||
$this->collection = $this->getDefaultCollection($sorted);
|
||||
}
|
||||
|
||||
return $this->collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean unneeded collection
|
||||
*
|
||||
* @param Collection|array $object
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function clean($object)
|
||||
{
|
||||
if (is_array($object)) {
|
||||
foreach ($object as $obj) {
|
||||
$this->clean($obj);
|
||||
}
|
||||
}
|
||||
unset($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set collection
|
||||
*
|
||||
* @param Collection $collection
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setCollection($collection)
|
||||
{
|
||||
if ($this->collection !== null) {
|
||||
$this->clean($this->collection);
|
||||
}
|
||||
$this->collection = $collection;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default collection
|
||||
*
|
||||
* @param boolean $sorted
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getDefaultCollection($sorted = false)
|
||||
{
|
||||
$collection = $this->collectionFactory->create();
|
||||
if ($sorted) {
|
||||
if (is_string($sorted)) {
|
||||
// $sorted is supposed to be attribute name
|
||||
$collection->addFieldToSort($sorted);
|
||||
} else {
|
||||
$collection->addFieldToSort('name');
|
||||
}
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executing parents move method and cleaning cache after it
|
||||
*
|
||||
* @param Node $category
|
||||
* @param Node $newParent
|
||||
* @param null $prevNode
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function move($category, $newParent, $prevNode = null)
|
||||
{
|
||||
$this->categoryResource->move($category->getId(), $newParent->getId());
|
||||
parent::move($category, $newParent, $prevNode);
|
||||
|
||||
$this->afterMove();
|
||||
}
|
||||
|
||||
/**
|
||||
* Move tree after
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function afterMove()
|
||||
{
|
||||
$this->cache->clean([CategoryModel::CACHE_TAG]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load whole Blog Category tree, that will include specified Categories ids.
|
||||
*
|
||||
* @param array $ids
|
||||
* @param bool $addCollectionData
|
||||
*
|
||||
* @return $this|bool
|
||||
*/
|
||||
public function loadByIds($ids, $addCollectionData = true)
|
||||
{
|
||||
$levelField = $this->_conn->quoteIdentifier('level');
|
||||
$pathField = $this->_conn->quoteIdentifier('path');
|
||||
// load first two levels, if no ids specified
|
||||
if (empty($ids)) {
|
||||
$select = $this->_conn
|
||||
->select()
|
||||
->from($this->_table, 'category_id')
|
||||
->where($levelField . ' <= 2');
|
||||
$ids = $this->_conn->fetchCol($select);
|
||||
}
|
||||
if (!is_array($ids)) {
|
||||
$ids = [$ids];
|
||||
}
|
||||
foreach ($ids as $key => $id) {
|
||||
$ids[$key] = (int) $id;
|
||||
}
|
||||
|
||||
// collect paths of specified IDs and prepare to collect all their parents and neighbours
|
||||
$select = $this->_conn
|
||||
->select()
|
||||
->from($this->_table, ['path', 'level'])
|
||||
->where('category_id IN (?)', $ids);
|
||||
$where = [$levelField . '=0' => true];
|
||||
|
||||
foreach ($this->_conn->fetchAll($select) as $item) {
|
||||
$pathIds = explode('/', $item['path']);
|
||||
$level = (int) $item['level'];
|
||||
while ($level > 0) {
|
||||
$lastId = end($pathIds);
|
||||
$lastIndex = key($lastId);
|
||||
$pathIds[$lastIndex] = '%';
|
||||
$path = implode('/', $pathIds);
|
||||
$where["{$levelField}={$level} AND {$pathField} LIKE '{$path}'"] = true;
|
||||
array_pop($pathIds);
|
||||
$level--;
|
||||
}
|
||||
}
|
||||
$where = array_keys($where);
|
||||
|
||||
// get all required records
|
||||
if ($addCollectionData) {
|
||||
$select = $this->createCollectionDataSelect();
|
||||
} else {
|
||||
$select = clone $this->_select;
|
||||
$select->order($this->_orderField . ' ' . Select::SQL_ASC);
|
||||
}
|
||||
$select->where(implode(' OR ', $where));
|
||||
|
||||
// get array of records and add them as nodes to the tree
|
||||
$arrNodes = $this->_conn->fetchAll($select);
|
||||
if (!$arrNodes) {
|
||||
return false;
|
||||
}
|
||||
$childrenItems = [];
|
||||
foreach ($arrNodes as $key => $nodeInfo) {
|
||||
$pathToParent = explode('/', $nodeInfo[$this->_pathField]);
|
||||
array_pop($pathToParent);
|
||||
$pathToParent = implode('/', $pathToParent);
|
||||
$childrenItems[$pathToParent][] = $nodeInfo;
|
||||
}
|
||||
$this->addChildNodes($childrenItems, '', null);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load array of category parents
|
||||
*
|
||||
* @param string $path
|
||||
* @param bool $addCollectionData
|
||||
* @param bool $withRootNode
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function loadBreadcrumbsArray($path, $addCollectionData = true, $withRootNode = false)
|
||||
{
|
||||
$pathIds = explode('/', $path);
|
||||
if (!$withRootNode) {
|
||||
array_shift($pathIds);
|
||||
}
|
||||
$result = [];
|
||||
if (!empty($pathIds)) {
|
||||
if ($addCollectionData) {
|
||||
$select = $this->createCollectionDataSelect(false);
|
||||
} else {
|
||||
$select = clone $this->_select;
|
||||
}
|
||||
$select->where(
|
||||
'e.category_id IN(?)',
|
||||
$pathIds
|
||||
)->order(
|
||||
$this->_conn->getLengthSql('e.path') . ' ' . Select::SQL_ASC
|
||||
);
|
||||
$result = $this->_conn->fetchAll($select);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $sorted
|
||||
* @return Select
|
||||
*/
|
||||
public function createCollectionDataSelect($sorted = true)
|
||||
{
|
||||
$select = $this->getDefaultCollection($sorted ? $this->_orderField : false)->getSelect();
|
||||
|
||||
return $select;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get real existing Blog Category ids by specified ids
|
||||
*
|
||||
* @param array $ids
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getExistingCategoryIdsBySpecifiedIds($ids)
|
||||
{
|
||||
if (empty($ids)) {
|
||||
return [];
|
||||
}
|
||||
if (!is_array($ids)) {
|
||||
$ids = [$ids];
|
||||
}
|
||||
$select = $this->_conn
|
||||
->select()
|
||||
->from($this->_table, ['category_id'])
|
||||
->where('category_id IN (?)', $ids);
|
||||
|
||||
return $this->_conn->fetchCol($select);
|
||||
}
|
||||
}
|
||||
+88
@@ -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\Model\ResourceModel;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Model\AbstractModel;
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
|
||||
/**
|
||||
* Class Comment
|
||||
* @package Mageplaza\Blog\Model\ResourceModel
|
||||
*/
|
||||
class Comment extends AbstractDb
|
||||
{
|
||||
/**
|
||||
* Initialize resource model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init('mageplaza_blog_comment', 'comment_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AbstractModel $object
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function _beforeSave(AbstractModel $object)
|
||||
{
|
||||
if (is_array($object->getStoreIds())) {
|
||||
$object->setStoreIds(implode(',', $object->getStoreIds()));
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check is imported category
|
||||
*
|
||||
* @param $importSource
|
||||
* @param $oldId
|
||||
*
|
||||
* @return string
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function isImported($importSource, $oldId)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$select = $adapter->select()
|
||||
->from($this->getMainTable(), 'comment_id')
|
||||
->where('import_source = :import_source');
|
||||
$binds = ['import_source' => $importSource . '-' . $oldId];
|
||||
|
||||
return $adapter->fetchOne($select, $binds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $importType
|
||||
*
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function deleteImportItems($importType)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$adapter->delete($this->getMainTable(), "`import_source` LIKE '" . $importType . "%'");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?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\Model\ResourceModel\Comment;
|
||||
|
||||
use Magento\Sales\Model\ResourceModel\Collection\AbstractCollection;
|
||||
use Mageplaza\Blog\Api\Data\SearchResult\CommentSearchResultInterface;
|
||||
use Mageplaza\Blog\Model\Comment;
|
||||
|
||||
/**
|
||||
* Class Collection
|
||||
* @package Mageplaza\Blog\Model\ResourceModel\Comment
|
||||
*/
|
||||
class Collection extends AbstractCollection implements CommentSearchResultInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_idFieldName = 'comment_id';
|
||||
|
||||
/**
|
||||
* Define model & resource model
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(Comment::class, \Mageplaza\Blog\Model\ResourceModel\Comment::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
<?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\Model\ResourceModel\Comment\Grid;
|
||||
|
||||
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
|
||||
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
|
||||
use Magento\Framework\Event\ManagerInterface as EventManager;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
|
||||
use Mageplaza\Blog\Model\ResourceModel\Comment;
|
||||
use Psr\Log\LoggerInterface as Logger;
|
||||
use Zend_Db_Expr;
|
||||
|
||||
/**
|
||||
* Class Collection
|
||||
* @package Mageplaza\Blog\Model\ResourceModel\Comment\Grid
|
||||
*/
|
||||
class Collection extends SearchResult
|
||||
{
|
||||
/**
|
||||
* Collection constructor.
|
||||
*
|
||||
* @param EntityFactory $entityFactory
|
||||
* @param Logger $logger
|
||||
* @param FetchStrategy $fetchStrategy
|
||||
* @param EventManager $eventManager
|
||||
* @param string $mainTable
|
||||
* @param string $resourceModel
|
||||
*
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
// phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod
|
||||
public function __construct(
|
||||
EntityFactory $entityFactory,
|
||||
Logger $logger,
|
||||
FetchStrategy $fetchStrategy,
|
||||
EventManager $eventManager,
|
||||
$mainTable = 'mageplaza_blog_comment',
|
||||
$resourceModel = Comment::class
|
||||
) {
|
||||
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
protected function _initSelect()
|
||||
{
|
||||
parent::_initSelect();
|
||||
|
||||
$this->addPostName();
|
||||
$this->addCustomerName();
|
||||
|
||||
$this->addExpressionFieldToSelect(
|
||||
'mp_created_at',
|
||||
'main_table.created_at',
|
||||
['mp_created_at' => 'created_at']
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
* @param string $direction
|
||||
*
|
||||
* @return SearchResult
|
||||
*/
|
||||
public function setOrder($field, $direction = self::SORT_ORDER_DESC)
|
||||
{
|
||||
switch ($field) {
|
||||
case 'customer_name':
|
||||
return parent::setOrder('user_name', $direction);
|
||||
case 'mp_created_at':
|
||||
return parent::setOrder('main_table.created_at', $direction);
|
||||
}
|
||||
|
||||
return parent::setOrder($field, $direction);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|string $field
|
||||
* @param null $condition
|
||||
*
|
||||
* @return SearchResult
|
||||
*/
|
||||
public function addFieldToFilter($field, $condition = null)
|
||||
{
|
||||
if ($field === 'customer_name') {
|
||||
return parent::addFieldToFilter('user_name', $condition);
|
||||
}
|
||||
|
||||
if ($field === 'post_name') {
|
||||
$field = 'mp.name';
|
||||
} elseif ($field === 'mp_created_at') {
|
||||
$field = 'main_table.created_at';
|
||||
}
|
||||
|
||||
return parent::addFieldToFilter($field, $condition);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function addPostName()
|
||||
{
|
||||
$this->getSelect()->joinLeft(
|
||||
['mp' => $this->getTable('mageplaza_blog_post')],
|
||||
'main_table.post_id = mp.post_id',
|
||||
['post_name' => 'name']
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function addCustomerName()
|
||||
{
|
||||
$this->getSelect()->joinLeft(
|
||||
['ce' => $this->getTable('customer_entity')],
|
||||
'main_table.entity_id = ce.entity_id',
|
||||
['firstname', 'lastname']
|
||||
)->columns([
|
||||
'customer_name' => new Zend_Db_Expr("CONCAT(`ce`.`firstname`,' ',`ce`.`lastname`)")
|
||||
]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
<?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\Model\ResourceModel;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
|
||||
/**
|
||||
* Class Like
|
||||
* @package Mageplaza\Blog\Model\ResourceModel
|
||||
*/
|
||||
class Like extends AbstractDb
|
||||
{
|
||||
/**
|
||||
* Initialize resource model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init('mageplaza_blog_comment_like', 'like_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?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\Model\ResourceModel\Like;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
use Mageplaza\Blog\Model\Like;
|
||||
use Mageplaza\Blog\Model\ResourceModel\Like as LikeResourceModel;
|
||||
|
||||
/**
|
||||
* Class Collection
|
||||
* @package Mageplaza\Blog\Model\ResourceModel\Like
|
||||
*/
|
||||
class Collection extends AbstractCollection
|
||||
{
|
||||
/**
|
||||
* Define model & resource model
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(Like::class, LikeResourceModel::class);
|
||||
}
|
||||
}
|
||||
+631
@@ -0,0 +1,631 @@
|
||||
<?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\Model\ResourceModel;
|
||||
|
||||
use Magento\Backend\Model\Auth;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Framework\Event\ManagerInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Model\AbstractModel;
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
use Magento\Framework\Model\ResourceModel\Db\Context;
|
||||
use Magento\Framework\Stdlib\DateTime\DateTime;
|
||||
use Mageplaza\Blog\Helper\Data;
|
||||
use Mageplaza\Blog\Model\Author;
|
||||
use Mageplaza\Blog\Model\AuthorFactory;
|
||||
use Mageplaza\Blog\Model\Post as PostModel;
|
||||
|
||||
/**
|
||||
* Class Post
|
||||
* @package Mageplaza\Blog\Model\ResourceModel
|
||||
*/
|
||||
class Post extends AbstractDb
|
||||
{
|
||||
/**
|
||||
* Date model
|
||||
*
|
||||
* @var DateTime
|
||||
*/
|
||||
public $date;
|
||||
|
||||
/**
|
||||
* Event Manager
|
||||
*
|
||||
* @var ManagerInterface
|
||||
*/
|
||||
public $eventManager;
|
||||
|
||||
/**
|
||||
* Tag relation model
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $postTagTable;
|
||||
|
||||
/**
|
||||
* Topic relation model
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $postTopicTable;
|
||||
|
||||
/**
|
||||
* Blog Category relation model
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $postCategoryTable;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $postProductTable;
|
||||
|
||||
/**
|
||||
* @var Data
|
||||
*/
|
||||
public $helperData;
|
||||
|
||||
/**
|
||||
* @var AuthorFactory
|
||||
*/
|
||||
protected $_authorFactory;
|
||||
|
||||
/**
|
||||
* @var Auth
|
||||
*/
|
||||
protected $_auth;
|
||||
|
||||
/**
|
||||
* @var RequestInterface
|
||||
*/
|
||||
protected $_request;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $postTrafficTable;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $postAuthorTable;
|
||||
|
||||
/**
|
||||
* Post constructor.
|
||||
*
|
||||
* @param Context $context
|
||||
* @param DateTime $date
|
||||
* @param ManagerInterface $eventManager
|
||||
* @param Auth $auth
|
||||
* @param Data $helperData
|
||||
* @param RequestInterface $request
|
||||
* @param AuthorFactory $authorFactory
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
DateTime $date,
|
||||
ManagerInterface $eventManager,
|
||||
Auth $auth,
|
||||
Data $helperData,
|
||||
RequestInterface $request,
|
||||
AuthorFactory $authorFactory
|
||||
) {
|
||||
$this->date = $date;
|
||||
$this->eventManager = $eventManager;
|
||||
$this->_auth = $auth;
|
||||
$this->helperData = $helperData;
|
||||
$this->_request = $request;
|
||||
$this->_authorFactory = $authorFactory;
|
||||
|
||||
parent::__construct($context);
|
||||
|
||||
$this->postTagTable = $this->getTable('mageplaza_blog_post_tag');
|
||||
$this->postTopicTable = $this->getTable('mageplaza_blog_post_topic');
|
||||
$this->postCategoryTable = $this->getTable('mageplaza_blog_post_category');
|
||||
$this->postProductTable = $this->getTable('mageplaza_blog_post_product');
|
||||
$this->postTrafficTable = $this->getTable('mageplaza_blog_post_traffic');
|
||||
$this->postAuthorTable = $this->getTable('mageplaza_blog_author');
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize resource model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init('mageplaza_blog_post', 'post_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves Post Name from DB by passed id.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return string
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function getPostNameById($id)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$select = $adapter->select()
|
||||
->from($this->getMainTable(), 'name')
|
||||
->where('post_id = :post_id');
|
||||
$binds = ['post_id' => (int) $id];
|
||||
|
||||
return $adapter->fetchOne($select, $binds);
|
||||
}
|
||||
|
||||
/**
|
||||
* before save callback
|
||||
*
|
||||
* @param AbstractModel $object
|
||||
*
|
||||
* @return $this
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
protected function _beforeSave(AbstractModel $object)
|
||||
{
|
||||
if (is_array($object->getStoreIds())) {
|
||||
$object->setStoreIds(implode(',', $object->getStoreIds()));
|
||||
}
|
||||
if (is_array($object->getData('meta_title'))) {
|
||||
$object->setData('meta_title', json_encode($object->getData('meta_title')));
|
||||
}
|
||||
if (is_array($object->getData('meta_description'))) {
|
||||
$object->setData('meta_description', json_encode($object->getData('meta_description')));
|
||||
}
|
||||
if (is_array($object->getData('meta_keywords'))) {
|
||||
$object->setData('meta_keywords', json_encode($object->getData('meta_keywords')));
|
||||
}
|
||||
if (is_array($object->getData('meta_robots'))) {
|
||||
$object->setData('meta_robots', json_encode($object->getData('meta_robots')));
|
||||
}
|
||||
|
||||
$object->setUrlKey(
|
||||
$this->helperData->generateUrlKey($this, $object, $object->getUrlKey() ?: $object->getName())
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostModel|AbstractModel $object
|
||||
* @return AbstractDb
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
protected function _afterSave(AbstractModel $object)
|
||||
{
|
||||
$this->saveTagRelation($object);
|
||||
$this->saveTopicRelation($object);
|
||||
$this->saveCategoryRelation($object);
|
||||
$this->saveProductRelation($object);
|
||||
|
||||
if ($this->_request->getActionName() !== 'manage') {
|
||||
$this->saveAuthor();
|
||||
}
|
||||
|
||||
return parent::_afterSave($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostModel $post
|
||||
*
|
||||
* @return $this
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function saveTagRelation(PostModel $post)
|
||||
{
|
||||
$post->setIsChangedTagList(false);
|
||||
$id = $post->getId();
|
||||
$tags = $post->getTagsIds();
|
||||
$oldTags = $post->getTagIds();
|
||||
|
||||
if ($tags === null) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$insert = array_diff($tags, $oldTags);
|
||||
$delete = array_diff($oldTags, $tags);
|
||||
$adapter = $this->getConnection();
|
||||
if (!empty($delete)) {
|
||||
$condition = ['tag_id IN(?)' => $delete, 'post_id=?' => $id];
|
||||
$adapter->delete($this->postTagTable, $condition);
|
||||
}
|
||||
if (!empty($insert)) {
|
||||
$data = [];
|
||||
foreach ($insert as $tagId) {
|
||||
$data[] = [
|
||||
'post_id' => (int) $id,
|
||||
'tag_id' => (int) $tagId,
|
||||
'position' => 1
|
||||
];
|
||||
}
|
||||
$adapter->insertMultiple($this->postTagTable, $data);
|
||||
}
|
||||
if (!empty($insert) || !empty($delete)) {
|
||||
$tagIds = array_unique(array_merge(array_keys($insert), array_keys($delete)));
|
||||
$this->eventManager->dispatch(
|
||||
'mageplaza_blog_post_change_tags',
|
||||
['post' => $post, 'tag_ids' => $tagIds]
|
||||
);
|
||||
}
|
||||
|
||||
if (!empty($insert) || !empty($delete)) {
|
||||
$post->setIsChangedTagList(true);
|
||||
$tagIds = array_keys($insert + $delete);
|
||||
$post->setAffectedTagIds($tagIds);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostModel $post
|
||||
*
|
||||
* @return $this
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function saveTopicRelation(PostModel $post)
|
||||
{
|
||||
$post->setIsChangedTopicList(false);
|
||||
$id = $post->getId();
|
||||
$topics = $post->getTopicsIds();
|
||||
$oldTopics = $post->getTopicIds();
|
||||
|
||||
if ($topics === null) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$insert = array_diff($topics, $oldTopics);
|
||||
$delete = array_diff($oldTopics, $topics);
|
||||
$adapter = $this->getConnection();
|
||||
if (!empty($delete)) {
|
||||
$condition = ['topic_id IN(?)' => $delete, 'post_id=?' => $id];
|
||||
$adapter->delete($this->postTopicTable, $condition);
|
||||
}
|
||||
if (!empty($insert)) {
|
||||
$data = [];
|
||||
foreach ($insert as $topicId) {
|
||||
$data[] = [
|
||||
'post_id' => (int) $id,
|
||||
'topic_id' => (int) $topicId,
|
||||
'position' => 1
|
||||
];
|
||||
}
|
||||
$adapter->insertMultiple($this->postTopicTable, $data);
|
||||
}
|
||||
|
||||
if (!empty($insert) || !empty($delete)) {
|
||||
$topicIds = array_unique(array_merge(array_keys($insert), array_keys($delete)));
|
||||
$this->eventManager->dispatch(
|
||||
'mageplaza_blog_post_change_topics',
|
||||
['post' => $post, 'topic_ids' => $topicIds]
|
||||
);
|
||||
}
|
||||
if (!empty($insert) || !empty($delete)) {
|
||||
$post->setIsChangedTopicList(true);
|
||||
$topicIds = array_keys($insert + $delete);
|
||||
$post->setAffectedTopicIds($topicIds);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostModel $post
|
||||
*
|
||||
* @return $this
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function saveCategoryRelation(PostModel $post)
|
||||
{
|
||||
$post->setIsChangedCategoryList(false);
|
||||
$id = $post->getId();
|
||||
$categories = $post->getCategoriesIds();
|
||||
$oldCategoryIds = $post->getCategoryIds();
|
||||
|
||||
if ($categories === null) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$insert = array_diff($categories, $oldCategoryIds);
|
||||
$delete = array_diff($oldCategoryIds, $categories);
|
||||
$adapter = $this->getConnection();
|
||||
|
||||
if (!empty($delete)) {
|
||||
$condition = ['category_id IN(?)' => $delete, 'post_id=?' => $id];
|
||||
$adapter->delete($this->postCategoryTable, $condition);
|
||||
}
|
||||
if (!empty($insert)) {
|
||||
$data = [];
|
||||
foreach ($insert as $categoryId) {
|
||||
$data[] = [
|
||||
'post_id' => (int) $id,
|
||||
'category_id' => (int) $categoryId,
|
||||
'position' => 1
|
||||
];
|
||||
}
|
||||
$adapter->insertMultiple($this->postCategoryTable, $data);
|
||||
}
|
||||
if (!empty($insert) || !empty($delete)) {
|
||||
$categoryIds = array_unique(array_merge(array_keys($insert), array_keys($delete)));
|
||||
$this->eventManager->dispatch(
|
||||
'mageplaza_blog_post_change_categories',
|
||||
['post' => $post, 'category_ids' => $categoryIds]
|
||||
);
|
||||
}
|
||||
if (!empty($insert) || !empty($delete)) {
|
||||
$post->setIsChangedCategoryList(true);
|
||||
$categoryIds = array_keys($insert + $delete);
|
||||
$post->setAffectedCategoryIds($categoryIds);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostModel $post
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getCategoryIds(PostModel $post)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$select = $adapter->select()->from(
|
||||
$this->postCategoryTable,
|
||||
'category_id'
|
||||
)
|
||||
->where(
|
||||
'post_id = ?',
|
||||
(int) $post->getId()
|
||||
);
|
||||
|
||||
return $adapter->fetchCol($select);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostModel $post
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTagIds(PostModel $post)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$select = $adapter->select()->from(
|
||||
$this->postTagTable,
|
||||
'tag_id'
|
||||
)
|
||||
->where(
|
||||
'post_id = ?',
|
||||
(int) $post->getId()
|
||||
);
|
||||
|
||||
return $adapter->fetchCol($select);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostModel $post
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTopicIds(PostModel $post)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$select = $adapter->select()->from($this->postTopicTable, 'topic_id')
|
||||
->where('post_id = ?', (int) $post->getId());
|
||||
|
||||
return $adapter->fetchCol($select);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostModel $post
|
||||
* @return array
|
||||
*/
|
||||
public function getAuthor(PostModel $post)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$select = $adapter->select()->from($this->postAuthorTable, '*')
|
||||
->where('user_id = ?', (int) $post->getAuthorId());
|
||||
|
||||
return $adapter->fetchRow($select);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostModel $post
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getViewTraffic(PostModel $post)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$select = $adapter->select()->from($this->postTrafficTable, 'numbers_view')
|
||||
->where('post_id = ?', (int) $post->getId());
|
||||
|
||||
return $adapter->fetchCol($select);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostModel $post
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function saveProductRelation(PostModel $post)
|
||||
{
|
||||
$post->setIsChangedProductList(false);
|
||||
$id = $post->getId();
|
||||
$products = $post->getProductsData();
|
||||
$oldProducts = $post->getProductsPosition();
|
||||
|
||||
if (is_array($products)) {
|
||||
$insert = array_diff_key($products, $oldProducts);
|
||||
$delete = array_diff_key($oldProducts, $products);
|
||||
$update = array_intersect_key($products, $oldProducts);
|
||||
$_update = [];
|
||||
foreach ($update as $key => $settings) {
|
||||
if (isset($oldProducts[$key]) && $oldProducts[$key] != $settings['position']) {
|
||||
$_update[$key] = $settings;
|
||||
}
|
||||
}
|
||||
$update = $_update;
|
||||
}
|
||||
$adapter = $this->getConnection();
|
||||
if ($products === null && $this->_request->getActionName() === 'save') {
|
||||
foreach (array_keys($oldProducts) as $value) {
|
||||
$condition = ['entity_id =?' => (int) $value, 'post_id=?' => (int) $id];
|
||||
$adapter->delete($this->postProductTable, $condition);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
if (!empty($delete)) {
|
||||
foreach (array_keys($delete) as $value) {
|
||||
$condition = ['entity_id =?' => (int) $value, 'post_id=?' => (int) $id];
|
||||
$adapter->delete($this->postProductTable, $condition);
|
||||
}
|
||||
}
|
||||
if (!empty($insert)) {
|
||||
$data = [];
|
||||
foreach ($insert as $entityId => $position) {
|
||||
$data[] = [
|
||||
'post_id' => (int) $id,
|
||||
'entity_id' => (int) $entityId,
|
||||
'position' => (int) $position['position']
|
||||
];
|
||||
}
|
||||
$adapter->insertMultiple($this->postProductTable, $data);
|
||||
}
|
||||
if (!empty($update)) {
|
||||
foreach ($update as $entityId => $position) {
|
||||
$where = ['post_id = ?' => (int) $id, 'entity_id = ?' => (int) $entityId];
|
||||
$bind = ['position' => (int) $position['position']];
|
||||
$adapter->update($this->postProductTable, $bind, $where);
|
||||
}
|
||||
}
|
||||
if (!empty($insert) || !empty($delete)) {
|
||||
$entityIds = array_unique(array_merge(array_keys($insert), array_keys($delete)));
|
||||
$this->eventManager->dispatch(
|
||||
'mageplaza_blog_post_change_products',
|
||||
['post' => $post, 'entity_ids' => $entityIds]
|
||||
);
|
||||
}
|
||||
if (!empty($insert) || !empty($update) || !empty($delete)) {
|
||||
$post->setIsChangedProductList(true);
|
||||
$entityIds = array_keys($insert + $delete + $update);
|
||||
$post->setAffectedEntityIds($entityIds);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostModel $post
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getProductsPosition(PostModel $post)
|
||||
{
|
||||
$select = $this->getConnection()->select()->from(
|
||||
$this->postProductTable,
|
||||
['entity_id', 'position']
|
||||
)
|
||||
->where(
|
||||
'post_id = :post_id'
|
||||
);
|
||||
$bind = ['post_id' => (int) $post->getId()];
|
||||
|
||||
return $this->getConnection()->fetchPairs($select, $bind);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check post url key is exists
|
||||
*
|
||||
* @param string $urlKey
|
||||
*
|
||||
* @return string
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function isDuplicateUrlKey($urlKey)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$select = $adapter->select()
|
||||
->from($this->getMainTable(), 'post_id')
|
||||
->where('url_key = :url_key');
|
||||
$binds = ['url_key' => $urlKey];
|
||||
|
||||
return $adapter->fetchOne($select, $binds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the post author when creating post
|
||||
*/
|
||||
public function saveAuthor()
|
||||
{
|
||||
$currentUser = $this->_auth->getUser();
|
||||
|
||||
if ($currentUser) {
|
||||
$currentUserId = $currentUser->getId();
|
||||
/** @var Author $author */
|
||||
$author = $this->_authorFactory->create()->load($currentUserId);
|
||||
|
||||
/** Create the new author if that author isn't exist */
|
||||
if (!$author->getId()) {
|
||||
$author->setId($currentUserId)
|
||||
->setName($currentUser->getName())->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check is imported post
|
||||
*
|
||||
* @param string $importSource
|
||||
* @param string $oldId
|
||||
*
|
||||
* @return string
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function isImported($importSource, $oldId)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$select = $adapter->select()
|
||||
->from($this->getMainTable(), 'post_id')
|
||||
->where('import_source = :import_source');
|
||||
$binds = ['import_source' => $importSource . '-' . $oldId];
|
||||
|
||||
return $adapter->fetchOne($select, $binds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $importType
|
||||
*
|
||||
* @return int
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function deleteImportItems($importType)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
|
||||
return $adapter->delete($this->getMainTable(), "`import_source` LIKE '" . $importType . "%'");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?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\Model\ResourceModel\Post;
|
||||
|
||||
use Magento\Framework\DB\Select;
|
||||
use Magento\Sales\Model\ResourceModel\Collection\AbstractCollection;
|
||||
use Mageplaza\Blog\Api\Data\SearchResult\PostSearchResultInterface;
|
||||
use Mageplaza\Blog\Model\Post;
|
||||
|
||||
/**
|
||||
* Class Collection
|
||||
* @package Mageplaza\Blog\Model\ResourceModel\Post
|
||||
*/
|
||||
class Collection extends AbstractCollection implements PostSearchResultInterface
|
||||
{
|
||||
/**
|
||||
* ID Field Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_idFieldName = 'post_id';
|
||||
|
||||
/**
|
||||
* Event prefix
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_eventPrefix = 'mageplaza_blog_post_collection';
|
||||
|
||||
/**
|
||||
* Event object
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_eventObject = 'post_collection';
|
||||
|
||||
/**
|
||||
* Define resource model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(Post::class, \Mageplaza\Blog\Model\ResourceModel\Post::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get SQL for get record count.
|
||||
* Extra GROUP BY strip added.
|
||||
*
|
||||
* @return Select
|
||||
*/
|
||||
public function getSelectCountSql()
|
||||
{
|
||||
$countSelect = parent::getSelectCountSql();
|
||||
$countSelect->reset(Select::GROUP);
|
||||
|
||||
return $countSelect;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $valueField
|
||||
* @param string $labelField
|
||||
* @param array $additional
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function _toOptionArray($valueField = null, $labelField = 'name', $additional = [])
|
||||
{
|
||||
$valueField = 'post_id';
|
||||
|
||||
return parent::_toOptionArray($valueField, $labelField, $additional); // TODO: Change the autogenerated stub
|
||||
}
|
||||
}
|
||||
@@ -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\Model\ResourceModel;
|
||||
|
||||
use Magento\Framework\Model\AbstractModel;
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
use Mageplaza\Blog\Helper\Data;
|
||||
|
||||
/**
|
||||
* Class PostLike
|
||||
* @package Mageplaza\Blog\Model\ResourceModel
|
||||
*/
|
||||
class PostHistory extends AbstractDb
|
||||
{
|
||||
/**
|
||||
* Initialize resource model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init('mageplaza_blog_post_history', 'history_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AbstractModel $object
|
||||
*
|
||||
* @return AbstractDb
|
||||
*/
|
||||
protected function _beforeSave(AbstractModel $object)
|
||||
{
|
||||
if (is_array($object->getData('store_ids'))) {
|
||||
$object->setData('store_ids', implode(',', $object->getData('store_ids')));
|
||||
}
|
||||
if (is_array($object->getData('categories_ids'))) {
|
||||
$object->setData('category_ids', implode(',', $object->getData('categories_ids')));
|
||||
}
|
||||
if (is_array($object->getData('topics_ids'))) {
|
||||
$object->setData('topic_ids', implode(',', $object->getData('topics_ids')));
|
||||
}
|
||||
if (is_array($object->getData('tags_ids'))) {
|
||||
$object->setData('tag_ids', implode(',', $object->getData('tags_ids')));
|
||||
}
|
||||
if (is_array($object->getData('products_data'))) {
|
||||
$data = $object->getData('products_data');
|
||||
foreach ($data as $key => $datum) {
|
||||
$data[$key]['position'] = $datum['position'] ?: '0';
|
||||
}
|
||||
|
||||
$object->setData('product_ids', Data::jsonEncode($data));
|
||||
}
|
||||
|
||||
return parent::_beforeSave($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function _afterLoad(AbstractModel $object)
|
||||
{
|
||||
$object->setData('categories_ids', explode(',', $object->getCategoryIds() ?? ''));
|
||||
$object->setData('tags_ids', explode(',', $object->getTagIds() ?? ''));
|
||||
$object->setData('topics_ids', explode(',', $object->getTopicIds() ?? ''));
|
||||
|
||||
return parent::_afterLoad($object);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
<?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\Model\ResourceModel\PostHistory;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeZone;
|
||||
use Exception;
|
||||
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface;
|
||||
use Magento\Framework\Data\Collection\EntityFactoryInterface;
|
||||
use Magento\Framework\DB\Adapter\AdapterInterface;
|
||||
use Magento\Framework\DB\Select;
|
||||
use Magento\Framework\Event\ManagerInterface;
|
||||
use Magento\Framework\Locale\ResolverInterface;
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
|
||||
use Mageplaza\Blog\Model\PostHistory;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Class Collection
|
||||
* @package Mageplaza\Blog\Model\ResourceModel\PostLike
|
||||
*/
|
||||
class Collection extends AbstractCollection
|
||||
{
|
||||
/**
|
||||
* @var TimezoneInterface
|
||||
*/
|
||||
private $localeDate;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $locale;
|
||||
|
||||
/**
|
||||
* Collection constructor.
|
||||
*
|
||||
* @param TimezoneInterface $localeDate
|
||||
* @param ResolverInterface $localeResolver
|
||||
* @param EntityFactoryInterface $entityFactory
|
||||
* @param LoggerInterface $logger
|
||||
* @param FetchStrategyInterface $fetchStrategy
|
||||
* @param ManagerInterface $eventManager
|
||||
* @param AdapterInterface|null $connection
|
||||
* @param AbstractDb|null $resource
|
||||
*/
|
||||
public function __construct(
|
||||
TimezoneInterface $localeDate,
|
||||
ResolverInterface $localeResolver,
|
||||
EntityFactoryInterface $entityFactory,
|
||||
LoggerInterface $logger,
|
||||
FetchStrategyInterface $fetchStrategy,
|
||||
ManagerInterface $eventManager,
|
||||
?AdapterInterface $connection = null,
|
||||
?AbstractDb $resource = null
|
||||
) {
|
||||
$this->localeDate = $localeDate;
|
||||
$this->locale = $localeResolver->getLocale();
|
||||
|
||||
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Define model & resource model
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(PostHistory::class, \Mageplaza\Blog\Model\ResourceModel\PostHistory::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add field filter to collection
|
||||
*
|
||||
* @param string|array $field
|
||||
* @param null|string|array $condition
|
||||
*
|
||||
* @return \Magento\Framework\Data\Collection\AbstractDb
|
||||
* @see self::_getConditionSql for $condition
|
||||
*
|
||||
*/
|
||||
public function addFieldToFilter($field, $condition = null)
|
||||
{
|
||||
if (is_array($field)) {
|
||||
$conditions = [];
|
||||
foreach ($field as $key => $value) {
|
||||
$conditions[] = $this->_translateCondition($value, isset($condition[$key]) ? $condition[$key] : null);
|
||||
}
|
||||
|
||||
$resultCondition = '(' . implode(') ' . \Magento\Framework\DB\Select::SQL_OR . ' (', $conditions) . ')';
|
||||
} else {
|
||||
$resultCondition = $this->_translateCondition($field, $condition);
|
||||
}
|
||||
|
||||
$this->_select->where($resultCondition, null, Select::TYPE_CONDITION);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function _afterLoad()
|
||||
{
|
||||
foreach ($this->getItems() as $item) {
|
||||
$convertedDate = $this->localeDate->date(
|
||||
new DateTime($item->getData('updated_at'), new DateTimeZone('UTC')),
|
||||
$this->locale,
|
||||
true
|
||||
);
|
||||
$item->setData('updated_at', $convertedDate->format('M j, Y h:i:s A'));
|
||||
}
|
||||
|
||||
return parent::_afterLoad();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?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\Model\ResourceModel;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
|
||||
/**
|
||||
* Class PostLike
|
||||
* @package Mageplaza\Blog\Model\ResourceModel
|
||||
*/
|
||||
class PostLike extends AbstractDb
|
||||
{
|
||||
/**
|
||||
* Initialize resource model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init('mageplaza_blog_post_like', 'like_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?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\Model\ResourceModel\PostLike;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
use Mageplaza\Blog\Model\PostLike;
|
||||
|
||||
/**
|
||||
* Class Collection
|
||||
* @package Mageplaza\Blog\Model\ResourceModel\PostLike
|
||||
*/
|
||||
class Collection extends AbstractCollection
|
||||
{
|
||||
/**
|
||||
* Define model & resource model
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(PostLike::class, \Mageplaza\Blog\Model\ResourceModel\PostLike::class);
|
||||
}
|
||||
}
|
||||
+293
@@ -0,0 +1,293 @@
|
||||
<?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\Model\ResourceModel;
|
||||
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Framework\Event\ManagerInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Model\AbstractModel;
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
use Magento\Framework\Model\ResourceModel\Db\Context;
|
||||
use Magento\Framework\Stdlib\DateTime\DateTime;
|
||||
use Mageplaza\Blog\Helper\Data;
|
||||
|
||||
/**
|
||||
* Class Tag
|
||||
* @package Mageplaza\Blog\Model\ResourceModel
|
||||
*/
|
||||
class Tag extends AbstractDb
|
||||
{
|
||||
/**
|
||||
* Date model
|
||||
*
|
||||
* @var DateTime
|
||||
*/
|
||||
public $date;
|
||||
|
||||
/**
|
||||
* Event Manager
|
||||
*
|
||||
* @var ManagerInterface
|
||||
*/
|
||||
public $eventManager;
|
||||
|
||||
/**
|
||||
* Post relation model
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $tagPostTable;
|
||||
|
||||
/**
|
||||
* @var Data
|
||||
*/
|
||||
public $helperData;
|
||||
|
||||
/**
|
||||
* @var RequestInterface
|
||||
*/
|
||||
private $request;
|
||||
|
||||
/**
|
||||
* Tag constructor.
|
||||
*
|
||||
* @param Context $context
|
||||
* @param ManagerInterface $eventManager
|
||||
* @param DateTime $date
|
||||
* @param RequestInterface $request
|
||||
* @param Data $helperData
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
ManagerInterface $eventManager,
|
||||
DateTime $date,
|
||||
RequestInterface $request,
|
||||
Data $helperData
|
||||
) {
|
||||
$this->helperData = $helperData;
|
||||
$this->date = $date;
|
||||
$this->eventManager = $eventManager;
|
||||
$this->request = $request;
|
||||
|
||||
parent::__construct($context);
|
||||
|
||||
$this->tagPostTable = $this->getTable('mageplaza_blog_post_tag');
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize resource model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init('mageplaza_blog_tag', 'tag_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves Tag Name from DB by passed id.
|
||||
*
|
||||
* @param $id
|
||||
*
|
||||
* @return string
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function getTagNameById($id)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$select = $adapter->select()
|
||||
->from($this->getMainTable(), 'name')
|
||||
->where('tag_id = :tag_id');
|
||||
$binds = ['tag_id' => (int)$id];
|
||||
|
||||
return $adapter->fetchOne($select, $binds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function _beforeSave(AbstractModel $object)
|
||||
{
|
||||
$object->setUpdatedAt($this->date->date());
|
||||
if ($object->isObjectNew()) {
|
||||
$object->setCreatedAt($this->date->date());
|
||||
}
|
||||
|
||||
if (is_array($object->getStoreIds())) {
|
||||
$object->setStoreIds(implode(',', $object->getStoreIds()));
|
||||
}
|
||||
|
||||
$object->setUrlKey(
|
||||
$this->helperData->generateUrlKey($this, $object, $object->getUrlKey() ?: $object->getName())
|
||||
);
|
||||
|
||||
return parent::_beforeSave($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function _afterSave(AbstractModel $object)
|
||||
{
|
||||
$this->savePostRelation($object);
|
||||
|
||||
return parent::_afterSave($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Mageplaza\Blog\Model\Tag $tag
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getPostsPosition(\Mageplaza\Blog\Model\Tag $tag)
|
||||
{
|
||||
$select = $this->getConnection()->select()
|
||||
->from($this->tagPostTable, ['post_id', 'position'])
|
||||
->where('tag_id = :tag_id');
|
||||
|
||||
$bind = ['tag_id' => (int)$tag->getId()];
|
||||
|
||||
return $this->getConnection()->fetchPairs($select, $bind);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Mageplaza\Blog\Model\Tag $tag
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function savePostRelation(\Mageplaza\Blog\Model\Tag $tag)
|
||||
{
|
||||
$tag->setIsChangedPostList(false);
|
||||
$id = $tag->getId();
|
||||
$posts = $tag->getPostsData();
|
||||
$oldPosts = $tag->getPostsPosition();
|
||||
if (is_array($posts)) {
|
||||
$insert = array_diff_key($posts, $oldPosts);
|
||||
$delete = array_diff_key($oldPosts, $posts);
|
||||
$update = array_intersect_key($posts, $oldPosts);
|
||||
$_update = [];
|
||||
foreach ($update as $key => $settings) {
|
||||
if (isset($oldPosts[$key]) && $oldPosts[$key] != $settings['position']) {
|
||||
$_update[$key] = $settings;
|
||||
}
|
||||
}
|
||||
$update = $_update;
|
||||
}
|
||||
$adapter = $this->getConnection();
|
||||
if ($posts === null && $this->request->getActionName() === 'save') {
|
||||
foreach (array_keys($oldPosts) as $value) {
|
||||
$condition = ['post_id =?' => (int)$value, 'tag_id=?' => (int)$id];
|
||||
$adapter->delete($this->tagPostTable, $condition);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
if (!empty($delete)) {
|
||||
foreach (array_keys($delete) as $value) {
|
||||
$condition = ['post_id =?' => (int)$value, 'tag_id=?' => (int)$id];
|
||||
$adapter->delete($this->tagPostTable, $condition);
|
||||
}
|
||||
}
|
||||
if (!empty($insert)) {
|
||||
$data = [];
|
||||
foreach ($insert as $postId => $position) {
|
||||
$data[] = [
|
||||
'tag_id' => (int)$id,
|
||||
'post_id' => (int)$postId,
|
||||
'position' => (int)$position['position']
|
||||
];
|
||||
}
|
||||
$adapter->insertMultiple($this->tagPostTable, $data);
|
||||
}
|
||||
if (!empty($update)) {
|
||||
foreach ($update as $postId => $position) {
|
||||
$where = ['tag_id = ?' => (int)$id, 'post_id = ?' => (int)$postId];
|
||||
$bind = ['position' => (int)$position['position']];
|
||||
$adapter->update($this->tagPostTable, $bind, $where);
|
||||
}
|
||||
}
|
||||
if (!empty($insert) || !empty($delete)) {
|
||||
$postIds = array_unique(array_merge(array_keys($insert), array_keys($delete)));
|
||||
$this->eventManager->dispatch(
|
||||
'mageplaza_blog_tag_change_posts',
|
||||
['tag' => $tag, 'post_ids' => $postIds]
|
||||
);
|
||||
}
|
||||
if (!empty($insert) || !empty($update) || !empty($delete)) {
|
||||
$tag->setIsChangedPostList(true);
|
||||
$postIds = array_keys($insert + $delete + $update);
|
||||
$tag->setAffectedPostIds($postIds);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check category url key is exists
|
||||
*
|
||||
* @param $urlKey
|
||||
*
|
||||
* @return string
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function isDuplicateUrlKey($urlKey)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$select = $adapter->select()
|
||||
->from($this->getMainTable(), 'tag_id')
|
||||
->where('url_key = :url_key');
|
||||
$binds = ['url_key' => $urlKey];
|
||||
|
||||
return $adapter->fetchOne($select, $binds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check is import tag
|
||||
*
|
||||
* @param $importSource
|
||||
* @param $oldId
|
||||
*
|
||||
* @return string
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function isImported($importSource, $oldId)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$select = $adapter->select()
|
||||
->from($this->getMainTable(), 'tag_id')
|
||||
->where('import_source = :import_source');
|
||||
$binds = ['import_source' => $importSource . '-' . $oldId];
|
||||
|
||||
return $adapter->fetchOne($select, $binds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $importType
|
||||
*
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function deleteImportItems($importType)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$adapter->delete($this->getMainTable(), "`import_source` LIKE '" . $importType . "%'");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
<?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\Model\ResourceModel\Tag;
|
||||
|
||||
use Magento\Framework\DB\Select;
|
||||
use Magento\Sales\Model\ResourceModel\Collection\AbstractCollection;
|
||||
use Mageplaza\Blog\Api\Data\SearchResult\TagSearchResultInterface;
|
||||
use Mageplaza\Blog\Model\Tag;
|
||||
use Mageplaza\Blog\Model\ResourceModel\Tag as TagResourceModel;
|
||||
|
||||
/**
|
||||
* Class Collection
|
||||
* @package Mageplaza\Blog\Model\ResourceModel\Tag
|
||||
*/
|
||||
class Collection extends AbstractCollection implements TagSearchResultInterface
|
||||
{
|
||||
/**
|
||||
* ID Field Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_idFieldName = 'tag_id';
|
||||
|
||||
/**
|
||||
* Event prefix
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_eventPrefix = 'mageplaza_blog_tag_collection';
|
||||
|
||||
/**
|
||||
* Event object
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_eventObject = 'tag_collection';
|
||||
|
||||
/**
|
||||
* Define resource model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(Tag::class, TagResourceModel::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get SQL for get record count.
|
||||
* Extra GROUP BY strip added.
|
||||
*
|
||||
* @return Select
|
||||
*/
|
||||
public function getSelectCountSql()
|
||||
{
|
||||
$countSelect = parent::getSelectCountSql();
|
||||
$countSelect->reset(Select::GROUP);
|
||||
|
||||
return $countSelect;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $valueField
|
||||
* @param string $labelField
|
||||
* @param array $additional
|
||||
* @return array
|
||||
*/
|
||||
protected function _toOptionArray($valueField = null, $labelField = 'name', $additional = [])
|
||||
{
|
||||
$valueField = 'tag_id';
|
||||
|
||||
return parent::_toOptionArray($valueField, $labelField, $additional); // TODO: Change the autogenerated stub
|
||||
}
|
||||
|
||||
/**
|
||||
* Add if filter
|
||||
*
|
||||
* @param array|int|string $tagIds
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addIdFilter($tagIds)
|
||||
{
|
||||
$condition = '';
|
||||
|
||||
if (is_array($tagIds)) {
|
||||
if (!empty($tagIds)) {
|
||||
$condition = ['in' => $tagIds];
|
||||
}
|
||||
} elseif (is_numeric($tagIds)) {
|
||||
$condition = $tagIds;
|
||||
} elseif (is_string($tagIds)) {
|
||||
$ids = explode(',', $tagIds);
|
||||
if (empty($ids)) {
|
||||
$condition = $tagIds;
|
||||
} else {
|
||||
$condition = ['in' => $ids];
|
||||
}
|
||||
}
|
||||
|
||||
if ($condition) {
|
||||
$this->addFieldToFilter('tag_id', $condition);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
+279
@@ -0,0 +1,279 @@
|
||||
<?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\Model\ResourceModel;
|
||||
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Framework\Event\ManagerInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Model\AbstractModel;
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
use Magento\Framework\Model\ResourceModel\Db\Context;
|
||||
use Magento\Framework\Stdlib\DateTime\DateTime;
|
||||
use Mageplaza\Blog\Helper\Data;
|
||||
|
||||
/**
|
||||
* Class Topic
|
||||
* @package Mageplaza\Blog\Model\ResourceModel
|
||||
*/
|
||||
class Topic extends AbstractDb
|
||||
{
|
||||
/**
|
||||
* Date model
|
||||
*
|
||||
* @var DateTime
|
||||
*/
|
||||
public $date;
|
||||
|
||||
/**
|
||||
* Event Manager
|
||||
*
|
||||
* @var ManagerInterface
|
||||
*/
|
||||
public $eventManager;
|
||||
|
||||
/**
|
||||
* Post relation model
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $topicPostTable;
|
||||
|
||||
/**
|
||||
* @var Data
|
||||
*/
|
||||
public $helperData;
|
||||
|
||||
/**
|
||||
* @var RequestInterface
|
||||
*/
|
||||
private $request;
|
||||
|
||||
/**
|
||||
* Topic constructor.
|
||||
*
|
||||
* @param Context $context
|
||||
* @param DateTime $date
|
||||
* @param ManagerInterface $eventManager
|
||||
* @param RequestInterface $request
|
||||
* @param Data $helperData
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
DateTime $date,
|
||||
ManagerInterface $eventManager,
|
||||
RequestInterface $request,
|
||||
Data $helperData
|
||||
) {
|
||||
$this->helperData = $helperData;
|
||||
$this->date = $date;
|
||||
$this->eventManager = $eventManager;
|
||||
$this->request = $request;
|
||||
|
||||
parent::__construct($context);
|
||||
|
||||
$this->topicPostTable = $this->getTable('mageplaza_blog_post_topic');
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize resource model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init('mageplaza_blog_topic', 'topic_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves Topic Name from DB by passed id.
|
||||
*
|
||||
* @param $id
|
||||
*
|
||||
* @return string
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function getTopicNameById($id)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$select = $adapter->select()
|
||||
->from($this->getMainTable(), 'name')
|
||||
->where('topic_id = :topic_id');
|
||||
$binds = ['topic_id' => (int)$id];
|
||||
|
||||
return $adapter->fetchOne($select, $binds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function _beforeSave(AbstractModel $object)
|
||||
{
|
||||
$object->setUpdatedAt($this->date->date());
|
||||
if ($object->isObjectNew()) {
|
||||
$object->setCreatedAt($this->date->date());
|
||||
}
|
||||
|
||||
if (is_array($object->getStoreIds())) {
|
||||
$object->setStoreIds(implode(',', $object->getStoreIds()));
|
||||
}
|
||||
|
||||
$object->setUrlKey(
|
||||
$this->helperData->generateUrlKey($this, $object, $object->getUrlKey() ?: $object->getName())
|
||||
);
|
||||
|
||||
return parent::_beforeSave($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AbstractModel $object
|
||||
*
|
||||
* @return AbstractDb
|
||||
*/
|
||||
protected function _afterSave(AbstractModel $object)
|
||||
{
|
||||
$this->savePostRelation($object);
|
||||
|
||||
return parent::_afterSave($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Mageplaza\Blog\Model\Topic $topic
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getPostsPosition(\Mageplaza\Blog\Model\Topic $topic)
|
||||
{
|
||||
$select = $this->getConnection()->select()->from(
|
||||
$this->topicPostTable,
|
||||
['post_id', 'position']
|
||||
)
|
||||
->where(
|
||||
'topic_id = :topic_id'
|
||||
);
|
||||
$bind = ['topic_id' => (int)$topic->getId()];
|
||||
|
||||
return $this->getConnection()->fetchPairs($select, $bind);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Mageplaza\Blog\Model\Topic $topic
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function savePostRelation(\Mageplaza\Blog\Model\Topic $topic)
|
||||
{
|
||||
$topic->setIsChangedPostList(false);
|
||||
$id = $topic->getId();
|
||||
$posts = $topic->getPostsData();
|
||||
$oldPosts = $topic->getPostsPosition();
|
||||
if (is_array($posts)) {
|
||||
$insert = array_diff_key($posts, $oldPosts);
|
||||
$delete = array_diff_key($oldPosts, $posts);
|
||||
$update = array_intersect_key($posts, $oldPosts);
|
||||
$_update = [];
|
||||
foreach ($update as $key => $settings) {
|
||||
if (isset($oldPosts[$key]) && $oldPosts[$key] != $settings['position']) {
|
||||
$_update[$key] = $settings;
|
||||
}
|
||||
}
|
||||
$update = $_update;
|
||||
}
|
||||
$adapter = $this->getConnection();
|
||||
if ($posts === null && $this->request->getActionName() === 'save') {
|
||||
foreach (array_keys($oldPosts) as $value) {
|
||||
$condition = ['post_id =?' => (int)$value, 'topic_id=?' => (int)$id];
|
||||
$adapter->delete($this->topicPostTable, $condition);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
if (!empty($delete)) {
|
||||
foreach (array_keys($delete) as $value) {
|
||||
$condition = ['post_id =?' => (int)$value, 'topic_id=?' => (int)$id];
|
||||
$adapter->delete($this->topicPostTable, $condition);
|
||||
}
|
||||
}
|
||||
if (!empty($insert)) {
|
||||
$data = [];
|
||||
foreach ($insert as $postId => $position) {
|
||||
$data[] = [
|
||||
'topic_id' => (int)$id,
|
||||
'post_id' => (int)$postId,
|
||||
'position' => (int)$position['position']
|
||||
];
|
||||
}
|
||||
$adapter->insertMultiple($this->topicPostTable, $data);
|
||||
}
|
||||
if (!empty($update)) {
|
||||
foreach ($update as $postId => $position) {
|
||||
$where = ['topic_id = ?' => (int)$id, 'post_id = ?' => (int)$postId];
|
||||
$bind = ['position' => (int)$position['position']];
|
||||
$adapter->update($this->topicPostTable, $bind, $where);
|
||||
}
|
||||
}
|
||||
if (!empty($insert) || !empty($delete)) {
|
||||
$postIds = array_unique(array_merge(array_keys($insert), array_keys($delete)));
|
||||
$this->eventManager->dispatch(
|
||||
'mageplaza_blog_topic_change_posts',
|
||||
['topic' => $topic, 'post_ids' => $postIds]
|
||||
);
|
||||
}
|
||||
if (!empty($insert) || !empty($update) || !empty($delete)) {
|
||||
$topic->setIsChangedPostList(true);
|
||||
$postIds = array_keys($insert + $delete + $update);
|
||||
$topic->setAffectedPostIds($postIds);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check is import topic
|
||||
*
|
||||
* @param $importSource
|
||||
* @param $oldId
|
||||
*
|
||||
* @return string
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function isImported($importSource, $oldId)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$select = $adapter->select()
|
||||
->from($this->getMainTable(), 'topic_id')
|
||||
->where('import_source = :import_source');
|
||||
$binds = ['import_source' => $importSource . '-' . $oldId];
|
||||
|
||||
return $adapter->fetchOne($select, $binds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $importType
|
||||
*
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function deleteImportItems($importType)
|
||||
{
|
||||
$adapter = $this->getConnection();
|
||||
$adapter->delete($this->getMainTable(), "`import_source` LIKE '" . $importType . "%'");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
<?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\Model\ResourceModel\Topic;
|
||||
|
||||
use Magento\Framework\DB\Select;
|
||||
use Magento\Sales\Model\ResourceModel\Collection\AbstractCollection;
|
||||
use Mageplaza\Blog\Api\Data\SearchResult\TopicSearchResultInterface;
|
||||
use Mageplaza\Blog\Model\Topic;
|
||||
use Mageplaza\Blog\Model\ResourceModel\Topic as TopicResourceModel;
|
||||
|
||||
/**
|
||||
* Class Collection
|
||||
* @package Mageplaza\Blog\Model\ResourceModel\Topic
|
||||
*/
|
||||
class Collection extends AbstractCollection implements TopicSearchResultInterface
|
||||
{
|
||||
/**
|
||||
* ID Field Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_idFieldName = 'topic_id';
|
||||
|
||||
/**
|
||||
* Event prefix
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_eventPrefix = 'mageplaza_blog_topic_collection';
|
||||
|
||||
/**
|
||||
* Event object
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_eventObject = 'topic_collection';
|
||||
|
||||
/**
|
||||
* Define resource model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(Topic::class, TopicResourceModel::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get SQL for get record count.
|
||||
* Extra GROUP BY strip added.
|
||||
*
|
||||
* @return Select
|
||||
*/
|
||||
public function getSelectCountSql()
|
||||
{
|
||||
$countSelect = parent::getSelectCountSql();
|
||||
$countSelect->reset(Select::GROUP);
|
||||
|
||||
return $countSelect;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $valueField
|
||||
* @param string $labelField
|
||||
* @param array $additional
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function _toOptionArray($valueField = null, $labelField = 'name', $additional = [])
|
||||
{
|
||||
$valueField = 'topic_id';
|
||||
|
||||
return parent::_toOptionArray($valueField, $labelField, $additional); // TODO: Change the autogenerated stub
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $topicIds
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addIdFilter($topicIds)
|
||||
{
|
||||
$condition = '';
|
||||
|
||||
if (is_array($topicIds)) {
|
||||
if (!empty($topicIds)) {
|
||||
$condition = ['in' => $topicIds];
|
||||
}
|
||||
} elseif (is_numeric($topicIds)) {
|
||||
$condition = $topicIds;
|
||||
} elseif (is_string($topicIds)) {
|
||||
$ids = explode(',', $topicIds);
|
||||
if (empty($ids)) {
|
||||
$condition = $topicIds;
|
||||
} else {
|
||||
$condition = ['in' => $ids];
|
||||
}
|
||||
}
|
||||
|
||||
if ($condition) {
|
||||
$this->addFieldToFilter('topic_id', $condition);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
<?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\Model\ResourceModel;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
|
||||
/**
|
||||
* Class Traffic
|
||||
* @package Mageplaza\Blog\Model\ResourceModel
|
||||
*/
|
||||
class Traffic extends AbstractDb
|
||||
{
|
||||
/**
|
||||
* Define main table
|
||||
*/
|
||||
public function _construct()
|
||||
{
|
||||
$this->_init('mageplaza_blog_post_traffic', 'traffic_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?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\Model\ResourceModel\Traffic;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
use Mageplaza\Blog\Model\Traffic;
|
||||
use Mageplaza\Blog\Model\ResourceModel\Traffic as TrafficResourceModel;
|
||||
|
||||
/**
|
||||
* Class Collection
|
||||
* @package Mageplaza\Blog\Model\ResourceModel\Traffic
|
||||
*/
|
||||
class Collection extends AbstractCollection
|
||||
{
|
||||
/**
|
||||
* Define model & resource model
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(Traffic::class, TrafficResourceModel::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user