Skip to content

Commit b3e6a06

Browse files
committed
Almost done with comments
1 parent cb1c37b commit b3e6a06

File tree

27 files changed

+400
-219
lines changed

27 files changed

+400
-219
lines changed

Block/Index.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,40 @@ protected function _getConfigValue($param)
4747
);
4848
}
4949

50+
51+
/**
52+
* Prepare breadcrumbs
53+
*
54+
* @param string $title
55+
* @param string $key
56+
* @throws \Magento\Framework\Exception\LocalizedException
57+
* @return void
58+
*/
59+
protected function _addBreadcrumbs($title = null, $key = null)
60+
{
61+
if ($breadcrumbsBlock = $this->getBreadcrumbsBlock()) {
62+
$breadcrumbsBlock->addCrumb(
63+
'home',
64+
[
65+
'label' => __('Home'),
66+
'title' => __('Go to Home Page'),
67+
'link' => $this->_storeManager->getStore()->getBaseUrl()
68+
]
69+
);
70+
71+
$blogTitle = $this->_scopeConfig->getValue(
72+
'mfblog/index_page/title',
73+
ScopeInterface::SCOPE_STORE
74+
);
75+
$breadcrumbsBlock->addCrumb(
76+
'blog',
77+
[
78+
'label' => __($blogTitle),
79+
'title' => __($blogTitle),
80+
'link' => null,
81+
]
82+
);
83+
}
84+
}
85+
5086
}

Block/Post/Info.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,16 @@ public function authorPageEnabled()
5656
);
5757
}
5858

59+
/**
60+
* Retrieve true if magefan comments are enabled
61+
* @return bool
62+
*/
63+
public function magefanCommentsEnabled()
64+
{
65+
return $this->_scopeConfig->getValue(
66+
'mfblog/post_view/comments/type',
67+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
68+
) == \Magefan\Blog\Model\Config\Source\CommetType::MAGEFAN;
69+
}
70+
5971
}

Block/Post/PostList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ protected function _addBreadcrumbs($title = null, $key = null)
124124
[
125125
'label' => __($blogTitle),
126126
'title' => __($blogTitle),
127-
'link' => $title ? $this->_url->getBaseUrl() : null,
127+
'link' => $this->_url->getBaseUrl(),
128128
]
129129
);
130130

Block/Post/View/Comments/Magefan.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
class Magefan extends \Magefan\Blog\Block\Post\View\Comments
1818
{
19+
/**
20+
* @var string
21+
*/
1922
protected $commetType = CommetType::MAGEFAN;
2023

2124
/**
@@ -103,20 +106,18 @@ public function getCommentHtml(\Magefan\Blog\Model\Comment $comment)
103106
*/
104107
protected function prepareCommentCollection()
105108
{
109+
106110
$this->commentCollection = $this->getPost()->getComments()
107111
->addActiveFilter()
108-
->setPageSize(5)
112+
->addFieldToFilter('parent_id', 0)
113+
/*->setPageSize($this->getNumberOfComments())*/
109114
->setOrder('creation_time', 'DESC');
110-
111-
//if ($this->getPageSize()) {
112-
$this->commentCollection->setPageSize(5);
113-
//}
114115
}
115116

116117
/**
117118
* Prepare posts collection
118119
*
119-
* @return \Magefan\Blog\Model\ResourceModel\Post\Collection
120+
* @return \Magefan\Blog\Model\ResourceModel\Comment\Collection
120121
*/
121122
public function getCommentsCollection()
122123
{
@@ -158,6 +159,19 @@ public function canPost()
158159
) || $this->getCustomerSession()->getCustomerGroupId();
159160
}
160161

