diff --git a/Module.php b/Module.php index 56fc799..1ba8372 100755 --- a/Module.php +++ b/Module.php @@ -34,7 +34,7 @@ * @var string comment model class */ public $commentModelClass = null; - + public $ratingModelClass = null; /** @@ -63,7 +63,7 @@ * @var bool */ public $enableRating = true; - + /** * Whether to enable comment premoderate or not. * @@ -72,6 +72,11 @@ public $enablePremoderate = true; /** + * @var string[] $cacheModels + */ + public $cacheModels = []; + + /** * Initializes the module. * This method is called after the module is created and initialized with property values * given in configuration. The default implementation will initialize @@ -99,7 +104,7 @@ } parent::init(); } - + /** * Prompt to register current module * @@ -110,4 +115,33 @@ throw new InvalidConfigException(\Yii::t('artbox-comment', 'Register artbox-comment module')); } + /** + * Rating model factory + * + * @param $config + * + * @return \yii\db\ActiveRecord + */ + public function createRating($config) + { + /** + * @var \yii\db\ActiveRecord $model + */ + $model = \Yii::createObject( + array_merge( + [ + 'class' => $this->ratingModelClass, + ], + $config + ) + ); + $behaviors = []; + foreach ($this->cacheModels as $cacheModel) { + $behaviors[] = \Yii::createObject($cacheModel); + } + if (!empty($behaviors)) { + $model->attachBehaviors($behaviors); + } + return $model; + } } diff --git a/controllers/DefaultController.php b/controllers/DefaultController.php index 6c7448a..d1c66b6 100755 --- a/controllers/DefaultController.php +++ b/controllers/DefaultController.php @@ -3,7 +3,6 @@ namespace artbox\webcomment\controllers; use artbox\webcomment\models\CommentModel; - use artbox\webcomment\models\RatingModel; use artbox\webcomment\Module; use yii\filters\AccessControl; use yii\filters\VerbFilter; @@ -45,7 +44,7 @@ ], ]; } - + /** * Create comment. * @@ -80,11 +79,7 @@ $model->setAttributes($entity_data); if ($model->save()) { if (empty($model->parent_id) && $module->enableRating) { - $ratingModelClass = $module->ratingModelClass; - /** - * @var RatingModel $rating - */ - $rating = new $ratingModelClass( + $rating = $module->createRating( [ 'model' => $model::className(), 'model_id' => $model->primaryKey, diff --git a/models/CommentModel.php b/models/CommentModel.php index 9007156..1ae618f 100755 --- a/models/CommentModel.php +++ b/models/CommentModel.php @@ -33,6 +33,7 @@ * @property string $link * @property \artbox\webcomment\models\CommentModel[] $children * @property \artbox\order\models\Customer $customer + * @property \artbox\webcomment\models\RatingModel $rating */ class CommentModel extends ActiveRecord implements CommentInterface { @@ -49,7 +50,7 @@ public $encryptedEntity; public $entityId; - + /** * @inheritdoc */ @@ -85,7 +86,7 @@ ]; return $scenarios; } - + /** * @inheritdoc */ @@ -93,7 +94,7 @@ { return '{{%artbox_comment}}'; } - + /** * @inheritdoc */ @@ -139,7 +140,7 @@ ], ]; } - + /** * @inheritdoc */ @@ -188,7 +189,7 @@ ], ]; } - + /** * @inheritdoc */ @@ -212,7 +213,7 @@ 'entity_id' => \Yii::t('artbox-comment', 'Entity ID'), ]; } - + /** * Set Entity of Comment model * @@ -222,7 +223,7 @@ { $this->entity = $entity; } - + /** * Get Entity of Comment model * @@ -232,7 +233,7 @@ { return $this->entity; } - + /** * Get ActiveDataProvider of comments for particular Entity according to its EntityId * @@ -281,7 +282,7 @@ ] ); } - + /** * Delete comment * @@ -296,7 +297,7 @@ } return false; } - + /** * Set EntityId of Comment model * @@ -306,7 +307,7 @@ { $this->entityId = $entityId; } - + /** * Get EntityId of Comment model * @@ -316,7 +317,7 @@ { return $this->entityId; } - + /** * Get children relation for current comment * @@ -328,7 +329,7 @@ ->andFilterWhere([ 'status' => self::STATUS_ACTIVE ]) ->inverseOf('parent'); } - + /** * Get parent relation for current comment * @@ -339,7 +340,7 @@ return $this->hasOne(self::className(), [ 'id' => 'parent_id' ]) ->inverseOf('children'); } - + /** * Get customer relation for current comment * @@ -349,7 +350,7 @@ { return $this->hasOne(self::getCustomerClass(), [ 'id' => 'customer_id' ]); } - + /** * Get rating relation for current model * @@ -366,7 +367,7 @@ ] ); } - + /** * Get entity model for current comment or false if not ActiveRecord * @@ -387,7 +388,7 @@ return false; } } - + /** * Get Customer model name * -- libgit2 0.21.4