Skip to content

Commit c52c5ea

Browse files
committed
Added Preview Mode for Posts, Include Parent Categories in Breadcrumb on Post Page, Now can add sufix to category url, Now can include parent category path in post or category url
1 parent 4406096 commit c52c5ea

File tree

22 files changed

+792
-238
lines changed

22 files changed

+792
-238
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © 2015-2017 Ihor Vansach ([email protected]). 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\Adminhtml\Edit;
9+
10+
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
11+
12+
/**
13+
* Class PreviewButton
14+
*/
15+
class PreviewButton extends GenericButton implements ButtonProviderInterface
16+
{
17+
/**
18+
* @return array
19+
*/
20+
public function getButtonData()
21+
{
22+
$data = [];
23+
if ($this->getPostId()) {
24+
$data = [
25+
'label' => __('Preview'),
26+
'class' => 'preview',
27+
'on_click' => 'window.open(\'' . $this->getPreviewUrl() . '\');',
28+
'sort_order' => 35,
29+
];
30+
}
31+
return $data;
32+
}
33+
34+
/**
35+
* @return string
36+
*/
37+
public function getPreviewUrl()
38+
{
39+
return $this->getUrl('*/*/preview', ['id' => $this->getPostId()]);
40+
}
41+
}

Block/Category/View.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ protected function _preparePostCollection()
2424
{
2525
parent::_preparePostCollection();
2626
if ($category = $this->getCategory()) {
27-
$categories = $category->getChildrenIds();
28-
$categories[] = $category->getId();
29-
$this->_postCollection->addCategoryFilter($categories);
27+
$this->_postCollection->addCategoryFilter($category);
3028
}
3129
}
3230

@@ -55,7 +53,7 @@ protected function _prepareLayout()
5553
$this->pageConfig->setKeywords($category->getMetaKeywords());
5654
$this->pageConfig->setDescription($category->getMetaDescription());
5755
$this->pageConfig->addRemotePageAsset(
58-
$category->getCategoryUrl(),
56+
$category->getCanonicalUrl(),
5957
'canonical',
6058
['attributes' => ['rel' => 'canonical']]
6159
);

Block/Post/View.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected function _prepareLayout()
2929
$this->pageConfig->setKeywords($post->getMetaKeywords());
3030
$this->pageConfig->setDescription($post->getMetaDescription());
3131
$this->pageConfig->addRemotePageAsset(
32-
$post->getPostUrl(),
32+
$post->getCanonicalUrl(),
3333
'canonical',
3434
['attributes' => ['rel' => 'canonical']]
3535
);
@@ -40,6 +40,10 @@ protected function _prepareLayout()
4040
$this->escapeHtml($post->getTitle())
4141
);
4242
}
43+
44+
if ($post->getIsPreviewMode()) {
45+
$this->pageConfig->setRobots('NOINDEX,FOLLOW');
46+
}
4347
}
4448

4549
return parent::_prepareLayout();
@@ -79,6 +83,23 @@ protected function _addBreadcrumbs($title = null, $key = null)
7983
'link' => $this->_url->getBaseUrl()
8084
]
8185
);
86+
87+
$parentCategories = [];
88+
$parentCategory = $this->getPost()->getParentCategory();
89+
while ($parentCategory ) {
90+
$parentCategories[] = $parentCategory;
91+
$parentCategory = $parentCategory->getParentCategory();
92+
}
93+
94+
for ($i = count($parentCategories) - 1; $i >= 0; $i--) {
95+
$parentCategory = $parentCategories[$i];
96+
$breadcrumbsBlock->addCrumb('blog_parent_category_' . $parentCategory->getId(), [
97+
'label' => $parentCategory->getTitle(),
98+
'title' => $parentCategory->getTitle(),
99+
'link' => $parentCategory->getCategoryUrl()
100+
]);
101+
}
102+
82103
$breadcrumbsBlock->addCrumb($key, [
83104
'label' => $title ,
84105
'title' => $title

Block/Widget/Recent.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ protected function _preparePostCollection()
9292
parent::_preparePostCollection();
9393

9494
if ($category = $this->getCategory()) {
95-
$categories = $category->getChildrenIds();
96-
$categories[] = $category->getId();
97-
$this->_postCollection->addCategoryFilter($categories);
95+
$this->_postCollection->addCategoryFilter($category);
9896
}
9997
}
10098

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright © 2015-2017 Ihor Vansach ([email protected]). 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+
9+
namespace Magefan\Blog\Controller\Adminhtml\Post;
10+
11+
/**
12+
* Blog post preview controller
13+
*/
14+
class Preview extends \Magefan\Blog\Controller\Adminhtml\Post
15+
{
16+
public function execute()
17+
{
18+
try {
19+
$post = $this->_getModel();
20+
if (!$post->getId()) {
21+
throw new \Exception("Item is not longer exist.", 1);
22+
}
23+
24+
$previewUrl = $this->_objectManager->get('\Magefan\Blog\Model\PreviewUrl');
25+
$redirectUrl = $previewUrl->getUrl(
26+
$post,
27+
$previewUrl::CONTROLLER_POST
28+
);
29+
30+
$this->getResponse()->setRedirect($redirectUrl);
31+
32+
} catch (\Exception $e) {
33+
$this->messageManager->addException(
34+
$e,
35+
__('Something went wrong %1', $e->getMessage())
36+
);
37+
$this->_redirect('*/*/edit', [$this->_idKey => $post->getId()]);
38+
}
39+
}
40+
}

Controller/Post/View.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,48 @@ public function execute()
6363
protected function _initPost()
6464
{
6565
$id = $this->getRequest()->getParam('id');
66+
$secret = $this->getRequest()->getParam('secret');
6667
$storeId = $this->_storeManager->getStore()->getId();
6768

6869
$post = $this->_objectManager->create('Magefan\Blog\Model\Post')->load($id);
6970

70-
if (!$post->isVisibleOnStore($storeId)) {
71+
if (!$post->isVisibleOnStore($storeId) && !$post->isValidSecret($secret)) {
7172
return false;
7273
}
7374

75+
if ($post->isValidSecret($secret)) {
76+
$post->setIsPreviewMode(true);
77+
}
78+
7479
$post->setStoreId($storeId);
7580

81+
if ($category = $this->_initCategory()) {
82+
$post->setData('parent_category', $category);
83+
}
84+
7685
return $post;
7786
}
7887

88+
/**
89+
* Init category
90+
*
91+
* @return \Magefan\Blog\Model\category || false
92+
*/
93+
protected function _initCategory()
94+
{
95+
$id = $this->getRequest()->getParam('category_id');
96+
97+
$storeId = $this->_storeManager->getStore()->getId();
98+
99+
$category = $this->_objectManager->create('Magefan\Blog\Model\Category')->load($id);
100+
101+
if (!$category->isVisibleOnStore($storeId)) {
102+
return false;
103+
}
104+
105+
$category->setStoreId($storeId);
106+
107+
return $category;
108+
}
109+
79110
}

0 commit comments

Comments
 (0)