162+
/**
163+
* Retrieve number of comments to display
164+
*
165+
* @return string
166+
*/
167+
public function getNumberOfComments()
168+
{
169+
return $this->_scopeConfig->getValue(
170+
\Magefan\Blog\Helper\Config::NUMBER_OF_COMMENTS,
171+
ScopeInterface::SCOPE_STORE
172+
);
173+
}
174+
161175
/**
162176
* Retrieve form url
163177
* @return string

Block/Post/View/Comments/Magefan/Comment.php

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,51 @@
1818
*/
1919
class Comment extends \Magento\Framework\View\Element\Template
2020
{
21+
/**
22+
* @var array
23+
*/
24+
protected $repliesCollection = [];
25+
26+
/**
27+
* Template file
28+
* @var string
29+
*/
2130
protected $_template = 'Magefan_Blog::post/view/comments/magefan/comment.phtml';
2231

23-
public function getReplies()
32+
/**
33+
* Retrieve sub-comments collection or empty array
34+
*
35+
* @return \Magefan\Blog\Model\ResourceModel\Comment\Collection | array
36+
*/
37+
public function getRepliesCollection()
2438
{
2539
$comment = $this->getComment();
2640
if (!$comment->isReply()) {
27-
return $comment->getChildComments()
28-
->addActiveFilter()
29-
->setPageSize(5)
30-
->setOrder('creation_time', 'DESC');
41+
$cId = $comment->getId();
42+
if (!isset($this->repliesCollection[$cId])) {
43+
$this->repliesCollection[$cId] = $this->getComment()->getChildComments()
44+
->addActiveFilter()
45+
/*->setPageSize($this->getNumberOfReplies())*/
46+
->setOrder('creation_time', 'DESC');
47+
}
48+
49+
return $this->repliesCollection[$cId];
3150
} else {
3251
return [];
3352
}
3453
}
54+
55+
/**
56+
* Retrieve number of replies to display
57+
*
58+
* @return string
59+
*/
60+
public function getNumberOfReplies()
61+
{
62+
return $this->_scopeConfig->getValue(
63+
\Magefan\Blog\Helper\Config::NUMBER_OF_REPLIES,
64+
ScopeInterface::SCOPE_STORE
65+
);
66+
}
67+
3568
}

Controller/Adminhtml/Comment/Save.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Save extends \Magefan\Blog\Controller\Adminhtml\Comment
2323
protected function filterParams($data)
2424
{
2525
/* Prepare dates */
26-
$dateFilter = $this->_objectManager->create('Magento\Framework\Stdlib\DateTime\Filter\Date');
26+
$dateFilter = $this->_objectManager->create('Magento\Framework\Stdlib\DateTime\Filter\DateTime');
2727

2828
$filterRules = [];
2929
foreach (['creation_time'] as $dateField) {

Controller/Comment/Post.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,24 @@ public function execute()
128128
);
129129

130130
try {
131-
if (!$this->initPost()
132-
|| ($request->getParam('parent_id') && !$this->initParentComment())
133-
) {
134-
throw new \Exception(__('We can\'t post your comment right now.'), 1);
131+
$post = $this->initPost();
132+
if (!$post) {
133+
throw new \Exception(__('You cannot post comment. Blog post is not longer exist.'), 1);
134+
}
135+
136+
if ($request->getParam('parent_id')) {
137+
$parentComment = $this->initParentComment();
138+
if (!$parentComment) {
139+
throw new \Exception(__('You cannot reply to this comment. Comment is not longer exist.'), 1);
140+
}
141+
142+
if (!$parentComment->getPost()
143+
|| $parentComment->getPost()->getId() != $post->getId()
144+
) {
145+
throw new \Exception(__('You cannot reply to this comment.'), 1);
146+
}
147+
148+
$comment->setParentId($parentComment->getId());
135149
}
136150

137151
$comment->save();
@@ -147,7 +161,7 @@ public function execute()
147161
'success' => true,
148162
'message' => ($comment->getStatus() == \Magefan\Blog\Model\Config\Source\CommentStatus::PENDING)
149163
? __('You submitted your comment for moderation.')
150-
: __('Thank you for your comment')
164+
: __('Thank you for your comment.')
151165
]));
152166
}
153167

Helper/Config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@ class Config extends \Magento\Framework\App\Helper\AbstractHelper
2020
*/
2121
const XML_PATH_EXTENSION_ENABLED = 'mfblog/general/enabled';
2222
const GUEST_COMMENT = 'mfblog/post_view/comments/guest_comments';
23+
const NUMBER_OF_COMMENTS = 'mfblog/post_view/comments/number_of_comments';
24+
const NUMBER_OF_REPLIES = 'mfblog/post_view/comments/number_of_replies';
2325
const COMMENT_STATUS = 'mfblog/post_view/comments/status';
26+
2427
}

Model/Category.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ public function load($modelId, $field = null)
123123
return $object;
124124
}
125125

