Pager.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Review\Helper\Action;
  7. use Magento\Framework\Exception\LocalizedException;
  8. /**
  9. * Action pager helper for iterating over search results
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Pager extends \Magento\Framework\App\Helper\AbstractHelper
  15. {
  16. const STORAGE_PREFIX = 'search_result_ids';
  17. /**
  18. * Storage id
  19. *
  20. * @var int
  21. */
  22. protected $_storageId = null;
  23. /**
  24. * Array of items
  25. *
  26. * @var array
  27. */
  28. protected $_items = null;
  29. /**
  30. * Backend session model
  31. *
  32. * @var \Magento\Backend\Model\Session
  33. */
  34. protected $_backendSession;
  35. /**
  36. * @param \Magento\Framework\App\Helper\Context $context
  37. * @param \Magento\Backend\Model\Session $backendSession
  38. */
  39. public function __construct(
  40. \Magento\Framework\App\Helper\Context $context,
  41. \Magento\Backend\Model\Session $backendSession
  42. ) {
  43. $this->_backendSession = $backendSession;
  44. parent::__construct($context);
  45. }
  46. /**
  47. * Set storage id
  48. *
  49. * @param int $storageId
  50. * @return void
  51. */
  52. public function setStorageId($storageId)
  53. {
  54. $this->_storageId = $storageId;
  55. }
  56. /**
  57. * Set items to storage
  58. *
  59. * @param array $items
  60. * @return $this
  61. */
  62. public function setItems(array $items)
  63. {
  64. $this->_items = $items;
  65. $this->_backendSession->setData($this->_getStorageKey(), $this->_items);
  66. return $this;
  67. }
  68. /**
  69. * Load stored items
  70. *
  71. * @return void
  72. */
  73. protected function _loadItems()
  74. {
  75. if ($this->_items === null) {
  76. $this->_items = (array)$this->_backendSession->getData($this->_getStorageKey());
  77. }
  78. }
  79. /**
  80. * Get next item id
  81. *
  82. * @param int $id
  83. * @return int|bool
  84. */
  85. public function getNextItemId($id)
  86. {
  87. $position = $this->_findItemPositionByValue($id);
  88. if ($position === false || $position == count($this->_items) - 1) {
  89. return false;
  90. }
  91. return $this->_items[$position + 1];
  92. }
  93. /**
  94. * Get previous item id
  95. *
  96. * @param int $id
  97. * @return int|bool
  98. */
  99. public function getPreviousItemId($id)
  100. {
  101. $position = $this->_findItemPositionByValue($id);
  102. if ($position === false || $position == 0) {
  103. return false;
  104. }
  105. return $this->_items[$position - 1];
  106. }
  107. /**
  108. * Return item position based on passed in value
  109. *
  110. * @param mixed $value
  111. * @return int|bool
  112. */
  113. protected function _findItemPositionByValue($value)
  114. {
  115. $this->_loadItems();
  116. return array_search($value, $this->_items);
  117. }
  118. /**
  119. * Get storage key
  120. *
  121. * @return string
  122. * @throws \Magento\Framework\Exception\LocalizedException
  123. */
  124. protected function _getStorageKey()
  125. {
  126. if (!$this->_storageId) {
  127. throw new LocalizedException(__("The storage key wasn't set. Add the storage key and try again."));
  128. }
  129. return self::STORAGE_PREFIX . $this->_storageId;
  130. }
  131. }