Skip to content

Commit 7edcc82

Browse files
author
Igor Chepurnoy
committed
add relatedTo attribute to CommentModel
1 parent 8fd1820 commit 7edcc82

File tree

7 files changed

+54
-37
lines changed

7 files changed

+54
-37
lines changed

Module.php

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

33
namespace yii2mod\comments;
44

5+
use Yii;
56
use yii2mod\comments\models\CommentModel;
67

78
/**
@@ -45,7 +46,7 @@ class Module extends \yii\base\Module
4546
public function init()
4647
{
4748
if ($this->userIdentityClass === null) {
48-
$this->userIdentityClass = \Yii::$app->getUser()->identityClass;
49+
$this->userIdentityClass = Yii::$app->getUser()->identityClass;
4950
}
5051
if ($this->commentModelClass === null) {
5152
$this->commentModelClass = CommentModel::className();

controllers/DefaultController.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ public function actionCreate($entity)
5151
if ($decryptEntity !== false) {
5252
$entityData = Json::decode($decryptEntity);
5353
/* @var $model CommentModel */
54-
$model = new $commentModelClass([
55-
'entity' => $entityData['entity'],
56-
'entityId' => $entityData['entityId'],
57-
]);
54+
$model = new $commentModelClass;
55+
$model->setAttributes($entityData);
5856
if ($model->load(Yii::$app->request->post()) && $model->save()) {
5957
return ['status' => 'success'];
6058
} else {

migrations/m010101_100001_init_comment.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function up()
2727
'level' => 'TINYINT(3) NOT NULL DEFAULT 1',
2828
'createdBy' => Schema::TYPE_INTEGER . ' NOT NULL',
2929
'updatedBy' => Schema::TYPE_INTEGER . ' NOT NULL',
30+
'relatedTo' => $this->string(500)->notNull(),
3031
'status' => 'TINYINT(2) NOT NULL DEFAULT 1',
3132
'createdAt' => Schema::TYPE_INTEGER . ' NOT NULL',
3233
'updatedAt' => Schema::TYPE_INTEGER . ' NOT NULL',

models/CommentModel.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* @property string $content
2323
* @property integer $createdBy
2424
* @property integer $updatedBy
25+
* @property string $relatedTo
2526
* @property integer $status
2627
* @property integer $level
2728
* @property integer $createdAt
@@ -52,7 +53,7 @@ public function rules()
5253
{
5354
return [
5455
[['entity', 'entityId', 'content'], 'required'],
55-
[['content', 'entity'], 'string'],
56+
[['content', 'entity', 'relatedTo'], 'string'],
5657
['parentId', 'validateParentID'],
5758
[['entityId', 'parentId', 'createdBy', 'updatedBy', 'status', 'createdAt', 'updatedAt', 'level'], 'integer'],
5859
];
@@ -113,6 +114,7 @@ public function attributeLabels()
113114
'level' => Yii::t('app', 'Level'),
114115
'createdBy' => Yii::t('app', 'Created by'),
115116
'updatedBy' => Yii::t('app', 'Updated by'),
117+
'relatedTo' => Yii::t('app', 'Related to'),
116118
'createdAt' => Yii::t('app', 'Created date'),
117119
'updatedAt' => Yii::t('app', 'Updated date'),
118120
];
@@ -294,15 +296,6 @@ public function getAvatar($imgOptions = [])
294296
return Html::img("http://gravatar.com/avatar/{$this->author->id}/?s=50", $imgOptions);
295297
}
296298

297-
/**
298-
* Return hash value of class name
299-
* @param $className
300-
* @return string
301-
*/
302-
public static function hashEntityClass($className)
303-
{
304-
return hash('crc32', $className);
305-
}
306299

307300
/**
308301
* This function used for filter in gridView, for attribute `createdBy`.

models/CommentSearchModel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class CommentSearchModel extends CommentModel
1818
public function rules()
1919
{
2020
return ArrayHelper::merge([
21-
[['id', 'createdBy', 'content', 'status'], 'safe'],
21+
[['id', 'createdBy', 'content', 'status', 'relatedTo'], 'safe'],
2222
], parent::rules());
2323
}
2424

@@ -52,6 +52,7 @@ public function search($params, $pageSize = 20)
5252
$query->andFilterWhere(['createdBy' => $this->createdBy]);
5353
$query->andFilterWhere(['status' => $this->status]);
5454
$query->andFilterWhere(['like', 'content', $this->content]);
55+
$query->andFilterWhere(['like', 'relatedTo', $this->relatedTo]);
5556

5657
return $dataProvider;
5758
}

views/manage/index.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
use yii\grid\GridView;
44
use yii\helpers\Html;
55
use yii\helpers\Json;
6+
use yii\helpers\StringHelper;
67
use yii\widgets\Pjax;
78
use yii2mod\comments\models\enums\CommentStatus;
89
use yii2mod\editable\EditableColumn;
910

1011
/* @var $this yii\web\View */
1112
/* @var $dataProvider yii\data\ActiveDataProvider */
1213
/* @var $searchModel \yii2mod\comments\models\CommentSearchModel */
14+
/* @var $commentModel \yii2mod\comments\models\CommentModel */
1315

