Skip to content

Release 2.13 #670

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
3 changes: 2 additions & 1 deletion Api/ManagementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ public function delete($id);
*
* @api
* @param int $id
* @param int|null $storeId
* @return bool
*/
public function get($id);
public function get($id, $storeId = null);

/**
* Get item by id and store id, only if item published
Expand Down
22 changes: 20 additions & 2 deletions Model/AbstractManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,20 @@ public function create($data)
public function update($id, $data)
{
try {
$data = json_decode($data, true);
$item = $this->_itemFactory->create();

if (!empty($data['store_id'])) {
$item->setStoreId((int)$data['store_id']);
$item->setData('data_to_update', $data);
}

$item->load($id);

if (!$item->getId()) {
return false;
}
$data = json_decode($data, true);

foreach ($this->_imagesMap as $key) {
if (empty($data[$key . '_name']) || empty($data[$key . '_content'])) {
unset($data[$key . '_name']);
Expand Down Expand Up @@ -162,12 +169,18 @@ public function delete($id)
* Get item by id
*
* @param int $id
* @param int|null $storeId
* @return bool
*/
public function get($id)
public function get($id, $storeId = 0)
{
try {
$item = $this->_itemFactory->create();

if ($storeId) {
$item->setStoreId((int)$storeId);
}

$item->load($id);

if (!$item->getId()) {
Expand All @@ -190,6 +203,11 @@ public function view($id, $storeId)
{
try {
$item = $this->_itemFactory->create();

if ($storeId) {
$item->setStoreId((int)$storeId);
}

$item->getResource()->load($item, $id);

if (!$item->isVisibleOnStore($storeId)) {
Expand Down
5 changes: 5 additions & 0 deletions Model/AuthorRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ public function getById($authorId, $editMode = false, $storeId = null, $forceRel
$cacheKey = implode('_', func_get_args());
if (!isset($this->instances[$cacheKey])) {
$author = $this->authorFactory->create();

if ($storeId) {
$author->setStoreId($storeId);
}

$this->authorResourceModel->load($author, $authorId);
if (!$author->getId()) {
throw new NoSuchEntityException(__('Requested item doesn\'t exist'));
Expand Down
5 changes: 5 additions & 0 deletions Model/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public function getById($categoryId, $editMode = false, $storeId = null, $forceR
$cacheKey = implode('_', func_get_args());
if (!isset($this->instances[$cacheKey])) {
$category = $this->categoryFactory->create();

if ($storeId) {
$category->setStoreId($storeId);
}

$this->categoryResourceModel->load($category, $categoryId);
if (!$category->getId()) {
throw new NoSuchEntityException(__('Requested item doesn\'t exist'));
Expand Down
6 changes: 6 additions & 0 deletions Model/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,13 @@ public function save(Post $post)
public function getById($postId, $editMode = false, $storeId = null, $forceReload = false)
{
$post = $this->postFactory->create();

if ($storeId) {
$post->setStoreId($storeId);
}

$this->postResourceModel->load($post, $postId);

if (!$post->getId()) {
throw new NoSuchEntityException(__('Requested item doesn\'t exist'));
}
Expand Down
8 changes: 8 additions & 0 deletions Model/ResourceModel/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,12 @@ public function lookupStoreIds($categoryId, $useCache = true)

return [];
}

/**
* @return string
*/
public function getEntityType()
{
return 'category';
}
}
10 changes: 10 additions & 0 deletions Model/ResourceModel/Category/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
*/
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
/**
* @inheritDoc
*/
protected $_eventPrefix = 'mfblog_category_collection';

/**
* @inheritDoc
*/
protected $_eventObject = 'blog_category_collection';

/**
* @inheritDoc
*/
Expand Down
8 changes: 8 additions & 0 deletions Model/ResourceModel/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,4 +527,12 @@ protected function _lookupAll($postId, $tableName, $field)

return $adapter->fetchAll($select);
}

/**
* @return string
*/
public function getEntityType()
{
return 'post';
}
}
29 changes: 18 additions & 11 deletions Model/ResourceModel/Post/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
*/
protected $_eventObject = 'blog_post_collection';

/**
* @var string[]
*/
protected $_ftiCollumns = ['title', 'meta_keywords', 'meta_description', 'identifier', 'content'];

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
Expand Down Expand Up @@ -103,7 +108,6 @@ protected function _construct()
$this->_map['fields']['store'] = 'store_table.store_id';
$this->_map['fields']['category'] = 'category_table.category_id';
$this->_map['fields']['tag'] = 'tag_table.tag_id';
$this->_map['fields']['tag'] = 'tag_table.tag_id';
$this->_map['fields']['relatedproduct'] = 'relatedproduct_table.related_id';
}

Expand Down Expand Up @@ -401,10 +405,7 @@ public function addSearchFilter($term)
$tagPostIds = array_slice($tagPostIds, 0, 200);
}

$fullExpression = '(0 ' .
'+ FORMAT(MATCH (title, meta_keywords, meta_description, identifier, content) AGAINST ('
. $this->getConnection()->quote($term)
. '), 4) ' .
$fullExpression = $this->getSearchRateExpression($term, $this->_ftiCollumns) .
'+ IF(main_table.post_id IN (' . implode(',', $tagPostIds) . '), "1", "0"))';

$fullExpression = new \Zend_Db_Expr($fullExpression);
Expand All @@ -420,19 +421,25 @@ public function addSearchFilter($term)
]
);

$fullExpression = '(0 ' .
'+ FORMAT(MATCH (title, meta_keywords, meta_description, identifier, content) AGAINST ('
. $this->getConnection()->quote($term)
. '), 4))';

$fullExpression = new \Zend_Db_Expr($fullExpression);
$fullExpression = new \Zend_Db_Expr($this->getSearchRateExpression($term, $this->_ftiCollumns));
$this->getSelect()->columns(['search_rate' => $fullExpression]);

//$this->expressionFieldsToSelect['search_rate'] = $fullExpression;
}

return $this;
}

/**
* @param $term
* @param array $columns
* @return string
*/
public function getSearchRateExpression($term, array $columns): string
{
return '(0 + FORMAT(MATCH (' . implode(',', $columns) . ') AGAINST (' . $this->getConnection()->quote($term) . '), 4)) ';
}

/**
* Add tag filter to collection
* @param array|int|string|\Magefan\Blog\Model\Tag $tag
Expand Down
36 changes: 23 additions & 13 deletions Model/ResourceModel/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,21 @@ protected function _beforeDelete(\Magento\Framework\Model\AbstractModel $object)
*/
protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
{
$object->setTitle(
trim(($object->getTitle()))
);

$tag = $object->getCollection()
->addFieldToFilter('title', $object->getTitle())
->addFieldToFilter('tag_id', ['neq' => $object->getId()])
->setPageSize(1)
->getFirstItem();
if ($tag->getId()) {
throw new \Magento\Framework\Exception\LocalizedException(
__('The tag is already exist.')
if ($object->getTitle()) {
$object->setTitle(
trim(($object->getTitle()))
);

$tag = $object->getCollection()
->addFieldToFilter('title', $object->getTitle())
->addFieldToFilter('tag_id', ['neq' => $object->getId()])
->setPageSize(1)
->getFirstItem();
if ($tag->getId()) {
throw new \Magento\Framework\Exception\LocalizedException(
__('The tag is already exist.')
);
}
}

$identifierGenerator = \Magento\Framework\App\ObjectManager::getInstance()
Expand Down Expand Up @@ -180,7 +182,7 @@ public function checkIdentifier($identifier, $storeIds)
$select = $this->_getLoadByIdentifierSelect($identifier, $storeIds);
$select->reset(\Zend_Db_Select::COLUMNS)->columns(['cp.tag_id', 'cp.identifier'])->order('cps.store_id DESC')->limit(1);



$row = $this->getConnection()->fetchRow($select);
if (isset($row['tag_id']) && isset($row['identifier'])
Expand Down Expand Up @@ -338,4 +340,12 @@ protected function _lookupAll($tagId, $tableName, $field)

return $adapter->fetchAll($select);
}

/**
* @return string
*/
public function getEntityType()
{
return 'tag';
}
}
10 changes: 10 additions & 0 deletions Model/ResourceModel/Tag/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
*/
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
/**
* @inheritDoc
*/
protected $_eventPrefix = 'mfblog_tag_collection';

/**
* @inheritDoc
*/
protected $_eventObject = 'blog_tag_collection';

/**
* @inheritDoc
*/
Expand Down
5 changes: 5 additions & 0 deletions Model/TagRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public function save(Tag $tag)
public function getById($tagId, $editMode = false, $storeId = null, $forceReload = false)
{
$tag = $this->tagFactory->create();

if ($storeId) {
$tag->setStoreId($storeId);
}

$this->tagResourceModel->load($tag, $tagId);
if (!$tag->getId()) {
throw new NoSuchEntityException(__('Requested item doesn\'t exist'));
Expand Down
Loading