View.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Copyright © 2017 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\Controller\Post;
  9. /**
  10. * Blog post view
  11. */
  12. class View extends \Magefan\Blog\App\Action\Action
  13. {
  14. /**
  15. * Store manager
  16. *
  17. * @var \Magento\Store\Model\StoreManagerInterface
  18. */
  19. protected $_storeManager;
  20. /**
  21. * @param \Magento\Framework\App\Action\Context $context
  22. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  23. */
  24. public function __construct(
  25. \Magento\Framework\App\Action\Context $context,
  26. \Magento\Store\Model\StoreManagerInterface $storeManager
  27. ) {
  28. parent::__construct($context);
  29. $this->_storeManager = $storeManager;
  30. }
  31. /**
  32. * View Blog post action
  33. *
  34. * @return \Magento\Framework\Controller\ResultInterface
  35. */
  36. public function execute()
  37. {
  38. if (!$this->moduleEnabled()) {
  39. return $this->_forwardNoroute();
  40. }
  41. $post = $this->_initPost();
  42. if (!$post) {
  43. return $this->_forwardNoroute();
  44. }
  45. $this->_objectManager->get('\Magento\Framework\Registry')
  46. ->register('current_blog_post', $post);
  47. $resultPage = $this->_objectManager->get('Magefan\Blog\Helper\Page')
  48. ->prepareResultPage($this, $post);
  49. return $resultPage;
  50. }
  51. /**
  52. * Init Post
  53. *
  54. * @return \Magefan\Blog\Model\Post || false
  55. */
  56. protected function _initPost()
  57. {
  58. $id = $this->getRequest()->getParam('id');
  59. $secret = $this->getRequest()->getParam('secret');
  60. $storeId = $this->_storeManager->getStore()->getId();
  61. $post = $this->_objectManager->create('Magefan\Blog\Model\Post')->load($id);
  62. if (!$post->isVisibleOnStore($storeId) && !$post->isValidSecret($secret)) {
  63. return false;
  64. }
  65. if ($post->isValidSecret($secret)) {
  66. $post->setIsPreviewMode(true);
  67. }
  68. $post->setStoreId($storeId);
  69. if ($category = $this->_initCategory()) {
  70. $post->setData('parent_category', $category);
  71. }
  72. return $post;
  73. }
  74. /**
  75. * Init category
  76. *
  77. * @return \Magefan\Blog\Model\category || false
  78. */
  79. protected function _initCategory()
  80. {
  81. $id = $this->getRequest()->getParam('category_id');
  82. $storeId = $this->_storeManager->getStore()->getId();
  83. $category = $this->_objectManager->create('Magefan\Blog\Model\Category')->load($id);
  84. if (!$category->isVisibleOnStore($storeId)) {
  85. return false;
  86. }
  87. $category->setStoreId($storeId);
  88. return $category;
  89. }
  90. }