1416
$this->title = Yii::t('app', 'Comments Management');
1517
$this->params['breadcrumbs'][] = $this->title;
@@ -30,9 +32,10 @@
3032
'attribute' => 'content',
3133
'contentOptions' => ['style' => 'max-width: 350px;'],
3234
'value' => function ($model) {
33-
return \yii\helpers\StringHelper::truncate($model->content, 100);
35+
return StringHelper::truncate($model->content, 100);
3436
}
3537
],
38+
'attribute' => 'relatedTo',
3639
[
3740
'attribute' => 'createdBy',
3841
'value' => function ($model) {

widgets/Comment.php

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ class Comment extends Widget
2020
*/
2121
public $model;
2222

23+
/**
24+
* @var string relatedTo custom text, for example: cms url: about-us, john comment about us page, etc.
25+
* By default - className:primaryKey of the current model
26+
*/
27+
public $relatedTo = '';
28+
2329
/**
2430
* @var string the view file that will render the comment tree and form for posting comments.
2531
*/
@@ -46,21 +52,47 @@ class Comment extends Widget
4652
public $clientOptions = [];
4753

4854
/**
49-
* @var null pjax container id
55+
* @var string hash(crc32) from class name of the widget model
56+
*/
57+
protected $entity;
58+
59+
/**
60+
* @var integer primary key value of the widget model
61+
*/
62+
protected $entityId;
63+
64+
/**
65+
* @var string encrypted entity key from params: entity, entityId, relatedTo
5066
*/
51-
private $pjaxContainerId = null;
67+
protected $encryptedEntityKey;
5268

5369
/**
54-
* Initializes the object.
55-
* This method is invoked at the end of the constructor after the object is initialized with the
56-
* given configuration.
70+
* @var string pjax container id, generated automatically
71+
*/
72+
protected $pjaxContainerId;
73+
74+
/**
75+
* Initializes the widget params.
5776
*/
5877
public function init()
5978
{
60-
if ($this->model === null) {
79+
if (empty($this->model)) {
6180
throw new InvalidConfigException('The "model" property must be set.');
6281
}
6382
$this->pjaxContainerId = 'comment-pjax-container-' . $this->getId();
83+
$this->entity = hash('crc32', get_class($this->model));
84+
$this->entityId = $this->model->{$this->entityIdAttribute};
85+
if (empty($this->entityId)) {
86+
throw new InvalidConfigException('The "entityIdAttribute" value for widget model cannot be empty.');
87+
}
88+
if (empty($this->relatedTo)) {
89+
$this->relatedTo = get_class($this->model) . ':' . $this->entityId;
90+
}
91+
$this->encryptedEntityKey = Yii::$app->getSecurity()->encryptByKey(Json::encode([
92+
'entity' => $this->entity,
93+
'entityId' => $this->entityId,
94+
'relatedTo' => $this->relatedTo
95+
]), Module::$name);
6496
$this->registerAssets();
6597
}
6698

@@ -72,27 +104,15 @@ public function run()
72104
{
73105
/* @var $module Module */
74106
$module = Yii::$app->getModule(Module::$name);
75-
//Get comment model class from `comment` Module
76107
$commentModelClass = $module->commentModelClass;
77-
//Get entity from widget and hash it.
78-
$entity = $this->model;
79-
$entityId = $entity->{$this->entityIdAttribute};
80-
$entity = hash('crc32', $entity::className());
81-
//Get comment tree by entity and entityId
82-
$comments = $commentModelClass::getTree($entity, $entityId, $this->maxLevel);
83-
//Create comment model
84108
$commentModel = Yii::createObject($commentModelClass);
85-
//Encrypt entity and entityId values
86-
$encryptedEntity = Yii::$app->getSecurity()->encryptByKey(Json::encode([
87-
'entity' => $entity,
88-
'entityId' => $entityId
89-
]), $module::$name);
109+
$comments = $commentModelClass::getTree($this->entity, $this->entityId, $this->maxLevel);
90110

91111
return $this->render($this->commentView, [
92112
'comments' => $comments,
93113
'commentModel' => $commentModel,
94114
'maxLevel' => $this->maxLevel,
95-
'encryptedEntity' => $encryptedEntity,
115+
'encryptedEntity' => $this->encryptedEntityKey,
96116
'pjaxContainerId' => $this->pjaxContainerId,
97117
'formId' => $this->formId
98118
]);

0 commit comments

Comments
 (0)