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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions app/code/Magento/Review/Block/Adminhtml/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Magento\Review\Block\Adminhtml;

use Magento\Review\Helper\Action\Pager;

/**
* Adminhtml reviews grid
*
Expand All @@ -26,14 +28,14 @@ class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
protected $_reviewActionPager = null;

/**
* Review data
* Review helper data
*
* @var \Magento\Review\Helper\Data
*/
protected $_reviewData = null;

/**
* Core registry
* Magento framework Core registry
*
* @var \Magento\Framework\Registry
*/
Expand Down Expand Up @@ -94,16 +96,17 @@ protected function _construct()
}

/**
* Save search results
* Executes after the collection is loaded. This method is intentionally overridden to preserve compatibility
*
* @return \Magento\Backend\Block\Widget\Grid
* @deprecated This method is no longer in used.We use it only to support compatibility
* @see Pager::getRelativeReviewId()
*/
protected function _afterLoadCollection()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this method could backward incompatible change, third-party extensions may override this method.

{
/** @var $actionPager \Magento\Review\Helper\Action\Pager */
$actionPager = $this->_reviewActionPager;
$actionPager->setStorageId('reviews');
$actionPager->setItems($this->getCollection()->getResultingIds());

return parent::_afterLoadCollection();
}
Expand Down
72 changes: 53 additions & 19 deletions app/code/Magento/Review/Helper/Action/Pager.php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Various methods has been removed from here, this could be backward incompatible change.

Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@

namespace Magento\Review\Helper\Action;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Review\Model\ResourceModel\Review\CollectionFactory;

/**
* Action pager helper for iterating over search results
*
* @api
* @since 100.0.2
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class Pager extends \Magento\Framework\App\Helper\AbstractHelper
{
const STORAGE_PREFIX = 'search_result_ids';
public const STORAGE_PREFIX = 'search_result_ids';

/**
* Storage id
* Key identifier for session storage id
*
* @var int
*/
Expand All @@ -39,15 +42,26 @@ class Pager extends \Magento\Framework\App\Helper\AbstractHelper
*/
protected $_backendSession;

/**
* Review collection model factory
*
* @var CollectionFactory|null
*/
protected $reviewCollectionFactory = null;

/**
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Backend\Model\Session $backendSession
* @param CollectionFactory|null $reviewCollectionFactory
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Backend\Model\Session $backendSession
\Magento\Backend\Model\Session $backendSession,
?CollectionFactory $reviewCollectionFactory = null
) {
$this->_backendSession = $backendSession;
$this->reviewCollectionFactory = $reviewCollectionFactory
?: ObjectManager::getInstance()->get(CollectionFactory::class);
parent::__construct($context);
}

Expand All @@ -56,6 +70,8 @@ public function __construct(
*
* @param int $storageId
* @return void
* @deprecated This method is no longer used for setting storage id.We use it only to support backward compatibility
* @see self::getRelativeReviewId()
*/
public function setStorageId($storageId)
{
Expand All @@ -67,6 +83,8 @@ public function setStorageId($storageId)
*
* @param array $items
* @return $this
* @deprecated This method is not being used.We use it only to support compatibility
* @see self::getRelativeReviewId()
*/
public function setItems(array $items)
{
Expand All @@ -80,6 +98,8 @@ public function setItems(array $items)
* Load stored items
*
* @return void
* @deprecated This method is not being used.We use it only to support compatibility
* @see self::getRelativeReviewId()
*/
protected function _loadItems()
{
Expand All @@ -89,42 +109,34 @@ protected function _loadItems()
}

/**
* Get next item id
* Get the next review id.
*
* @param int $id
* @return int|bool
*/
public function getNextItemId($id)
public function getNextItemId($id): int|bool
{
$position = $this->_findItemPositionByValue($id);
if ($position === false || $position == count($this->_items) - 1) {
return false;
}

return $this->_items[$position + 1];
return $this->getRelativeReviewId($id, 'gt', 'ASC');
}

/**
* Get previous item id
* Get the previous review id.
*
* @param int $id
* @return int|bool
*/
public function getPreviousItemId($id)
public function getPreviousItemId($id): int|bool
{
$position = $this->_findItemPositionByValue($id);
if ($position === false || $position == 0) {
return false;
}

return $this->_items[$position - 1];
return $this->getRelativeReviewId($id, 'lt', 'DESC');
}

/**
* Return item position based on passed in value
*
* @param mixed $value
* @return int|bool
* @deprecated This method is not being used.We use it only to support compatibility
* @see self::getRelativeReviewId()
*/
protected function _findItemPositionByValue($value)
{
Expand All @@ -137,6 +149,8 @@ protected function _findItemPositionByValue($value)
*
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
* @deprecated This method is not being used.We use it only to support compatibility
* @see self::getRelativeReviewId()
*/
protected function _getStorageKey()
{
Expand All @@ -146,4 +160,24 @@ protected function _getStorageKey()

return self::STORAGE_PREFIX . $this->_storageId;
}

/**
* Get the review id based on comparison and order.
*
* @param int $id
* @param string $operator
* @param string $order
* @return int|bool
*/
private function getRelativeReviewId($id, $operator, $order): int|bool
{
$collection = $this->reviewCollectionFactory->create();
$collection->addFieldToFilter('main_table.review_id', [$operator => $id])
->setOrder('main_table.review_id', $order)
->setPageSize(1)
->setCurPage(1);

$item = $collection->getFirstItem();
return $item->getId() ? (int)$item->getId() : false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use Magento\Framework\DB\Select;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Review\Helper\Action\Pager;

/**
* Review Product Collection
Expand Down Expand Up @@ -405,6 +406,8 @@ public function getAllIds($limit = null, $offset = null)
* Get result sorted ids
*
* @return array
* @deprecated This method is not being used to fetch ids anymore.We use it only to support backward compatibility
* @see Pager::getRelativeReviewId()
*/
public function getResultingIds()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this method could be backward incompatible change.

{
Expand Down
Loading