Commit b8a993f6b59c9c507839f9e208ea6333bbddf82b
1 parent
3a423558
-Image behavior added
Showing
6 changed files
with
166 additions
and
169 deletions
Show diff stats
backend/views/blog/_form.php
common/behaviors/SaveImgBehavior.php
| 1 | 1 | <?php |
| 2 | - | |
| 3 | -namespace common\behaviors; | |
| 4 | - | |
| 5 | -use common\modules\comment\models\CommentModel; | |
| 6 | -use yii\base\Behavior; | |
| 7 | -use yii\db\ActiveRecord; | |
| 8 | -use yii\web\UploadedFile; | |
| 9 | -/** | |
| 10 | - * Class RatingBehavior | |
| 11 | - * @property CommentModel $owner | |
| 12 | - * @package common\behaviors | |
| 13 | - */ | |
| 14 | -class SaveImgBehavior extends Behavior | |
| 15 | -{ | |
| 16 | - | |
| 17 | - | |
| 18 | - public $directory; | |
| 19 | - | |
| 20 | - public function events() | |
| 2 | + | |
| 3 | + namespace common\behaviors; | |
| 4 | + | |
| 5 | + use yii\base\Behavior; | |
| 6 | + use yii\base\ModelEvent; | |
| 7 | + use yii\db\ActiveRecord; | |
| 8 | + use yii\web\UploadedFile; | |
| 9 | + /** | |
| 10 | + * Class Save Image Behavior | |
| 11 | + * @property ActiveRecord $owner | |
| 12 | + * @package common\behaviors | |
| 13 | + */ | |
| 14 | + class SaveImgBehavior extends Behavior | |
| 21 | 15 | { |
| 22 | - return [ | |
| 23 | - ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeUpdate', | |
| 24 | - ActiveRecord::EVENT_BEFORE_INSERT => 'beforeInsert', | |
| 25 | - ]; | |
| 26 | - } | |
| 27 | - | |
| 28 | - public function beforeUpdate($event) | |
| 29 | - { | |
| 30 | - | |
| 31 | - | |
| 32 | - if ( ($image = UploadedFile::getInstance($this->owner, 'image')) ) { | |
| 33 | - $this->owner->image = $image->name; | |
| 34 | - } | |
| 35 | - | |
| 36 | - if(!$this->owner->image){ | |
| 37 | - $this->owner->image = $this->owner->getOldAttribute('image'); | |
| 16 | + | |
| 17 | + public $fields; | |
| 18 | + public $is_language = false; | |
| 19 | + | |
| 20 | + public function events() | |
| 21 | + { | |
| 22 | + return [ | |
| 23 | + ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeSave', | |
| 24 | + ActiveRecord::EVENT_BEFORE_INSERT => 'beforeSave', | |
| 25 | + ]; | |
| 38 | 26 | } |
| 39 | - | |
| 40 | - | |
| 41 | - if ($image) { | |
| 42 | - $imgDir = \Yii::getAlias('@storage/'.$this->directory.'/'); | |
| 43 | - | |
| 44 | - if(!is_dir($imgDir)) { | |
| 45 | - mkdir($imgDir, 0755, true); | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * @param ModelEvent $event | |
| 30 | + */ | |
| 31 | + public function beforeSave($event) { | |
| 32 | + foreach($this->fields as $field){ | |
| 33 | + $field_name = $field['name']; | |
| 34 | + $name = $field_name; | |
| 35 | + if($this->is_language) { | |
| 36 | + $name = '['.$this->owner->language_id.']'.$name; | |
| 37 | + } | |
| 38 | + | |
| 39 | + $image = UploadedFile::getInstance($this->owner, $name); | |
| 40 | + | |
| 41 | + if(empty($image) && $event->name == ActiveRecord::EVENT_BEFORE_UPDATE) { | |
| 42 | + $this->owner->$field_name = $this->owner->getOldAttribute($field_name); | |
| 43 | + } elseif(!empty($image)) { | |
| 44 | + $imgDir = \Yii::getAlias('@storage/'.$field['directory'].'/'); | |
| 45 | + | |
| 46 | + if(!is_dir($imgDir)) { | |
| 47 | + mkdir($imgDir, 0755, true); | |
| 48 | + } | |
| 49 | + | |
| 50 | + $baseName = $image->baseName; | |
| 51 | + | |
| 52 | + $iteration = 0; | |
| 53 | + $file_name = $imgDir.$baseName.'.'.$image->extension; | |
| 54 | + while(file_exists($file_name)) { | |
| 55 | + $baseName = $image->baseName.'_'.++$iteration; | |
| 56 | + $file_name = $imgDir.$baseName.'.'.$image->extension; | |
| 57 | + } | |
| 58 | + unset($iteration); | |
| 59 | + | |
| 60 | + $this->owner->$field_name = $baseName.'.'.$image->extension; | |
| 61 | + | |
| 62 | + $image->saveAs($file_name); | |
| 63 | + } | |
| 46 | 64 | } |
| 47 | - | |
| 48 | - $image->saveAs(\Yii::getAlias('@storage/'.$this->directory.'/' . $image->name)); | |
| 49 | 65 | } |
| 50 | - } | |
| 51 | - | |
| 52 | - | |
| 53 | - public function beforeInsert($event) | |
| 54 | - { | |
| 55 | - | |
| 56 | - | |
| 57 | - if ( ($image = UploadedFile::getInstance($this->owner, 'image')) ) { | |
| 58 | - $this->owner->image = $image->name; | |
| 66 | + | |
| 67 | + public function getImageFile($image = 'image') { | |
| 68 | + return empty($this->owner->$image) ? null : '/storage/'.$this->fields[0]['directory'].'/'. $this->owner->$image; | |
| 59 | 69 | } |
| 60 | - | |
| 61 | - | |
| 62 | - | |
| 63 | - if ($image) { | |
| 64 | - $imgDir = \Yii::getAlias('@storage/'.$this->directory.'/'); | |
| 65 | - | |
| 66 | - if(!is_dir($imgDir)) { | |
| 67 | - mkdir($imgDir, 0755, true); | |
| 68 | - } | |
| 69 | - | |
| 70 | - $image->saveAs(\Yii::getAlias('@storage/'.$this->directory.'/' . $image->name)); | |
| 70 | + | |
| 71 | + public function getImageUrl($image = 'image') { | |
| 72 | + return empty($this->owner->$image) ? null : '/storage/'.$this->fields[0]['directory'].'/'. $this->owner->$image; | |
| 71 | 73 | } |
| 72 | - } | |
| 73 | - | |
| 74 | - | |
| 75 | - | |
| 76 | - public function getImageFile() { | |
| 77 | - return empty($this->owner->image) ? null : '/storage/'.$this->directory.'/'. $this->owner->image; | |
| 78 | - } | |
| 79 | - | |
| 80 | - public function getImageUrl() { | |
| 81 | - return empty($this->owner->image) ? null : '/storage/'.$this->directory.'/'. $this->owner->image; | |
| 82 | - } | |
| 83 | -} | |
| 84 | 74 | \ No newline at end of file |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + } | |
| 85 | 79 | \ No newline at end of file | ... | ... |
common/models/Articles.php
| ... | ... | @@ -49,6 +49,12 @@ class Articles extends \yii\db\ActiveRecord |
| 49 | 49 | ], |
| 50 | 50 | [ |
| 51 | 51 | 'class' => SaveImgBehavior::className(), |
| 52 | + 'fields' => [ | |
| 53 | + [ | |
| 54 | + 'name' => 'image', | |
| 55 | + 'directory' => 'articles', | |
| 56 | + ] | |
| 57 | + ], | |
| 52 | 58 | ], |
| 53 | 59 | ]; |
| 54 | 60 | } |
| ... | ... | @@ -65,9 +71,7 @@ class Articles extends \yii\db\ActiveRecord |
| 65 | 71 | [['date', 'date_end'], 'safe'], |
| 66 | 72 | [['title', 'body'], 'required'], |
| 67 | 73 | [['body', 'body_preview', 'seo_text'], 'string'], |
| 68 | - [['title', 'image', 'translit', 'meta_title', 'meta_keywords', 'meta_description', 'h1'], 'string', 'max' => 255], | |
| 69 | - [['imageUpload'], 'safe'], | |
| 70 | - [['imageUpload'], 'file', 'extensions' => 'jpg, gif, png'], | |
| 74 | + [['title', 'translit', 'meta_title', 'meta_keywords', 'meta_description', 'h1'], 'string', 'max' => 255], | |
| 71 | 75 | [['date', 'date_end'], 'filter', 'filter' => function($value) { |
| 72 | 76 | return strtotime($value)?:time(); |
| 73 | 77 | }], |
| ... | ... | @@ -97,42 +101,9 @@ class Articles extends \yii\db\ActiveRecord |
| 97 | 101 | 'imageUpload' => 'ะะฐััะธะฝะบะฐ', |
| 98 | 102 | ]; |
| 99 | 103 | } |
| 100 | - | |
| 101 | - public function getImageFile() { | |
| 102 | - return empty($this->image) ? null : Yii::getAlias('@imagesDir/articles/'. $this->image); | |
| 103 | - } | |
| 104 | - | |
| 105 | - public function getImageUrl() | |
| 106 | - { | |
| 107 | - return empty($this->image) ? null : Yii::getAlias('@imagesUrl/articles/' . $this->image); | |
| 108 | - } | |
| 109 | - | |
| 110 | - public function recalculateRating() { | |
| 111 | - /** | |
| 112 | - * @var ArticleToRating $averageRating | |
| 113 | - */ | |
| 114 | - $average = $this->getComments()->joinWith('rating')->select(['average' => 'avg(artbox_comment_rating.value)::float'])->scalar(); | |
| 115 | - if(!$average) { | |
| 116 | - $average = 0; | |
| 117 | - } | |
| 118 | - $averageRating = $this->averageRating; | |
| 119 | - if(!empty($averageRating)) { | |
| 120 | - $averageRating->value = $average; | |
| 121 | - } else { | |
| 122 | - $averageRating = new ArticleToRating(['articles_id' => $this->id, 'value' => $average]); | |
| 123 | - } | |
| 124 | - if($averageRating->save()) { | |
| 125 | - return true; | |
| 126 | - } else { | |
| 127 | - return false; | |
| 128 | - } | |
| 129 | - } | |
| 130 | 104 | |
| 131 | 105 | public function getComments() { |
| 132 | 106 | return $this->hasMany(CommentModel::className(), ['entity_id' => 'id'])->where(['artbox_comment.entity' => self::className(), 'artbox_comment.status' => CommentModel::STATUS_ACTIVE, 'artbox_comment.artbox_comment_pid' => NULL]); |
| 133 | 107 | } |
| 134 | 108 | |
| 135 | - public function getAverageRating() { | |
| 136 | - return $this->hasOne(ArticleToRating::className(), ['articles_id' => 'id']); | |
| 137 | - } | |
| 138 | 109 | } | ... | ... |
common/models/Blog.php
| ... | ... | @@ -30,8 +30,6 @@ |
| 30 | 30 | { |
| 31 | 31 | private $_categoryItems; |
| 32 | 32 | |
| 33 | - public $imageUpload; | |
| 34 | - | |
| 35 | 33 | /** |
| 36 | 34 | * @inheritdoc |
| 37 | 35 | */ |
| ... | ... | @@ -54,6 +52,12 @@ |
| 54 | 52 | ], |
| 55 | 53 | [ |
| 56 | 54 | 'class' => SaveImgBehavior::className(), |
| 55 | + 'fields' => [ | |
| 56 | + [ | |
| 57 | + 'name' => 'image', | |
| 58 | + 'directory' => 'blog', | |
| 59 | + ], | |
| 60 | + ], | |
| 57 | 61 | ], |
| 58 | 62 | ]; |
| 59 | 63 | } |
| ... | ... | @@ -96,7 +100,6 @@ |
| 96 | 100 | [ |
| 97 | 101 | [ |
| 98 | 102 | 'title', |
| 99 | - 'image', | |
| 100 | 103 | 'translit', |
| 101 | 104 | 'meta_title', |
| 102 | 105 | 'meta_keywords', |
| ... | ... | @@ -107,15 +110,6 @@ |
| 107 | 110 | 'max' => 255, |
| 108 | 111 | ], |
| 109 | 112 | [ |
| 110 | - [ 'imageUpload' ], | |
| 111 | - 'safe', | |
| 112 | - ], | |
| 113 | - [ | |
| 114 | - [ 'imageUpload' ], | |
| 115 | - 'file', | |
| 116 | - 'extensions' => 'jpg, gif, png', | |
| 117 | - ], | |
| 118 | - [ | |
| 119 | 113 | [ |
| 120 | 114 | 'date', |
| 121 | 115 | 'date_end', |
| ... | ... | @@ -159,16 +153,6 @@ |
| 159 | 153 | ]; |
| 160 | 154 | } |
| 161 | 155 | |
| 162 | - public function getImageFile() | |
| 163 | - { | |
| 164 | - return empty( $this->image ) ? NULL : Yii::getAlias('@imagesDir/blog/' . $this->image); | |
| 165 | - } | |
| 166 | - | |
| 167 | - public function getImageUrl() | |
| 168 | - { | |
| 169 | - return empty( $this->image ) ? NULL : Yii::getAlias('@imagesUrl/blog/' . $this->image); | |
| 170 | - } | |
| 171 | - | |
| 172 | 156 | /** |
| 173 | 157 | * @return ActiveQuery |
| 174 | 158 | */ | ... | ... |
common/modules/product/models/Import.php
| ... | ... | @@ -207,12 +207,11 @@ class Import extends Model { |
| 207 | 207 | |
| 208 | 208 | // 2 ะัะตะฝะด |
| 209 | 209 | $brand_name = $data[1]; |
| 210 | - if (empty ($brand_name)) | |
| 211 | - { | |
| 212 | - $result_items[] = "ะะต ัะบะฐะทะฐะฝ ะฑัะตะฝะด (ัััะพะบะฐ $j)"; | |
| 213 | - continue; | |
| 214 | - } | |
| 215 | - | |
| 210 | +// if (empty ($brand_name)) | |
| 211 | +// { | |
| 212 | +// $result_items[] = "ะะต ัะบะฐะทะฐะฝ ะฑัะตะฝะด (ัััะพะบะฐ $j)"; | |
| 213 | +// continue; | |
| 214 | +// } | |
| 216 | 215 | // 3 ะะฐะทะฒะฐะฝะธะต ัะพะฒะฐัะฐ |
| 217 | 216 | $product_name = $data[2]; |
| 218 | 217 | if (empty ($product_name)) |
| ... | ... | @@ -284,7 +283,7 @@ class Import extends Model { |
| 284 | 283 | $category_id[] = $category->category_id; |
| 285 | 284 | } |
| 286 | 285 | |
| 287 | - $_product->_categories = $category_id; | |
| 286 | + $_product->categories = $category_id; | |
| 288 | 287 | |
| 289 | 288 | // ===== Set brand ==== |
| 290 | 289 | if ( $brand_name ) { |
| ... | ... | @@ -366,7 +365,7 @@ class Import extends Model { |
| 366 | 365 | |
| 367 | 366 | |
| 368 | 367 | if (isset($variants_options) && !empty($variants_options)) { |
| 369 | - $_productVariant->_options = $variants_options; | |
| 368 | + $_productVariant->options = $variants_options; | |
| 370 | 369 | } |
| 371 | 370 | |
| 372 | 371 | $_productVariant->save(false); |
| ... | ... | @@ -397,7 +396,7 @@ class Import extends Model { |
| 397 | 396 | |
| 398 | 397 | |
| 399 | 398 | if (isset($options) && !empty($options)) { |
| 400 | - $_product->_options = $options; | |
| 399 | + $_product->options = $options; | |
| 401 | 400 | } |
| 402 | 401 | |
| 403 | 402 | $_product->save(); | ... | ... |
common/modules/product/models/Product.php
| ... | ... | @@ -368,49 +368,98 @@ |
| 368 | 368 | ->sum('quantity'); |
| 369 | 369 | } |
| 370 | 370 | |
| 371 | +// public function afterSave($insert, $changedAttributes) | |
| 372 | +// { | |
| 373 | +// parent::afterSave($insert, $changedAttributes); | |
| 374 | +// | |
| 375 | +// $this->unlinkAll('categories', true); | |
| 376 | +// $this->unlinkAll('options', true); | |
| 377 | +// | |
| 378 | +// if($this->_categories) { | |
| 379 | +// $categories = Category::findAll($this->_categories); | |
| 380 | +// foreach($categories as $category) { | |
| 381 | +// $this->link('categories', $category); | |
| 382 | +// } | |
| 383 | +// } | |
| 384 | +// $options = TaxOption::findAll($this->_options); | |
| 385 | +// | |
| 386 | +// foreach($options as $option) { | |
| 387 | +// $this->link('options', $option); | |
| 388 | +// } | |
| 389 | +// | |
| 390 | +// | |
| 391 | +// if(!empty( $this->_variants )) { | |
| 392 | +// $todel = []; | |
| 393 | +// foreach($this->variants ? : [] as $_variant) { | |
| 394 | +// $todel[ $_variant->product_variant_id ] = $_variant->product_variant_id; | |
| 395 | +// } | |
| 396 | +// foreach($this->_variants as $_variant) { | |
| 397 | +// if(!is_array($_variant)) { | |
| 398 | +// return; | |
| 399 | +// } | |
| 400 | +// if(!empty( $_variant[ 'product_variant_id' ] )) { | |
| 401 | +// unset( $todel[ $_variant[ 'product_variant_id' ] ] ); | |
| 402 | +// $model = ProductVariant::findOne($_variant[ 'product_variant_id' ]); | |
| 403 | +// } else { | |
| 404 | +// $model = new ProductVariant(); | |
| 405 | +// } | |
| 406 | +// $_variant[ 'product_id' ] = $this->product_id; | |
| 407 | +// $model->load([ 'ProductVariant' => $_variant ]); | |
| 408 | +// $model->product_id = $this->product_id; | |
| 409 | +// $model->save(); | |
| 410 | +// } | |
| 411 | +// if(!empty( $todel )) { | |
| 412 | +// ProductVariant::deleteAll([ 'product_variant_id' => $todel ]); | |
| 413 | +// } | |
| 414 | +// } | |
| 415 | +// } | |
| 371 | 416 | public function afterSave($insert, $changedAttributes) |
| 372 | 417 | { |
| 373 | 418 | parent::afterSave($insert, $changedAttributes); |
| 374 | - | |
| 375 | - $this->unlinkAll('categories', true); | |
| 376 | - $this->unlinkAll('options', true); | |
| 377 | - | |
| 378 | - $categories = Category::findAll($this->_categories); | |
| 379 | - $options = TaxOption::findAll($this->_options); | |
| 380 | - | |
| 381 | - foreach($options as $option) { | |
| 382 | - $this->link('options', $option); | |
| 419 | + | |
| 420 | + | |
| 421 | + if(!empty($this->categories)){ | |
| 422 | + $categories = Category::findAll($this->categories); | |
| 423 | + $this->unlinkAll('categories', true); | |
| 424 | + foreach($categories as $category){ | |
| 425 | + $this->link('categories', $category); | |
| 426 | + } | |
| 383 | 427 | } |
| 384 | - foreach($categories as $category) { | |
| 385 | - $this->link('categories', $category); | |
| 428 | + | |
| 429 | + if(!empty($this->options)){ | |
| 430 | + $options = TaxOption::findAll($this->options); | |
| 431 | + $this->unlinkAll('options',true); | |
| 432 | + foreach($options as $option){ | |
| 433 | + $this->link('options', $option); | |
| 434 | + } | |
| 386 | 435 | } |
| 387 | - | |
| 388 | - if(!empty( $this->_variants )) { | |
| 436 | + | |
| 437 | + | |
| 438 | + if (!empty($this->_variants)) { | |
| 389 | 439 | $todel = []; |
| 390 | - foreach($this->variants ? : [] as $_variant) { | |
| 391 | - $todel[ $_variant->product_variant_id ] = $_variant->product_variant_id; | |
| 440 | + foreach ($this->variants ?: [] as $_variant) { | |
| 441 | + $todel[$_variant->product_variant_id] = $_variant->product_variant_id; | |
| 392 | 442 | } |
| 393 | - foreach($this->_variants as $_variant) { | |
| 394 | - if(!is_array($_variant)) { | |
| 443 | + foreach ($this->_variants as $_variant) { | |
| 444 | + if (!is_array($_variant)) { | |
| 395 | 445 | return; |
| 396 | 446 | } |
| 397 | - if(!empty( $_variant[ 'product_variant_id' ] )) { | |
| 398 | - unset( $todel[ $_variant[ 'product_variant_id' ] ] ); | |
| 399 | - $model = ProductVariant::findOne($_variant[ 'product_variant_id' ]); | |
| 447 | + if (!empty($_variant['product_variant_id'])) { | |
| 448 | + unset($todel[$_variant['product_variant_id']]); | |
| 449 | + $model = ProductVariant::findOne($_variant['product_variant_id']); | |
| 400 | 450 | } else { |
| 401 | 451 | $model = new ProductVariant(); |
| 402 | 452 | } |
| 403 | - $_variant[ 'product_id' ] = $this->product_id; | |
| 404 | - $model->load([ 'ProductVariant' => $_variant ]); | |
| 453 | + $_variant['product_id'] = $this->product_id; | |
| 454 | + $model->load(['ProductVariant' => $_variant]); | |
| 405 | 455 | $model->product_id = $this->product_id; |
| 406 | 456 | $model->save(); |
| 407 | 457 | } |
| 408 | - if(!empty( $todel )) { | |
| 409 | - ProductVariant::deleteAll([ 'product_variant_id' => $todel ]); | |
| 458 | + if (!empty($todel)) { | |
| 459 | + ProductVariant::deleteAll(['product_variant_id' => $todel]); | |
| 410 | 460 | } |
| 411 | 461 | } |
| 412 | 462 | } |
| 413 | - | |
| 414 | 463 | public function imagesUpload() |
| 415 | 464 | { |
| 416 | 465 | if($this->validate()) { | ... | ... |