Skip to content

Commit a164f2c

Browse files
author
igor-chepurnoi
committed
Add view link to comments grid view on the manage comments page.
1 parent 8c295bf commit a164f2c

File tree

8 files changed

+94
-9
lines changed

8 files changed

+94
-9
lines changed

assets/js/comment.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@
6868
var $commentForm = $(this);
6969
var settings = $commentForm.data('comment');
7070
var pjaxSettings = $.extend({container: settings.pjaxContainerId}, settings.pjaxSettings);
71+
var formData = $commentForm.serializeArray();
72+
formData.push({'name': 'CommentModel[url]', 'value': getCurrentUrl()});
7173

7274
$commentForm.find(':submit').prop('disabled', true).text(settings.submitBtnLoadingText);
7375

74-
$.post($commentForm.attr("action"), $commentForm.serialize(), function (data) {
76+
$.post($commentForm.attr("action"), formData, function (data) {
7577
if (data.status == 'success') {
7678
$.pjax(pjaxSettings).done(function () {
7779
$commentForm.trigger("reset");
@@ -145,4 +147,12 @@
145147
});
146148
}
147149

148-
})(window.jQuery);
150+
/**
151+
* Get current url without `hostname`
152+
* @returns {string}
153+
*/
154+
function getCurrentUrl() {
155+
return window.location.pathname + window.location.search;
156+
}
157+
158+
})(window.jQuery);

messages/en/yii2mod.comments.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
'Created by' => 'Created by',
2828
'Updated by' => 'Related to',
2929
'Related to' => 'Related to',
30+
'Url' => 'Url',
3031
'Created date' => 'Created date',
3132
'Updated date' => 'Updated date',
3233
'Update' => 'Update',
@@ -48,5 +49,5 @@
4849
'Update Comment: {0}' => 'Update Comment: {0}',
4950
'Active' => 'Active',
5051
'Deleted' => 'Deleted',
51-
'Go Back' => 'Go Back'
52+
'Go Back' => 'Go Back',
5253
];

messages/ru/yii2mod.comments.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
'Created by' => 'Создан',
2828
'Updated by' => 'Обновлен',
2929
'Related to' => 'Относится к',
30+
'Url' => 'Урл',
3031
'Created date' => 'Дата создания',
3132
'Updated date' => 'Дата обновления',
3233
'Update' => 'Обновить',
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use yii\db\Migration;
4+
5+
/**
6+
* Handles adding url to table `comment`.
7+
*/
8+
class m161114_094902_add_url_column_to_comment_table extends Migration
9+
{
10+
/**
11+
* @inheritdoc
12+
*/
13+
public function up()
14+
{
15+
$this->addColumn('comment', 'url', $this->text()->after('relatedTo'));
16+
}
17+
18+
/**
19+
* @inheritdoc
20+
*/
21+
public function down()
22+
{
23+
$this->dropColumn('comment', 'url');
24+
}
25+
}

models/CommentModel.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* @property integer $createdBy
2323
* @property integer $updatedBy
2424
* @property string $relatedTo
25+
* @property string $url
2526
* @property integer $status
2627
* @property integer $level
2728
* @property integer $createdAt
@@ -55,7 +56,7 @@ public function rules()
5556
return [
5657
[['entity', 'entityId'], 'required'],
5758
['content', 'required', 'message' => Yii::t('yii2mod.comments', 'Comment cannot be blank.')],
58-
[['content', 'entity', 'relatedTo'], 'string'],
59+
[['content', 'entity', 'relatedTo', 'url'], 'string'],
5960
['status', 'default', 'value' => CommentStatus::ACTIVE],
6061
['status', 'in', 'range' => [CommentStatus::ACTIVE, CommentStatus::DELETED]],
6162
['level', 'default', 'value' => 1],
@@ -129,6 +130,7 @@ public function attributeLabels()
129130
'createdBy' => Yii::t('yii2mod.comments', 'Created by'),
130131
'updatedBy' => Yii::t('yii2mod.comments', 'Updated by'),
131132
'relatedTo' => Yii::t('yii2mod.comments', 'Related to'),
133+
'url' => Yii::t('yii2mod.comments', 'Url'),
132134
'createdAt' => Yii::t('yii2mod.comments', 'Created date'),
133135
'updatedAt' => Yii::t('yii2mod.comments', 'Updated date'),
134136
];
@@ -383,4 +385,28 @@ public function getCommentsCount($onlyActiveComments = true)
383385

