Commit badb4dbc12794cd08543ab6cbb1021b0dca36e53
Merge remote-tracking branch 'origin/master'
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 | ||
| @@ -494,70 +516,6 @@ | @@ -494,70 +516,6 @@ | ||
| 494 | } | 516 | } |
| 495 | 517 | ||
| 496 | /** | 518 | /** |
| 497 | - * Recalculate rating for artboxcomment module | ||
| 498 | - * | ||
| 499 | - * @todo Rewrite with behavior | ||
| 500 | - * @return bool | ||
| 501 | - */ | ||
| 502 | -// public function recalculateRating():bool | ||
| 503 | -// { | ||
| 504 | -// /** | ||
| 505 | -// * @var ProductToRating $averageRating | ||
| 506 | -// */ | ||
| 507 | -// $average = $this->getComments() | ||
| 508 | -// ->joinWith('rating') | ||
| 509 | -// ->select([ 'average' => 'avg(artbox_comment_rating.value)::float' ]) | ||
| 510 | -// ->scalar(); | ||
| 511 | -// if (!$average) { | ||
| 512 | -// $average = 0; | ||
| 513 | -// } | ||
| 514 | -// $averageRating = $this->averageRating; | ||
| 515 | -// if (!empty( $averageRating )) { | ||
| 516 | -// $averageRating->value = $average; | ||
| 517 | -// } else { | ||
| 518 | -// $averageRating = new ProductToRating( | ||
| 519 | -// [ | ||
| 520 | -// 'product_id' => $this->id, | ||
| 521 | -// 'value' => $average, | ||
| 522 | -// ] | ||
| 523 | -// ); | ||
| 524 | -// } | ||
| 525 | -// if ($averageRating->save()) { | ||
| 526 | -// return true; | ||
| 527 | -// } else { | ||
| 528 | -// return false; | ||
| 529 | -// } | ||
| 530 | -// } | ||
| 531 | - | ||
| 532 | - /** | ||
| 533 | - * Get CommmentModel query for artboxcomment module | ||
| 534 | - * | ||
| 535 | - * @todo Rewrite with behavior | ||
| 536 | - * @return ActiveQuery | ||
| 537 | - */ | ||
| 538 | -// public function getComments() | ||
| 539 | -// { | ||
| 540 | -// return $this->hasMany(CommentModel::className(), [ 'entity_id' => 'id' ]) | ||
| 541 | -// ->where( | ||
| 542 | -// [ | ||
| 543 | -// 'artbox_comment.entity' => self::className(), | ||
| 544 | -// 'artbox_comment.status' => CommentModel::STATUS_ACTIVE, | ||
| 545 | -// 'artbox_comment.artbox_comment_pid' => null, | ||
| 546 | -// ] | ||
| 547 | -// ); | ||
| 548 | -// } | ||
| 549 | - | ||
| 550 | - /** | ||
| 551 | - * Get ProductToRating query in order to get average rating for current Product | ||
| 552 | - * | ||
| 553 | - * @return \yii\db\ActiveQuery | ||
| 554 | - */ | ||
| 555 | -// public function getAverageRating() | ||
| 556 | -// { | ||
| 557 | -// return $this->hasOne(ProductToRating::className(), [ 'product_id' => 'id' ]); | ||
| 558 | -// } | ||
| 559 | - | ||
| 560 | - /** | ||
| 561 | * Get TaxGroupToCategories query via product_category table | 519 | * Get TaxGroupToCategories query via product_category table |
| 562 | * | 520 | * |
| 563 | * @return ActiveQuery | 521 | * @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 | + } |