Commit faff2c48bfd09f5ee437dc3d513eb8ed4665c2b0
1 parent
2ac6a2f4
Artbox comment created
Showing
30 changed files
with
969 additions
and
617 deletions
Show diff stats
1 | +# Change Log | |
2 | +All notable changes to this project will be documented in this file. | |
3 | + | |
4 | +## 1.0.0 - 2017-03-21 | |
5 | +### Added | |
6 | +- This CHANGELOG file to hopefully serve as an evolving example of a standardized open source project CHANGELOG. | |
7 | +- Added initial Artbox Comment extension. | |
0 | 8 | \ No newline at end of file | ... | ... |
1 | +The Yii framework is free software. It is released under the terms of | |
2 | +the following BSD License. | |
3 | + | |
4 | +Copyright © 2008 by Yii Software LLC (http://www.yiisoft.com) | |
5 | +All rights reserved. | |
6 | + | |
7 | +Redistribution and use in source and binary forms, with or without | |
8 | +modification, are permitted provided that the following conditions | |
9 | +are met: | |
10 | + | |
11 | + * Redistributions of source code must retain the above copyright | |
12 | + notice, this list of conditions and the following disclaimer. | |
13 | + * Redistributions in binary form must reproduce the above copyright | |
14 | + notice, this list of conditions and the following disclaimer in | |
15 | + the documentation and/or other materials provided with the | |
16 | + distribution. | |
17 | + * Neither the name of Yii Software LLC nor the names of its | |
18 | + contributors may be used to endorse or promote products derived | |
19 | + from this software without specific prior written permission. | |
20 | + | |
21 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
22 | +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
23 | +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
24 | +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
25 | +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
26 | +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
27 | +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
28 | +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
29 | +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
30 | +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
31 | +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
32 | +POSSIBILITY OF SUCH DAMAGE. | ... | ... |
Module.php
1 | 1 | <?php |
2 | 2 | |
3 | - namespace artweb\artbox\comment; | |
3 | + namespace artbox\webcomment; | |
4 | 4 | |
5 | - use artweb\artbox\comment\models\CommentModel; | |
6 | - use artweb\artbox\comment\models\RatingModel; | |
5 | + use artbox\webcomment\models\CommentModel; | |
6 | + use artbox\webcomment\models\RatingModel; | |
7 | 7 | use Yii; |
8 | + use yii\base\InvalidConfigException; | |
8 | 9 | use yii\console\Application; |
9 | - | |
10 | + | |
10 | 11 | /** |
11 | 12 | * Class Module |
13 | + * | |
12 | 14 | * @package artweb\artbox\comment |
13 | 15 | */ |
14 | 16 | class Module extends \yii\base\Module |
... | ... | @@ -20,39 +22,44 @@ |
20 | 22 | public static $name = 'artbox-comment'; |
21 | 23 | |
22 | 24 | /** |
23 | - * User identity class, default to artweb\artbox\comment\models\User | |
25 | + * User identity class, default to artbox\order\models\Customer | |
26 | + * | |
24 | 27 | * @var string|null |
25 | 28 | */ |
26 | - public $userIdentityClass = NULL; | |
29 | + public $userIdentityClass = null; | |
27 | 30 | |
28 | 31 | /** |
29 | - * Comment model class, default to artweb\artbox\comment\modules\models\CommentModel | |
32 | + * Comment model class, default to artbox\webcomment\modules\models\CommentModel | |
33 | + * | |
30 | 34 | * @var string comment model class |
31 | 35 | */ |
32 | - public $commentModelClass = NULL; | |
33 | - | |
34 | - public $ratingModelClass = NULL; | |
36 | + public $commentModelClass = null; | |
37 | + | |
38 | + public $ratingModelClass = null; | |
35 | 39 | |
36 | 40 | /** |
37 | 41 | * This namespace will be used to load controller classes by prepending it to the controller |
38 | 42 | * class name. |
43 | + * | |
39 | 44 | * @var string the namespace that controller classes are in. |
40 | 45 | */ |
41 | - public $controllerNamespace = 'artweb\artbox\comment\controllers'; | |
46 | + public $controllerNamespace = 'artbox\webcomment\controllers'; | |
42 | 47 | |
43 | 48 | /** |
44 | 49 | * @var \yii\db\Connection DB connection, default to \Yii::$app->db |
45 | 50 | */ |
46 | - public $db = NULL; | |
51 | + public $db = null; | |
47 | 52 | |
48 | 53 | /** |
49 | 54 | * Key, used to encrypt and decrypt comment service data. |
55 | + * | |
50 | 56 | * @var string Encryption key |
51 | 57 | */ |
52 | 58 | public static $encryptionKey = 'artbox-comment'; |
53 | 59 | |
54 | 60 | /** |
55 | 61 | * Whether to enable comment rating or not. |
62 | + * | |
56 | 63 | * @var bool |
57 | 64 | */ |
58 | 65 | public static $enableRating = true; |
... | ... | @@ -66,22 +73,34 @@ |
66 | 73 | */ |
67 | 74 | public function init() |
68 | 75 | { |
69 | - if($this->commentModelClass === NULL) { | |
76 | + if ($this->commentModelClass === null) { | |
70 | 77 | $this->commentModelClass = CommentModel::className(); |
71 | 78 | } |
72 | - if(self::$enableRating && $this->ratingModelClass === NULL) { | |
79 | + if (self::$enableRating && $this->ratingModelClass === null) { | |
73 | 80 | $this->ratingModelClass = RatingModel::className(); |
74 | 81 | } |
75 | - if(\Yii::$app instanceof Application) { | |
76 | - $this->controllerNamespace = 'artweb\artbox\comment\commands'; | |
77 | - } elseif($this->userIdentityClass === NULL) { | |
82 | + if (\Yii::$app instanceof Application) { | |
83 | + $this->controllerNamespace = 'artbox\webcomment\commands'; | |
84 | + } elseif ($this->userIdentityClass === null) { | |
78 | 85 | $this->userIdentityClass = Yii::$app->getUser()->identityClass; |
79 | 86 | } |
80 | - if($this->db === NULL) { | |
87 | + if ($this->db === null) { | |
81 | 88 | $this->db = \Yii::$app->db; |
82 | 89 | } |
83 | - Yii::setAlias('@artbox-comment', __DIR__); | |
90 | + if (!Yii::getAlias('@artbox/webcomment', false)) { | |
91 | + Yii::setAlias('@artbox/webcomment', __DIR__); | |
92 | + } | |
84 | 93 | parent::init(); |
85 | 94 | } |
95 | + | |
96 | + /** | |
97 | + * Prompt to register current module | |
98 | + * | |
99 | + * @throws \yii\base\InvalidConfigException | |
100 | + */ | |
101 | + public static function registerMe() | |
102 | + { | |
103 | + throw new InvalidConfigException(\Yii::t('artbox-comment', 'Register artbox-comment module')); | |
104 | + } | |
86 | 105 | |
87 | 106 | } | ... | ... |
1 | +Artbox Comment | |
2 | +=============================== | |
3 | + | |
4 | +Artbox Comment is an extension for working with comments developed by Artweb written with [Yii 2 framework](http://www.yiiframework.com/). | |
5 | + | |
6 | +Comment extension includes functionality for creating and maintaining comments on the website. | |
7 | + | |
8 | +This extension works with Artbox Core application and enough to add fully functional comment system to the website. | |
9 | + | |
10 | +To prepare your application you should run migrations: | |
11 | + | |
12 | + php yii migrate --migationPath=vendor/artweb/artbox-webcomment/migrations | |
13 | + | |
14 | +DIRECTORY STRUCTURE | |
15 | +------------------- | |
16 | + | |
17 | +``` | |
18 | +assets contains AssetBundles | |
19 | +behaviors contains Behaviors classes | |
20 | +components contains custom Classes, which don't belong to other groups | |
21 | +controllers contains controllers for core models | |
22 | +helpers contains helper classes to manipulate, for example static files | |
23 | + and HTML | |
24 | +messages contains translations for core strings | |
25 | +migrations contains migrations, which should be applied after extension | |
26 | + installation | |
27 | +models contains core models | |
28 | +views contains views files for core controllers | |
29 | +web contains assets and other files, which should be web available | |
30 | +widgets contains widgets | |
31 | +``` | ... | ... |
assets/CommentAsset.php
1 | 1 | <?php |
2 | 2 | |
3 | - namespace artweb\artbox\comment\assets; | |
3 | + namespace artbox\webcomment\assets; | |
4 | 4 | |
5 | 5 | use yii\web\AssetBundle; |
6 | 6 | |
7 | 7 | /** |
8 | 8 | * Class CommentAsset |
9 | - * @package artweb\artbox\comment\assets | |
10 | 9 | */ |
11 | 10 | class CommentAsset extends AssetBundle |
12 | 11 | { |
... | ... | @@ -14,7 +13,7 @@ |
14 | 13 | /** |
15 | 14 | * @inheritdoc |
16 | 15 | */ |
17 | - public $sourcePath = '@artbox-comment/resources'; | |
16 | + public $sourcePath = '@artbox/webcomment/resources'; | |
18 | 17 | |
19 | 18 | /** |
20 | 19 | * @inheritdoc | ... | ... |
behaviors/CommentBehavior.php
1 | 1 | <?php |
2 | 2 | |
3 | - namespace artweb\artbox\comment\behaviors; | |
3 | + namespace artbox\webcomment\behaviors; | |
4 | 4 | |
5 | - use artweb\artbox\comment\models\CommentModel; | |
6 | - use artweb\artbox\comment\models\interfaces\RatingCacheInterface; | |
5 | + use artbox\webcomment\models\CommentModel; | |
6 | + use artbox\webcomment\models\interfaces\RatingCacheInterface; | |
7 | 7 | use yii\base\Behavior; |
8 | 8 | use yii\base\InvalidConfigException; |
9 | + use yii\base\InvalidParamException; | |
9 | 10 | use yii\db\ActiveQuery; |
10 | 11 | use yii\db\ActiveRecord; |
11 | 12 | |
... | ... | @@ -14,7 +15,7 @@ |
14 | 15 | * |
15 | 16 | * @property ActiveQuery $averageRating |
16 | 17 | * @property CommentModel[] $comments |
17 | - * @package artweb\artbox\comment\behaviors | |
18 | + * @package artbox\webcomment\behaviors | |
18 | 19 | */ |
19 | 20 | class CommentBehavior extends Behavior |
20 | 21 | { |
... | ... | @@ -46,7 +47,7 @@ |
46 | 47 | public function attach($owner) |
47 | 48 | { |
48 | 49 | if ($this->cacheRating) { |
49 | - if (empty( $this->cacheModelName ) || !is_string($this->cacheModelName)) { | |
50 | + if (empty($this->cacheModelName) || !is_string($this->cacheModelName)) { | |
50 | 51 | throw new InvalidConfigException( |
51 | 52 | 'To use rating cache you must set $cacheModelName where to store it' |
52 | 53 | ); |
... | ... | @@ -67,7 +68,7 @@ |
67 | 68 | * |
68 | 69 | * @return string |
69 | 70 | */ |
70 | - public function getCacheModelName():string | |
71 | + public function getCacheModelName(): string | |
71 | 72 | { |
72 | 73 | return $this->cacheModelName; |
73 | 74 | } |
... | ... | @@ -84,10 +85,8 @@ |
84 | 85 | |
85 | 86 | /** |
86 | 87 | * Get model to hold rating cache |
87 | - * | |
88 | - * @return \artweb\artbox\comment\models\interfaces\RatingCacheInterface | |
89 | 88 | */ |
90 | - public function getCacheModel():RatingCacheInterface | |
89 | + public function getCacheModel(): RatingCacheInterface | |
91 | 90 | { |
92 | 91 | return $this->cacheModel; |
93 | 92 | } |
... | ... | @@ -95,7 +94,7 @@ |
95 | 94 | /** |
96 | 95 | * Set model to hold cache |
97 | 96 | * |
98 | - * @param \artweb\artbox\comment\models\interfaces\RatingCacheInterface|null $value | |
97 | + * @param \artbox\webcomment\models\interfaces\RatingCacheInterface|null $value | |
99 | 98 | */ |
100 | 99 | private function setCacheModel(RatingCacheInterface $value = null) |
101 | 100 | { |
... | ... | @@ -133,13 +132,19 @@ |
133 | 132 | * @var ActiveRecord $owner |
134 | 133 | */ |
135 | 134 | $owner = $this->owner; |
136 | - $pk = $owner->primaryKey()[ 0 ]; | |
135 | + $pkKeys = $owner->primaryKey(); | |
136 | + if (!empty($pkKeys)) { | |
137 | + $pkKey = $pkKeys[ 0 ]; | |
138 | + } else { | |
139 | + throw new InvalidParamException('Entity must have primary key.'); | |
140 | + } | |
141 | + $pk = $owner->getAttribute($pkKey); | |
137 | 142 | $query = $owner->hasMany(CommentModel::className(), [ 'entity_id' => $pk ]) |
138 | 143 | ->where( |
139 | 144 | [ |
140 | - 'artbox_comment.entity' => $owner::className(), | |
141 | - 'artbox_comment.status' => CommentModel::STATUS_ACTIVE, | |
142 | - 'artbox_comment.artbox_comment_pid' => null, | |
145 | + 'artbox_comment.entity' => $owner::className(), | |
146 | + 'artbox_comment.status' => CommentModel::STATUS_ACTIVE, | |
147 | + 'artbox_comment.parent_id' => null, | |
143 | 148 | ] |
144 | 149 | ); |
145 | 150 | return $query; |
... | ... | @@ -180,7 +185,7 @@ |
180 | 185 | } |
181 | 186 | $averageRating = $this->getAverageRating() |
182 | 187 | ->one(); |
183 | - if (!empty( $averageRating )) { | |
188 | + if (!empty($averageRating)) { | |
184 | 189 | $averageRating->setValue($average); |
185 | 190 | } else { |
186 | 191 | /** | ... | ... |
behaviors/ParentBehavior.php
1 | 1 | <?php |
2 | - namespace artweb\artbox\comment\behaviors; | |
3 | 2 | |
4 | - use artweb\artbox\comment\models\CommentModel; | |
3 | + namespace artbox\webcomment\behaviors; | |
4 | + | |
5 | + use artbox\webcomment\models\CommentModel; | |
5 | 6 | use yii\base\Behavior; |
6 | - use yii\base\Event; | |
7 | 7 | use yii\db\ActiveRecord; |
8 | 8 | |
9 | 9 | class ParentBehavior extends Behavior |
... | ... | @@ -17,24 +17,24 @@ |
17 | 17 | } |
18 | 18 | |
19 | 19 | /** |
20 | - * @param Event $event | |
20 | + * After validate event | |
21 | 21 | */ |
22 | - public function afterValidate($event) | |
22 | + public function afterValidate() | |
23 | 23 | { |
24 | 24 | /** |
25 | 25 | * @var CommentModel $owner |
26 | 26 | */ |
27 | 27 | $owner = $this->owner; |
28 | - if(!empty( $owner->artbox_comment_pid )) { | |
28 | + if (!empty($owner->parent_id)) { | |
29 | 29 | /** |
30 | 30 | * @var CommentModel $parent |
31 | 31 | */ |
32 | 32 | $parent = CommentModel::find() |
33 | - ->where([ 'artbox_comment_id' => $owner->artbox_comment_pid ]) | |
33 | + ->where([ 'id' => $owner->parent_id ]) | |
34 | 34 | ->one(); |
35 | - if(!empty( $parent->artbox_comment_pid )) { | |
36 | - $owner->related_id = $owner->artbox_comment_pid; | |
37 | - $owner->artbox_comment_pid = $parent->artbox_comment_pid; | |
35 | + if (!empty($parent->parent_id)) { | |
36 | + $owner->related_id = $owner->parent_id; | |
37 | + $owner->parent_id = $parent->parent_id; | |
38 | 38 | } |
39 | 39 | } |
40 | 40 | } | ... | ... |
composer.json
1 | 1 | { |
2 | - "name": "artweb/artbox-comment", | |
3 | - "description": "Yii2 light-weight CMS", | |
2 | + "name": "artweb/artbox-webcomment", | |
3 | + "description": "Artbox comment extension", | |
4 | 4 | "license": "BSD-3-Clause", |
5 | 5 | "minimum-stability": "dev", |
6 | 6 | "type": "yii2-extension", |
7 | 7 | "require": { |
8 | - "php": ">=5.4", | |
9 | - "yiisoft/yii2": "*", | |
10 | - "artweb/artbox": "dev-master" | |
8 | + "php": ">=7.0", | |
9 | + "yiisoft/yii2": "~2.0", | |
10 | + "artweb/artbox-core": "~0.0.1" | |
11 | 11 | }, |
12 | 12 | "autoload": { |
13 | 13 | "psr-4": { |
14 | - "artweb\\artbox\\comment\": "" | |
14 | + "artbox\\webcomment\": "" | |
15 | 15 | } |
16 | 16 | } |
17 | 17 | } |
18 | 18 | \ No newline at end of file | ... | ... |
controllers/DefaultController.php
1 | 1 | <?php |
2 | - namespace artweb\artbox\comment\controllers; | |
3 | 2 | |
4 | - use artweb\artbox\comment\models\CommentModel; | |
5 | - use artweb\artbox\comment\models\RatingModel; | |
6 | - use artweb\artbox\comment\Module; | |
3 | + namespace artbox\webcomment\controllers; | |
4 | + | |
5 | + use artbox\webcomment\models\CommentModel; | |
6 | + use artbox\webcomment\models\RatingModel; | |
7 | + use artbox\webcomment\Module; | |
7 | 8 | use yii\filters\AccessControl; |
8 | 9 | use yii\filters\VerbFilter; |
9 | 10 | use yii\helpers\Json; |
... | ... | @@ -16,6 +17,7 @@ |
16 | 17 | |
17 | 18 | /** |
18 | 19 | * Returns a list of behaviors that this component should behave as. |
20 | + * | |
19 | 21 | * @return array |
20 | 22 | */ |
21 | 23 | public function behaviors() |
... | ... | @@ -49,41 +51,53 @@ |
49 | 51 | * |
50 | 52 | * @param string $entity |
51 | 53 | * |
52 | - * @return array|null|Response | |
54 | + * @return array|null|\yii\web\Response | |
55 | + * @throws \yii\base\InvalidConfigException | |
53 | 56 | */ |
54 | 57 | public function actionCreate(string $entity) |
55 | 58 | { |
56 | 59 | \Yii::$app->response->format = Response::FORMAT_JSON; |
57 | 60 | /* @var $module Module */ |
58 | 61 | $module = \Yii::$app->getModule(Module::$name); |
62 | + if (!$module) { | |
63 | + Module::registerMe(); | |
64 | + } | |
59 | 65 | $entity_data_json = \Yii::$app->getSecurity() |
60 | 66 | ->decryptByKey($entity, $module::$encryptionKey); |
61 | - if($entity_data_json != false) { | |
67 | + if ($entity_data_json != false) { | |
62 | 68 | $entity_data = Json::decode($entity_data_json); |
63 | 69 | $commentModelClass = $module->commentModelClass; |
64 | 70 | /** |
65 | 71 | * @var CommentModel $model |
66 | 72 | */ |
67 | - $model = new $commentModelClass([ | |
68 | - 'scenario' => \Yii::$app->user->getIsGuest() ? $commentModelClass::SCENARIO_GUEST : $commentModelClass::SCENARIO_USER, | |
69 | - ]); | |
70 | - if($model->load(\Yii::$app->request->post())) { | |
73 | + $model = new $commentModelClass( | |
74 | + [ | |
75 | + 'scenario' => \Yii::$app->user->getIsGuest( | |
76 | + ) ? CommentModel::SCENARIO_GUEST : CommentModel::SCENARIO_USER, | |
77 | + ] | |
78 | + ); | |
79 | + if ($model->load(\Yii::$app->request->post())) { | |
71 | 80 | $model->setAttributes($entity_data); |
72 | - if($model->save()) { | |
73 | - if(empty( $model->artbox_comment_pid ) && $module::$enableRating) { | |
81 | + if ($model->save()) { | |
82 | + if (empty($model->parent_id) && $module::$enableRating) { | |
74 | 83 | $ratingModelClass = $module->ratingModelClass; |
75 | 84 | /** |
76 | 85 | * @var RatingModel $rating |
77 | 86 | */ |
78 | - $rating = new $ratingModelClass([ | |
79 | - 'model' => $model::className(), | |
80 | - 'model_id' => $model->primaryKey, | |
81 | - ]); | |
82 | - if($rating->load(\Yii::$app->request->post())) { | |
87 | + $rating = new $ratingModelClass( | |
88 | + [ | |
89 | + 'model' => $model::className(), | |
90 | + 'model_id' => $model->primaryKey, | |
91 | + ] | |
92 | + ); | |
93 | + if ($rating->load(\Yii::$app->request->post())) { | |
83 | 94 | $rating->save(); |
84 | 95 | } |
85 | 96 | } |
86 | - \Yii::$app->session->setFlash('artbox_comment_success', \Yii::t('artbox-comment', 'Comment posted')); | |
97 | + \Yii::$app->session->setFlash( | |
98 | + 'artbox_comment_success', | |
99 | + \Yii::t('artbox-comment', 'Comment posted') | |
100 | + ); | |
87 | 101 | return [ 'status' => 'success' ]; |
88 | 102 | } else { |
89 | 103 | return [ |
... | ... | @@ -104,20 +118,23 @@ |
104 | 118 | * |
105 | 119 | * @param integer $id Comment ID |
106 | 120 | * |
107 | - * @return string Comment text | |
121 | + * @return array Comment text | |
108 | 122 | */ |
109 | 123 | public function actionDelete($id) |
110 | 124 | { |
111 | 125 | \Yii::$app->response->format = Response::FORMAT_JSON; |
112 | 126 | $model = $this->findModel($id); |
113 | - if($model->deleteComment()) { | |
127 | + if ($model->deleteComment()) { | |
114 | 128 | return [ |
115 | 129 | 'status' => 'success', |
116 | - 'message' => \Yii::t('yii2mod.comments', 'Comment has been deleted.'), | |
130 | + 'message' => \Yii::t('artbox-comment', 'Comment has been deleted.'), | |
117 | 131 | ]; |
118 | 132 | } else { |
119 | 133 | \Yii::$app->response->setStatusCode(500); |
120 | - return \Yii::t('yii2mod.comments', 'Comment has not been deleted. Please try again!'); | |
134 | + return [ | |
135 | + 'status' => 'error', | |
136 | + 'message' => \Yii::t('artbox-comment', 'Comment has not been deleted. Please try again!'), | |
137 | + ]; | |
121 | 138 | } |
122 | 139 | } |
123 | 140 | |
... | ... | @@ -126,17 +143,26 @@ |
126 | 143 | * |
127 | 144 | * @param integer|array $id Comment ID |
128 | 145 | * |
129 | - * @return CommentModel | |
130 | - * @throws NotFoundHttpException | |
146 | + * @return \artbox\webcomment\models\CommentModel | |
147 | + * @throws \yii\base\InvalidConfigException | |
148 | + * @throws \yii\web\NotFoundHttpException | |
131 | 149 | */ |
132 | 150 | protected function findModel(int $id): CommentModel |
133 | 151 | { |
152 | + /** | |
153 | + * @var Module $module | |
154 | + */ | |
155 | + $module = \Yii::$app->getModule(Module::$name); | |
156 | + if (!$module) { | |
157 | + Module::registerMe(); | |
158 | + } | |
134 | 159 | /** @var CommentModel $model */ |
135 | - $commentModelClass = \Yii::$app->getModule(Module::$name)->commentModelClass; | |
136 | - if(( $model = $commentModelClass::findOne($id) ) !== NULL) { | |
137 | - return $model; | |
138 | - } else { | |
139 | - throw new NotFoundHttpException(\Yii::t('yii2mod.comments', 'The requested page does not exist.')); | |
160 | + $commentModelClass = $module->commentModelClass; | |
161 | + if (method_exists($commentModelClass, 'findOne')) { | |
162 | + if ($model = $commentModelClass::findOne($id) !== null) { | |
163 | + return $model; | |
164 | + } | |
140 | 165 | } |
166 | + throw new NotFoundHttpException(\Yii::t('artbox-comment', 'The requested page does not exist.')); | |
141 | 167 | } |
142 | 168 | } |
143 | 169 | \ No newline at end of file | ... | ... |
controllers/ManageController.php
1 | 1 | <?php |
2 | 2 | |
3 | - namespace artweb\artbox\comment\controllers; | |
3 | + namespace artbox\webcomment\controllers; | |
4 | 4 | |
5 | - use artweb\artbox\comment\models\CommentModel; | |
6 | - use artweb\artbox\comment\models\CommentModelSearch; | |
7 | - use artweb\artbox\comment\Module; | |
5 | + use artbox\webcomment\models\CommentModel; | |
6 | + use artbox\webcomment\models\CommentModelSearch; | |
7 | + use artbox\webcomment\Module; | |
8 | 8 | use Yii; |
9 | 9 | use yii\filters\VerbFilter; |
10 | 10 | use yii\web\Controller; |
... | ... | @@ -42,9 +42,16 @@ |
42 | 42 | */ |
43 | 43 | public function actionIndex() |
44 | 44 | { |
45 | + /** | |
46 | + * @var Module $module | |
47 | + */ | |
48 | + $module = \Yii::$app->getModule(Module::$name); | |
49 | + if (!$module) { | |
50 | + Module::registerMe(); | |
51 | + } | |
45 | 52 | $searchModel = new CommentModelSearch(); |
46 | 53 | $dataProvider = $searchModel->search(Yii::$app->request->queryParams); |
47 | - $commentModel = Yii::$app->getModule(Module::$name)->commentModelClass; | |
54 | + $commentModel = $module->commentModelClass; | |
48 | 55 | |
49 | 56 | return $this->render( |
50 | 57 | 'index', |
... | ... | @@ -67,10 +74,11 @@ |
67 | 74 | public function actionUpdate($id) |
68 | 75 | { |
69 | 76 | $model = $this->findModel($id); |
77 | + $model->scenario = $model::SCENARIO_ADMIN; | |
70 | 78 | |
71 | 79 | if ($model->load(Yii::$app->request->post()) && $model->save()) { |
72 | 80 | Yii::$app->session->setFlash( |
73 | - 'artbox_comment_success', /*Yii::t('yii2mod.comments', 'Comment has been saved.')*/ | |
81 | + 'artbox_comment_success', | |
74 | 82 | 'Comment has been saved.' |
75 | 83 | ); |
76 | 84 | return $this->redirect([ 'index' ]); |
... | ... | @@ -84,12 +92,23 @@ |
84 | 92 | ); |
85 | 93 | |
86 | 94 | } |
87 | - | |
95 | + | |
96 | + /** | |
97 | + * Answer to particular comment from backend | |
98 | + * | |
99 | + * @param $id | |
100 | + * | |
101 | + * @return string|\yii\web\Response | |
102 | + */ | |
88 | 103 | public function actionAnswer($id) |
89 | 104 | { |
90 | 105 | $model = $this->findModel($id); |
91 | - | |
92 | - $answer = new CommentModel(); | |
106 | + | |
107 | + $answer = new CommentModel( | |
108 | + [ | |
109 | + 'scenario' => CommentModel::SCENARIO_ADMIN_ANSWER, | |
110 | + ] | |
111 | + ); | |
93 | 112 | |
94 | 113 | if ($answer->load(Yii::$app->request->post()) && $answer->save()) { |
95 | 114 | Yii::$app->session->setFlash( |
... | ... | @@ -138,11 +157,11 @@ |
138 | 157 | */ |
139 | 158 | protected function findModel($id) |
140 | 159 | { |
141 | - if (( $model = CommentModel::findOne($id) ) !== NULL) { | |
160 | + if (( $model = CommentModel::findOne($id) ) !== null) { | |
142 | 161 | return $model; |
143 | 162 | } else { |
144 | - throw new NotFoundHttpException(/*Yii::t('yii2mod.comments', 'The requested page does not exist.')*/ | |
145 | - 'The requested page does not exist.' | |
163 | + throw new NotFoundHttpException( | |
164 | + \Yii::t('artbox-comment', 'The requested page does not exist.') | |
146 | 165 | ); |
147 | 166 | } |
148 | 167 | } | ... | ... |
migrations/m160724_162347_artbox_comment.php
... | ... | @@ -10,43 +10,40 @@ |
10 | 10 | $this->createTable( |
11 | 11 | '{{%artbox_comment}}', |
12 | 12 | [ |
13 | - 'artbox_comment_id' => $this->primaryKey(), | |
14 | - 'text' => $this->text() | |
15 | - ->notNull(), | |
16 | - 'user_id' => $this->integer(), | |
17 | - 'username' => $this->string(), | |
18 | - 'email' => $this->string(), | |
19 | - 'created_at' => $this->integer() | |
20 | - ->notNull(), | |
21 | - 'updated_at' => $this->integer() | |
22 | - ->notNull(), | |
23 | - 'deleted_at' => $this->integer(), | |
24 | - 'status' => $this->integer() | |
25 | - ->notNull() | |
26 | - ->defaultValue(1), | |
27 | - 'artbox_comment_pid' => $this->integer(), | |
28 | - 'related_id' => $this->integer(), | |
29 | - 'ip' => $this->string() | |
30 | - ->notNull(), | |
31 | - 'info' => $this->text(), | |
13 | + 'id' => $this->primaryKey(), | |
14 | + 'text' => $this->text() | |
15 | + ->notNull(), | |
16 | + 'customer_id' => $this->integer(), | |
17 | + 'username' => $this->string(), | |
18 | + 'email' => $this->string(), | |
19 | + 'created_at' => $this->integer() | |
20 | + ->notNull(), | |
21 | + 'updated_at' => $this->integer() | |
22 | + ->notNull(), | |
23 | + 'deleted_at' => $this->integer(), | |
24 | + 'status' => $this->integer() | |
25 | + ->notNull() | |
26 | + ->defaultValue(1), | |
27 | + 'parent_id' => $this->integer(), | |
28 | + 'related_id' => $this->integer(), | |
29 | + 'ip' => $this->string() | |
30 | + ->notNull(), | |
31 | + 'info' => $this->text(), | |
32 | + 'entity' => $this->string() | |
33 | + ->notNull() | |
34 | + ->defaultValue(''), | |
35 | + 'entity_id' => $this->integer() | |
36 | + ->notNull() | |
37 | + ->defaultValue(1), | |
32 | 38 | ] |
33 | 39 | ); |
34 | 40 | |
35 | 41 | $this->addForeignKey( |
36 | - 'user_id_user', | |
42 | + 'parent_id_artbox_comment', | |
37 | 43 | '{{%artbox_comment}}', |
38 | - 'user_id', | |
39 | - 'customer', | |
40 | - 'id', | |
41 | - 'CASCADE', | |
42 | - 'CASCADE' | |
43 | - ); | |
44 | - $this->addForeignKey( | |
45 | - 'artbox_comment_pid_artbox_comment', | |
46 | - '{{%artbox_comment}}', | |
47 | - 'artbox_comment_pid', | |
44 | + 'parent_id', | |
48 | 45 | 'artbox_comment', |
49 | - 'artbox_comment_id', | |
46 | + 'id', | |
50 | 47 | 'CASCADE', |
51 | 48 | 'CASCADE' |
52 | 49 | ); |
... | ... | @@ -55,7 +52,7 @@ |
55 | 52 | '{{%artbox_comment}}', |
56 | 53 | 'related_id', |
57 | 54 | 'artbox_comment', |
58 | - 'artbox_comment_id', | |
55 | + 'id', | |
59 | 56 | 'CASCADE', |
60 | 57 | 'CASCADE' |
61 | 58 | ); |
... | ... | @@ -63,10 +60,10 @@ |
63 | 60 | $this->createTable( |
64 | 61 | '{{%artbox_like}}', |
65 | 62 | [ |
66 | - 'artbox_like_id' => $this->primaryKey(), | |
63 | + 'id' => $this->primaryKey(), | |
67 | 64 | 'artbox_comment_id' => $this->integer() |
68 | 65 | ->notNull(), |
69 | - 'user_id' => $this->integer(), | |
66 | + 'customer_id' => $this->integer(), | |
70 | 67 | 'created_at' => $this->integer() |
71 | 68 | ->notNull(), |
72 | 69 | 'is_like' => $this->integer() |
... | ... | @@ -80,17 +77,16 @@ |
80 | 77 | '{{%artbox_like}}', |
81 | 78 | 'artbox_comment_id', |
82 | 79 | 'artbox_comment', |
83 | - 'artbox_comment_id', | |
80 | + 'id', | |
84 | 81 | 'CASCADE', |
85 | 82 | 'CASCADE' |
86 | 83 | ); |
87 | - $this->addForeignKey('user_id_user', '{{%artbox_like}}', 'user_id', 'customer', 'id', 'CASCADE', 'CASCADE'); | |
88 | 84 | $this->createIndex( |
89 | 85 | 'artbox_like_unique', |
90 | 86 | '{{%artbox_like}}', |
91 | 87 | [ |
92 | 88 | 'artbox_comment_id', |
93 | - 'user_id', | |
89 | + 'customer_id', | |
94 | 90 | 'is_like', |
95 | 91 | ], |
96 | 92 | true |
... | ... | @@ -100,11 +96,9 @@ |
100 | 96 | |
101 | 97 | public function down() |
102 | 98 | { |
103 | - $this->dropForeignKey('user_id_user', '{{%artbox_comment}}'); | |
104 | - $this->dropForeignKey('artbox_comment_pid_artbox_comment', '{{%artbox_comment}}'); | |
99 | + $this->dropForeignKey('parent_id_artbox_comment', '{{%artbox_comment}}'); | |
105 | 100 | $this->dropForeignKey('related_id_artbox_comment', '{{%artbox_comment}}'); |
106 | 101 | $this->dropForeignKey('artbox_comment_id_artbox_comment', '{{%artbox_like}}'); |
107 | - $this->dropForeignKey('user_id_user', '{{%artbox_like}}'); | |
108 | 102 | $this->dropIndex('artbox_like_unique', '{{%artbox_like}}'); |
109 | 103 | $this->dropTable('{{%artbox_comment}}'); |
110 | 104 | $this->dropTable('{{%artbox_like}}'); | ... | ... |
migrations/m160726_092634_add_entity_fields.php deleted
1 | -<?php | |
2 | - | |
3 | - use yii\db\Migration; | |
4 | - | |
5 | - class m160726_092634_add_entity_fields extends Migration | |
6 | - { | |
7 | - | |
8 | - public function up() | |
9 | - { | |
10 | - $this->addColumn('{{%artbox_comment}}', 'entity', $this->string() | |
11 | - ->notNull() | |
12 | - ->defaultValue('')); | |
13 | - $this->addColumn('{{%artbox_comment}}', 'entity_id', $this->integer() | |
14 | - ->notNull() | |
15 | - ->defaultValue(1)); | |
16 | - } | |
17 | - | |
18 | - public function down() | |
19 | - { | |
20 | - $this->dropColumn('{{%artbox_comment}}', 'entity'); | |
21 | - $this->dropColumn('{{%artbox_comment}}', 'entity_id'); | |
22 | - } | |
23 | - } |
migrations/m160726_211227_create_artbox_comment_rating.php
... | ... | @@ -10,33 +10,23 @@ |
10 | 10 | $this->createTable( |
11 | 11 | '{{%artbox_comment_rating}}', |
12 | 12 | [ |
13 | - 'artbox_comment_rating_id' => $this->primaryKey(), | |
14 | - 'created_at' => $this->integer() | |
15 | - ->notNull(), | |
16 | - 'updated_at' => $this->integer() | |
17 | - ->notNull(), | |
18 | - 'user_id' => $this->integer(), | |
19 | - 'value' => $this->float(), | |
20 | - 'model' => $this->string() | |
21 | - ->notNull(), | |
22 | - 'model_id' => $this->integer() | |
23 | - ->notNull(), | |
13 | + 'id' => $this->primaryKey(), | |
14 | + 'created_at' => $this->integer() | |
15 | + ->notNull(), | |
16 | + 'updated_at' => $this->integer() | |
17 | + ->notNull(), | |
18 | + 'customer_id' => $this->integer(), | |
19 | + 'value' => $this->float(), | |
20 | + 'model' => $this->string() | |
21 | + ->notNull(), | |
22 | + 'model_id' => $this->integer() | |
23 | + ->notNull(), | |
24 | 24 | ] |
25 | 25 | ); |
26 | - $this->addForeignKey( | |
27 | - 'user_id_user', | |
28 | - '{{%artbox_comment_rating}}', | |
29 | - 'user_id', | |
30 | - 'customer', | |
31 | - 'id', | |
32 | - 'CASCADE', | |
33 | - 'CASCADE' | |
34 | - ); | |
35 | 26 | } |
36 | 27 | |
37 | 28 | public function down() |
38 | 29 | { |
39 | - $this->dropForeignKey('user_id_user', '{{%artbox_comment_rating}}'); | |
40 | 30 | $this->dropTable('{{%artbox_comment_rating}}'); |
41 | 31 | } |
42 | 32 | } | ... | ... |
migrations/order-to-comment/m170525_095043_order_to_comment.php
0 → 100755
1 | +<?php | |
2 | + | |
3 | + use yii\db\Migration; | |
4 | + | |
5 | + class m170525_095043_order_to_comment extends Migration | |
6 | + { | |
7 | + public function safeUp() | |
8 | + { | |
9 | + $this->addForeignKey( | |
10 | + 'customer_id_customer', | |
11 | + '{{%artbox_comment}}', | |
12 | + 'customer_id', | |
13 | + 'customer', | |
14 | + 'id', | |
15 | + 'CASCADE', | |
16 | + 'CASCADE' | |
17 | + ); | |
18 | + | |
19 | + $this->addForeignKey( | |
20 | + 'customer_id_customer', | |
21 | + '{{%artbox_like}}', | |
22 | + 'customer_id', | |
23 | + 'customer', | |
24 | + 'id', | |
25 | + 'CASCADE', | |
26 | + 'CASCADE' | |
27 | + ); | |
28 | + } | |
29 | + | |
30 | + public function safeDown() | |
31 | + { | |
32 | + $this->dropForeignKey('customer_id_customer', '{{%artbox_comment}}'); | |
33 | + $this->dropForeignKey('customer_id_customer', '{{%artbox_like}}'); | |
34 | + } | |
35 | + } | ... | ... |
migrations/order-to-comment/m170525_095044_order_to_rating.php
0 → 100755
1 | +<?php | |
2 | + | |
3 | + use yii\db\Migration; | |
4 | + | |
5 | + class m170525_095044_order_to_rating extends Migration | |
6 | + { | |
7 | + public function safeUp() | |
8 | + { | |
9 | + $this->addForeignKey( | |
10 | + 'customer_id_customer', | |
11 | + '{{%artbox_comment_rating}}', | |
12 | + 'customer_id', | |
13 | + 'customer', | |
14 | + 'id', | |
15 | + 'CASCADE', | |
16 | + 'CASCADE' | |
17 | + ); | |
18 | + } | |
19 | + | |
20 | + public function safeDown() | |
21 | + { | |
22 | + $this->dropForeignKey('customer_id_customer', '{{%artbox_comment_rating}}'); | |
23 | + | |
24 | + } | |
25 | + } | ... | ... |
models/CommentModel.php
1 | 1 | <?php |
2 | - namespace artweb\artbox\comment\models; | |
3 | 2 | |
4 | - use artweb\artbox\comment\behaviors\ParentBehavior; | |
5 | - use artweb\artbox\comment\models\interfaces\CommentInterface; | |
6 | - use artweb\artbox\ecommerce\models\Product; | |
3 | + namespace artbox\webcomment\models; | |
4 | + | |
5 | + use artbox\webcomment\behaviors\ParentBehavior; | |
6 | + use artbox\webcomment\models\interfaces\CommentInterface; | |
7 | 7 | use yii\behaviors\AttributeBehavior; |
8 | 8 | use yii\behaviors\BlameableBehavior; |
9 | 9 | use yii\behaviors\TimestampBehavior; |
10 | 10 | use yii\data\ActiveDataProvider; |
11 | 11 | use yii\db\ActiveRecord; |
12 | - use yii\helpers\Html; | |
13 | - use yii\helpers\VarDumper; | |
14 | 12 | |
15 | 13 | /** |
16 | 14 | * Class CommentModel |
17 | 15 | * |
18 | - * @property int $artbox_comment_id | |
19 | - * @property string $text | |
20 | - * @property int $user_id | |
21 | - * @property string $username | |
22 | - * @property string $email | |
23 | - * @property int $created_at | |
24 | - * @property int $updated_at | |
25 | - * @property int $deleted_at | |
26 | - * @property int $status | |
27 | - * @property int $artbox_comment_pid | |
28 | - * @property int $related_id | |
29 | - * @property string $ip | |
30 | - * @property string $info | |
31 | - * @property string $entity | |
32 | - * @property int $entity_id | |
33 | - * @package artweb\artbox\comment\models | |
34 | - * @property ActiveRecord $entityModel | |
35 | - * @property string $link | |
16 | + * @property int $id | |
17 | + * @property string $text | |
18 | + * @property int $customer_id | |
19 | + * @property string $username | |
20 | + * @property string $email | |
21 | + * @property int $created_at | |
22 | + * @property int $updated_at | |
23 | + * @property int $deleted_at | |
24 | + * @property int $status | |
25 | + * @property int $parent_id | |
26 | + * @property int $related_id | |
27 | + * @property string $ip | |
28 | + * @property string $info | |
29 | + * @property string $entity | |
30 | + * @property int $entity_id | |
31 | + * @property ActiveRecord $entityModel | |
32 | + * @property string $link | |
33 | + * @property \artbox\webcomment\models\CommentModel[] $children | |
34 | + * @property \artbox\order\models\Customer $customer | |
36 | 35 | */ |
37 | 36 | class CommentModel extends ActiveRecord implements CommentInterface |
38 | 37 | { |
... | ... | @@ -43,11 +42,16 @@ |
43 | 42 | |
44 | 43 | const SCENARIO_USER = 'user'; |
45 | 44 | const SCENARIO_GUEST = 'guest'; |
45 | + const SCENARIO_ADMIN = 'admin'; | |
46 | + const SCENARIO_ADMIN_ANSWER = 'admin'; | |
46 | 47 | |
47 | 48 | public $encryptedEntity; |
48 | 49 | |
49 | 50 | public $entityId; |
50 | - | |
51 | + | |
52 | + /** | |
53 | + * @inheritdoc | |
54 | + */ | |
51 | 55 | public function scenarios() |
52 | 56 | { |
53 | 57 | $scenarios = parent::scenarios(); |
... | ... | @@ -55,7 +59,7 @@ |
55 | 59 | 'text', |
56 | 60 | 'entity', |
57 | 61 | 'entity_id', |
58 | - 'artbox_comment_pid', | |
62 | + 'parent_id', | |
59 | 63 | 'status', |
60 | 64 | ]; |
61 | 65 | $scenarios[ self::SCENARIO_GUEST ] = [ |
... | ... | @@ -66,14 +70,32 @@ |
66 | 70 | 'email', |
67 | 71 | 'status', |
68 | 72 | ]; |
73 | + $scenarios[ self::SCENARIO_ADMIN ] = [ | |
74 | + 'text', | |
75 | + 'status', | |
76 | + ]; | |
77 | + $scenarios[ self::SCENARIO_ADMIN_ANSWER ] = [ | |
78 | + 'text', | |
79 | + 'parent_id', | |
80 | + 'customer_id', | |
81 | + 'entity', | |
82 | + 'entity_id', | |
83 | + 'status', | |
84 | + ]; | |
69 | 85 | return $scenarios; |
70 | 86 | } |
71 | - | |
87 | + | |
88 | + /** | |
89 | + * @inheritdoc | |
90 | + */ | |
72 | 91 | public static function tableName() |
73 | 92 | { |
74 | 93 | return '{{%artbox_comment}}'; |
75 | 94 | } |
76 | - | |
95 | + | |
96 | + /** | |
97 | + * @inheritdoc | |
98 | + */ | |
77 | 99 | public function rules() |
78 | 100 | { |
79 | 101 | return [ |
... | ... | @@ -82,16 +104,10 @@ |
82 | 104 | 'text', |
83 | 105 | 'entity', |
84 | 106 | 'entity_id', |
85 | - ], | |
86 | - 'required', | |
87 | - ], | |
88 | - [ | |
89 | - [ | |
90 | 107 | 'username', |
91 | 108 | 'email', |
92 | 109 | ], |
93 | 110 | 'required', |
94 | - 'on' => self::SCENARIO_GUEST, | |
95 | 111 | ], |
96 | 112 | [ |
97 | 113 | [ |
... | ... | @@ -110,7 +126,7 @@ |
110 | 126 | [ |
111 | 127 | [ |
112 | 128 | 'entity_id', |
113 | - 'artbox_comment_pid', | |
129 | + 'parent_id', | |
114 | 130 | ], |
115 | 131 | 'integer', |
116 | 132 | ], |
... | ... | @@ -120,14 +136,17 @@ |
120 | 136 | 'value' => 0, |
121 | 137 | ], |
122 | 138 | [ |
123 | - [ 'artbox_comment_pid' ], | |
139 | + [ 'parent_id' ], | |
124 | 140 | 'exist', |
125 | - 'targetAttribute' => 'artbox_comment_id', | |
141 | + 'targetAttribute' => 'id', | |
126 | 142 | 'skipOnError' => true, |
127 | 143 | ], |
128 | 144 | ]; |
129 | 145 | } |
130 | - | |
146 | + | |
147 | + /** | |
148 | + * @inheritdoc | |
149 | + */ | |
131 | 150 | public function behaviors() |
132 | 151 | { |
133 | 152 | return [ |
... | ... | @@ -136,7 +155,7 @@ |
136 | 155 | ], |
137 | 156 | [ |
138 | 157 | 'class' => BlameableBehavior::className(), |
139 | - 'createdByAttribute' => 'user_id', | |
158 | + 'createdByAttribute' => 'customer_id', | |
140 | 159 | 'updatedByAttribute' => false, |
141 | 160 | ], |
142 | 161 | [ |
... | ... | @@ -144,7 +163,7 @@ |
144 | 163 | 'attributes' => [ |
145 | 164 | ActiveRecord::EVENT_BEFORE_INSERT => 'ip', |
146 | 165 | ], |
147 | - 'value' => function($event) { | |
166 | + 'value' => function () { | |
148 | 167 | return \Yii::$app->request->userIP; |
149 | 168 | }, |
150 | 169 | ], |
... | ... | @@ -153,58 +172,88 @@ |
153 | 172 | ], |
154 | 173 | ]; |
155 | 174 | } |
156 | - | |
175 | + | |
176 | + /** | |
177 | + * @inheritdoc | |
178 | + */ | |
157 | 179 | public function attributeLabels() |
158 | 180 | { |
159 | 181 | return [ |
160 | - 'artbox_comment_id' => \Yii::t('artbox-comment', 'ID'), | |
161 | - 'text' => \Yii::t('artbox-comment', 'Text'), | |
162 | - 'user_id' => \Yii::t('artbox-comment', 'User'), | |
163 | - 'username' => \Yii::t('artbox-comment', 'Username'), | |
164 | - 'email' => 'Email', | |
165 | - 'date_add' => \Yii::t('artbox-comment', 'Date add'), | |
166 | - 'updated_at' => \Yii::t('artbox-comment', 'Date update'), | |
167 | - 'deleted_at' => \Yii::t('artbox-comment', 'Date delete'), | |
168 | - 'status' => \Yii::t('artbox-comment', 'Status'), | |
169 | - 'artbox_comment_pid' => \Yii::t('artbox-comment', 'Comment parent'), | |
170 | - 'related_id' => \Yii::t('artbox-comment', 'Comment related'), | |
171 | - 'ip' => 'IP', | |
172 | - 'entity' => \Yii::t('artbox-comment', 'Entity'), | |
173 | - 'info' => \Yii::t('artbox-comment', 'Info'), | |
174 | - 'entity_id' => \Yii::t('artbox-comment', 'Entity ID'), | |
182 | + 'id' => \Yii::t('artbox-comment', 'ID'), | |
183 | + 'text' => \Yii::t('artbox-comment', 'Text'), | |
184 | + 'customer_id' => \Yii::t('artbox-comment', 'User'), | |
185 | + 'username' => \Yii::t('artbox-comment', 'Username'), | |
186 | + 'email' => 'Email', | |
187 | + 'created_at' => \Yii::t('artbox-comment', 'Date add'), | |
188 | + 'updated_at' => \Yii::t('artbox-comment', 'Date update'), | |
189 | + 'deleted_at' => \Yii::t('artbox-comment', 'Date delete'), | |
190 | + 'status' => \Yii::t('artbox-comment', 'Status'), | |
191 | + 'parent_id' => \Yii::t('artbox-comment', 'Comment parent'), | |
192 | + 'related_id' => \Yii::t('artbox-comment', 'Comment related'), | |
193 | + 'ip' => 'IP', | |
194 | + 'entity' => \Yii::t('artbox-comment', 'Entity'), | |
195 | + 'info' => \Yii::t('artbox-comment', 'Info'), | |
196 | + 'entity_id' => \Yii::t('artbox-comment', 'Entity ID'), | |
175 | 197 | ]; |
176 | 198 | } |
177 | - | |
199 | + | |
200 | + /** | |
201 | + * Set Entity of Comment model | |
202 | + * | |
203 | + * @param string $entity | |
204 | + */ | |
178 | 205 | public function setEntity(string $entity) |
179 | 206 | { |
180 | 207 | $this->entity = $entity; |
181 | 208 | } |
182 | - | |
209 | + | |
210 | + /** | |
211 | + * Get Entity of Comment model | |
212 | + * | |
213 | + * @return string | |
214 | + */ | |
183 | 215 | public function getEntity(): string |
184 | 216 | { |
185 | 217 | return $this->entity; |
186 | 218 | } |
187 | - | |
219 | + | |
220 | + /** | |
221 | + * Get ActiveDataProvider of comments for particular Entity according to its EntityId | |
222 | + * | |
223 | + * @param string $entity Entity name | |
224 | + * @param int $entityId Entity Id | |
225 | + * | |
226 | + * @return \yii\data\ActiveDataProvider | |
227 | + */ | |
188 | 228 | public static function getTree(string $entity, int $entityId): ActiveDataProvider |
189 | 229 | { |
230 | + $query = self::find() | |
231 | + ->with( | |
232 | + [ | |
233 | + 'children' => function ($query) { | |
234 | + /** | |
235 | + * @var \yii\db\ActiveQuery $query | |
236 | + */ | |
237 | + if (class_exists(self::getCustomerClass())) { | |
238 | + $query->with('customer'); | |
239 | + } | |
240 | + }, | |
241 | + ] | |
242 | + ) | |
243 | + ->where( | |
244 | + [ | |
245 | + 'entity' => $entity, | |
246 | + 'entity_id' => $entityId, | |
247 | + 'status' => self::STATUS_ACTIVE, | |
248 | + 'parent_id' => null, | |
249 | + ] | |
250 | + ); | |
251 | + if (class_exists(self::getCustomerClass())) { | |
252 | + $query->with('customer'); | |
253 | + } | |
190 | 254 | return new ActiveDataProvider( |
191 | 255 | [ |
192 | - 'query' => self::find() | |
193 | - ->with( | |
194 | - [ | |
195 | - 'children', | |
196 | - 'user', | |
197 | - 'children.user', | |
198 | - ] | |
199 | - ) | |
200 | - ->where( | |
201 | - [ | |
202 | - 'entity' => $entity, | |
203 | - 'entity_id' => $entityId, | |
204 | - 'status' => 1, | |
205 | - 'artbox_comment_pid' => NULL, | |
206 | - ] | |
207 | - ), | |
256 | + 'query' => $query, | |
208 | 257 | 'pagination' => [ |
209 | 258 | 'pageSize' => 20, |
210 | 259 | ], |
... | ... | @@ -216,93 +265,128 @@ |
216 | 265 | ] |
217 | 266 | ); |
218 | 267 | } |
219 | - | |
268 | + | |
269 | + /** | |
270 | + * Delete comment | |
271 | + * | |
272 | + * @return bool | |
273 | + */ | |
220 | 274 | public function deleteComment(): bool |
221 | 275 | { |
222 | - if (\Yii::$app->user->id != NULL && \Yii::$app->user->id == $this->user_id) { | |
276 | + if (!\Yii::$app->user->isGuest && \Yii::$app->user->id == $this->customer_id) { | |
223 | 277 | if ($this->delete()) { |
224 | 278 | return true; |
225 | 279 | } |
226 | 280 | } |
227 | 281 | return false; |
228 | 282 | } |
229 | - | |
283 | + | |
284 | + /** | |
285 | + * Set EntityId of Comment model | |
286 | + * | |
287 | + * @param int $entityId | |
288 | + */ | |
230 | 289 | public function setEntityId(int $entityId) |
231 | 290 | { |
232 | 291 | $this->entityId = $entityId; |
233 | 292 | } |
234 | - | |
293 | + | |
294 | + /** | |
295 | + * Get EntityId of Comment model | |
296 | + * | |
297 | + * @return int | |
298 | + */ | |
235 | 299 | public function getEntityId(): int |
236 | 300 | { |
237 | 301 | return $this->entityId; |
238 | 302 | } |
239 | - | |
303 | + | |
304 | + /** | |
305 | + * Get children relation for current comment | |
306 | + * | |
307 | + * @return \yii\db\ActiveQuery | |
308 | + */ | |
240 | 309 | public function getChildren() |
241 | 310 | { |
242 | - return $this->hasMany(self::className(), [ 'artbox_comment_pid' => 'artbox_comment_id' ]) | |
311 | + return $this->hasMany(self::className(), [ 'parent_id' => 'id' ]) | |
243 | 312 | ->andFilterWhere([ 'status' => self::STATUS_ACTIVE ]) |
244 | 313 | ->inverseOf('parent'); |
245 | 314 | } |
246 | - | |
315 | + | |
316 | + /** | |
317 | + * Get parent relation for current comment | |
318 | + * | |
319 | + * @return \yii\db\ActiveQuery | |
320 | + */ | |
247 | 321 | public function getParent() |
248 | 322 | { |
249 | - return $this->hasOne(self::className(), [ 'artbox_comment_id' => 'artbox_comment_pid' ]) | |
323 | + return $this->hasOne(self::className(), [ 'id' => 'parent_id' ]) | |
250 | 324 | ->inverseOf('children'); |
251 | 325 | } |
252 | - | |
253 | - public function getUser() | |
326 | + | |
327 | + /** | |
328 | + * Get customer relation for current comment | |
329 | + * | |
330 | + * @return \yii\db\ActiveQuery | |
331 | + */ | |
332 | + public function getCustomer() | |
254 | 333 | { |
255 | - $module = \Yii::$app->getModule('artbox-comment'); | |
256 | - return $this->hasOne($module->userIdentityClass, [ 'id' => 'user_id' ]); | |
334 | + return $this->hasOne(self::getCustomerClass(), [ 'id' => 'customer_id' ]); | |
257 | 335 | } |
258 | - | |
336 | + | |
337 | + /** | |
338 | + * Get rating relation for current model | |
339 | + * | |
340 | + * @return \yii\db\ActiveQuery | |
341 | + */ | |
259 | 342 | public function getRating() |
260 | 343 | { |
261 | - return $this->hasOne(RatingModel::className(), [ 'model_id' => 'artbox_comment_id' ]) | |
344 | + return $this->hasOne(RatingModel::className(), [ 'model_id' => 'id' ]) | |
262 | 345 | ->andWhere( |
263 | 346 | [ |
264 | 347 | 'or', |
265 | - [ 'artbox_comment_rating.model' => NULL ], | |
348 | + [ 'artbox_comment_rating.model' => null ], | |
266 | 349 | [ 'artbox_comment_rating.model' => self::className() ], |
267 | 350 | ] |
268 | 351 | ); |
269 | 352 | } |
270 | - | |
353 | + | |
354 | + /** | |
355 | + * Get entity model for current comment or false if not ActiveRecord | |
356 | + * | |
357 | + * @return ActiveRecord|false | |
358 | + */ | |
271 | 359 | public function getEntityModel() |
272 | 360 | { |
273 | - $model = call_user_func_array( | |
274 | - [ | |
275 | - $this->entity, | |
276 | - 'findOne', | |
277 | - ], | |
278 | - [ $this->entity_id ] | |
279 | - ); | |
280 | - return $model; | |
281 | - } | |
282 | - | |
283 | - public function getLink() | |
284 | - { | |
285 | - $model = $this->getEntityModel(); | |
286 | - if (empty($model)) { | |
287 | - return Html::a('Страница не найдена', '#'); | |
288 | - } | |
289 | - if (Product::className() == $this->entity) { | |
290 | - return Html::a( | |
291 | - 'Перейти на товар', | |
292 | - \Yii::$app->urlManagerFrontend->createAbsoluteUrl( | |
293 | - [ | |
294 | - 'catalog/product', | |
295 | - 'product' => $model->lang->alias, | |
296 | - 'variant' => $model->variant->sku, | |
297 | - ] | |
298 | - ), | |
361 | + if (method_exists($this->entity, 'findOne')) { | |
362 | + $model = call_user_func_array( | |
299 | 363 | [ |
300 | - 'target' => '_blank', | |
301 | - 'data-pjax' => '0', | |
302 | - ] | |
364 | + $this->entity, | |
365 | + 'findOne', | |
366 | + ], | |
367 | + [ $this->entity_id ] | |
303 | 368 | ); |
369 | + return $model; | |
370 | + } else { | |
371 | + return false; | |
372 | + } | |
373 | + } | |
374 | + | |
375 | + /** | |
376 | + * Get Customer model name | |
377 | + * | |
378 | + * @return string | |
379 | + */ | |
380 | + protected static function getCustomerClass() | |
381 | + { | |
382 | + /** | |
383 | + * @var \artbox\webcomment\Module $module | |
384 | + */ | |
385 | + $module = \Yii::$app->getModule('artbox-comment'); | |
386 | + if ($module) { | |
387 | + return $module->userIdentityClass; | |
304 | 388 | } else { |
305 | - return Html::a('Неизвестная модель', '#'); | |
389 | + return 'artbox\order\models\Customer'; | |
306 | 390 | } |
307 | 391 | } |
308 | 392 | } | ... | ... |
models/CommentModelSearch.php
1 | 1 | <?php |
2 | 2 | |
3 | - namespace artweb\artbox\comment\models; | |
3 | + namespace artbox\webcomment\models; | |
4 | 4 | |
5 | 5 | use yii\base\Model; |
6 | 6 | use yii\data\ActiveDataProvider; |
7 | 7 | |
8 | 8 | /** |
9 | 9 | * CommentModelSearch represents the model behind the search form about |
10 | - * `artweb\artbox\comment\models\CommentModel`. | |
10 | + * `artbox\webcomment\models\CommentModel`. | |
11 | 11 | */ |
12 | 12 | class CommentModelSearch extends CommentModel |
13 | 13 | { |
... | ... | @@ -24,12 +24,12 @@ |
24 | 24 | return [ |
25 | 25 | [ |
26 | 26 | [ |
27 | - 'artbox_comment_id', | |
27 | + 'id', | |
28 | 28 | 'created_at', |
29 | 29 | 'updated_at', |
30 | 30 | 'deleted_at', |
31 | 31 | 'status', |
32 | - 'artbox_comment_pid', | |
32 | + 'parent_id', | |
33 | 33 | 'related_id', |
34 | 34 | 'entity_id', |
35 | 35 | ], |
... | ... | @@ -52,7 +52,7 @@ |
52 | 52 | ], |
53 | 53 | [ |
54 | 54 | [ |
55 | - 'user_id', | |
55 | + 'customer_id', | |
56 | 56 | 'text', |
57 | 57 | 'username', |
58 | 58 | 'email', |
... | ... | @@ -64,14 +64,17 @@ |
64 | 64 | ], |
65 | 65 | ]; |
66 | 66 | } |
67 | - | |
67 | + | |
68 | + /** | |
69 | + * @inheritdoc | |
70 | + */ | |
68 | 71 | public function attributeLabels() |
69 | 72 | { |
70 | 73 | return array_merge( |
71 | 74 | parent::attributeLabels(), |
72 | 75 | [ |
73 | - 'ratingValue' => 'Рейтинг', | |
74 | - 'childrenCount' => 'Количество ответов', | |
76 | + 'ratingValue' => \Yii::t('artbox-comment', 'Рейтинг'), | |
77 | + 'childrenCount' => \Yii::t('artbox-comment', 'Количество ответов'), | |
75 | 78 | ] |
76 | 79 | ); |
77 | 80 | } |
... | ... | @@ -98,7 +101,7 @@ |
98 | 101 | ->joinWith( |
99 | 102 | [ |
100 | 103 | 'rating', |
101 | - 'user', | |
104 | + 'customer', | |
102 | 105 | ] |
103 | 106 | ); |
104 | 107 | |
... | ... | @@ -113,10 +116,9 @@ |
113 | 116 | 'asc' => [ 'artbox_comment_rating.value' => SORT_ASC ], |
114 | 117 | 'desc' => [ 'artbox_comment_rating.value' => SORT_DESC ], |
115 | 118 | ], |
116 | - 'artbox_comment_id', | |
117 | - 'date_add', | |
119 | + 'id', | |
118 | 120 | 'text', |
119 | - 'user_id', | |
121 | + 'customer_id', | |
120 | 122 | 'status', |
121 | 123 | 'entity', |
122 | 124 | 'entity_id', |
... | ... | @@ -140,12 +142,12 @@ |
140 | 142 | // grid filtering conditions |
141 | 143 | $query->andFilterWhere( |
142 | 144 | [ |
143 | - 'artbox_comment_id' => $this->artbox_comment_id, | |
145 | + 'id' => $this->id, | |
144 | 146 | 'created_at' => $this->created_at, |
145 | 147 | 'updated_at' => $this->updated_at, |
146 | 148 | 'deleted_at' => $this->deleted_at, |
147 | 149 | 'artbox_comment.status' => $this->status, |
148 | - 'artbox_comment_pid' => $this->artbox_comment_pid, | |
150 | + 'parent_id' => $this->parent_id, | |
149 | 151 | 'related_id' => $this->related_id, |
150 | 152 | 'entity_id' => $this->entity_id, |
151 | 153 | ] |
... | ... | @@ -198,26 +200,26 @@ |
198 | 200 | 'artbox_comment_rating.value' => $this->ratingValue, |
199 | 201 | ] |
200 | 202 | ); |
201 | - | |
202 | - if (!empty( $this->user_id )) { | |
203 | + | |
204 | + if (!empty($this->customer_id)) { | |
203 | 205 | $query->andWhere( |
204 | 206 | [ |
205 | 207 | 'or', |
206 | - [ 'artbox_comment.user_id' => (int) $this->user_id ], | |
208 | + [ 'artbox_comment.customer_id' => (int) $this->customer_id ], | |
207 | 209 | [ |
208 | 210 | 'like', |
209 | 211 | 'user.username', |
210 | - $this->user_id, | |
212 | + $this->customer_id, | |
211 | 213 | ], |
212 | 214 | [ |
213 | 215 | 'like', |
214 | 216 | 'artbox_comment.username', |
215 | - $this->user_id, | |
217 | + $this->customer_id, | |
216 | 218 | ], |
217 | 219 | [ |
218 | 220 | 'like', |
219 | 221 | 'artbox_comment.email', |
220 | - $this->user_id, | |
222 | + $this->customer_id, | |
221 | 223 | ], |
222 | 224 | ] |
223 | 225 | ); | ... | ... |
models/LikeModel.php
models/RatingModel.php
1 | 1 | <?php |
2 | 2 | |
3 | - namespace artweb\artbox\comment\models; | |
3 | + namespace artbox\webcomment\models; | |
4 | 4 | |
5 | 5 | use Yii; |
6 | 6 | use yii\behaviors\BlameableBehavior; |
... | ... | @@ -10,12 +10,10 @@ |
10 | 10 | /** |
11 | 11 | * This is the model class for table "artbox_comment_rating". |
12 | 12 | * |
13 | - * @todo Refactor User to Identity | |
14 | - * | |
15 | - * @property integer $artbox_comment_rating_id | |
13 | + * @property integer $id | |
16 | 14 | * @property string $created_at |
17 | 15 | * @property string $updated_at |
18 | - * @property integer $user_id | |
16 | + * @property integer $customer_id | |
19 | 17 | * @property integer $value |
20 | 18 | * @property string $model |
21 | 19 | * @property integer $model_id |
... | ... | @@ -58,7 +56,7 @@ |
58 | 56 | ], |
59 | 57 | [ |
60 | 58 | 'class' => BlameableBehavior::className(), |
61 | - 'createdByAttribute' => 'user_id', | |
59 | + 'createdByAttribute' => 'customer_id', | |
62 | 60 | 'updatedByAttribute' => false, |
63 | 61 | ], |
64 | 62 | ]; |
... | ... | @@ -70,18 +68,30 @@ |
70 | 68 | public function attributeLabels() |
71 | 69 | { |
72 | 70 | return [ |
73 | - 'rating_id' => Yii::t('app', 'Rating ID'), | |
74 | - 'date_add' => Yii::t('app', 'Date Add'), | |
75 | - 'updated_at' => Yii::t('app', 'Date Update'), | |
76 | - 'user_id' => Yii::t('app', 'User ID'), | |
77 | - 'entity' => Yii::t('app', 'Entity'), | |
78 | - 'value' => Yii::t('app', 'Value'), | |
71 | + 'id' => Yii::t('app', 'Rating ID'), | |
72 | + 'created_at' => Yii::t('app', 'Date Add'), | |
73 | + 'updated_at' => Yii::t('app', 'Date Update'), | |
74 | + 'customer_id' => Yii::t('app', 'User ID'), | |
75 | + 'entity' => Yii::t('app', 'Entity'), | |
76 | + 'value' => Yii::t('app', 'Value'), | |
79 | 77 | ]; |
80 | 78 | } |
81 | - | |
79 | + | |
80 | + /** | |
81 | + * Get entity model for current model | |
82 | + * | |
83 | + * @return \yii\db\ActiveQuery|null | |
84 | + */ | |
82 | 85 | public function getModel() |
83 | 86 | { |
84 | 87 | $model = $this->model; |
85 | - return $this->hasOne($model, [ $model::primaryKey() => 'model_id' ]); | |
88 | + if (method_exists($model, 'primaryKey')) { | |
89 | + /** | |
90 | + * @var ActiveRecord $model | |
91 | + */ | |
92 | + return $this->hasOne($model, [ $model::primaryKey()[ 0 ] => 'model_id' ]); | |
93 | + } else { | |
94 | + return null; | |
95 | + } | |
86 | 96 | } |
87 | 97 | } | ... | ... |
models/interfaces/CommentInterface.php
1 | 1 | <?php |
2 | 2 | |
3 | - namespace artweb\artbox\comment\models\interfaces; | |
3 | + namespace artbox\webcomment\models\interfaces; | |
4 | 4 | |
5 | 5 | use yii\data\ActiveDataProvider; |
6 | 6 | |
7 | 7 | /** |
8 | 8 | * Interface CommentInterface |
9 | - * @package artweb\artbox\comment\models\interfaces | |
10 | 9 | */ |
11 | 10 | interface CommentInterface |
12 | 11 | { | ... | ... |
models/interfaces/RatingCacheInterface.php
resources/artbox_comment.js
... | ... | @@ -174,8 +174,11 @@ |
174 | 174 | var form = $(data.formSelector + '-reply'); |
175 | 175 | var button = this; |
176 | 176 | var item = $(button).parents(data.itemContainerSelector); |
177 | + console.log(item); | |
177 | 178 | var item_id = $(item).data('key'); |
178 | - $(form).find('#commentmodel-artbox_comment_pid-reply').val(item_id); | |
179 | + $(form) | |
180 | + .find('#commentmodel-parent_id-reply') | |
181 | + .val(item_id); | |
179 | 182 | $(item).find(data.itemReplySelector).append(form); |
180 | 183 | } |
181 | 184 | |
... | ... | @@ -252,8 +255,11 @@ |
252 | 255 | var form = $(data.formSelector + '-reply'); |
253 | 256 | var button = this; |
254 | 257 | var item = $(button).parents(data.childContainerSelector); |
258 | + console.log(item); | |
255 | 259 | var item_id = $(item).data('key'); |
256 | - $(form).find('#commentmodel-artbox_comment_pid-reply').val(item_id); | |
260 | + $(form) | |
261 | + .find('#commentmodel-parent_id-reply') | |
262 | + .val(item_id); | |
257 | 263 | $(item).find(data.childReplySelector).append(form); |
258 | 264 | } |
259 | 265 | ... | ... |
views/artbox_comment_form.php
1 | 1 | <?php |
2 | - use artweb\artbox\comment\models\CommentModel; | |
3 | - use artweb\artbox\comment\models\RatingModel; | |
2 | + use artbox\webcomment\models\CommentModel; | |
3 | + use artbox\webcomment\models\RatingModel; | |
4 | 4 | use yii\base\Model; |
5 | 5 | use yii\helpers\Html; |
6 | 6 | use yii\helpers\Url; |
... | ... | @@ -15,42 +15,54 @@ |
15 | 15 | * @var View $this |
16 | 16 | * @var RatingModel|NULL $rating_model |
17 | 17 | */ |
18 | - $form = ActiveForm::begin([ | |
19 | - 'id' => $formId, | |
20 | - 'action' => Url::to([ | |
21 | - 'artbox-comment/default/create', | |
22 | - 'entity' => $comment_model->encryptedEntity, | |
23 | - ]), | |
24 | - ]); | |
18 | + $form = ActiveForm::begin( | |
19 | + [ | |
20 | + 'id' => $formId, | |
21 | + 'action' => Url::to( | |
22 | + [ | |
23 | + 'artbox-comment/default/create', | |
24 | + 'entity' => $comment_model->encryptedEntity, | |
25 | + ] | |
26 | + ), | |
27 | + ] | |
28 | + ); | |
25 | 29 | ?> |
26 | - <div class="form-comm-wr"> | |
27 | - <?php | |
28 | - if(!empty( $rating_model )) { | |
29 | - ?> | |
30 | - <div class="input_bl stars-wr_"> | |
31 | - <?php | |
32 | - echo $form->field($rating_model, 'value', [ 'enableClientValidation' => false ]) | |
33 | - ->hiddenInput() | |
34 | - ->label(false); | |
35 | - echo Html::tag('div', '', [ | |
30 | + <div class="form-comm-wr"> | |
31 | + <?php | |
32 | + if (!empty($rating_model)) { | |
33 | + ?> | |
34 | + <div class="input_bl stars-wr_"> | |
35 | + <?php | |
36 | + echo $form->field($rating_model, 'value', [ 'enableClientValidation' => false ]) | |
37 | + ->hiddenInput() | |
38 | + ->label(false); | |
39 | + echo Html::tag( | |
40 | + 'div', | |
41 | + '', | |
42 | + [ | |
36 | 43 | 'class' => 'rateit', |
37 | 44 | 'data-rateit-backingfld' => '#' . Html::getInputId($rating_model, 'value'), |
38 | - ]); | |
39 | - ?> | |
40 | - </div> | |
41 | - <?php | |
42 | - } | |
43 | - if(\Yii::$app->user->isGuest) { | |
44 | - echo $form->field($comment_model, 'username', [ 'options' => [ 'class' => 'form-group input_bl' ] ]) | |
45 | - ->textInput(); | |
46 | - echo $form->field($comment_model, 'email', [ 'options' => [ 'class' => 'form-group input_bl' ] ]) | |
47 | - ->textInput(); | |
48 | - } | |
49 | - echo $form->field($comment_model, 'text', [ 'options' => [ 'class' => 'form-group input_bl area_bl' ] ]) | |
50 | - ->textarea(); | |
51 | - echo Html::tag('div', Html::submitButton(Yii::t('artbox-comment', 'Submit')), [ 'class' => 'input_bl submit_btn' ]); | |
52 | - ?> | |
53 | - </div> | |
45 | + ] | |
46 | + ); | |
47 | + ?> | |
48 | + </div> | |
49 | + <?php | |
50 | + } | |
51 | + if (\Yii::$app->user->isGuest) { | |
52 | + echo $form->field($comment_model, 'username', [ 'options' => [ 'class' => 'form-group input_bl' ] ]) | |
53 | + ->textInput(); | |
54 | + echo $form->field($comment_model, 'email', [ 'options' => [ 'class' => 'form-group input_bl' ] ]) | |
55 | + ->textInput(); | |
56 | + } | |
57 | + echo $form->field($comment_model, 'text', [ 'options' => [ 'class' => 'form-group input_bl area_bl' ] ]) | |
58 | + ->textarea(); | |
59 | + echo Html::tag( | |
60 | + 'div', | |
61 | + Html::submitButton(Yii::t('artbox-comment', 'Submit')), | |
62 | + [ 'class' => 'input_bl submit_btn' ] | |
63 | + ); | |
64 | + ?> | |
65 | + </div> | |
54 | 66 | <?php |
55 | 67 | ActiveForm::end(); |
56 | 68 | ?> |
57 | 69 | \ No newline at end of file | ... | ... |
views/artbox_comment_item.php
1 | 1 | <?php |
2 | - use artweb\artbox\comment\models\CommentModel; | |
2 | + use artbox\webcomment\models\CommentModel; | |
3 | 3 | use yii\helpers\Html; |
4 | 4 | use yii\helpers\Url; |
5 | 5 | use yii\widgets\ListView; |
... | ... | @@ -12,139 +12,149 @@ |
12 | 12 | */ |
13 | 13 | ?> |
14 | 14 | <div class="comments-wr"> |
15 | - <div class="artbox_item_info"> | |
16 | - <div class="user-ico"> | |
17 | - <?php | |
18 | - echo Html::img('/img/user-noimage.png'); | |
19 | - ?> | |
20 | - </div> | |
21 | - <div class="user_data" itemprop="datePublished"> | |
22 | - <?php | |
23 | - echo date('d.m.Y', $model->created_at); | |
24 | - ?> | |
25 | - </div> | |
26 | - <div class="user_name" itemprop="author"> | |
27 | - <?php | |
28 | - if(!empty( $model->user )) { | |
29 | - echo $model->user->username; | |
30 | - } else { | |
31 | - echo $model->username . ' (' . Yii::t('artbox-comment', 'Guest') . ')'; | |
32 | - } | |
33 | - ?> | |
34 | - </div> | |
15 | + <div class="artbox_item_info"> | |
16 | + <div class="user-ico"> | |
35 | 17 | <?php |
36 | - if(!empty( $model->rating )) { | |
37 | - ?> | |
38 | - <div class="user_rating" itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating"> | |
39 | - <span itemprop="worstRating" style="display: none">1</span> | |
40 | - <span itemprop="ratingValue" style="display: none"><?php echo $model->rating->value; ?></span> | |
41 | - <span itemprop="bestRating" style="display: none">5</span> | |
42 | - <div class="rateit" data-rateit-value="<?php echo $model->rating->value; ?>" data-rateit-ispreset="true" data-rateit-readonly="true"></div> | |
43 | - </div> | |
44 | - <?php | |
45 | - } | |
18 | + echo Html::img('/img/no-image.png'); | |
46 | 19 | ?> |
47 | - <div class="user_txt" itemprop="description"> | |
48 | - <?php | |
49 | - echo $model->text; | |
50 | - ?> | |
51 | - </div> | |
52 | 20 | </div> |
53 | - <div class="artbox_item_tools comment-panel"> | |
21 | + <div class="user_data" itemprop="datePublished"> | |
54 | 22 | <?php |
55 | - if(!\Yii::$app->user->isGuest) { | |
56 | - ?> | |
57 | - <a href="" class="btn-comm-answer" data-action="reply">Ответить</a> | |
58 | - <?php | |
59 | - } | |
60 | - if(!\Yii::$app->user->isGuest && \Yii::$app->user->id == $model->user_id) { | |
61 | - ?> | |
62 | - <a href="" class="btn-comm-delete" data-action="delete" data-url="<?php echo Url::to([ | |
63 | - 'artbox-comment/default/delete', | |
64 | - 'id' => $model->artbox_comment_id, | |
65 | - ]); ?>">Удалить</a> | |
66 | - <?php | |
23 | + echo date('d.m.Y', $model->created_at); | |
24 | + ?> | |
25 | + </div> | |
26 | + <div class="user_name" itemprop="author"> | |
27 | + <?php | |
28 | + if (!empty($model->customer)) { | |
29 | + echo $model->customer->username; | |
30 | + } else { | |
31 | + echo $model->username . ' (' . Yii::t('artbox-comment', 'Guest') . ')'; | |
67 | 32 | } |
68 | - // Like / dislike to be done | |
69 | - /* | |
70 | - ?> | |
71 | - <a href="" class="btn-comm-like" data-action="like" data-url="<?php echo Url::to([ | |
72 | - 'artbox-comment/default/like', | |
73 | - 'id' => $model->artbox_comment_id, | |
74 | - ]); ?>">Like</a> | |
75 | - <a href="" class="btn-comm-dislike" data-action="dislike" data-url="<?php echo Url::to([ | |
76 | - 'artbox-comment/default/dislike', | |
77 | - 'id' => $model->artbox_comment_id, | |
78 | - ]); ?>">Dislike</a> | |
79 | - <?php | |
80 | - */ | |
81 | 33 | ?> |
82 | - <div class="artbox_item_reply"></div> | |
83 | 34 | </div> |
35 | + <?php | |
36 | + if (!empty($model->rating)) { | |
37 | + ?> | |
38 | + <div class="user_rating" itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating"> | |
39 | + <span itemprop="worstRating" style="display: none">1</span> | |
40 | + <span itemprop="ratingValue" style="display: none"><?php echo $model->rating->value; ?></span> | |
41 | + <span itemprop="bestRating" style="display: none">5</span> | |
42 | + <div class="rateit" data-rateit-value="<?php echo $model->rating->value; ?>" data-rateit-ispreset="true" data-rateit-readonly="true"></div> | |
43 | + </div> | |
44 | + <?php | |
45 | + } | |
46 | + ?> | |
47 | + <div class="user_txt" itemprop="description"> | |
48 | + <?php | |
49 | + echo $model->text; | |
50 | + ?> | |
51 | + </div> | |
52 | + </div> | |
53 | + <div class="artbox_item_tools comment-panel"> | |
54 | + <?php | |
55 | + if (!\Yii::$app->user->isGuest) { | |
56 | + ?> | |
57 | + <a href="" class="btn-comm-answer" data-action="reply"><?php echo \Yii::t( | |
58 | + 'artbox-comment', | |
59 | + 'Ответить' | |
60 | + ); ?></a> | |
61 | + <?php | |
62 | + } | |
63 | + if (!\Yii::$app->user->isGuest && \Yii::$app->user->id == $model->customer_id) { | |
64 | + ?> | |
65 | + <a href="" class="btn-comm-delete" data-action="delete" data-url="<?php echo Url::to( | |
66 | + [ | |
67 | + 'artbox-comment/default/delete', | |
68 | + 'id' => $model->id, | |
69 | + ] | |
70 | + ); ?>"><?php echo \Yii::t('artbox-comment', 'Удалить'); ?></a> | |
71 | + <?php | |
72 | + } | |
73 | + // Like / dislike to be done | |
74 | + /* | |
75 | + ?> | |
76 | + <a href="" class="btn-comm-like" data-action="like" data-url="<?php echo Url::to([ | |
77 | + 'artbox-comment/default/like', | |
78 | + 'id' => $model->artbox_comment_id, | |
79 | + ]); ?>">Like</a> | |
80 | + <a href="" class="btn-comm-dislike" data-action="dislike" data-url="<?php echo Url::to([ | |
81 | + 'artbox-comment/default/dislike', | |
82 | + 'id' => $model->artbox_comment_id, | |
83 | + ]); ?>">Dislike</a> | |
84 | + <?php | |
85 | + */ | |
86 | + ?> | |
87 | + <div class="artbox_item_reply"></div> | |
88 | + </div> | |
84 | 89 | </div> |
85 | 90 | <div class="artbox_children_container"> |
86 | 91 | <?php |
87 | - if(!empty( $model->children )) { | |
88 | - foreach($model->children as $index => $child) { | |
92 | + if (!empty($model->children)) { | |
93 | + foreach ($model->children as $index => $child) { | |
89 | 94 | ?> |
90 | - <div class="artbox_child_container comment-answer"> | |
91 | - <div class="artbox_child_info"> | |
92 | - <div class="user-ico"> | |
93 | - <?php | |
94 | - echo Html::img('/img/user-noimage.png'); | |
95 | - ?> | |
96 | - </div> | |
97 | - <div class="user_data"> | |
98 | - <?php | |
99 | - echo date('d.m.Y', $child->created_at); | |
100 | - ?> | |
101 | - </div> | |
102 | - <div class="user_name"> | |
103 | - <?php | |
104 | - if(!empty( $child->user )) { | |
105 | - echo $child->user->username; | |
106 | - } else { | |
107 | - echo $child->username . ' (' . Yii::t('artbox-comment', 'Guest') . ')'; | |
108 | - } | |
95 | + <div class="artbox_child_container comment-answer" data-key="<?php echo $child->id; ?>"> | |
96 | + <div class="artbox_child_info"> | |
97 | + <div class="user-ico"> | |
98 | + <?php | |
99 | + echo Html::img('/img/no-image.png'); | |
100 | + ?> | |
101 | + </div> | |
102 | + <div class="user_data"> | |
103 | + <?php | |
104 | + echo date('d.m.Y', $child->created_at); | |
105 | + ?> | |
106 | + </div> | |
107 | + <div class="user_name"> | |
108 | + <?php | |
109 | + if (!empty($child->customer)) { | |
110 | + echo $child->customer->username; | |
111 | + } else { | |
112 | + echo $child->username . ' (' . Yii::t('artbox-comment', 'Guest') . ')'; | |
113 | + } | |
114 | + ?> | |
115 | + </div> | |
116 | + <div class="user_txt"> | |
117 | + <?php | |
118 | + echo $child->text; | |
119 | + ?> | |
120 | + </div> | |
121 | + </div> | |
122 | + <div class="artbox_child_tools comment-panel"> | |
123 | + <?php | |
124 | + if (!\Yii::$app->user->isGuest) { | |
109 | 125 | ?> |
110 | - </div> | |
111 | - <div class="user_txt"> | |
126 | + <a href="" class="btn-comm-answer" data-action="reply"><?php echo \Yii::t( | |
127 | + 'artbox-comment', | |
128 | + 'Ответить' | |
129 | + ); ?></a> | |
112 | 130 | <?php |
113 | - echo $child->text; | |
131 | + } | |
132 | + if (!\Yii::$app->user->isGuest && \Yii::$app->user->id == $child->customer_id) { | |
114 | 133 | ?> |
115 | - </div> | |
116 | - </div> | |
117 | - <div class="artbox_child_tools comment-panel"> | |
118 | - <?php | |
119 | - if(!\Yii::$app->user->isGuest) { | |
120 | - ?> | |
121 | - <a href="" class="btn-comm-answer" data-action="reply">Ответить</a> | |
122 | - <?php | |
123 | - } | |
124 | - if(!\Yii::$app->user->isGuest && \Yii::$app->user->id == $child->user_id) { | |
125 | - ?> | |
126 | - <a href="" class="btn-comm-delete" data-action="delete" data-url="<?php echo Url::to([ | |
127 | - 'artbox-comment/default/delete', | |
128 | - 'id' => $child->artbox_comment_id, | |
129 | - ]); ?>">Удалить</a> | |
130 | - <?php | |
131 | - } | |
132 | - /* Like /dislike to be done | |
133 | - ?> | |
134 | - <a href="" class="btn-comm-like" data-action="like" data-url="<?php echo Url::to([ | |
135 | - 'artbox-comment/default/like', | |
136 | - 'id' => $child->artbox_comment_id, | |
137 | - ]); ?>">Like</a> | |
138 | - <a href="" class="btn-comm-dislike" data-action="dislike" data-url="<?php echo Url::to([ | |
139 | - 'artbox-comment/default/dislike', | |
140 | - 'id' => $child->artbox_comment_id, | |
141 | - ]); ?>">Dislike</a> | |
134 | + <a href="" class="btn-comm-delete" data-action="delete" data-url="<?php echo Url::to( | |
135 | + [ | |
136 | + 'artbox-comment/default/delete', | |
137 | + 'id' => $child->id, | |
138 | + ] | |
139 | + ); ?>"><?php echo \Yii::t('artbox-comment', 'Удалить'); ?></a> | |
142 | 140 | <?php |
143 | - */ | |
141 | + } | |
142 | + /* Like /dislike to be done | |
144 | 143 | ?> |
145 | - <div class="artbox_child_reply"></div> | |
146 | - </div> | |
144 | + <a href="" class="btn-comm-like" data-action="like" data-url="<?php echo Url::to([ | |
145 | + 'artbox-comment/default/like', | |
146 | + 'id' => $child->artbox_comment_id, | |
147 | + ]); ?>">Like</a> | |
148 | + <a href="" class="btn-comm-dislike" data-action="dislike" data-url="<?php echo Url::to([ | |
149 | + 'artbox-comment/default/dislike', | |
150 | + 'id' => $child->artbox_comment_id, | |
151 | + ]); ?>">Dislike</a> | |
152 | + <?php | |
153 | + */ | |
154 | + ?> | |
155 | + <div class="artbox_child_reply"></div> | |
147 | 156 | </div> |
157 | + </div> | |
148 | 158 | <?php |
149 | 159 | } |
150 | 160 | } | ... | ... |
views/artbox_comment_list.php
views/artbox_comment_reply.php
1 | 1 | <?php |
2 | - use artweb\artbox\comment\models\CommentModel; | |
2 | + use artbox\webcomment\models\CommentModel; | |
3 | 3 | use yii\base\Model; |
4 | 4 | use yii\helpers\Html; |
5 | 5 | use yii\helpers\Url; |
... | ... | @@ -14,14 +14,14 @@ |
14 | 14 | * @var View $this |
15 | 15 | */ |
16 | 16 | $text_input_id = Html::getInputId($comment_model, 'text') . '-reply'; |
17 | - $artbox_comment_pid_input_id = Html::getInputId($comment_model, 'artbox_comment_pid') . '-reply'; | |
17 | + $parent_id_input_id = Html::getInputId($comment_model, 'parent_id') . '-reply'; | |
18 | 18 | $text_input_selectors = [ |
19 | 19 | 'container' => '.field-' . $text_input_id, |
20 | 20 | 'input' => '#' . $text_input_id, |
21 | 21 | ]; |
22 | - $artbox_comment_pid_input_selectors = [ | |
23 | - 'container' => '.field-' . $artbox_comment_pid_input_id, | |
24 | - 'input' => '#' . $artbox_comment_pid_input_id, | |
22 | + $parent_id_input_selectors = [ | |
23 | + 'container' => '.field-' . $parent_id_input_id, | |
24 | + 'input' => '#' . $parent_id_input_id, | |
25 | 25 | ]; |
26 | 26 | $form = ActiveForm::begin([ |
27 | 27 | 'id' => $formId . '-reply', |
... | ... | @@ -33,10 +33,13 @@ |
33 | 33 | ?> |
34 | 34 | <div class="answer-form"> |
35 | 35 | <?php |
36 | - echo $form->field($comment_model, 'artbox_comment_pid', [ | |
37 | - 'selectors' => $artbox_comment_pid_input_selectors, | |
36 | + echo $form->field( | |
37 | + $comment_model, | |
38 | + 'parent_id', | |
39 | + [ | |
40 | + 'selectors' => $parent_id_input_selectors, | |
38 | 41 | 'inputOptions' => [ |
39 | - 'id' => $artbox_comment_pid_input_id, | |
42 | + 'id' => $parent_id_input_id, | |
40 | 43 | 'class' => 'form-control', |
41 | 44 | ], |
42 | 45 | ]) | ... | ... |
1 | 1 | <?php |
2 | - use artweb\artbox\comment\models\CommentModel; | |
2 | + use artbox\webcomment\models\CommentModel; | |
3 | 3 | use yii\helpers\Html; |
4 | 4 | use yii\web\View; |
5 | 5 | use yii\widgets\ActiveForm; |
... | ... | @@ -10,14 +10,13 @@ |
10 | 10 | * @var CommentModel $answer |
11 | 11 | */ |
12 | 12 | |
13 | - $this->title = 'Ответить на комментарий# ' . $model->artbox_comment_id; | |
13 | + $this->title = \Yii::t('artbox-comment', 'Ответить на комментарий') . '# ' . $model->id; | |
14 | 14 | |
15 | 15 | $this->params[ 'breadcrumbs' ][] = [ |
16 | - 'label' => 'Комментарии', | |
16 | + 'label' => \Yii::t('artbox-comment', 'Комментарии'), | |
17 | 17 | 'url' => [ 'index' ], |
18 | 18 | ]; |
19 | 19 | $this->params[ 'breadcrumbs' ][] = $this->title; |
20 | - | |
21 | 20 | ?> |
22 | 21 | |
23 | 22 | <div class="comment-answer"> |
... | ... | @@ -26,16 +25,16 @@ |
26 | 25 | <div class="form-group"> |
27 | 26 | <?= $form->field($answer, 'text') |
28 | 27 | ->textarea() ?> |
29 | - | |
30 | - <?= $form->field($answer, 'artbox_comment_pid') | |
28 | + | |
29 | + <?= $form->field($answer, 'parent_id') | |
31 | 30 | ->hiddenInput( |
32 | 31 | [ |
33 | - 'value' => $model->artbox_comment_id, | |
32 | + 'value' => $model->id, | |
34 | 33 | ] |
35 | 34 | ) |
36 | 35 | ->label(false) ?> |
37 | - | |
38 | - <?= $form->field($answer, 'user_id') | |
36 | + | |
37 | + <?= $form->field($answer, 'customer_id') | |
39 | 38 | ->hiddenInput( |
40 | 39 | [ |
41 | 40 | 'value' => 1, |
... | ... | @@ -60,7 +59,7 @@ |
60 | 59 | ->label(false) ?> |
61 | 60 | |
62 | 61 | <?= Html::submitButton( |
63 | - \Yii::t('app', 'Ответить'), | |
62 | + \Yii::t('artbox-comment', 'Ответить'), | |
64 | 63 | [ 'class' => 'btn btn-primary' ] |
65 | 64 | ) ?> |
66 | 65 | </div> | ... | ... |
views/manage/index.php
1 | 1 | <?php |
2 | - use artweb\artbox\comment\models\CommentModel; | |
3 | - use artweb\artbox\comment\models\CommentModelSearch; | |
2 | + use artbox\webcomment\models\CommentModel; | |
3 | + use artbox\webcomment\models\CommentModelSearch; | |
4 | 4 | use yii\data\ActiveDataProvider; |
5 | 5 | use yii\grid\GridView; |
6 | 6 | use yii\helpers\Html; |
... | ... | @@ -14,17 +14,17 @@ |
14 | 14 | * @var string $commentModel |
15 | 15 | * @var View $this |
16 | 16 | */ |
17 | - $this->title = 'Комментарии'; | |
17 | + $this->title = \Yii::t('artbox-comment', 'Комментарии'); | |
18 | 18 | |
19 | 19 | $this->params[ 'breadcrumbs' ][] = $this->title; |
20 | 20 | |
21 | 21 | $statuses = [ |
22 | - $searchModel::STATUS_ACTIVE => 'Активный', | |
23 | - $searchModel::STATUS_HIDDEN => 'Скрытый', | |
24 | - $searchModel::STATUS_DELETED => 'Удаленный', | |
22 | + $searchModel::STATUS_ACTIVE => \Yii::t('artbox-comment', 'Активный'), | |
23 | + $searchModel::STATUS_HIDDEN => \Yii::t('artbox-comment', 'Скрытый'), | |
24 | + $searchModel::STATUS_DELETED => \Yii::t('artbox-comment', 'Удаленный'), | |
25 | 25 | ]; |
26 | 26 | Pjax::begin(); |
27 | - if (( $success = \Yii::$app->session->getFlash('artbox_comment_success') ) != NULL) { | |
27 | + if (( $success = \Yii::$app->session->getFlash('artbox_comment_success') ) != null) { | |
28 | 28 | echo Html::tag('p', $success); |
29 | 29 | } |
30 | 30 | echo GridView::widget( |
... | ... | @@ -32,19 +32,7 @@ |
32 | 32 | 'dataProvider' => $dataProvider, |
33 | 33 | 'filterModel' => $searchModel, |
34 | 34 | 'columns' => [ |
35 | - [ | |
36 | - 'attribute' => 'artbox_comment_id', | |
37 | - 'label' => 'Id', | |
38 | - ], | |
39 | - [ | |
40 | - 'label' => 'url', | |
41 | - 'content' => function (CommentModel $model) { | |
42 | - return $model->getLink(); | |
43 | - }, | |
44 | - ], | |
45 | - [ | |
46 | - 'label' => 'Ссылка', | |
47 | - ], | |
35 | + 'id', | |
48 | 36 | [ |
49 | 37 | 'attribute' => 'created_at', |
50 | 38 | 'format' => [ |
... | ... | @@ -54,51 +42,70 @@ |
54 | 42 | 'filter' => false, |
55 | 43 | ], |
56 | 44 | [ |
57 | - 'label' => 'Комментарий', | |
58 | - 'content' => function(CommentModel $model) { | |
45 | + 'label' => \Yii::t('artbox-comment', 'Комментарий'), | |
46 | + 'content' => function (CommentModel $model) { | |
59 | 47 | return StringHelper::truncate($model->text, 40, '...'); |
60 | 48 | }, |
61 | 49 | ], |
62 | 50 | [ |
63 | - 'attribute' => 'user_id', | |
64 | - 'value' => function($model) { | |
65 | - if (!empty($model->user)) { | |
66 | - return $model->user->username . ' (id:' . $model->user->id . ')'; | |
51 | + 'attribute' => 'customer_id', | |
52 | + 'value' => function ($model) { | |
53 | + /** | |
54 | + * @var CommentModel $model | |
55 | + */ | |
56 | + if (!empty($model->customer)) { | |
57 | + return $model->customer->username . ' (id:' . $model->customer->id . ')'; | |
67 | 58 | } else { |
68 | - return $model->username . ' ' . $model->email . ' (Гость)'; | |
59 | + return $model->username . ' ' . $model->email . ' (' . \Yii::t( | |
60 | + 'artbox-comment', | |
61 | + 'Гость' | |
62 | + ) . ')'; | |
69 | 63 | } |
70 | 64 | }, |
71 | 65 | ], |
72 | 66 | [ |
73 | 67 | 'attribute' => 'status', |
74 | 68 | 'filter' => $statuses, |
75 | - 'value' => function($model) use ($statuses) { | |
76 | - return $statuses[ $model->status ]; | |
69 | + 'value' => function ($model) use ($statuses) { | |
70 | + /** | |
71 | + * @var CommentModel $model | |
72 | + */ | |
73 | + if (array_key_exists($model->status, $statuses)) { | |
74 | + return $statuses[ $model->status ]; | |
75 | + } else { | |
76 | + return null; | |
77 | + } | |
77 | 78 | }, |
78 | 79 | ], |
79 | 80 | [ |
80 | 81 | 'attribute' => 'ratingValue', |
81 | - 'value' => function($model) { | |
82 | + 'value' => function ($model) { | |
83 | + /** | |
84 | + * @var CommentModel $model | |
85 | + */ | |
82 | 86 | if (!empty($model->rating)) { |
83 | 87 | return $model->rating->value; |
84 | 88 | } |
85 | - return NULL; | |
89 | + return null; | |
86 | 90 | }, |
87 | 91 | ], |
88 | 92 | [ |
89 | 93 | 'attribute' => 'childrenCount', |
90 | - 'value' => function($model) { | |
94 | + 'value' => function ($model) { | |
95 | + /** | |
96 | + * @var CommentModel $model | |
97 | + */ | |
91 | 98 | return count($model->children); |
92 | 99 | }, |
93 | 100 | ], |
94 | 101 | [ |
95 | 102 | 'class' => 'yii\grid\ActionColumn', |
96 | 103 | 'buttons' => [ |
97 | - 'answer' => function(string $url) { | |
104 | + 'answer' => function (string $url) { | |
98 | 105 | return Html::a(Html::tag('i', '', [ 'class' => 'glyphicon glyphicon-bullhorn' ]), $url); |
99 | 106 | }, |
100 | 107 | ], |
101 | - 'template' => \Yii::$app->user->identity->isAdmin() ? '{update} {answer} {delete}' : '{update} {answer}', | |
108 | + 'template' => '{update} {answer} {delete}', | |
102 | 109 | ], |
103 | 110 | ], |
104 | 111 | ] | ... | ... |
views/manage/update.php
1 | 1 | <?php |
2 | - use artweb\artbox\comment\models\CommentModel; | |
2 | + use artbox\webcomment\models\CommentModel; | |
3 | 3 | use yii\helpers\Html; |
4 | 4 | use yii\widgets\ActiveForm; |
5 | 5 | |
... | ... | @@ -7,14 +7,14 @@ |
7 | 7 | * @var CommentModel $model |
8 | 8 | */ |
9 | 9 | $statuses = [ |
10 | - $model::STATUS_ACTIVE => 'Активный', | |
11 | - $model::STATUS_HIDDEN => 'Скрытый', | |
12 | - $model::STATUS_DELETED => 'Удаленный', | |
10 | + $model::STATUS_ACTIVE => \Yii::t('artbox-comment', 'Активный'), | |
11 | + $model::STATUS_HIDDEN => \Yii::t('artbox-comment', 'Скрытый'), | |
12 | + $model::STATUS_DELETED => \Yii::t('artbox-comment', 'Удаленный'), | |
13 | 13 | ]; |
14 | 14 | $form = ActiveForm::begin(); |
15 | 15 | echo $form->field($model, 'text') |
16 | 16 | ->textarea(); |
17 | 17 | echo $form->field($model, 'status') |
18 | 18 | ->dropDownList($statuses); |
19 | - echo Html::submitButton('Обновить'); | |
19 | + echo Html::submitButton(\Yii::t('artbox-comment', 'Обновить')); | |
20 | 20 | $form->end(); |
21 | 21 | \ No newline at end of file | ... | ... |
widgets/CommentWidget.php
1 | 1 | <?php |
2 | 2 | |
3 | - namespace artweb\artbox\comment\widgets; | |
3 | + namespace artbox\webcomment\widgets; | |
4 | 4 | |
5 | - use artweb\artbox\comment\assets\CommentAsset; | |
6 | - use artweb\artbox\comment\models\interfaces\CommentInterface; | |
7 | - use artweb\artbox\comment\models\RatingModel; | |
8 | - use artweb\artbox\comment\Module; | |
9 | - use artweb\artbox\models\Customer; | |
5 | + use artbox\webcomment\assets\CommentAsset; | |
6 | + use artbox\webcomment\models\CommentModel; | |
7 | + use artbox\webcomment\models\interfaces\CommentInterface; | |
8 | + use artbox\webcomment\models\RatingModel; | |
9 | + use artbox\webcomment\Module; | |
10 | 10 | use Yii; |
11 | 11 | use yii\base\InvalidConfigException; |
12 | 12 | use yii\base\Model; |
... | ... | @@ -19,20 +19,22 @@ |
19 | 19 | |
20 | 20 | /** |
21 | 21 | * Class CommentWidget |
22 | + * | |
22 | 23 | * @property Model $model Model, to which comment attached |
23 | - * @package artweb\artbox\comment\widgets | |
24 | 24 | */ |
25 | 25 | class CommentWidget extends Widget |
26 | 26 | { |
27 | 27 | |
28 | 28 | /** |
29 | 29 | * Model, to which comment attached |
30 | + * | |
30 | 31 | * @var Model Model |
31 | 32 | */ |
32 | 33 | //public $model; |
33 | 34 | |
34 | 35 | /** |
35 | 36 | * Options |
37 | + * | |
36 | 38 | * @var array |
37 | 39 | */ |
38 | 40 | public $options = [ |
... | ... | @@ -43,10 +45,11 @@ |
43 | 45 | /** |
44 | 46 | * @var string the view file that will render comment form. |
45 | 47 | */ |
46 | - public $formView = '@artbox-comment/views/artbox_comment_form'; | |
48 | + public $formView = '@artbox/webcomment/views/artbox_comment_form'; | |
47 | 49 | |
48 | 50 | /** |
49 | 51 | * Form options |
52 | + * | |
50 | 53 | * @var array |
51 | 54 | */ |
52 | 55 | public $formOptions = [ |
... | ... | @@ -55,6 +58,7 @@ |
55 | 58 | |
56 | 59 | /** |
57 | 60 | * Params to be passed to form |
61 | + * | |
58 | 62 | * @var array |
59 | 63 | */ |
60 | 64 | public $formParams = []; |
... | ... | @@ -62,10 +66,11 @@ |
62 | 66 | /** |
63 | 67 | * @var string the view file that will render comments list. |
64 | 68 | */ |
65 | - public $listView = '@artbox-comment/views/artbox_comment_list'; | |
69 | + public $listView = '@artbox/webcomment/views/artbox_comment_list'; | |
66 | 70 | |
67 | 71 | /** |
68 | 72 | * List options |
73 | + * | |
69 | 74 | * @var array |
70 | 75 | */ |
71 | 76 | public $listOptions = [ |
... | ... | @@ -74,12 +79,14 @@ |
74 | 79 | |
75 | 80 | /** |
76 | 81 | * List params |
82 | + * | |
77 | 83 | * @var array |
78 | 84 | */ |
79 | 85 | public $listParams = []; |
80 | 86 | |
81 | 87 | /** |
82 | 88 | * Reply options |
89 | + * | |
83 | 90 | * @var array |
84 | 91 | */ |
85 | 92 | public $replyOptions = [ |
... | ... | @@ -89,30 +96,35 @@ |
89 | 96 | |
90 | 97 | /** |
91 | 98 | * Reply view |
99 | + * | |
92 | 100 | * @var string |
93 | 101 | */ |
94 | - public $replyView = '@artbox-comment/views/artbox_comment_reply'; | |
102 | + public $replyView = '@artbox/webcomment/views/artbox_comment_reply'; | |
95 | 103 | |
96 | 104 | /** |
97 | 105 | * Comment form ID. If you have multiple forms on the same page, please use unique IDs. |
106 | + * | |
98 | 107 | * @var string Form ID |
99 | 108 | */ |
100 | 109 | public $formId = 'artbox-comment-form'; |
101 | 110 | |
102 | 111 | /** |
103 | 112 | * Comment list ID. If you have multiple forms on the same page, please use unique IDs. |
113 | + * | |
104 | 114 | * @var string List ID |
105 | 115 | */ |
106 | 116 | public $listId = 'artbox-comment-list'; |
107 | 117 | |
108 | 118 | /** |
109 | 119 | * Item view |
120 | + * | |
110 | 121 | * @var string |
111 | 122 | */ |
112 | - public $itemView = '@artbox-comment/views/artbox_comment_item'; | |
123 | + public $itemView = '@artbox/webcomment/views/artbox_comment_item'; | |
113 | 124 | |
114 | 125 | /** |
115 | 126 | * Item options |
127 | + * | |
116 | 128 | * @var array |
117 | 129 | */ |
118 | 130 | public $itemOptions = [ |
... | ... | @@ -125,24 +137,26 @@ |
125 | 137 | /** |
126 | 138 | * Entity ID attribute, default to primaryKey() if ActiveRecord and throws exception if not |
127 | 139 | * set |
140 | + * | |
128 | 141 | * @var string entity id attribute |
129 | 142 | */ |
130 | 143 | public $entityIdAttribute; |
131 | 144 | |
132 | 145 | /** |
133 | 146 | * Info to be passed to Comment Model |
147 | + * | |
134 | 148 | * @var string $info Additional info |
135 | 149 | */ |
136 | - public $info = NULL; | |
150 | + public $info = null; | |
137 | 151 | |
138 | 152 | /** |
139 | 153 | * Client options to be passed to JS |
154 | + * | |
140 | 155 | * @var array comment widget client options |
141 | 156 | */ |
142 | 157 | public $clientOptions = []; |
143 | 158 | |
144 | 159 | /** |
145 | - * @todo Check if needed | |
146 | 160 | * @var string pjax container id |
147 | 161 | */ |
148 | 162 | public $pjaxContainerId; |
... | ... | @@ -151,12 +165,14 @@ |
151 | 165 | |
152 | 166 | /** |
153 | 167 | * Model fully namespaced classname |
168 | + * | |
154 | 169 | * @var string Model namespace |
155 | 170 | */ |
156 | 171 | protected $entity; |
157 | 172 | |
158 | 173 | /** |
159 | 174 | * Entity ID for attached model |
175 | + * | |
160 | 176 | * @var integer Entity ID |
161 | 177 | */ |
162 | 178 | protected $entityId; |
... | ... | @@ -166,12 +182,14 @@ |
166 | 182 | * * Model::className() |
167 | 183 | * * entityId |
168 | 184 | * * info (optional) |
185 | + * | |
169 | 186 | * @var string encrypted entity key |
170 | 187 | */ |
171 | 188 | protected $encryptedEntityKey; |
172 | 189 | |
173 | 190 | /** |
174 | 191 | * Parts for widget |
192 | + * | |
175 | 193 | * @var array $parts |
176 | 194 | */ |
177 | 195 | protected $parts; |
... | ... | @@ -182,26 +200,28 @@ |
182 | 200 | public function init() |
183 | 201 | { |
184 | 202 | // Module init |
185 | - Yii::$app->getModule(Module::$name); | |
203 | + $module = Yii::$app->getModule(Module::$name); | |
204 | + if (!$module) { | |
205 | + Module::registerMe(); | |
206 | + } | |
186 | 207 | // Model init |
187 | 208 | $model = $this->getModel(); |
188 | - | |
189 | - /** | |
190 | - * @todo Check if needed | |
191 | - */ | |
192 | - if(empty( $this->pjaxContainerId )) { | |
209 | + | |
210 | + if (empty($this->pjaxContainerId)) { | |
193 | 211 | $this->pjaxContainerId = 'comment-pjax-container-' . $this->getId(); |
194 | 212 | } |
195 | 213 | |
196 | 214 | $this->entity = $model::className(); |
197 | 215 | // Entity ID init |
198 | - if(!empty( $this->entityIdAttribute ) && $this->model->hasProperty($this->entityIdAttribute)) { | |
216 | + if (!empty($this->entityIdAttribute) && $this->model->hasProperty($this->entityIdAttribute)) { | |
199 | 217 | $this->entityId = $this->model->{$this->entityIdAttribute}; |
200 | 218 | } else { |
201 | - if($this->model instanceof ActiveRecord && !empty( $this->model->getPrimaryKey() )) { | |
219 | + if ($this->model instanceof ActiveRecord && !empty($this->model->getPrimaryKey())) { | |
202 | 220 | $this->entityId = (int) $this->model->getPrimaryKey(); |
203 | 221 | } else { |
204 | - throw new InvalidConfigException(/*Yii::t('artbox-comment', 'The "entityIdAttribute" value for widget model cannot be empty.')*/); | |
222 | + throw new InvalidConfigException( | |
223 | + Yii::t('artbox-comment', 'The "entityIdAttribute" value for widget model cannot be empty.') | |
224 | + ); | |
205 | 225 | } |
206 | 226 | } |
207 | 227 | |
... | ... | @@ -213,27 +233,38 @@ |
213 | 233 | |
214 | 234 | /** |
215 | 235 | * Executes the widget. |
236 | + * | |
216 | 237 | * @return string the result of widget execution to be outputted. |
217 | 238 | */ |
218 | 239 | public function run() |
219 | 240 | { |
220 | 241 | /* @var Module $module */ |
221 | 242 | $module = Yii::$app->getModule(Module::$name); |
243 | + if (!$module) { | |
244 | + Module::registerMe(); | |
245 | + } | |
222 | 246 | $commentModelClass = $module->commentModelClass; |
223 | - $commentModel = $this->createModel($commentModelClass, [ | |
224 | - 'entity' => $this->entity, | |
225 | - 'entityId' => $this->entityId, | |
226 | - 'encryptedEntity' => $this->encryptedEntityKey, | |
227 | - 'scenario' => \Yii::$app->user->getIsGuest() ? $commentModelClass::SCENARIO_GUEST : $commentModelClass::SCENARIO_USER, | |
228 | - ]); | |
229 | - if($module::$enableRating) { | |
247 | + $commentModel = $this->createModel( | |
248 | + $commentModelClass, | |
249 | + [ | |
250 | + 'entity' => $this->entity, | |
251 | + 'entityId' => $this->entityId, | |
252 | + 'encryptedEntity' => $this->encryptedEntityKey, | |
253 | + 'scenario' => \Yii::$app->user->getIsGuest( | |
254 | + ) ? CommentModel::SCENARIO_GUEST : CommentModel::SCENARIO_USER, | |
255 | + ] | |
256 | + ); | |
257 | + if ($module::$enableRating) { | |
230 | 258 | $ratingModelClass = $module->ratingModelClass; |
231 | 259 | $ratingModel = $this->createRating($ratingModelClass); |
232 | 260 | } else { |
233 | - $ratingModel = NULL; | |
261 | + $ratingModel = null; | |
262 | + } | |
263 | + if (method_exists($commentModelClass, 'getTree')) { | |
264 | + $comments = $commentModelClass::getTree($this->entity, $this->entityId); | |
265 | + } else { | |
266 | + $comments = []; | |
234 | 267 | } |
235 | - | |
236 | - $comments = $commentModelClass::getTree($this->entity, $this->entityId); | |
237 | 268 | |
238 | 269 | $this->buildParts($commentModel, $comments, $ratingModel); |
239 | 270 | |
... | ... | @@ -255,16 +286,22 @@ |
255 | 286 | |
256 | 287 | /** |
257 | 288 | * Get encrypted entity key |
289 | + * | |
258 | 290 | * @return string |
259 | 291 | */ |
260 | 292 | protected function generateEntityKey() |
261 | 293 | { |
262 | 294 | return Yii::$app->getSecurity() |
263 | - ->encryptByKey(Json::encode([ | |
264 | - 'entity' => $this->entity, | |
265 | - 'entity_id' => $this->entityId, | |
266 | - 'info' => $this->info, | |
267 | - ]), Module::$encryptionKey); | |
295 | + ->encryptByKey( | |
296 | + Json::encode( | |
297 | + [ | |
298 | + 'entity' => $this->entity, | |
299 | + 'entity_id' => $this->entityId, | |
300 | + 'info' => $this->info, | |
301 | + ] | |
302 | + ), | |
303 | + Module::$encryptionKey | |
304 | + ); | |
268 | 305 | } |
269 | 306 | |
270 | 307 | /** |
... | ... | @@ -280,10 +317,12 @@ |
280 | 317 | { |
281 | 318 | $options = array_merge($config, [ 'class' => $className ]); |
282 | 319 | $object = Yii::createObject($options); |
283 | - if($object instanceof CommentInterface) { | |
320 | + if ($object instanceof CommentInterface) { | |
284 | 321 | return $object; |
285 | 322 | } |
286 | - throw new InvalidConfigException(/*Yii::t(\'artbox-comment\', \'Comment model must be instance of CommentInterface.\')*/); | |
323 | + throw new InvalidConfigException( | |
324 | + Yii::t('artbox-comment', 'Comment model must be instance of CommentInterface.') | |
325 | + ); | |
287 | 326 | } |
288 | 327 | |
289 | 328 | /** |
... | ... | @@ -299,10 +338,12 @@ |
299 | 338 | { |
300 | 339 | $options = array_merge($config, [ 'class' => $className ]); |
301 | 340 | $object = Yii::createObject($options); |
302 | - if($object instanceof RatingModel) { | |
341 | + if ($object instanceof RatingModel) { | |
303 | 342 | return $object; |
304 | 343 | } |
305 | - throw new InvalidConfigException(Yii::t('artbox-comment', 'Comment model must be instance of RatingModel.')); | |
344 | + throw new InvalidConfigException( | |
345 | + Yii::t('artbox-comment', 'Comment model must be instance of RatingModel.') | |
346 | + ); | |
306 | 347 | } |
307 | 348 | |
308 | 349 | /** |
... | ... | @@ -312,36 +353,57 @@ |
312 | 353 | * @param ActiveDataProvider $comments |
313 | 354 | * @param null|RatingModel $ratingModel |
314 | 355 | */ |
315 | - protected function buildParts(CommentInterface $commentModel, ActiveDataProvider $comments, $ratingModel = NULL) | |
356 | + protected function buildParts(CommentInterface $commentModel, ActiveDataProvider $comments, $ratingModel = null) | |
316 | 357 | { |
317 | 358 | $form_options = $this->formOptions; |
318 | - $this->parts[ 'form' ] = Html::tag(ArrayHelper::remove($form_options, 'tag', 'div'), $this->render($this->formView, [ | |
319 | - 'comment_model' => $commentModel, | |
320 | - 'form_params' => $this->formParams, | |
321 | - 'model' => $this->getModel(), | |
322 | - 'formId' => $this->formId, | |
323 | - 'rating_model' => $ratingModel, | |
324 | - ]), $form_options); | |
325 | - | |
326 | - if(!\Yii::$app->user->isGuest) { | |
359 | + $this->parts[ 'form' ] = Html::tag( | |
360 | + ArrayHelper::remove($form_options, 'tag', 'div'), | |
361 | + $this->render( | |
362 | + $this->formView, | |
363 | + [ | |
364 | + 'comment_model' => $commentModel, | |
365 | + 'form_params' => $this->formParams, | |
366 | + 'model' => $this->getModel(), | |
367 | + 'formId' => $this->formId, | |
368 | + 'rating_model' => $ratingModel, | |
369 | + ] | |
370 | + ), | |
371 | + $form_options | |
372 | + ); | |
373 | + | |
374 | + if (!\Yii::$app->user->isGuest) { | |
327 | 375 | $reply_options = $this->replyOptions; |
328 | - $this->parts[ 'reply_form' ] = Html::tag(ArrayHelper::remove($reply_options, 'tag', 'div'), $this->render($this->replyView, [ | |
329 | - 'comment_model' => $commentModel, | |
330 | - 'form_params' => $this->formParams, | |
331 | - 'model' => $this->getModel(), | |
332 | - 'formId' => $this->formId, | |
333 | - ]), $reply_options); | |
376 | + $this->parts[ 'reply_form' ] = Html::tag( | |
377 | + ArrayHelper::remove($reply_options, 'tag', 'div'), | |
378 | + $this->render( | |
379 | + $this->replyView, | |
380 | + [ | |
381 | + 'comment_model' => $commentModel, | |
382 | + 'form_params' => $this->formParams, | |
383 | + 'model' => $this->getModel(), | |
384 | + 'formId' => $this->formId, | |
385 | + ] | |
386 | + ), | |
387 | + $reply_options | |
388 | + ); | |
334 | 389 | } |
335 | 390 | |
336 | 391 | $list_options = array_merge($this->listOptions, [ 'id' => $this->listId ]); |
337 | - $this->parts[ 'list' ] = Html::tag(ArrayHelper::remove($list_options, 'tag', 'div'), $this->render($this->listView, [ | |
338 | - 'comment_model' => $commentModel, | |
339 | - 'list_params' => $this->listParams, | |
340 | - 'model' => $this->getModel(), | |
341 | - 'comments' => $comments, | |
342 | - 'item_options' => $this->itemOptions, | |
343 | - 'item_view' => $this->itemView, | |
344 | - ]), $list_options); | |
392 | + $this->parts[ 'list' ] = Html::tag( | |
393 | + ArrayHelper::remove($list_options, 'tag', 'div'), | |
394 | + $this->render( | |
395 | + $this->listView, | |
396 | + [ | |
397 | + 'comment_model' => $commentModel, | |
398 | + 'list_params' => $this->listParams, | |
399 | + 'model' => $this->getModel(), | |
400 | + 'comments' => $comments, | |
401 | + 'item_options' => $this->itemOptions, | |
402 | + 'item_view' => $this->itemView, | |
403 | + ] | |
404 | + ), | |
405 | + $list_options | |
406 | + ); | |
345 | 407 | } |
346 | 408 | |
347 | 409 | /** |
... | ... | @@ -366,9 +428,9 @@ |
366 | 428 | |
367 | 429 | public function getModel(): Model |
368 | 430 | { |
369 | - if(!empty( $this->model )) { | |
431 | + if (!empty($this->model)) { | |
370 | 432 | return $this->model; |
371 | 433 | } |
372 | - throw new InvalidConfigException(/*Yii::t(\'artbox-comment\', \'The "model" property must be set.\')*/); | |
434 | + throw new InvalidConfigException(Yii::t('artbox-comment', 'The "model" property must be set.')); | |
373 | 435 | } |
374 | 436 | } |
375 | 437 | \ No newline at end of file | ... | ... |