384386
return $query->count();
385387
}
386-
}
388+
389+
/**
390+
* Get anchor url for comment
391+
*
392+
* @return string
393+
*/
394+
public function getAnchorUrl()
395+
{
396+
return "#comment-{$this->id}";
397+
}
398+
399+
/**
400+
* Get view comment url
401+
*
402+
* @return null|string
403+
*/
404+
public function getViewUrl()
405+
{
406+
if (!empty($this->url)) {
407+
return $this->url . $this->getAnchorUrl();
408+
}
409+
410+
return null;
411+
}
412+
}

tests/TestCase.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ protected function setupTestDbData()
104104
'createdBy' => 'integer not null',
105105
'updatedBy' => 'integer not null',
106106
'relatedTo' => 'string(500) not null',
107+
'url' => 'text',
107108
'status' => 'smallint not null default 1',
108109
'createdAt' => 'integer not null',
109110
'updatedAt' => 'integer not null',
@@ -131,6 +132,7 @@ protected function setupTestDbData()
131132
'createdBy' => 1,
132133
'updatedBy' => 1,
133134
'relatedTo' => 'test comment',
135+
'url' => 'custom-url',
134136
'createdAt' => time(),
135137
'updatedAt' => time()
136138
])->execute();
@@ -154,4 +156,4 @@ protected function createRuntimeFolder()
154156
{
155157
FileHelper::createDirectory(dirname(__DIR__) . '/tests/runtime');
156158
}
157-
}
159+
}

views/manage/index.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use yii\helpers\Html;
55
use yii\helpers\Json;
66
use yii\helpers\StringHelper;
7+
use yii\helpers\Url;
78
use yii\widgets\Pjax;
89
use yii2mod\comments\models\enums\CommentStatus;
910
use yii2mod\editable\EditableColumn;
@@ -71,7 +72,26 @@
7172
[
7273
'header' => 'Actions',
7374
'class' => 'yii\grid\ActionColumn',
74-
'template' => '{update}{delete}',
75+
'template' => '{view}{update}{delete}',
76+
'buttons' => [
77+
'view' => function ($url, $model, $key) {
78+
$title = Yii::t('yii2mod.comments', 'View');
79+
$options = [
80+
'title' => $title,
81+
'aria-label' => $title,
82+
'data-pjax' => '0',
83+
'target' => '_blank'
84+
];
85+
$icon = Html::tag('span', '', ['class' => 'glyphicon glyphicon-eye-open']);
86+
$url = $model->getViewUrl();
87+
88+
if (!empty($url)) {
89+
return Html::a($icon, $url, $options);
90+
}
91+
92+
return null;
93+
}
94+
]
7595
]
7696
],
7797
]);

widgets/views/_list.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<?php echo Html::tag('meta', null, ['content' => Yii::$app->formatter->asDatetime($comment->updatedAt, 'php:c'), 'itemprop' => 'dateModified']); ?>
3131
<div class="comment-author-name" itemprop="creator" itemscope itemtype="http://schema.org/Person">
3232
<span itemprop="name"><?php echo $comment->getAuthorName(); ?></span>
33-
<?php echo Html::a($comment->getPostedDate(), "#comment-{$comment->id}", ['class' => 'comment-date']); ?>
33+
<?php echo Html::a($comment->getPostedDate(), $comment->getAnchorUrl(), ['class' => 'comment-date']); ?>
3434
</div>
3535
<div class="comment-body" itemprop="text">
3636
<?php echo $comment->getContent(); ?>
@@ -44,4 +44,4 @@
4444
<?php endif; ?>
4545
</li>
4646
<?php endforeach; ?>
47-
<?php endif; ?>
47+
<?php endif; ?>

0 commit comments

Comments
 (0)