NextPrev.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Copyright © 2016 Ihor Vansach (ihor@magefan.com). All rights reserved.
  4. * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
  5. *
  6. * Glory to Ukraine! Glory to the heroes!
  7. */
  8. namespace Magefan\Blog\Block\Post\View;
  9. use Magento\Store\Model\ScopeInterface;
  10. /**
  11. * Blog post next and prev post links
  12. */
  13. class NextPrev extends \Magento\Framework\View\Element\Template
  14. {
  15. /**
  16. * Previous post
  17. *
  18. * @var \Magefan\Blog\Model\Post
  19. */
  20. protected $_prevPost;
  21. /**
  22. * Next post
  23. *
  24. * @var \Magefan\Blog\Model\Post
  25. */
  26. protected $_nextPost;
  27. /**
  28. * @var \Magefan\Blog\Model\ResourceModel\Post\CollectionFactory
  29. */
  30. protected $_postCollectionFactory;
  31. /**
  32. * @var Magento\Framework\Registry
  33. */
  34. protected $_coreRegistry;
  35. /**
  36. * Construct
  37. *
  38. * @param \Magento\Framework\View\Element\Context $context
  39. * @param \Magefan\Blog\Model\ResourceModel\Post\CollectionFactory $_tagCollectionFactory
  40. * @param \Magento\Framework\Registry $coreRegistry
  41. * @param array $data
  42. */
  43. public function __construct(
  44. \Magento\Framework\View\Element\Template\Context $context,
  45. \Magefan\Blog\Model\ResourceModel\Post\CollectionFactory $postCollectionFactory,
  46. \Magento\Framework\Registry $coreRegistry,
  47. array $data = []
  48. ) {
  49. parent::__construct($context, $data);
  50. $this->_postCollectionFactory = $postCollectionFactory;
  51. $this->_coreRegistry = $coreRegistry;
  52. }
  53. /**
  54. * Retrieve true if need to display next-prev links
  55. *
  56. * @return boolean
  57. */
  58. public function displayLinks()
  59. {
  60. return (bool)$this->_scopeConfig->getValue(
  61. 'mfblog/post_view/nextprev/enabled',
  62. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  63. );
  64. }
  65. /**
  66. * Retrieve prev post
  67. * @return \Magefan\Blog\Model\Post || bool
  68. */
  69. public function getPrevPost()
  70. {
  71. if ($this->_prevPost === null) {
  72. $this->_prevPost = false;
  73. $collection = $this->_getFrontendCollection()->addFieldToFilter(
  74. 'publish_time', [
  75. 'gteq' => $this->getPost()->getPublishTime()
  76. ])
  77. ->setOrder('publish_time', 'ASC')
  78. ->setPageSize(1);
  79. $post = $collection->getFirstItem();
  80. if ($post->getId()) {
  81. $this->_prevPost = $post;
  82. }
  83. }
  84. return $this->_prevPost;
  85. }
  86. /**
  87. * Retrieve next post
  88. * @return \Magefan\Blog\Model\Post || bool
  89. */
  90. public function getNextPost()
  91. {
  92. if ($this->_nextPost === null) {
  93. $this->_nextPost = false;
  94. $collection = $this->_getFrontendCollection()->addFieldToFilter(
  95. 'publish_time', [
  96. 'lteq' => $this->getPost()->getPublishTime()
  97. ])
  98. ->setOrder('publish_time', 'DESC')
  99. ->setPageSize(1);
  100. $post = $collection->getFirstItem();
  101. if ($post->getId()) {
  102. $this->_nextPost = $post;
  103. }
  104. }
  105. return $this->_nextPost;
  106. }
  107. /**
  108. * Retrieve post collection with frontend filters and order
  109. * @return bool
  110. */
  111. protected function _getFrontendCollection()
  112. {
  113. $collection = $this->_postCollectionFactory->create();
  114. $collection->addActiveFilter()
  115. ->addFieldToFilter('post_id', ['neq' => $this->getPost()->getId()])
  116. ->addStoreFilter($this->_storeManager->getStore()->getId())
  117. ->setOrder('publish_time', 'DESC')
  118. ->setPageSize(1);
  119. return $collection;
  120. }
  121. /**
  122. * Retrieve post instance
  123. *
  124. * @return \Magefan\Blog\Model\Post
  125. */
  126. public function getPost()
  127. {
  128. return $this->_coreRegistry->registry('current_blog_post');
  129. }
  130. }