Skip to content

Commit aae211a

Browse files
author
Igor Chepurnoy
committed
add ability to edit comments on frontend
1 parent 3699ee6 commit aae211a

File tree

10 files changed

+108
-40
lines changed

10 files changed

+108
-40
lines changed

CommentAsset.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@
1212
class CommentAsset extends AssetBundle
1313
{
1414
/**
15-
* {@inheritdoc}
15+
* @inheritdoc
1616
*/
1717
public $sourcePath = '@vendor/yii2mod/yii2-comments/assets';
1818

1919
/**
20-
* {@inheritdoc}
20+
* @inheritdoc
2121
*/
2222
public $js = [
2323
'js/comment.js',
2424
];
2525

2626
/**
27-
* {@inheritdoc}
27+
* @inheritdoc
2828
*/
2929
public $css = [
3030
'css/comment.css',
3131
];
3232

3333
/**
34-
* {@inheritdoc}
34+
* @inheritdoc
3535
*/
3636
public $depends = [
3737
'yii\web\JqueryAsset',

Module.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
*/
1212
class Module extends \yii\base\Module
1313
{
14-
/**
15-
* @var string module name
16-
*/
17-
public static $name = 'comment';
18-
1914
/**
2015
* @var string the class name of the [[identity]] object
2116
*/
@@ -31,6 +26,11 @@ class Module extends \yii\base\Module
3126
*/
3227
public $controllerNamespace = 'yii2mod\comments\controllers';
3328

29+
/**
30+
* @var bool whether admin can edit comment on frontend
31+
*/
32+
public $enableInlineEdit = false;
33+
3434
/**
3535
* {@inheritdoc}
3636
*/

controllers/DefaultController.php

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace yii2mod\comments\controllers;
44

55
use Yii;
6+
use yii\filters\AccessControl;
67
use yii\filters\VerbFilter;
78
use yii\helpers\Json;
89
use yii\web\BadRequestHttpException;
@@ -12,8 +13,8 @@
1213
use yii\widgets\ActiveForm;
1314
use yii2mod\comments\events\CommentEvent;
1415
use yii2mod\comments\models\CommentModel;
15-
use yii2mod\comments\Module;
16-
use yii2mod\moderation\ModerationBehavior;
16+
use yii2mod\comments\traits\ModuleTrait;
17+
use yii2mod\editable\EditableAction;
1718

1819
/**
1920
* Class DefaultController
@@ -22,6 +23,8 @@
2223
*/
2324
class DefaultController extends Controller
2425
{
26+
use ModuleTrait;
27+
2528
/**
2629
* Event is triggered before creating a new comment.
2730
* Triggered with yii2mod\comments\events\CommentEvent
@@ -46,12 +49,35 @@ class DefaultController extends Controller
4649
*/
4750
const EVENT_AFTER_DELETE = 'afterDelete';
4851

52+
/**
53+
* @inheritdoc
54+
*/
55+
public function actions()
56+
{
57+
return [
58+
'quick-edit' => [
59+
'class' => EditableAction::class,
60+
'modelClass' => CommentModel::class,
61+
],
62+
];
63+
}
64+
4965
/**
5066
* @inheritdoc
5167
*/
5268
public function behaviors()
5369
{
5470
return [
71+
'access' => [
72+
'class' => AccessControl::class,
73+
'only' => ['quick-edit'],
74+
'rules' => [
75+
[
76+
'allow' => true,
77+
'roles' => ['admin'],
78+
],
79+
],
80+
],
5581
'verbs' => [
5682
'class' => VerbFilter::class,
5783
'actions' => [
@@ -79,7 +105,7 @@ public function behaviors()
79105
public function actionCreate($entity)
80106
{
81107
/* @var $commentModel CommentModel */
82-
$commentModel = Yii::createObject(Yii::$app->getModule(Module::$name)->commentModelClass);
108+
$commentModel = Yii::createObject($this->getModule()->commentModelClass);
83109
$event = Yii::createObject(['class' => CommentEvent::class, 'commentModel' => $commentModel]);
84110
$commentModel->setAttributes($this->getCommentAttributesFromEntity($entity));
85111
$this->trigger(self::EVENT_BEFORE_CREATE, $event);
@@ -124,15 +150,14 @@ public function actionDelete($id)
124150
*
125151
* @param int|array $id Comment ID
126152
*
127-
* @return null|CommentModel|ModerationBehavior
153+
* @return CommentModel
128154
*
129155
* @throws NotFoundHttpException
130156
*/
131157
protected function findModel($id)
132158
{
133-
/** @var CommentModel $model */
134-
$commentModelClass = Yii::$app->getModule(Module::$name)->commentModelClass;
135-
if (($model = $commentModelClass::findOne($id)) !== null) {
159+
$commentModel = $this->getModule()->commentModelClass;
160+
if (($model = $commentModel::findOne($id)) !== null) {
136161
return $model;
137162
} else {
138163
throw new NotFoundHttpException(Yii::t('yii2mod.comments', 'The requested page does not exist.'));
@@ -150,7 +175,7 @@ protected function findModel($id)
150175
*/
151176
protected function getCommentAttributesFromEntity($entity)
152177
{
153-
$decryptEntity = Yii::$app->getSecurity()->decryptByKey(utf8_decode($entity), Module::$name);
178+
$decryptEntity = Yii::$app->getSecurity()->decryptByKey(utf8_decode($entity), $this->getModule()->id);
154179
if ($decryptEntity !== false) {
155180
return Json::decode($decryptEntity);
156181
}

controllers/ManageController.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace yii2mod\comments\controllers;
44

5-
use paulzi\adjacencyList\AdjacencyListBehavior;
65
use Yii;
76
use yii\filters\VerbFilter;
87
use yii\web\Controller;
98
use yii\web\NotFoundHttpException;
109
use yii2mod\comments\models\CommentModel;
11-
use yii2mod\comments\Module;
10+
use yii2mod\comments\traits\ModuleTrait;
1211

1312
/**
1413
* Class ManageController
@@ -17,6 +16,8 @@
1716
*/
1817
class ManageController extends Controller
1918
{
19+
use ModuleTrait;
20+
2021
/**
2122
* @var string path to index view file, which is used in admin panel
2223
*/
@@ -58,7 +59,7 @@ public function actionIndex()
5859
{
5960
$searchModel = Yii::createObject($this->searchClass);
6061
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
61-
$commentModel = Yii::$app->getModule(Module::$name)->commentModelClass;
62+
$commentModel = $this->getModule()->commentModelClass;
6263

6364
return $this->render($this->indexView, [
6465
'dataProvider' => $dataProvider,
@@ -117,11 +118,11 @@ public function actionDelete($id)
117118
*
118119
* @throws NotFoundHttpException if the model cannot be found
119120
*
120-
* @return CommentModel|AdjacencyListBehavior the loaded model
121+
* @return CommentModel
121122
*/
122123
protected function findModel($id)
123124
{
124-
$commentModel = Yii::$app->getModule(Module::$name)->commentModelClass;
125+
$commentModel = $this->getModule()->commentModelClass;
125126

126127
if (($model = $commentModel::findOne($id)) !== null) {
127128
return $model;

models/CommentModel.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use yii\db\ActiveRecord;
1111
use yii\helpers\ArrayHelper;
1212
use yii2mod\behaviors\PurifyBehavior;
13-
use yii2mod\comments\Module;
13+
use yii2mod\comments\traits\ModuleTrait;
1414
use yii2mod\moderation\enums\Status;
1515
use yii2mod\moderation\ModerationBehavior;
1616
use yii2mod\moderation\ModerationQuery;
@@ -35,9 +35,13 @@
3535
* @method ActiveRecord makeRoot()
3636
* @method ActiveRecord appendTo($node)
3737
* @method ActiveQuery getDescendants()
38+
* @method ModerationBehavior markRejected()
39+
* @method AdjacencyListBehavior deleteWithChildren()
3840
*/
3941
class CommentModel extends ActiveRecord
4042
{
43+
use ModuleTrait;
44+
4145
/**
4246
* @var null|array|ActiveRecord[] comment children
4347
*/
@@ -166,7 +170,7 @@ public function beforeSave($insert)
166170
if (parent::beforeSave($insert)) {
167171
if ($this->parentId > 0) {
168172
$parentNodeLevel = static::find()->select('level')->where(['id' => $this->parentId])->scalar();
169-
$this->level = $parentNodeLevel + 1;
173+
$this->level += $parentNodeLevel;
170174
}
171175

172176
return true;
@@ -214,9 +218,7 @@ public function saveComment()
214218
*/
215219
public function getAuthor()
216220
{
217-
$module = Yii::$app->getModule(Module::$name);
218-
219-
return $this->hasOne($module->userIdentityClass, ['id' => 'createdBy']);
221+
return $this->hasOne($this->getModule()->userIdentityClass, ['id' => 'createdBy']);
220222
}
221223

222224
/**

models/search/CommentSearch.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class CommentSearch extends CommentModel
2323
public function rules()
2424
{
2525
return [
26-
[['id', 'createdBy', 'content', 'status', 'relatedTo'], 'safe'],
26+
[['id', 'createdBy', 'status'], 'integer'],
27+
[['content', 'relatedTo'], 'safe'],
2728
];
2829
}
2930

@@ -34,7 +35,7 @@ public function rules()
3435
*
3536
* @return ActiveDataProvider
3637
*/
37-
public function search($params)
38+
public function search(array $params)
3839
{
3940
$query = CommentModel::find();
4041

@@ -49,15 +50,18 @@ public function search($params)
4950
'defaultOrder' => ['id' => SORT_DESC],
5051
]);
5152

52-
// load the search form data and validate
53-
if (!($this->load($params))) {
53+
$this->load($params);
54+
55+
if (!$this->validate()) {
5456
return $dataProvider;
5557
}
5658

57-
// adjust the query by adding the filters
58-
$query->andFilterWhere(['id' => $this->id]);
59-
$query->andFilterWhere(['createdBy' => $this->createdBy]);
60-
$query->andFilterWhere(['status' => $this->status]);
59+
$query->andFilterWhere([
60+
'id' => $this->id,
61+
'createdBy' => $this->createdBy,
62+
'status' => $this->status,
63+
]);
64+
6165
$query->andFilterWhere(['like', 'content', $this->content]);
6266
$query->andFilterWhere(['like', 'relatedTo', $this->relatedTo]);
6367

traits/ModuleTrait.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace yii2mod\comments\traits;
4+
5+
use Yii;
6+
use yii2mod\comments\Module;
7+
8+
/**
9+
* Class ModuleTrait
10+
*
11+
* @package yii2mod\comments\traits
12+
*/
13+
trait ModuleTrait
14+
{
15+
/**
16+
* @return Module
17+
*/
18+
public function getModule()
19+
{
20+
return Yii::$app->getModule('comment');
21+
}
22+
}

views/manage/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
/* @var $this yii\web\View */
1010
/* @var $dataProvider yii\data\ActiveDataProvider */
11-
/* @var $searchModel \yii2mod\comments\models\CommentSearchModel */
11+
/* @var $searchModel \yii2mod\comments\models\search\CommentSearch */
1212
/* @var $commentModel \yii2mod\comments\models\CommentModel */
1313

1414
$this->title = Yii::t('yii2mod.comments', 'Comments Management');

widgets/Comment.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use yii\helpers\Json;
1010
use yii2mod\comments\CommentAsset;
1111
use yii2mod\comments\models\CommentModel;
12-
use yii2mod\comments\Module;
12+
use yii2mod\comments\traits\ModuleTrait;
1313

1414
/**
1515
* Class Comment
@@ -18,6 +18,8 @@
1818
*/
1919
class Comment extends Widget
2020
{
21+
use ModuleTrait;
22+
2123
/**
2224
* @var \yii\db\ActiveRecord|null Widget model
2325
*/
@@ -134,7 +136,7 @@ public function init()
134136
*/
135137
public function run()
136138
{
137-
$commentClass = Yii::$app->getModule(Module::$name)->commentModelClass;
139+
$commentClass = $this->getModule()->commentModelClass;
138140
$commentModel = Yii::createObject([
139141
'class' => $commentClass,
140142
'entity' => $this->entity,
@@ -165,7 +167,7 @@ protected function getEncryptedEntity()
165167
'entity' => $this->entity,
166168
'entityId' => $this->entityId,
167169
'relatedTo' => $this->relatedTo,
168-
]), Module::$name));
170+
]), $this->getModule()->id));
169171
}
170172

171173
/**

widgets/views/_list.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use yii\helpers\Html;
44
use yii\helpers\Url;
5+
use yii2mod\editable\Editable;
56

67
/* @var $this \yii\web\View */
78
/* @var $model \yii2mod\comments\models\CommentModel */
@@ -26,7 +27,18 @@
2627
<?php echo Html::a($model->getPostedDate(), $model->getAnchorUrl(), ['class' => 'comment-date']); ?>
2728
</div>
2829
<div class="comment-body">
29-
<?php echo $model->getContent(); ?>
30+
<?php if (Yii::$app->getModule('comment')->enableInlineEdit): ?>
31+
<?php echo Editable::widget([
32+
'model' => $model,
33+
'attribute' => 'content',
34+
'url' => '/comment/default/quick-edit',
35+
'options' => [
36+
'id' => 'editable-comment-' . $model->id,
37+
]
38+
]); ?>
39+
<?php else: ?>
40+
<?php echo $model->getContent(); ?>
41+
<?php endif; ?>
3042
</div>
3143
</div>
3244
</div>

0 commit comments

Comments
 (0)