Skip to content

Commit c58fb73

Browse files
author
Igor Chepurnoy
authored
Merge pull request #78 from nrob81/master
skip the validation of parentId on deletion
2 parents 2083417 + 057c958 commit c58fb73

10 files changed

+17
-14
lines changed

Module.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function init()
3838
{
3939
parent::init();
4040

41-
if ($this->userIdentityClass === null) {
41+
if (null === $this->userIdentityClass) {
4242
$this->userIdentityClass = Yii::$app->getUser()->identityClass;
4343
}
4444
}

controllers/DefaultController.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public function actionCreate($entity)
131131
public function actionDelete($id)
132132
{
133133
$commentModel = $this->findModel($id);
134+
$commentModel->setScenario(CommentModel::SCENARIO_MODERATION);
134135
$event = Yii::createObject(['class' => CommentEvent::class, 'commentModel' => $commentModel]);
135136
$this->trigger(self::EVENT_BEFORE_DELETE, $event);
136137

@@ -157,7 +158,7 @@ public function actionDelete($id)
157158
protected function findModel($id)
158159
{
159160
$commentModel = $this->getModule()->commentModelClass;
160-
if (($model = $commentModel::findOne($id)) !== null) {
161+
if (null !== ($model = $commentModel::findOne($id))) {
161162
return $model;
162163
} else {
163164
throw new NotFoundHttpException(Yii::t('yii2mod.comments', 'The requested page does not exist.'));
@@ -176,7 +177,7 @@ protected function findModel($id)
176177
protected function getCommentAttributesFromEntity($entity)
177178
{
178179
$decryptEntity = Yii::$app->getSecurity()->decryptByKey(utf8_decode($entity), $this->getModule()->id);
179-
if ($decryptEntity !== false) {
180+
if (false !== $decryptEntity) {
180181
return Json::decode($decryptEntity);
181182
}
182183

controllers/ManageController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ protected function findModel($id)
142142
{
143143
$commentModel = $this->getModule()->commentModelClass;
144144

145-
if (($model = $commentModel::findOne($id)) !== null) {
145+
if (null !== ($model = $commentModel::findOne($id))) {
146146
return $model;
147147
} else {
148148
throw new NotFoundHttpException(Yii::t('yii2mod.comments', 'The requested page does not exist.'));

migrations/m010101_100001_init_comment.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function up()
1414
{
1515
$tableOptions = null;
1616

17-
if ($this->db->driverName === 'mysql') {
17+
if ('mysql' === $this->db->driverName) {
1818
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
1919
}
2020

migrations/m161109_092304_rename_comment_table.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ class m161109_092304_rename_comment_table extends Migration
66
{
77
public function up()
88
{
9-
if (Yii::$app->db->schema->getTableSchema('{{%comment}}') === null) {
9+
if (null === Yii::$app->db->schema->getTableSchema('{{%comment}}')) {
1010
$this->renameTable('{{%Comment}}', '{{%comment}}');
1111
}
1212
}
1313

1414
public function down()
1515
{
16-
if (Yii::$app->db->schema->getTableSchema('{{%Comment}}') === null) {
16+
if (null === Yii::$app->db->schema->getTableSchema('{{%Comment}}')) {
1717
$this->renameTable('{{%comment}}', '{{%Comment}}');
1818
}
1919
}

models/CommentModel.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class CommentModel extends ActiveRecord
4242
{
4343
use ModuleTrait;
4444

45+
const SCENARIO_MODERATION = 'moderation';
46+
4547
/**
4648
* @var null|array|ActiveRecord[] comment children
4749
*/
@@ -67,7 +69,7 @@ public function rules()
6769
['status', 'default', 'value' => Status::APPROVED],
6870
['status', 'in', 'range' => Status::getConstantsByName()],
6971
['level', 'default', 'value' => 1],
70-
['parentId', 'validateParentID'],
72+
['parentId', 'validateParentID', 'except' => static::SCENARIO_MODERATION],
7173
[['entityId', 'parentId', 'status', 'level'], 'integer'],
7274
];
7375
}
@@ -77,7 +79,7 @@ public function rules()
7779
*/
7880
public function validateParentID($attribute)
7981
{
80-
if ($this->{$attribute} !== null) {
82+
if (null !== $this->{$attribute}) {
8183
$parentCommentExist = static::find()
8284
->approved()
8385
->andWhere([

views/manage/index.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
?>
1717
<div class="comments-index">
1818

19-
<h1><?php echo Html::encode($this->title) ?></h1>
19+
<h1><?php echo Html::encode($this->title); ?></h1>
2020
<?php Pjax::begin(['timeout' => 10000]); ?>
2121
<?php echo GridView::widget([
2222
'dataProvider' => $dataProvider,

views/manage/update.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
?>
1616
<div class="comment-update">
1717

18-
<h1><?php echo Html::encode($this->title) ?></h1>
18+
<h1><?php echo Html::encode($this->title); ?></h1>
1919

2020
<div class="comment-form">
2121
<?php $form = ActiveForm::begin(); ?>
@@ -30,7 +30,7 @@
3030
?>
3131
<?php echo $form->field($model, 'status')->dropDownList(Status::listData()); ?>
3232
<div class="form-group">
33-
<?php echo Html::submitButton(Yii::t('yii2mod.comments', 'Update'), ['class' => 'btn btn-primary']) ?>
33+
<?php echo Html::submitButton(Yii::t('yii2mod.comments', 'Update'), ['class' => 'btn btn-primary']); ?>
3434
<?php echo Html::a(Yii::t('yii2mod.comments', 'Go Back'), ['index'], ['class' => 'btn btn-default']); ?>
3535
</div>
3636
<?php ActiveForm::end(); ?>

widgets/views/_form.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
'validateOnBlur' => false,
2121
]); ?>
2222

23-
<?php echo $form->field($commentModel, 'content', ['template' => '{input}{error}'])->textarea(['placeholder' => Yii::t('yii2mod.comments', 'Add a comment...'), 'rows' => 4, 'data' => ['comment' => 'content']]) ?>
23+
<?php echo $form->field($commentModel, 'content', ['template' => '{input}{error}'])->textarea(['placeholder' => Yii::t('yii2mod.comments', 'Add a comment...'), 'rows' => 4, 'data' => ['comment' => 'content']]); ?>
2424
<?php echo $form->field($commentModel, 'parentId', ['template' => '{input}'])->hiddenInput(['data' => ['comment' => 'parent-id']]); ?>
2525
<div class="comment-box-partial">
2626
<div class="button-container show">

widgets/views/_list.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/* @var $maxLevel null|integer comments max level */
1010
?>
1111
<li class="comment" id="comment-<?php echo $model->id; ?>">
12-
<div class="comment-content" data-comment-content-id="<?php echo $model->id ?>">
12+
<div class="comment-content" data-comment-content-id="<?php echo $model->id; ?>">
1313
<div class="comment-author-avatar">
1414
<?php echo Html::img($model->getAvatar(), ['alt' => $model->getAuthorName()]); ?>
1515
</div>

0 commit comments

Comments
 (0)