From faff2c48bfd09f5ee437dc3d513eb8ed4665c2b0 Mon Sep 17 00:00:00 2001 From: Yarik Date: Thu, 25 May 2017 18:03:35 +0300 Subject: [PATCH] Artbox comment created --- CHANGELOG.md | 7 +++++++ LICENSE.md | 32 ++++++++++++++++++++++++++++++++ Module.php | 57 ++++++++++++++++++++++++++++++++++++++------------------- README.md | 31 +++++++++++++++++++++++++++++++ assets/CommentAsset.php | 5 ++--- behaviors/CommentBehavior.php | 35 ++++++++++++++++++++--------------- behaviors/ParentBehavior.php | 20 ++++++++++---------- composer.json | 12 ++++++------ controllers/DefaultController.php | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------- controllers/ManageController.php | 43 +++++++++++++++++++++++++++++++------------ migrations/m160724_162347_artbox_comment.php | 74 ++++++++++++++++++++++++++++++++++---------------------------------------- migrations/m160726_092634_add_entity_fields.php | 23 ----------------------- migrations/m160726_211227_create_artbox_comment_rating.php | 32 +++++++++++--------------------- migrations/order-to-comment/m170525_095043_order_to_comment.php | 35 +++++++++++++++++++++++++++++++++++ migrations/order-to-comment/m170525_095044_order_to_rating.php | 25 +++++++++++++++++++++++++ models/CommentModel.php | 326 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------- models/CommentModelSearch.php | 42 ++++++++++++++++++++++-------------------- models/LikeModel.php | 3 +-- models/RatingModel.php | 38 ++++++++++++++++++++++++-------------- models/interfaces/CommentInterface.php | 3 +-- models/interfaces/RatingCacheInterface.php | 2 +- resources/artbox_comment.js | 10 ++++++++-- views/artbox_comment_form.php | 82 +++++++++++++++++++++++++++++++++++++++++++++++----------------------------------- views/artbox_comment_item.php | 244 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------------------------------------------------------------- views/artbox_comment_list.php | 2 +- views/artbox_comment_reply.php | 19 +++++++++++-------- views/manage/answer.php | 19 +++++++++---------- views/manage/index.php | 75 +++++++++++++++++++++++++++++++++++++++++---------------------------------- views/manage/update.php | 10 +++++----- widgets/CommentWidget.php | 196 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------- 30 files changed, 969 insertions(+), 617 deletions(-) create mode 100755 CHANGELOG.md create mode 100755 LICENSE.md create mode 100755 README.md delete mode 100755 migrations/m160726_092634_add_entity_fields.php create mode 100755 migrations/order-to-comment/m170525_095043_order_to_comment.php create mode 100755 migrations/order-to-comment/m170525_095044_order_to_rating.php mode change 100644 => 100755 views/manage/answer.php diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100755 index 0000000..807169e --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,7 @@ +# Change Log +All notable changes to this project will be documented in this file. + +## 1.0.0 - 2017-03-21 +### Added +- This CHANGELOG file to hopefully serve as an evolving example of a standardized open source project CHANGELOG. +- Added initial Artbox Comment extension. \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100755 index 0000000..e98f03d --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,32 @@ +The Yii framework is free software. It is released under the terms of +the following BSD License. + +Copyright © 2008 by Yii Software LLC (http://www.yiisoft.com) +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + * Neither the name of Yii Software LLC nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. diff --git a/Module.php b/Module.php index 673d0a2..412d833 100755 --- a/Module.php +++ b/Module.php @@ -1,14 +1,16 @@ db */ - public $db = NULL; + public $db = null; /** * Key, used to encrypt and decrypt comment service data. + * * @var string Encryption key */ public static $encryptionKey = 'artbox-comment'; /** * Whether to enable comment rating or not. + * * @var bool */ public static $enableRating = true; @@ -66,22 +73,34 @@ */ public function init() { - if($this->commentModelClass === NULL) { + if ($this->commentModelClass === null) { $this->commentModelClass = CommentModel::className(); } - if(self::$enableRating && $this->ratingModelClass === NULL) { + if (self::$enableRating && $this->ratingModelClass === null) { $this->ratingModelClass = RatingModel::className(); } - if(\Yii::$app instanceof Application) { - $this->controllerNamespace = 'artweb\artbox\comment\commands'; - } elseif($this->userIdentityClass === NULL) { + if (\Yii::$app instanceof Application) { + $this->controllerNamespace = 'artbox\webcomment\commands'; + } elseif ($this->userIdentityClass === null) { $this->userIdentityClass = Yii::$app->getUser()->identityClass; } - if($this->db === NULL) { + if ($this->db === null) { $this->db = \Yii::$app->db; } - Yii::setAlias('@artbox-comment', __DIR__); + if (!Yii::getAlias('@artbox/webcomment', false)) { + Yii::setAlias('@artbox/webcomment', __DIR__); + } parent::init(); } + + /** + * Prompt to register current module + * + * @throws \yii\base\InvalidConfigException + */ + public static function registerMe() + { + throw new InvalidConfigException(\Yii::t('artbox-comment', 'Register artbox-comment module')); + } } diff --git a/README.md b/README.md new file mode 100755 index 0000000..b493584 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +Artbox Comment +=============================== + +Artbox Comment is an extension for working with comments developed by Artweb written with [Yii 2 framework](http://www.yiiframework.com/). + +Comment extension includes functionality for creating and maintaining comments on the website. + +This extension works with Artbox Core application and enough to add fully functional comment system to the website. + +To prepare your application you should run migrations: + + php yii migrate --migationPath=vendor/artweb/artbox-webcomment/migrations + +DIRECTORY STRUCTURE +------------------- + +``` +assets contains AssetBundles +behaviors contains Behaviors classes +components contains custom Classes, which don't belong to other groups +controllers contains controllers for core models +helpers contains helper classes to manipulate, for example static files + and HTML +messages contains translations for core strings +migrations contains migrations, which should be applied after extension + installation +models contains core models +views contains views files for core controllers +web contains assets and other files, which should be web available +widgets contains widgets +``` diff --git a/assets/CommentAsset.php b/assets/CommentAsset.php index c0e0ea7..8b8fe45 100755 --- a/assets/CommentAsset.php +++ b/assets/CommentAsset.php @@ -1,12 +1,11 @@ cacheRating) { - if (empty( $this->cacheModelName ) || !is_string($this->cacheModelName)) { + if (empty($this->cacheModelName) || !is_string($this->cacheModelName)) { throw new InvalidConfigException( 'To use rating cache you must set $cacheModelName where to store it' ); @@ -67,7 +68,7 @@ * * @return string */ - public function getCacheModelName():string + public function getCacheModelName(): string { return $this->cacheModelName; } @@ -84,10 +85,8 @@ /** * Get model to hold rating cache - * - * @return \artweb\artbox\comment\models\interfaces\RatingCacheInterface */ - public function getCacheModel():RatingCacheInterface + public function getCacheModel(): RatingCacheInterface { return $this->cacheModel; } @@ -95,7 +94,7 @@ /** * Set model to hold cache * - * @param \artweb\artbox\comment\models\interfaces\RatingCacheInterface|null $value + * @param \artbox\webcomment\models\interfaces\RatingCacheInterface|null $value */ private function setCacheModel(RatingCacheInterface $value = null) { @@ -133,13 +132,19 @@ * @var ActiveRecord $owner */ $owner = $this->owner; - $pk = $owner->primaryKey()[ 0 ]; + $pkKeys = $owner->primaryKey(); + if (!empty($pkKeys)) { + $pkKey = $pkKeys[ 0 ]; + } else { + throw new InvalidParamException('Entity must have primary key.'); + } + $pk = $owner->getAttribute($pkKey); $query = $owner->hasMany(CommentModel::className(), [ 'entity_id' => $pk ]) ->where( [ - 'artbox_comment.entity' => $owner::className(), - 'artbox_comment.status' => CommentModel::STATUS_ACTIVE, - 'artbox_comment.artbox_comment_pid' => null, + 'artbox_comment.entity' => $owner::className(), + 'artbox_comment.status' => CommentModel::STATUS_ACTIVE, + 'artbox_comment.parent_id' => null, ] ); return $query; @@ -180,7 +185,7 @@ } $averageRating = $this->getAverageRating() ->one(); - if (!empty( $averageRating )) { + if (!empty($averageRating)) { $averageRating->setValue($average); } else { /** diff --git a/behaviors/ParentBehavior.php b/behaviors/ParentBehavior.php index eab7c88..6b8bef5 100755 --- a/behaviors/ParentBehavior.php +++ b/behaviors/ParentBehavior.php @@ -1,9 +1,9 @@ owner; - if(!empty( $owner->artbox_comment_pid )) { + if (!empty($owner->parent_id)) { /** * @var CommentModel $parent */ $parent = CommentModel::find() - ->where([ 'artbox_comment_id' => $owner->artbox_comment_pid ]) + ->where([ 'id' => $owner->parent_id ]) ->one(); - if(!empty( $parent->artbox_comment_pid )) { - $owner->related_id = $owner->artbox_comment_pid; - $owner->artbox_comment_pid = $parent->artbox_comment_pid; + if (!empty($parent->parent_id)) { + $owner->related_id = $owner->parent_id; + $owner->parent_id = $parent->parent_id; } } } diff --git a/composer.json b/composer.json index b5980b8..cca0d34 100755 --- a/composer.json +++ b/composer.json @@ -1,17 +1,17 @@ { - "name": "artweb/artbox-comment", - "description": "Yii2 light-weight CMS", + "name": "artweb/artbox-webcomment", + "description": "Artbox comment extension", "license": "BSD-3-Clause", "minimum-stability": "dev", "type": "yii2-extension", "require": { - "php": ">=5.4", - "yiisoft/yii2": "*", - "artweb/artbox": "dev-master" + "php": ">=7.0", + "yiisoft/yii2": "~2.0", + "artweb/artbox-core": "~0.0.1" }, "autoload": { "psr-4": { - "artweb\\artbox\\comment\\": "" + "artbox\\webcomment\\": "" } } } \ No newline at end of file diff --git a/controllers/DefaultController.php b/controllers/DefaultController.php index 3ebecda..8d36503 100755 --- a/controllers/DefaultController.php +++ b/controllers/DefaultController.php @@ -1,9 +1,10 @@ response->format = Response::FORMAT_JSON; /* @var $module Module */ $module = \Yii::$app->getModule(Module::$name); + if (!$module) { + Module::registerMe(); + } $entity_data_json = \Yii::$app->getSecurity() ->decryptByKey($entity, $module::$encryptionKey); - if($entity_data_json != false) { + if ($entity_data_json != false) { $entity_data = Json::decode($entity_data_json); $commentModelClass = $module->commentModelClass; /** * @var CommentModel $model */ - $model = new $commentModelClass([ - 'scenario' => \Yii::$app->user->getIsGuest() ? $commentModelClass::SCENARIO_GUEST : $commentModelClass::SCENARIO_USER, - ]); - if($model->load(\Yii::$app->request->post())) { + $model = new $commentModelClass( + [ + 'scenario' => \Yii::$app->user->getIsGuest( + ) ? CommentModel::SCENARIO_GUEST : CommentModel::SCENARIO_USER, + ] + ); + if ($model->load(\Yii::$app->request->post())) { $model->setAttributes($entity_data); - if($model->save()) { - if(empty( $model->artbox_comment_pid ) && $module::$enableRating) { + if ($model->save()) { + if (empty($model->parent_id) && $module::$enableRating) { $ratingModelClass = $module->ratingModelClass; /** * @var RatingModel $rating */ - $rating = new $ratingModelClass([ - 'model' => $model::className(), - 'model_id' => $model->primaryKey, - ]); - if($rating->load(\Yii::$app->request->post())) { + $rating = new $ratingModelClass( + [ + 'model' => $model::className(), + 'model_id' => $model->primaryKey, + ] + ); + if ($rating->load(\Yii::$app->request->post())) { $rating->save(); } } - \Yii::$app->session->setFlash('artbox_comment_success', \Yii::t('artbox-comment', 'Comment posted')); + \Yii::$app->session->setFlash( + 'artbox_comment_success', + \Yii::t('artbox-comment', 'Comment posted') + ); return [ 'status' => 'success' ]; } else { return [ @@ -104,20 +118,23 @@ * * @param integer $id Comment ID * - * @return string Comment text + * @return array Comment text */ public function actionDelete($id) { \Yii::$app->response->format = Response::FORMAT_JSON; $model = $this->findModel($id); - if($model->deleteComment()) { + if ($model->deleteComment()) { return [ 'status' => 'success', - 'message' => \Yii::t('yii2mod.comments', 'Comment has been deleted.'), + 'message' => \Yii::t('artbox-comment', 'Comment has been deleted.'), ]; } else { \Yii::$app->response->setStatusCode(500); - return \Yii::t('yii2mod.comments', 'Comment has not been deleted. Please try again!'); + return [ + 'status' => 'error', + 'message' => \Yii::t('artbox-comment', 'Comment has not been deleted. Please try again!'), + ]; } } @@ -126,17 +143,26 @@ * * @param integer|array $id Comment ID * - * @return CommentModel - * @throws NotFoundHttpException + * @return \artbox\webcomment\models\CommentModel + * @throws \yii\base\InvalidConfigException + * @throws \yii\web\NotFoundHttpException */ protected function findModel(int $id): CommentModel { + /** + * @var Module $module + */ + $module = \Yii::$app->getModule(Module::$name); + if (!$module) { + Module::registerMe(); + } /** @var CommentModel $model */ - $commentModelClass = \Yii::$app->getModule(Module::$name)->commentModelClass; - if(( $model = $commentModelClass::findOne($id) ) !== NULL) { - return $model; - } else { - throw new NotFoundHttpException(\Yii::t('yii2mod.comments', 'The requested page does not exist.')); + $commentModelClass = $module->commentModelClass; + if (method_exists($commentModelClass, 'findOne')) { + if ($model = $commentModelClass::findOne($id) !== null) { + return $model; + } } + throw new NotFoundHttpException(\Yii::t('artbox-comment', 'The requested page does not exist.')); } } \ No newline at end of file diff --git a/controllers/ManageController.php b/controllers/ManageController.php index 5001066..ac0ffc7 100755 --- a/controllers/ManageController.php +++ b/controllers/ManageController.php @@ -1,10 +1,10 @@ getModule(Module::$name); + if (!$module) { + Module::registerMe(); + } $searchModel = new CommentModelSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); - $commentModel = Yii::$app->getModule(Module::$name)->commentModelClass; + $commentModel = $module->commentModelClass; return $this->render( 'index', @@ -67,10 +74,11 @@ public function actionUpdate($id) { $model = $this->findModel($id); + $model->scenario = $model::SCENARIO_ADMIN; if ($model->load(Yii::$app->request->post()) && $model->save()) { Yii::$app->session->setFlash( - 'artbox_comment_success', /*Yii::t('yii2mod.comments', 'Comment has been saved.')*/ + 'artbox_comment_success', 'Comment has been saved.' ); return $this->redirect([ 'index' ]); @@ -84,12 +92,23 @@ ); } - + + /** + * Answer to particular comment from backend + * + * @param $id + * + * @return string|\yii\web\Response + */ public function actionAnswer($id) { $model = $this->findModel($id); - - $answer = new CommentModel(); + + $answer = new CommentModel( + [ + 'scenario' => CommentModel::SCENARIO_ADMIN_ANSWER, + ] + ); if ($answer->load(Yii::$app->request->post()) && $answer->save()) { Yii::$app->session->setFlash( @@ -138,11 +157,11 @@ */ protected function findModel($id) { - if (( $model = CommentModel::findOne($id) ) !== NULL) { + if (( $model = CommentModel::findOne($id) ) !== null) { return $model; } else { - throw new NotFoundHttpException(/*Yii::t('yii2mod.comments', 'The requested page does not exist.')*/ - 'The requested page does not exist.' + throw new NotFoundHttpException( + \Yii::t('artbox-comment', 'The requested page does not exist.') ); } } diff --git a/migrations/m160724_162347_artbox_comment.php b/migrations/m160724_162347_artbox_comment.php index 5183e61..e4022bb 100755 --- a/migrations/m160724_162347_artbox_comment.php +++ b/migrations/m160724_162347_artbox_comment.php @@ -10,43 +10,40 @@ $this->createTable( '{{%artbox_comment}}', [ - 'artbox_comment_id' => $this->primaryKey(), - 'text' => $this->text() - ->notNull(), - 'user_id' => $this->integer(), - 'username' => $this->string(), - 'email' => $this->string(), - 'created_at' => $this->integer() - ->notNull(), - 'updated_at' => $this->integer() - ->notNull(), - 'deleted_at' => $this->integer(), - 'status' => $this->integer() - ->notNull() - ->defaultValue(1), - 'artbox_comment_pid' => $this->integer(), - 'related_id' => $this->integer(), - 'ip' => $this->string() - ->notNull(), - 'info' => $this->text(), + 'id' => $this->primaryKey(), + 'text' => $this->text() + ->notNull(), + 'customer_id' => $this->integer(), + 'username' => $this->string(), + 'email' => $this->string(), + 'created_at' => $this->integer() + ->notNull(), + 'updated_at' => $this->integer() + ->notNull(), + 'deleted_at' => $this->integer(), + 'status' => $this->integer() + ->notNull() + ->defaultValue(1), + 'parent_id' => $this->integer(), + 'related_id' => $this->integer(), + 'ip' => $this->string() + ->notNull(), + 'info' => $this->text(), + 'entity' => $this->string() + ->notNull() + ->defaultValue(''), + 'entity_id' => $this->integer() + ->notNull() + ->defaultValue(1), ] ); $this->addForeignKey( - 'user_id_user', + 'parent_id_artbox_comment', '{{%artbox_comment}}', - 'user_id', - 'customer', - 'id', - 'CASCADE', - 'CASCADE' - ); - $this->addForeignKey( - 'artbox_comment_pid_artbox_comment', - '{{%artbox_comment}}', - 'artbox_comment_pid', + 'parent_id', 'artbox_comment', - 'artbox_comment_id', + 'id', 'CASCADE', 'CASCADE' ); @@ -55,7 +52,7 @@ '{{%artbox_comment}}', 'related_id', 'artbox_comment', - 'artbox_comment_id', + 'id', 'CASCADE', 'CASCADE' ); @@ -63,10 +60,10 @@ $this->createTable( '{{%artbox_like}}', [ - 'artbox_like_id' => $this->primaryKey(), + 'id' => $this->primaryKey(), 'artbox_comment_id' => $this->integer() ->notNull(), - 'user_id' => $this->integer(), + 'customer_id' => $this->integer(), 'created_at' => $this->integer() ->notNull(), 'is_like' => $this->integer() @@ -80,17 +77,16 @@ '{{%artbox_like}}', 'artbox_comment_id', 'artbox_comment', - 'artbox_comment_id', + 'id', 'CASCADE', 'CASCADE' ); - $this->addForeignKey('user_id_user', '{{%artbox_like}}', 'user_id', 'customer', 'id', 'CASCADE', 'CASCADE'); $this->createIndex( 'artbox_like_unique', '{{%artbox_like}}', [ 'artbox_comment_id', - 'user_id', + 'customer_id', 'is_like', ], true @@ -100,11 +96,9 @@ public function down() { - $this->dropForeignKey('user_id_user', '{{%artbox_comment}}'); - $this->dropForeignKey('artbox_comment_pid_artbox_comment', '{{%artbox_comment}}'); + $this->dropForeignKey('parent_id_artbox_comment', '{{%artbox_comment}}'); $this->dropForeignKey('related_id_artbox_comment', '{{%artbox_comment}}'); $this->dropForeignKey('artbox_comment_id_artbox_comment', '{{%artbox_like}}'); - $this->dropForeignKey('user_id_user', '{{%artbox_like}}'); $this->dropIndex('artbox_like_unique', '{{%artbox_like}}'); $this->dropTable('{{%artbox_comment}}'); $this->dropTable('{{%artbox_like}}'); diff --git a/migrations/m160726_092634_add_entity_fields.php b/migrations/m160726_092634_add_entity_fields.php deleted file mode 100755 index 43ce77e..0000000 --- a/migrations/m160726_092634_add_entity_fields.php +++ /dev/null @@ -1,23 +0,0 @@ -addColumn('{{%artbox_comment}}', 'entity', $this->string() - ->notNull() - ->defaultValue('')); - $this->addColumn('{{%artbox_comment}}', 'entity_id', $this->integer() - ->notNull() - ->defaultValue(1)); - } - - public function down() - { - $this->dropColumn('{{%artbox_comment}}', 'entity'); - $this->dropColumn('{{%artbox_comment}}', 'entity_id'); - } - } diff --git a/migrations/m160726_211227_create_artbox_comment_rating.php b/migrations/m160726_211227_create_artbox_comment_rating.php index bb1048f..50efaab 100755 --- a/migrations/m160726_211227_create_artbox_comment_rating.php +++ b/migrations/m160726_211227_create_artbox_comment_rating.php @@ -10,33 +10,23 @@ $this->createTable( '{{%artbox_comment_rating}}', [ - 'artbox_comment_rating_id' => $this->primaryKey(), - 'created_at' => $this->integer() - ->notNull(), - 'updated_at' => $this->integer() - ->notNull(), - 'user_id' => $this->integer(), - 'value' => $this->float(), - 'model' => $this->string() - ->notNull(), - 'model_id' => $this->integer() - ->notNull(), + 'id' => $this->primaryKey(), + 'created_at' => $this->integer() + ->notNull(), + 'updated_at' => $this->integer() + ->notNull(), + 'customer_id' => $this->integer(), + 'value' => $this->float(), + 'model' => $this->string() + ->notNull(), + 'model_id' => $this->integer() + ->notNull(), ] ); - $this->addForeignKey( - 'user_id_user', - '{{%artbox_comment_rating}}', - 'user_id', - 'customer', - 'id', - 'CASCADE', - 'CASCADE' - ); } public function down() { - $this->dropForeignKey('user_id_user', '{{%artbox_comment_rating}}'); $this->dropTable('{{%artbox_comment_rating}}'); } } diff --git a/migrations/order-to-comment/m170525_095043_order_to_comment.php b/migrations/order-to-comment/m170525_095043_order_to_comment.php new file mode 100755 index 0000000..ec44808 --- /dev/null +++ b/migrations/order-to-comment/m170525_095043_order_to_comment.php @@ -0,0 +1,35 @@ +addForeignKey( + 'customer_id_customer', + '{{%artbox_comment}}', + 'customer_id', + 'customer', + 'id', + 'CASCADE', + 'CASCADE' + ); + + $this->addForeignKey( + 'customer_id_customer', + '{{%artbox_like}}', + 'customer_id', + 'customer', + 'id', + 'CASCADE', + 'CASCADE' + ); + } + + public function safeDown() + { + $this->dropForeignKey('customer_id_customer', '{{%artbox_comment}}'); + $this->dropForeignKey('customer_id_customer', '{{%artbox_like}}'); + } + } diff --git a/migrations/order-to-comment/m170525_095044_order_to_rating.php b/migrations/order-to-comment/m170525_095044_order_to_rating.php new file mode 100755 index 0000000..2e920f3 --- /dev/null +++ b/migrations/order-to-comment/m170525_095044_order_to_rating.php @@ -0,0 +1,25 @@ +addForeignKey( + 'customer_id_customer', + '{{%artbox_comment_rating}}', + 'customer_id', + 'customer', + 'id', + 'CASCADE', + 'CASCADE' + ); + } + + public function safeDown() + { + $this->dropForeignKey('customer_id_customer', '{{%artbox_comment_rating}}'); + + } + } diff --git a/models/CommentModel.php b/models/CommentModel.php index 0fc5182..995f987 100755 --- a/models/CommentModel.php +++ b/models/CommentModel.php @@ -1,38 +1,37 @@ self::SCENARIO_GUEST, ], [ [ @@ -110,7 +126,7 @@ [ [ 'entity_id', - 'artbox_comment_pid', + 'parent_id', ], 'integer', ], @@ -120,14 +136,17 @@ 'value' => 0, ], [ - [ 'artbox_comment_pid' ], + [ 'parent_id' ], 'exist', - 'targetAttribute' => 'artbox_comment_id', + 'targetAttribute' => 'id', 'skipOnError' => true, ], ]; } - + + /** + * @inheritdoc + */ public function behaviors() { return [ @@ -136,7 +155,7 @@ ], [ 'class' => BlameableBehavior::className(), - 'createdByAttribute' => 'user_id', + 'createdByAttribute' => 'customer_id', 'updatedByAttribute' => false, ], [ @@ -144,7 +163,7 @@ 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => 'ip', ], - 'value' => function($event) { + 'value' => function () { return \Yii::$app->request->userIP; }, ], @@ -153,58 +172,88 @@ ], ]; } - + + /** + * @inheritdoc + */ public function attributeLabels() { return [ - 'artbox_comment_id' => \Yii::t('artbox-comment', 'ID'), - 'text' => \Yii::t('artbox-comment', 'Text'), - 'user_id' => \Yii::t('artbox-comment', 'User'), - 'username' => \Yii::t('artbox-comment', 'Username'), - 'email' => 'Email', - 'date_add' => \Yii::t('artbox-comment', 'Date add'), - 'updated_at' => \Yii::t('artbox-comment', 'Date update'), - 'deleted_at' => \Yii::t('artbox-comment', 'Date delete'), - 'status' => \Yii::t('artbox-comment', 'Status'), - 'artbox_comment_pid' => \Yii::t('artbox-comment', 'Comment parent'), - 'related_id' => \Yii::t('artbox-comment', 'Comment related'), - 'ip' => 'IP', - 'entity' => \Yii::t('artbox-comment', 'Entity'), - 'info' => \Yii::t('artbox-comment', 'Info'), - 'entity_id' => \Yii::t('artbox-comment', 'Entity ID'), + 'id' => \Yii::t('artbox-comment', 'ID'), + 'text' => \Yii::t('artbox-comment', 'Text'), + 'customer_id' => \Yii::t('artbox-comment', 'User'), + 'username' => \Yii::t('artbox-comment', 'Username'), + 'email' => 'Email', + 'created_at' => \Yii::t('artbox-comment', 'Date add'), + 'updated_at' => \Yii::t('artbox-comment', 'Date update'), + 'deleted_at' => \Yii::t('artbox-comment', 'Date delete'), + 'status' => \Yii::t('artbox-comment', 'Status'), + 'parent_id' => \Yii::t('artbox-comment', 'Comment parent'), + 'related_id' => \Yii::t('artbox-comment', 'Comment related'), + 'ip' => 'IP', + 'entity' => \Yii::t('artbox-comment', 'Entity'), + 'info' => \Yii::t('artbox-comment', 'Info'), + 'entity_id' => \Yii::t('artbox-comment', 'Entity ID'), ]; } - + + /** + * Set Entity of Comment model + * + * @param string $entity + */ public function setEntity(string $entity) { $this->entity = $entity; } - + + /** + * Get Entity of Comment model + * + * @return string + */ public function getEntity(): string { return $this->entity; } - + + /** + * Get ActiveDataProvider of comments for particular Entity according to its EntityId + * + * @param string $entity Entity name + * @param int $entityId Entity Id + * + * @return \yii\data\ActiveDataProvider + */ public static function getTree(string $entity, int $entityId): ActiveDataProvider { + $query = self::find() + ->with( + [ + 'children' => function ($query) { + /** + * @var \yii\db\ActiveQuery $query + */ + if (class_exists(self::getCustomerClass())) { + $query->with('customer'); + } + }, + ] + ) + ->where( + [ + 'entity' => $entity, + 'entity_id' => $entityId, + 'status' => self::STATUS_ACTIVE, + 'parent_id' => null, + ] + ); + if (class_exists(self::getCustomerClass())) { + $query->with('customer'); + } return new ActiveDataProvider( [ - 'query' => self::find() - ->with( - [ - 'children', - 'user', - 'children.user', - ] - ) - ->where( - [ - 'entity' => $entity, - 'entity_id' => $entityId, - 'status' => 1, - 'artbox_comment_pid' => NULL, - ] - ), + 'query' => $query, 'pagination' => [ 'pageSize' => 20, ], @@ -216,93 +265,128 @@ ] ); } - + + /** + * Delete comment + * + * @return bool + */ public function deleteComment(): bool { - if (\Yii::$app->user->id != NULL && \Yii::$app->user->id == $this->user_id) { + if (!\Yii::$app->user->isGuest && \Yii::$app->user->id == $this->customer_id) { if ($this->delete()) { return true; } } return false; } - + + /** + * Set EntityId of Comment model + * + * @param int $entityId + */ public function setEntityId(int $entityId) { $this->entityId = $entityId; } - + + /** + * Get EntityId of Comment model + * + * @return int + */ public function getEntityId(): int { return $this->entityId; } - + + /** + * Get children relation for current comment + * + * @return \yii\db\ActiveQuery + */ public function getChildren() { - return $this->hasMany(self::className(), [ 'artbox_comment_pid' => 'artbox_comment_id' ]) + return $this->hasMany(self::className(), [ 'parent_id' => 'id' ]) ->andFilterWhere([ 'status' => self::STATUS_ACTIVE ]) ->inverseOf('parent'); } - + + /** + * Get parent relation for current comment + * + * @return \yii\db\ActiveQuery + */ public function getParent() { - return $this->hasOne(self::className(), [ 'artbox_comment_id' => 'artbox_comment_pid' ]) + return $this->hasOne(self::className(), [ 'id' => 'parent_id' ]) ->inverseOf('children'); } - - public function getUser() + + /** + * Get customer relation for current comment + * + * @return \yii\db\ActiveQuery + */ + public function getCustomer() { - $module = \Yii::$app->getModule('artbox-comment'); - return $this->hasOne($module->userIdentityClass, [ 'id' => 'user_id' ]); + return $this->hasOne(self::getCustomerClass(), [ 'id' => 'customer_id' ]); } - + + /** + * Get rating relation for current model + * + * @return \yii\db\ActiveQuery + */ public function getRating() { - return $this->hasOne(RatingModel::className(), [ 'model_id' => 'artbox_comment_id' ]) + return $this->hasOne(RatingModel::className(), [ 'model_id' => 'id' ]) ->andWhere( [ 'or', - [ 'artbox_comment_rating.model' => NULL ], + [ 'artbox_comment_rating.model' => null ], [ 'artbox_comment_rating.model' => self::className() ], ] ); } - + + /** + * Get entity model for current comment or false if not ActiveRecord + * + * @return ActiveRecord|false + */ public function getEntityModel() { - $model = call_user_func_array( - [ - $this->entity, - 'findOne', - ], - [ $this->entity_id ] - ); - return $model; - } - - public function getLink() - { - $model = $this->getEntityModel(); - if (empty($model)) { - return Html::a('Страница не найдена', '#'); - } - if (Product::className() == $this->entity) { - return Html::a( - 'Перейти на товар', - \Yii::$app->urlManagerFrontend->createAbsoluteUrl( - [ - 'catalog/product', - 'product' => $model->lang->alias, - 'variant' => $model->variant->sku, - ] - ), + if (method_exists($this->entity, 'findOne')) { + $model = call_user_func_array( [ - 'target' => '_blank', - 'data-pjax' => '0', - ] + $this->entity, + 'findOne', + ], + [ $this->entity_id ] ); + return $model; + } else { + return false; + } + } + + /** + * Get Customer model name + * + * @return string + */ + protected static function getCustomerClass() + { + /** + * @var \artbox\webcomment\Module $module + */ + $module = \Yii::$app->getModule('artbox-comment'); + if ($module) { + return $module->userIdentityClass; } else { - return Html::a('Неизвестная модель', '#'); + return 'artbox\order\models\Customer'; } } } diff --git a/models/CommentModelSearch.php b/models/CommentModelSearch.php index 094a721..06cf885 100755 --- a/models/CommentModelSearch.php +++ b/models/CommentModelSearch.php @@ -1,13 +1,13 @@ 'Рейтинг', - 'childrenCount' => 'Количество ответов', + 'ratingValue' => \Yii::t('artbox-comment', 'Рейтинг'), + 'childrenCount' => \Yii::t('artbox-comment', 'Количество ответов'), ] ); } @@ -98,7 +101,7 @@ ->joinWith( [ 'rating', - 'user', + 'customer', ] ); @@ -113,10 +116,9 @@ 'asc' => [ 'artbox_comment_rating.value' => SORT_ASC ], 'desc' => [ 'artbox_comment_rating.value' => SORT_DESC ], ], - 'artbox_comment_id', - 'date_add', + 'id', 'text', - 'user_id', + 'customer_id', 'status', 'entity', 'entity_id', @@ -140,12 +142,12 @@ // grid filtering conditions $query->andFilterWhere( [ - 'artbox_comment_id' => $this->artbox_comment_id, + 'id' => $this->id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, 'deleted_at' => $this->deleted_at, 'artbox_comment.status' => $this->status, - 'artbox_comment_pid' => $this->artbox_comment_pid, + 'parent_id' => $this->parent_id, 'related_id' => $this->related_id, 'entity_id' => $this->entity_id, ] @@ -198,26 +200,26 @@ 'artbox_comment_rating.value' => $this->ratingValue, ] ); - - if (!empty( $this->user_id )) { + + if (!empty($this->customer_id)) { $query->andWhere( [ 'or', - [ 'artbox_comment.user_id' => (int) $this->user_id ], + [ 'artbox_comment.customer_id' => (int) $this->customer_id ], [ 'like', 'user.username', - $this->user_id, + $this->customer_id, ], [ 'like', 'artbox_comment.username', - $this->user_id, + $this->customer_id, ], [ 'like', 'artbox_comment.email', - $this->user_id, + $this->customer_id, ], ] ); diff --git a/models/LikeModel.php b/models/LikeModel.php index 82534ca..d71b250 100755 --- a/models/LikeModel.php +++ b/models/LikeModel.php @@ -1,12 +1,11 @@ BlameableBehavior::className(), - 'createdByAttribute' => 'user_id', + 'createdByAttribute' => 'customer_id', 'updatedByAttribute' => false, ], ]; @@ -70,18 +68,30 @@ public function attributeLabels() { return [ - 'rating_id' => Yii::t('app', 'Rating ID'), - 'date_add' => Yii::t('app', 'Date Add'), - 'updated_at' => Yii::t('app', 'Date Update'), - 'user_id' => Yii::t('app', 'User ID'), - 'entity' => Yii::t('app', 'Entity'), - 'value' => Yii::t('app', 'Value'), + 'id' => Yii::t('app', 'Rating ID'), + 'created_at' => Yii::t('app', 'Date Add'), + 'updated_at' => Yii::t('app', 'Date Update'), + 'customer_id' => Yii::t('app', 'User ID'), + 'entity' => Yii::t('app', 'Entity'), + 'value' => Yii::t('app', 'Value'), ]; } - + + /** + * Get entity model for current model + * + * @return \yii\db\ActiveQuery|null + */ public function getModel() { $model = $this->model; - return $this->hasOne($model, [ $model::primaryKey() => 'model_id' ]); + if (method_exists($model, 'primaryKey')) { + /** + * @var ActiveRecord $model + */ + return $this->hasOne($model, [ $model::primaryKey()[ 0 ] => 'model_id' ]); + } else { + return null; + } } } diff --git a/models/interfaces/CommentInterface.php b/models/interfaces/CommentInterface.php index 31d7d1c..c12064c 100755 --- a/models/interfaces/CommentInterface.php +++ b/models/interfaces/CommentInterface.php @@ -1,12 +1,11 @@ $formId, - 'action' => Url::to([ - 'artbox-comment/default/create', - 'entity' => $comment_model->encryptedEntity, - ]), - ]); + $form = ActiveForm::begin( + [ + 'id' => $formId, + 'action' => Url::to( + [ + 'artbox-comment/default/create', + 'entity' => $comment_model->encryptedEntity, + ] + ), + ] + ); ?> -
- -
- field($rating_model, 'value', [ 'enableClientValidation' => false ]) - ->hiddenInput() - ->label(false); - echo Html::tag('div', '', [ +
+ +
+ field($rating_model, 'value', [ 'enableClientValidation' => false ]) + ->hiddenInput() + ->label(false); + echo Html::tag( + 'div', + '', + [ 'class' => 'rateit', 'data-rateit-backingfld' => '#' . Html::getInputId($rating_model, 'value'), - ]); - ?> -
- user->isGuest) { - echo $form->field($comment_model, 'username', [ 'options' => [ 'class' => 'form-group input_bl' ] ]) - ->textInput(); - echo $form->field($comment_model, 'email', [ 'options' => [ 'class' => 'form-group input_bl' ] ]) - ->textInput(); - } - echo $form->field($comment_model, 'text', [ 'options' => [ 'class' => 'form-group input_bl area_bl' ] ]) - ->textarea(); - echo Html::tag('div', Html::submitButton(Yii::t('artbox-comment', 'Submit')), [ 'class' => 'input_bl submit_btn' ]); - ?> -
+ ] + ); + ?> +
+ user->isGuest) { + echo $form->field($comment_model, 'username', [ 'options' => [ 'class' => 'form-group input_bl' ] ]) + ->textInput(); + echo $form->field($comment_model, 'email', [ 'options' => [ 'class' => 'form-group input_bl' ] ]) + ->textInput(); + } + echo $form->field($comment_model, 'text', [ 'options' => [ 'class' => 'form-group input_bl area_bl' ] ]) + ->textarea(); + echo Html::tag( + 'div', + Html::submitButton(Yii::t('artbox-comment', 'Submit')), + [ 'class' => 'input_bl submit_btn' ] + ); + ?> +
\ No newline at end of file diff --git a/views/artbox_comment_item.php b/views/artbox_comment_item.php index 9ed016e..4d56650 100755 --- a/views/artbox_comment_item.php +++ b/views/artbox_comment_item.php @@ -1,5 +1,5 @@
-
-
- -
- - +
+
rating )) { - ?> -
- 1 - rating->value; ?> - 5 -
-
- -
- text; - ?> -
-
+ + + rating)) { + ?> +
+ 1 + rating->value; ?> + 5 +
+
+ +
+ text; + ?> +
+
+
+ user->isGuest) { + ?> + + user->isGuest && \Yii::$app->user->id == $model->customer_id) { + ?> + + + + Dislike + +
+
children )) { - foreach($model->children as $index => $child) { + if (!empty($model->children)) { + foreach ($model->children as $index => $child) { ?> -
-
-
- -
-
- created_at); - ?> -
-
- user )) { - echo $child->user->username; - } else { - echo $child->username . ' (' . Yii::t('artbox-comment', 'Guest') . ')'; - } +
+
+
+ +
+
+ created_at); + ?> +
+
+ customer)) { + echo $child->customer->username; + } else { + echo $child->username . ' (' . Yii::t('artbox-comment', 'Guest') . ')'; + } + ?> +
+
+ text; + ?> +
+
+
+ user->isGuest) { ?> -
-
+ text; + } + if (!\Yii::$app->user->isGuest && \Yii::$app->user->id == $child->customer_id) { ?> -
-
-
- user->isGuest) { - ?> - Ответить - user->isGuest && \Yii::$app->user->id == $child->user_id) { - ?> - Удалить - - - Dislike + -
-
+ + Dislike + +
+
'.field-' . $text_input_id, 'input' => '#' . $text_input_id, ]; - $artbox_comment_pid_input_selectors = [ - 'container' => '.field-' . $artbox_comment_pid_input_id, - 'input' => '#' . $artbox_comment_pid_input_id, + $parent_id_input_selectors = [ + 'container' => '.field-' . $parent_id_input_id, + 'input' => '#' . $parent_id_input_id, ]; $form = ActiveForm::begin([ 'id' => $formId . '-reply', @@ -33,10 +33,13 @@ ?>
field($comment_model, 'artbox_comment_pid', [ - 'selectors' => $artbox_comment_pid_input_selectors, + echo $form->field( + $comment_model, + 'parent_id', + [ + 'selectors' => $parent_id_input_selectors, 'inputOptions' => [ - 'id' => $artbox_comment_pid_input_id, + 'id' => $parent_id_input_id, 'class' => 'form-control', ], ]) diff --git a/views/manage/answer.php b/views/manage/answer.php old mode 100644 new mode 100755 index 9e3a828..513296f --- a/views/manage/answer.php +++ b/views/manage/answer.php @@ -1,5 +1,5 @@ title = 'Ответить на комментарий# ' . $model->artbox_comment_id; + $this->title = \Yii::t('artbox-comment', 'Ответить на комментарий') . '# ' . $model->id; $this->params[ 'breadcrumbs' ][] = [ - 'label' => 'Комментарии', + 'label' => \Yii::t('artbox-comment', 'Комментарии'), 'url' => [ 'index' ], ]; $this->params[ 'breadcrumbs' ][] = $this->title; - ?>
@@ -26,16 +25,16 @@
field($answer, 'text') ->textarea() ?> - - field($answer, 'artbox_comment_pid') + + field($answer, 'parent_id') ->hiddenInput( [ - 'value' => $model->artbox_comment_id, + 'value' => $model->id, ] ) ->label(false) ?> - - field($answer, 'user_id') + + field($answer, 'customer_id') ->hiddenInput( [ 'value' => 1, @@ -60,7 +59,7 @@ ->label(false) ?> 'btn btn-primary' ] ) ?>
diff --git a/views/manage/index.php b/views/manage/index.php index 20c81f3..5b1c4a1 100755 --- a/views/manage/index.php +++ b/views/manage/index.php @@ -1,6 +1,6 @@ title = 'Комментарии'; + $this->title = \Yii::t('artbox-comment', 'Комментарии'); $this->params[ 'breadcrumbs' ][] = $this->title; $statuses = [ - $searchModel::STATUS_ACTIVE => 'Активный', - $searchModel::STATUS_HIDDEN => 'Скрытый', - $searchModel::STATUS_DELETED => 'Удаленный', + $searchModel::STATUS_ACTIVE => \Yii::t('artbox-comment', 'Активный'), + $searchModel::STATUS_HIDDEN => \Yii::t('artbox-comment', 'Скрытый'), + $searchModel::STATUS_DELETED => \Yii::t('artbox-comment', 'Удаленный'), ]; Pjax::begin(); - if (( $success = \Yii::$app->session->getFlash('artbox_comment_success') ) != NULL) { + if (( $success = \Yii::$app->session->getFlash('artbox_comment_success') ) != null) { echo Html::tag('p', $success); } echo GridView::widget( @@ -32,19 +32,7 @@ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ - [ - 'attribute' => 'artbox_comment_id', - 'label' => 'Id', - ], - [ - 'label' => 'url', - 'content' => function (CommentModel $model) { - return $model->getLink(); - }, - ], - [ - 'label' => 'Ссылка', - ], + 'id', [ 'attribute' => 'created_at', 'format' => [ @@ -54,51 +42,70 @@ 'filter' => false, ], [ - 'label' => 'Комментарий', - 'content' => function(CommentModel $model) { + 'label' => \Yii::t('artbox-comment', 'Комментарий'), + 'content' => function (CommentModel $model) { return StringHelper::truncate($model->text, 40, '...'); }, ], [ - 'attribute' => 'user_id', - 'value' => function($model) { - if (!empty($model->user)) { - return $model->user->username . ' (id:' . $model->user->id . ')'; + 'attribute' => 'customer_id', + 'value' => function ($model) { + /** + * @var CommentModel $model + */ + if (!empty($model->customer)) { + return $model->customer->username . ' (id:' . $model->customer->id . ')'; } else { - return $model->username . ' ' . $model->email . ' (Гость)'; + return $model->username . ' ' . $model->email . ' (' . \Yii::t( + 'artbox-comment', + 'Гость' + ) . ')'; } }, ], [ 'attribute' => 'status', 'filter' => $statuses, - 'value' => function($model) use ($statuses) { - return $statuses[ $model->status ]; + 'value' => function ($model) use ($statuses) { + /** + * @var CommentModel $model + */ + if (array_key_exists($model->status, $statuses)) { + return $statuses[ $model->status ]; + } else { + return null; + } }, ], [ 'attribute' => 'ratingValue', - 'value' => function($model) { + 'value' => function ($model) { + /** + * @var CommentModel $model + */ if (!empty($model->rating)) { return $model->rating->value; } - return NULL; + return null; }, ], [ 'attribute' => 'childrenCount', - 'value' => function($model) { + 'value' => function ($model) { + /** + * @var CommentModel $model + */ return count($model->children); }, ], [ 'class' => 'yii\grid\ActionColumn', 'buttons' => [ - 'answer' => function(string $url) { + 'answer' => function (string $url) { return Html::a(Html::tag('i', '', [ 'class' => 'glyphicon glyphicon-bullhorn' ]), $url); }, ], - 'template' => \Yii::$app->user->identity->isAdmin() ? '{update} {answer} {delete}' : '{update} {answer}', + 'template' => '{update} {answer} {delete}', ], ], ] diff --git a/views/manage/update.php b/views/manage/update.php index aa1acab..bfb418e 100755 --- a/views/manage/update.php +++ b/views/manage/update.php @@ -1,5 +1,5 @@ 'Активный', - $model::STATUS_HIDDEN => 'Скрытый', - $model::STATUS_DELETED => 'Удаленный', + $model::STATUS_ACTIVE => \Yii::t('artbox-comment', 'Активный'), + $model::STATUS_HIDDEN => \Yii::t('artbox-comment', 'Скрытый'), + $model::STATUS_DELETED => \Yii::t('artbox-comment', 'Удаленный'), ]; $form = ActiveForm::begin(); echo $form->field($model, 'text') ->textarea(); echo $form->field($model, 'status') ->dropDownList($statuses); - echo Html::submitButton('Обновить'); + echo Html::submitButton(\Yii::t('artbox-comment', 'Обновить')); $form->end(); \ No newline at end of file diff --git a/widgets/CommentWidget.php b/widgets/CommentWidget.php index 979d75c..8872a56 100755 --- a/widgets/CommentWidget.php +++ b/widgets/CommentWidget.php @@ -1,12 +1,12 @@ getModule(Module::$name); + $module = Yii::$app->getModule(Module::$name); + if (!$module) { + Module::registerMe(); + } // Model init $model = $this->getModel(); - - /** - * @todo Check if needed - */ - if(empty( $this->pjaxContainerId )) { + + if (empty($this->pjaxContainerId)) { $this->pjaxContainerId = 'comment-pjax-container-' . $this->getId(); } $this->entity = $model::className(); // Entity ID init - if(!empty( $this->entityIdAttribute ) && $this->model->hasProperty($this->entityIdAttribute)) { + if (!empty($this->entityIdAttribute) && $this->model->hasProperty($this->entityIdAttribute)) { $this->entityId = $this->model->{$this->entityIdAttribute}; } else { - if($this->model instanceof ActiveRecord && !empty( $this->model->getPrimaryKey() )) { + if ($this->model instanceof ActiveRecord && !empty($this->model->getPrimaryKey())) { $this->entityId = (int) $this->model->getPrimaryKey(); } else { - throw new InvalidConfigException(/*Yii::t('artbox-comment', 'The "entityIdAttribute" value for widget model cannot be empty.')*/); + throw new InvalidConfigException( + Yii::t('artbox-comment', 'The "entityIdAttribute" value for widget model cannot be empty.') + ); } } @@ -213,27 +233,38 @@ /** * Executes the widget. + * * @return string the result of widget execution to be outputted. */ public function run() { /* @var Module $module */ $module = Yii::$app->getModule(Module::$name); + if (!$module) { + Module::registerMe(); + } $commentModelClass = $module->commentModelClass; - $commentModel = $this->createModel($commentModelClass, [ - 'entity' => $this->entity, - 'entityId' => $this->entityId, - 'encryptedEntity' => $this->encryptedEntityKey, - 'scenario' => \Yii::$app->user->getIsGuest() ? $commentModelClass::SCENARIO_GUEST : $commentModelClass::SCENARIO_USER, - ]); - if($module::$enableRating) { + $commentModel = $this->createModel( + $commentModelClass, + [ + 'entity' => $this->entity, + 'entityId' => $this->entityId, + 'encryptedEntity' => $this->encryptedEntityKey, + 'scenario' => \Yii::$app->user->getIsGuest( + ) ? CommentModel::SCENARIO_GUEST : CommentModel::SCENARIO_USER, + ] + ); + if ($module::$enableRating) { $ratingModelClass = $module->ratingModelClass; $ratingModel = $this->createRating($ratingModelClass); } else { - $ratingModel = NULL; + $ratingModel = null; + } + if (method_exists($commentModelClass, 'getTree')) { + $comments = $commentModelClass::getTree($this->entity, $this->entityId); + } else { + $comments = []; } - - $comments = $commentModelClass::getTree($this->entity, $this->entityId); $this->buildParts($commentModel, $comments, $ratingModel); @@ -255,16 +286,22 @@ /** * Get encrypted entity key + * * @return string */ protected function generateEntityKey() { return Yii::$app->getSecurity() - ->encryptByKey(Json::encode([ - 'entity' => $this->entity, - 'entity_id' => $this->entityId, - 'info' => $this->info, - ]), Module::$encryptionKey); + ->encryptByKey( + Json::encode( + [ + 'entity' => $this->entity, + 'entity_id' => $this->entityId, + 'info' => $this->info, + ] + ), + Module::$encryptionKey + ); } /** @@ -280,10 +317,12 @@ { $options = array_merge($config, [ 'class' => $className ]); $object = Yii::createObject($options); - if($object instanceof CommentInterface) { + if ($object instanceof CommentInterface) { return $object; } - throw new InvalidConfigException(/*Yii::t(\'artbox-comment\', \'Comment model must be instance of CommentInterface.\')*/); + throw new InvalidConfigException( + Yii::t('artbox-comment', 'Comment model must be instance of CommentInterface.') + ); } /** @@ -299,10 +338,12 @@ { $options = array_merge($config, [ 'class' => $className ]); $object = Yii::createObject($options); - if($object instanceof RatingModel) { + if ($object instanceof RatingModel) { return $object; } - throw new InvalidConfigException(Yii::t('artbox-comment', 'Comment model must be instance of RatingModel.')); + throw new InvalidConfigException( + Yii::t('artbox-comment', 'Comment model must be instance of RatingModel.') + ); } /** @@ -312,36 +353,57 @@ * @param ActiveDataProvider $comments * @param null|RatingModel $ratingModel */ - protected function buildParts(CommentInterface $commentModel, ActiveDataProvider $comments, $ratingModel = NULL) + protected function buildParts(CommentInterface $commentModel, ActiveDataProvider $comments, $ratingModel = null) { $form_options = $this->formOptions; - $this->parts[ 'form' ] = Html::tag(ArrayHelper::remove($form_options, 'tag', 'div'), $this->render($this->formView, [ - 'comment_model' => $commentModel, - 'form_params' => $this->formParams, - 'model' => $this->getModel(), - 'formId' => $this->formId, - 'rating_model' => $ratingModel, - ]), $form_options); - - if(!\Yii::$app->user->isGuest) { + $this->parts[ 'form' ] = Html::tag( + ArrayHelper::remove($form_options, 'tag', 'div'), + $this->render( + $this->formView, + [ + 'comment_model' => $commentModel, + 'form_params' => $this->formParams, + 'model' => $this->getModel(), + 'formId' => $this->formId, + 'rating_model' => $ratingModel, + ] + ), + $form_options + ); + + if (!\Yii::$app->user->isGuest) { $reply_options = $this->replyOptions; - $this->parts[ 'reply_form' ] = Html::tag(ArrayHelper::remove($reply_options, 'tag', 'div'), $this->render($this->replyView, [ - 'comment_model' => $commentModel, - 'form_params' => $this->formParams, - 'model' => $this->getModel(), - 'formId' => $this->formId, - ]), $reply_options); + $this->parts[ 'reply_form' ] = Html::tag( + ArrayHelper::remove($reply_options, 'tag', 'div'), + $this->render( + $this->replyView, + [ + 'comment_model' => $commentModel, + 'form_params' => $this->formParams, + 'model' => $this->getModel(), + 'formId' => $this->formId, + ] + ), + $reply_options + ); } $list_options = array_merge($this->listOptions, [ 'id' => $this->listId ]); - $this->parts[ 'list' ] = Html::tag(ArrayHelper::remove($list_options, 'tag', 'div'), $this->render($this->listView, [ - 'comment_model' => $commentModel, - 'list_params' => $this->listParams, - 'model' => $this->getModel(), - 'comments' => $comments, - 'item_options' => $this->itemOptions, - 'item_view' => $this->itemView, - ]), $list_options); + $this->parts[ 'list' ] = Html::tag( + ArrayHelper::remove($list_options, 'tag', 'div'), + $this->render( + $this->listView, + [ + 'comment_model' => $commentModel, + 'list_params' => $this->listParams, + 'model' => $this->getModel(), + 'comments' => $comments, + 'item_options' => $this->itemOptions, + 'item_view' => $this->itemView, + ] + ), + $list_options + ); } /** @@ -366,9 +428,9 @@ public function getModel(): Model { - if(!empty( $this->model )) { + if (!empty($this->model)) { return $this->model; } - throw new InvalidConfigException(/*Yii::t(\'artbox-comment\', \'The "model" property must be set.\')*/); + throw new InvalidConfigException(Yii::t('artbox-comment', 'The "model" property must be set.')); } } \ No newline at end of file -- libgit2 0.21.4