126+
/**
127+
* Load category by id
128+
* @param int $categoryId
129+
* @return self
130+
*/
126131
private function loadFromRepository($categoryId)
127132
{
128133
if (!isset(self::$loadedCategoriesRepository[$categoryId])) {

Model/Comment.php

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ class Comment extends AbstractModel
6565
*/
6666
protected $author;
6767

68+
/**
69+
* @var \Magefan\Blog\Model\ResourceModel\Comment\Collection
70+
*/
71+
protected $comments;
72+
6873
/**
6974
* Initialize dependencies.
7075
* @param \Magento\Framework\Model\Context $context
@@ -189,18 +194,39 @@ public function getAuthor()
189194
return $this->author;
190195
}
191196

197+
/**
198+
* Retrieve parent comment
199+
* @return self || false
200+
*/
201+
public function getParentComment()
202+
{
203+
$k = 'parent_comment';
204+
if (null === $this->getData($k)) {
205+
$this->setData($k, false);
206+
if ($pId = $this->getParentId()) {
207+
$comment = clone $this;
208+
$comment->load($pId);
209+
if ($comment->getId()) {
210+
$this->setData($k, $comment);
211+
}
212+
}
213+
}
214+
215+
return $this->getData($k);
216+
}
217+
192218
/**
193219
* Retrieve child comments
194-
* @return \Magefan\Blog\Model\ResourceModel\Tag\Collection
220+
* @return \Magefan\Blog\Model\ResourceModel\Comment\Collection
195221
*/
196222
public function getChildComments()
197223
{
198-
if (is_null($this->_comments)) {
199-
$this->_comments = $this->_commentCollectionFactory->create()
224+
if (is_null($this->comments)) {
225+
$this->comments = $this->commentCollectionFactory->create()
200226
->addFieldToFilter('parent_id', $this->getId());
201227
}
202228

203-
return $this->_comments;
229+
return $this->comments;
204230
}
205231

206232
/**
@@ -232,4 +258,17 @@ public function validate()
232258
throw new \Exception(__('Comment text is too short.'), 1);
233259
}
234260
}
261+
262+
/**
263+
* Retrieve post publish date using format
264+
* @param string $format
265+
* @return string
266+
*/
267+
public function getPublishDate($format = 'Y-m-d H:i:s')
268+
{
269+
return \Magefan\Blog\Helper\Data::getTranslatedDate(
270+
$format,
271+
$this->getData('creation_time')
272+
);
273+
}
235274
}

Model/Post.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,10 @@ public function getTagsCount()
610610

611611
/**
612612
* Retrieve post comments
613+
* @param boolean $active
613614
* @return \Magefan\Blog\Model\ResourceModel\Comment\Collection
614615
*/
615-
public function getComments()
616+
public function getComments($active = true)
616617
{
617618
if (null === $this->comments) {
618619
$this->comments = $this->_commentCollectionFactory->create()
@@ -622,6 +623,22 @@ public function getComments()
622623
return $this->comments;
623624
}
624625

626+
/**
627+
* Retrieve active comments count
628+
* @return int
629+
*/
630+
public function getCommentsCount()
631+
{
632+
if (!$this->hasData('comments_count')) {
633+
$comments = $this->_commentCollectionFactory->create()
634+
->addFieldToFilter('post_id', $this->getId())
635+
->addActiveFilter()
636+
->addFieldToFilter('parent_id', 0);
637+
$this->setData('comments_count', (int)$comments->getSize());
638+
}
639+
return $this->getData('comments_count');
640+
}
641+
625642
/**
626643
* Retrieve post related posts
627644
* @return \Magefan\Blog\Model\ResourceModel\Post\Collection

Model/ResourceModel/Comment.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
5959
$gmtDate = $this->date->gmtDate();
6060

6161
if ($object->isObjectNew() && !$object->getCreationTime()) {
62-
echo 'a'; exit();
6362
$object->setCreationTime($gmtDate);
6463
}
6564

Ui/DataProvider/Comment/Form/CommentDataProvider.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,24 @@ public function getData()
119119
];
120120
break;
121121
}
122+
123+
if ($comment->getParentId()
124+
&& ($parentComment = $comment->getParentComment())
125+
) {
126+
$text = (mb_strlen($parentComment->getText()) > 200) ? (mb_substr($parentComment->getText(), 0, 200) . '...') : $parentComment->getText();
127+
$text = htmlspecialchars($text);
128+
$this->loadedData[$comment->getId()]['parent_url'] = [
129+
'url' => $this->url->getUrl('blog/comment/edit', ['id' => $parentComment->getId()]),
130+
'title' => htmlspecialchars($parentComment->getText()),
131+
'text' => '#' . $parentComment->getId() . '. ' . $text,
132+
];
133+
} else {
134+
$this->loadedData[$comment->getId()]['parent_url'] = [
135+
'url' => '',
136+
'title' => '',
137+
'text' => '',
138+
];
139+
}
122140
}
123141

124142
$data = $this->dataPersistor->get('blog_comment_form_data');

0 commit comments

Comments
 (0)