Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 62 additions & 18 deletions Block/Post/View/NextPrev.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,38 @@ public function getPrevPost()
{
if ($this->_prevPost === null) {
$this->_prevPost = false;
$currentPost = $this->getPost();
$collection = $this->_getFrontendCollection()->addFieldToFilter(
'publish_time',
[
'gteq' => $this->getPost()->getPublishTime()
]
)
->setOrder('publish_time', 'ASC')
->setPageSize(1);

$post = $collection->getFirstItem();
['gteq' => $currentPost->getPublishTime()]
)->setOrder('publish_time', 'ASC');

$post = $collection->getFirstItem();

if ($currentPost->getPublishTime() == $post->getPublishTime()) {

$collection = $this->_postCollectionFactory->create();
$collection->addActiveFilter()
->addStoreFilter($this->_storeManager->getStore()->getId())
->addFieldToFilter('publish_time', $currentPost->getPublishTime())
->setOrder('post_id', 'DESC');

if ($collection->getFirstItem()->getId() != $currentPost->getId()) {
foreach ($collection as $item) {
if ($item->getId() != $currentPost->getId()) {
$post = $item;
} else {
break;
}
}
} else {
$collection = $this->_getFrontendCollection()->addFieldToFilter(
'publish_time',
['gt' => $this->getPost()->getPublishTime()]
);
$post = $collection->getFirstItem();
}
}

if ($post->getId()) {
$this->_prevPost = $post;
Expand All @@ -106,25 +128,47 @@ public function getNextPost()
{
if ($this->_nextPost === null) {
$this->_nextPost = false;
$currentPost = $this->getPost();
$collection = $this->_getFrontendCollection()->addFieldToFilter(
'publish_time',
[
'lteq' => $this->getPost()->getPublishTime()
]
)
->setOrder('publish_time', 'DESC')
->setPageSize(1);

$post = $collection->getFirstItem();
['lteq' => $currentPost->getPublishTime()]
)->setOrder('publish_time', 'DESC');

$post = $collection->getFirstItem();

if ($currentPost->getPublishTime() == $post->getPublishTime()) {

$collection = $this->_postCollectionFactory->create();
$collection->addActiveFilter()
->addStoreFilter($this->_storeManager->getStore()->getId())
->addFieldToFilter('publish_time', $currentPost->getPublishTime())
->setOrder('post_id', 'ASC');

if ($collection->getFirstItem()->getId() != $currentPost->getId()) {
foreach ($collection as $item) {
if ($item->getId() != $currentPost->getId()) {
$post = $item;
} else {
break;
}
}
} else {
$collection = $this->_getFrontendCollection()->addFieldToFilter(
'publish_time',
['lt' => $this->getPost()->getPublishTime()]
);
$post = $collection->getFirstItem();
}
}

if ($post->getId()) {
$this->_nextPost = $post;
}
}

return $this->_nextPost;
}


/**
* Retrieve post collection with frontend filters and order
* @return bool
Expand All @@ -135,7 +179,7 @@ protected function _getFrontendCollection()
$collection->addActiveFilter()
->addFieldToFilter('post_id', ['neq' => $this->getPost()->getId()])
->addStoreFilter($this->_storeManager->getStore()->getId())
->setOrder('publish_time', 'DESC')
//->setOrder('publish_time', 'DESC')
->setPageSize(1);
return $collection;
}
Expand Down
17 changes: 17 additions & 0 deletions Model/ResourceModel/Post/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,21 @@ protected function _renderFiltersBefore()
}
parent::_renderFiltersBefore();
}

/**
* Add select order
*
* @param string $field
* @param string $direction
* @return $this
*/
public function setOrder($field, $direction = self::SORT_ORDER_DESC)
{
parent::setOrder($field, $direction);

if (is_string($field) && $field == 'publish_time') {
parent::setOrder('post_id', $direction);
}
return $this;
}
}