Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add blog post rating [in progress] #535

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Block/Post/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,30 @@ public function viewsCountEnabled()
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}

/**
* Get blog post vote action
*
* @return string
*/
public function getAction()
{
return $this->getUrl(
'blogextra/post/vote',
[
'_secure' => $this->getRequest()->isSecure(),
'id' => $this->getPostId(),
]
);
}

/**
* Get vote post id
*
* @return int
*/
protected function getPostId()
{
return $this->getRequest()->getParam('id', false);
}
}
7 changes: 7 additions & 0 deletions Controller/Adminhtml/Post/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class Save extends \Magefan\Blog\Controller\Adminhtml\Post
*/
protected function _beforeSave($model, $request)
{
$postRepository = $this->_objectManager->get(\Magefan\Blog\Api\PostRepositoryInterface::class);
$post = $postRepository->getById($model->getId());

if ($post->getSummaryRating() && $post->getSummaryRating() !== $request->getPost('summary_rating')) {
$model->setData('votes_count', 1);
}

/* Prepare author */
if (!$model->getAuthorId()) {
$authSession = $this->_objectManager->get(\Magento\Backend\Model\Auth\Session::class);
Expand Down
4 changes: 2 additions & 2 deletions Controller/Comment/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function execute()
$this->customerSession->getCustomerId()
)->setAuthorNickname(
$this->customerSession->getCustomer()->getName()
)->setAauthorEmail(
)->setAuthorEmail(
$this->customerSession->getCustomer()->getEmail()
)->setAuthorType(
\Magefan\Blog\Model\Config\Source\AuthorType::CUSTOMER
Expand All @@ -109,7 +109,7 @@ public function execute()
]));
return;
}

$comment->setCustomerId(0)->setAuthorType(
\Magefan\Blog\Model\Config\Source\AuthorType::GUEST
);
Expand Down
74 changes: 74 additions & 0 deletions view/frontend/templates/post/info.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
?>

<?php $_post = $block->getPost() ?>
<?php $ratingValues = [1, 2, 3, 4, 5] ?>

<div class="post-info">
<?php if ($_post->isPublishDateEnabled()) { ?>
Expand Down Expand Up @@ -92,4 +93,77 @@
<?php } ?>
<?php } ?>

<?php if (true) { ?>
<?php if ($rating = $_post->getSummaryRating()) { ?>
<div class="product-reviews-summary<?= !$rating ? ' no-rating' : '' ?>">
<?php if ($rating):?>
<div class="rating-summary">
<span class="label"><span><?= $block->escapeHtml(__('Rating')) ?>:</span></span>
<div class="rating-result"
id="rating-result_<?= /* @noEscape */ $_post->getId() ?>"
title="<?= $block->escapeHtmlAttr($rating) ?>%">
<span>
<span>
<span><?= $block->escapeHtml($rating); ?>
</span>% of <span>100</span>
</span>
</span>
</div>
<?= /* @noEscape */ $secureRenderer->renderStyleAsTag(
'width:' . $block->escapeHtmlAttr($rating) . '%',
'#rating-result_' . $_post->getId() . ' span'
) ?>
</div>
<?php endif;?>
<div class="reviews-actions">
<span><?= $block->escapeHtml($_post->getVotesCount()) ?></span>&nbsp;
<span><?= ($block->getVotesCount() == 1) ? $block->escapeHtml(__('Vote')) :
$block->escapeHtml(__('Votes')) ?>
</span>
</div>
</div>
<hr />
<form action="<?= $block->escapeUrl($block->getAction()) ?>" class="review-form" method="post" id="mf-rate-form">
<?= $block->getBlockHtml('formkey') ?>
<fieldset class="fieldset review-fieldset">
<span id="input-message-box"></span>
<fieldset class="field review-field-ratings">
<legend class="label"><span>Your Rating</span></legend><br>
<div class="control">
<div class="nested" id="product-review-table">
<div class="field choice review-field-rating">
<div class="control review-control-vote">
<?php foreach ($ratingValues as $rating) { ?>
<input
type="radio"
name="ratings[<?= $_post->getId() ?>]"
id="Price_<?= $rating ?>"
value="<?= $rating * 20 ?>"
class="radio"
>
<label
class="rating-<?= $rating ?>"
for="Price_<?= $rating ?>"
title="<?= $block->escapeHtml(__('%1 %2', $rating, $rating > 1 ? __('stars') : __('star'))) ?>"
id="Price_<?= $rating ?>_label"
>
<span><?= $block->escapeHtml(__('%1 %2', $rating, $rating > 1 ? __('stars') : __('star'))) ?></span>
</label>
<?php } ?>
</div>
</div>
</div>
</div>
</fieldset>
</fieldset>
</form>
<script>
require(['jquery'], function($) {
$('#mf-rate-form .radio').click(function() {
$('#mf-rate-form').submit();
});
})
</script>
<?php } ?>
<?php } ?>
</div>
2 changes: 1 addition & 1 deletion view/frontend/templates/post/view.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ $_postName = $block->escapeHtml($_post->getTitle(), null);
<?= $block->getChildHtml('blog.post.bottom') ?>
</div>
</div>
</div>
</div>