Archive.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\Sidebar;
  9. /**
  10. * Blog sidebar archive block
  11. */
  12. class Archive extends \Magefan\Blog\Block\Post\PostList\AbstractList
  13. {
  14. use Widget;
  15. /**
  16. * @var string
  17. */
  18. protected $_widgetKey = 'archive';
  19. /**
  20. * Available months
  21. * @var array
  22. */
  23. protected $_months;
  24. /**
  25. * Prepare posts collection
  26. *
  27. * @return void
  28. */
  29. protected function _preparePostCollection()
  30. {
  31. parent::_preparePostCollection();
  32. $this->_postCollection->getSelect()->group(
  33. 'MONTH(main_table.publish_time)',
  34. 'DESC'
  35. );
  36. }
  37. /**
  38. * Retrieve available months
  39. * @return array
  40. */
  41. public function getMonths()
  42. {
  43. if (is_null($this->_months)) {
  44. $this->_months = [];
  45. $this->_preparePostCollection();
  46. foreach($this->_postCollection as $post) {
  47. $time = strtotime($post->getData('publish_time'));
  48. $this->_months[date('Y-m', $time)] = $time;
  49. }
  50. }
  51. return $this->_months;
  52. }
  53. /**
  54. * Retrieve year by time
  55. * @param int $time
  56. * @return string
  57. */
  58. public function getYear($time)
  59. {
  60. return date('Y', $time);
  61. }
  62. /**
  63. * Retrieve month by time
  64. * @param int $time
  65. * @return string
  66. */
  67. public function getMonth($time)
  68. {
  69. return __(date('F', $time));
  70. }
  71. /**
  72. * Retrieve archive url by time
  73. * @param int $time
  74. * @return string
  75. */
  76. public function getTimeUrl($time)
  77. {
  78. return $this->_url->getUrl(
  79. date('Y-m', $time),
  80. \Magefan\Blog\Model\Url::CONTROLLER_ARCHIVE
  81. );
  82. }
  83. /**
  84. * Retrieve blog identities
  85. * @return array
  86. */
  87. public function getIdentities()
  88. {
  89. return [\Magento\Cms\Model\Block::CACHE_TAG . '_blog_archive_widget'];
  90. }
  91. }