Commit b40c1cce3909c794ac79d6019b9a5ba375939fc3
1 parent
2e22f66f
CommentBehavior
Showing
2 changed files
with
128 additions
and
68 deletions
Show diff stats
models/Product.php
@@ -2,10 +2,11 @@ | @@ -2,10 +2,11 @@ | ||
2 | 2 | ||
3 | namespace artweb\artbox\ecommerce\models; | 3 | namespace artweb\artbox\ecommerce\models; |
4 | 4 | ||
5 | + use artweb\artbox\comment\models\CommentModel; | ||
6 | + use artweb\artbox\comment\models\interfaces\RatingCacheInterface; | ||
5 | use artweb\artbox\ecommerce\behaviors\DefaultVariantBehavior; | 7 | use artweb\artbox\ecommerce\behaviors\DefaultVariantBehavior; |
6 | use artweb\artbox\behaviors\MultipleImgBehavior; | 8 | use artweb\artbox\behaviors\MultipleImgBehavior; |
7 | use artweb\artbox\behaviors\SaveMultipleFileBehavior; | 9 | use artweb\artbox\behaviors\SaveMultipleFileBehavior; |
8 | -// use artweb\artbox\comment\models\CommentModel; | ||
9 | use artweb\artbox\language\behaviors\LanguageBehavior; | 10 | use artweb\artbox\language\behaviors\LanguageBehavior; |
10 | use Yii; | 11 | use Yii; |
11 | use yii\base\InvalidParamException; | 12 | use yii\base\InvalidParamException; |
@@ -76,6 +77,22 @@ | @@ -76,6 +77,22 @@ | ||
76 | * @method array getImagesConfig() | 77 | * @method array getImagesConfig() |
77 | * @method array getImagesHTML( string $preset ) | 78 | * @method array getImagesHTML( string $preset ) |
78 | * * End multipleImage behavior | 79 | * * End multipleImage behavior |
80 | + * * From CommentBehavior ** | ||
81 | + * @property string $cacheModelName | ||
82 | + * @property RatingCacheInterface $cacheModel | ||
83 | + * @property boolean $cacheRating | ||
84 | + * @property CommentModel[] $comments | ||
85 | + * @property RatingCacheInterface $averageRating | ||
86 | + * @method string getCacheModelName() | ||
87 | + * @method void setCacheModelName( string $value ) | ||
88 | + * @method RatingCacheInterface getCacheModel() | ||
89 | + * @method void setCacheModel( RatingCacheInterface $value ) | ||
90 | + * @method boolean getCacheRating() | ||
91 | + * @method void setCacheRating( bool $value ) | ||
92 | + * @method ActiveQuery getComments() | ||
93 | + * @method ActiveQuery getAverageRating() | ||
94 | + * @method bool recalculateRating() | ||
95 | + * * End CommentBehavior ** | ||
79 | */ | 96 | */ |
80 | class Product extends ActiveRecord | 97 | class Product extends ActiveRecord |
81 | { | 98 | { |
@@ -88,7 +105,7 @@ | @@ -88,7 +105,7 @@ | ||
88 | public function behaviors() | 105 | public function behaviors() |
89 | { | 106 | { |
90 | return [ | 107 | return [ |
91 | - 'images' => [ | 108 | + 'images' => [ |
92 | 'class' => SaveMultipleFileBehavior::className(), | 109 | 'class' => SaveMultipleFileBehavior::className(), |
93 | 'name' => 'imagesUpload', | 110 | 'name' => 'imagesUpload', |
94 | 'directory' => 'products', | 111 | 'directory' => 'products', |
@@ -98,7 +115,7 @@ | @@ -98,7 +115,7 @@ | ||
98 | ], | 115 | ], |
99 | 'model' => ProductImage::className(), | 116 | 'model' => ProductImage::className(), |
100 | ], | 117 | ], |
101 | - 'multipleImage' => [ | 118 | + 'multipleImage' => [ |
102 | 'class' => MultipleImgBehavior::className(), | 119 | 'class' => MultipleImgBehavior::className(), |
103 | 'links' => [ | 120 | 'links' => [ |
104 | 'product_id' => 'id', | 121 | 'product_id' => 'id', |
@@ -113,10 +130,15 @@ | @@ -113,10 +130,15 @@ | ||
113 | 'id' => 'id', | 130 | 'id' => 'id', |
114 | ], | 131 | ], |
115 | ], | 132 | ], |
116 | - 'language' => [ | 133 | + 'language' => [ |
117 | 'class' => LanguageBehavior::className(), | 134 | 'class' => LanguageBehavior::className(), |
118 | ], | 135 | ], |
119 | 'defaultVariant' => DefaultVariantBehavior::className(), | 136 | 'defaultVariant' => DefaultVariantBehavior::className(), |
137 | + 'comment' => [ | ||
138 | + 'class' => 'artweb\artbox\comment\behaviors\CommentBehavior', | ||
139 | + 'cacheRating' => true, | ||
140 | + 'cacheModelName' => ProductToRating::className(), | ||
141 | + ], | ||
120 | ]; | 142 | ]; |
121 | } | 143 | } |
122 | 144 | ||
@@ -490,70 +512,6 @@ | @@ -490,70 +512,6 @@ | ||
490 | } | 512 | } |
491 | 513 | ||
492 | /** | 514 | /** |
493 | - * Recalculate rating for artboxcomment module | ||
494 | - * | ||
495 | - * @todo Rewrite with behavior | ||
496 | - * @return bool | ||
497 | - */ | ||
498 | -// public function recalculateRating():bool | ||
499 | -// { | ||
500 | -// /** | ||
501 | -// * @var ProductToRating $averageRating | ||
502 | -// */ | ||
503 | -// $average = $this->getComments() | ||
504 | -// ->joinWith('rating') | ||
505 | -// ->select([ 'average' => 'avg(artbox_comment_rating.value)::float' ]) | ||
506 | -// ->scalar(); | ||
507 | -// if (!$average) { | ||
508 | -// $average = 0; | ||
509 | -// } | ||
510 | -// $averageRating = $this->averageRating; | ||
511 | -// if (!empty( $averageRating )) { | ||
512 | -// $averageRating->value = $average; | ||
513 | -// } else { | ||
514 | -// $averageRating = new ProductToRating( | ||
515 | -// [ | ||
516 | -// 'product_id' => $this->id, | ||
517 | -// 'value' => $average, | ||
518 | -// ] | ||
519 | -// ); | ||
520 | -// } | ||
521 | -// if ($averageRating->save()) { | ||
522 | -// return true; | ||
523 | -// } else { | ||
524 | -// return false; | ||
525 | -// } | ||
526 | -// } | ||
527 | - | ||
528 | - /** | ||
529 | - * Get CommmentModel query for artboxcomment module | ||
530 | - * | ||
531 | - * @todo Rewrite with behavior | ||
532 | - * @return ActiveQuery | ||
533 | - */ | ||
534 | -// public function getComments() | ||
535 | -// { | ||
536 | -// return $this->hasMany(CommentModel::className(), [ 'entity_id' => 'id' ]) | ||
537 | -// ->where( | ||
538 | -// [ | ||
539 | -// 'artbox_comment.entity' => self::className(), | ||
540 | -// 'artbox_comment.status' => CommentModel::STATUS_ACTIVE, | ||
541 | -// 'artbox_comment.artbox_comment_pid' => null, | ||
542 | -// ] | ||
543 | -// ); | ||
544 | -// } | ||
545 | - | ||
546 | - /** | ||
547 | - * Get ProductToRating query in order to get average rating for current Product | ||
548 | - * | ||
549 | - * @return \yii\db\ActiveQuery | ||
550 | - */ | ||
551 | -// public function getAverageRating() | ||
552 | -// { | ||
553 | -// return $this->hasOne(ProductToRating::className(), [ 'product_id' => 'id' ]); | ||
554 | -// } | ||
555 | - | ||
556 | - /** | ||
557 | * Get TaxGroupToCategories query via product_category table | 515 | * Get TaxGroupToCategories query via product_category table |
558 | * | 516 | * |
559 | * @return ActiveQuery | 517 | * @return ActiveQuery |
1 | +<?php | ||
2 | + | ||
3 | + namespace artweb\artbox\ecommerce\models; | ||
4 | + | ||
5 | + use artweb\artbox\comment\models\interfaces\RatingCacheInterface; | ||
6 | + use Yii; | ||
7 | + use yii\db\ActiveRecord; | ||
8 | + | ||
9 | + /** | ||
10 | + * This is the model class for table "product_to_rating". | ||
11 | + * | ||
12 | + * @property integer $id | ||
13 | + * @property integer $product_id | ||
14 | + * @property double $value | ||
15 | + * @property Product $product | ||
16 | + */ | ||
17 | + class ProductToRating extends ActiveRecord implements RatingCacheInterface | ||
18 | + { | ||
19 | + /** | ||
20 | + * @inheritdoc | ||
21 | + */ | ||
22 | + public static function tableName() | ||
23 | + { | ||
24 | + return 'product_to_rating'; | ||
25 | + } | ||
26 | + | ||
27 | + /** | ||
28 | + * @inheritdoc | ||
29 | + */ | ||
30 | + public function rules() | ||
31 | + { | ||
32 | + return [ | ||
33 | + [ | ||
34 | + [ 'product_id' ], | ||
35 | + 'required', | ||
36 | + ], | ||
37 | + [ | ||
38 | + [ 'product_id' ], | ||
39 | + 'integer', | ||
40 | + ], | ||
41 | + [ | ||
42 | + [ 'value' ], | ||
43 | + 'number', | ||
44 | + ], | ||
45 | + [ | ||
46 | + [ 'product_id' ], | ||
47 | + 'unique', | ||
48 | + ], | ||
49 | + [ | ||
50 | + [ 'product_id' ], | ||
51 | + 'exist', | ||
52 | + 'skipOnError' => true, | ||
53 | + 'targetClass' => Product::className(), | ||
54 | + 'targetAttribute' => [ 'product_id' => 'id' ], | ||
55 | + ], | ||
56 | + ]; | ||
57 | + } | ||
58 | + | ||
59 | + /** | ||
60 | + * @inheritdoc | ||
61 | + */ | ||
62 | + public function attributeLabels() | ||
63 | + { | ||
64 | + return [ | ||
65 | + 'id' => Yii::t('app', 'ID'), | ||
66 | + 'product_id' => Yii::t('app', 'Product ID'), | ||
67 | + 'value' => Yii::t('app', 'Value'), | ||
68 | + ]; | ||
69 | + } | ||
70 | + | ||
71 | + /** | ||
72 | + * @return \yii\db\ActiveQuery | ||
73 | + */ | ||
74 | + public function getProduct() | ||
75 | + { | ||
76 | + return $this->hasOne(Product::className(), [ 'id' => 'product_id' ]); | ||
77 | + } | ||
78 | + | ||
79 | + /** | ||
80 | + * @inheritdoc | ||
81 | + */ | ||
82 | + public function getLinkAttribute(): string | ||
83 | + { | ||
84 | + return 'product_id'; | ||
85 | + } | ||
86 | + | ||
87 | + /** | ||
88 | + * @inheritdoc | ||
89 | + */ | ||
90 | + public function getValue(): float | ||
91 | + { | ||
92 | + return $this->value; | ||
93 | + } | ||
94 | + | ||
95 | + /** | ||
96 | + * @inheritdoc | ||
97 | + */ | ||
98 | + public function setValue(float $value) | ||
99 | + { | ||
100 | + $this->value = $value; | ||
101 | + } | ||
102 | + } |