From b8a993f6b59c9c507839f9e208ea6333bbddf82b Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 10 Oct 2016 16:48:06 +0300 Subject: [PATCH] -Image behavior added --- backend/views/blog/_form.php | 2 +- common/behaviors/SaveImgBehavior.php | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------- common/models/Articles.php | 43 +++++++------------------------------------ common/models/Blog.php | 28 ++++++---------------------- common/modules/product/models/Import.php | 17 ++++++++--------- common/modules/product/models/Product.php | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------- 6 files changed, 166 insertions(+), 169 deletions(-) diff --git a/backend/views/blog/_form.php b/backend/views/blog/_form.php index 025198e..15a5f31 100755 --- a/backend/views/blog/_form.php +++ b/backend/views/blog/_form.php @@ -52,7 +52,7 @@ ]), ]) ?> - field($model, 'imageUpload') + field($model, 'image') ->widget(\kartik\file\FileInput::classname(), [ 'language' => 'ru', 'options' => [ diff --git a/common/behaviors/SaveImgBehavior.php b/common/behaviors/SaveImgBehavior.php index 2c16501..8b16a06 100755 --- a/common/behaviors/SaveImgBehavior.php +++ b/common/behaviors/SaveImgBehavior.php @@ -1,83 +1,77 @@ 'beforeUpdate', - ActiveRecord::EVENT_BEFORE_INSERT => 'beforeInsert', - ]; - } - - public function beforeUpdate($event) - { - - - if ( ($image = UploadedFile::getInstance($this->owner, 'image')) ) { - $this->owner->image = $image->name; - } - - if(!$this->owner->image){ - $this->owner->image = $this->owner->getOldAttribute('image'); + + public $fields; + public $is_language = false; + + public function events() + { + return [ + ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeSave', + ActiveRecord::EVENT_BEFORE_INSERT => 'beforeSave', + ]; } - - - if ($image) { - $imgDir = \Yii::getAlias('@storage/'.$this->directory.'/'); - - if(!is_dir($imgDir)) { - mkdir($imgDir, 0755, true); + + /** + * @param ModelEvent $event + */ + public function beforeSave($event) { + foreach($this->fields as $field){ + $field_name = $field['name']; + $name = $field_name; + if($this->is_language) { + $name = '['.$this->owner->language_id.']'.$name; + } + + $image = UploadedFile::getInstance($this->owner, $name); + + if(empty($image) && $event->name == ActiveRecord::EVENT_BEFORE_UPDATE) { + $this->owner->$field_name = $this->owner->getOldAttribute($field_name); + } elseif(!empty($image)) { + $imgDir = \Yii::getAlias('@storage/'.$field['directory'].'/'); + + if(!is_dir($imgDir)) { + mkdir($imgDir, 0755, true); + } + + $baseName = $image->baseName; + + $iteration = 0; + $file_name = $imgDir.$baseName.'.'.$image->extension; + while(file_exists($file_name)) { + $baseName = $image->baseName.'_'.++$iteration; + $file_name = $imgDir.$baseName.'.'.$image->extension; + } + unset($iteration); + + $this->owner->$field_name = $baseName.'.'.$image->extension; + + $image->saveAs($file_name); + } } - - $image->saveAs(\Yii::getAlias('@storage/'.$this->directory.'/' . $image->name)); } - } - - - public function beforeInsert($event) - { - - - if ( ($image = UploadedFile::getInstance($this->owner, 'image')) ) { - $this->owner->image = $image->name; + + public function getImageFile($image = 'image') { + return empty($this->owner->$image) ? null : '/storage/'.$this->fields[0]['directory'].'/'. $this->owner->$image; } - - - - if ($image) { - $imgDir = \Yii::getAlias('@storage/'.$this->directory.'/'); - - if(!is_dir($imgDir)) { - mkdir($imgDir, 0755, true); - } - - $image->saveAs(\Yii::getAlias('@storage/'.$this->directory.'/' . $image->name)); + + public function getImageUrl($image = 'image') { + return empty($this->owner->$image) ? null : '/storage/'.$this->fields[0]['directory'].'/'. $this->owner->$image; } - } - - - - public function getImageFile() { - return empty($this->owner->image) ? null : '/storage/'.$this->directory.'/'. $this->owner->image; - } - - public function getImageUrl() { - return empty($this->owner->image) ? null : '/storage/'.$this->directory.'/'. $this->owner->image; - } -} \ No newline at end of file + + + + } \ No newline at end of file diff --git a/common/models/Articles.php b/common/models/Articles.php index 6c47da1..b6649fe 100755 --- a/common/models/Articles.php +++ b/common/models/Articles.php @@ -49,6 +49,12 @@ class Articles extends \yii\db\ActiveRecord ], [ 'class' => SaveImgBehavior::className(), + 'fields' => [ + [ + 'name' => 'image', + 'directory' => 'articles', + ] + ], ], ]; } @@ -65,9 +71,7 @@ class Articles extends \yii\db\ActiveRecord [['date', 'date_end'], 'safe'], [['title', 'body'], 'required'], [['body', 'body_preview', 'seo_text'], 'string'], - [['title', 'image', 'translit', 'meta_title', 'meta_keywords', 'meta_description', 'h1'], 'string', 'max' => 255], - [['imageUpload'], 'safe'], - [['imageUpload'], 'file', 'extensions' => 'jpg, gif, png'], + [['title', 'translit', 'meta_title', 'meta_keywords', 'meta_description', 'h1'], 'string', 'max' => 255], [['date', 'date_end'], 'filter', 'filter' => function($value) { return strtotime($value)?:time(); }], @@ -97,42 +101,9 @@ class Articles extends \yii\db\ActiveRecord 'imageUpload' => 'Картинка', ]; } - - public function getImageFile() { - return empty($this->image) ? null : Yii::getAlias('@imagesDir/articles/'. $this->image); - } - - public function getImageUrl() - { - return empty($this->image) ? null : Yii::getAlias('@imagesUrl/articles/' . $this->image); - } - - public function recalculateRating() { - /** - * @var ArticleToRating $averageRating - */ - $average = $this->getComments()->joinWith('rating')->select(['average' => 'avg(artbox_comment_rating.value)::float'])->scalar(); - if(!$average) { - $average = 0; - } - $averageRating = $this->averageRating; - if(!empty($averageRating)) { - $averageRating->value = $average; - } else { - $averageRating = new ArticleToRating(['articles_id' => $this->id, 'value' => $average]); - } - if($averageRating->save()) { - return true; - } else { - return false; - } - } public function getComments() { 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]); } - public function getAverageRating() { - return $this->hasOne(ArticleToRating::className(), ['articles_id' => 'id']); - } } diff --git a/common/models/Blog.php b/common/models/Blog.php index de98c23..62ab2b1 100755 --- a/common/models/Blog.php +++ b/common/models/Blog.php @@ -30,8 +30,6 @@ { private $_categoryItems; - public $imageUpload; - /** * @inheritdoc */ @@ -54,6 +52,12 @@ ], [ 'class' => SaveImgBehavior::className(), + 'fields' => [ + [ + 'name' => 'image', + 'directory' => 'blog', + ], + ], ], ]; } @@ -96,7 +100,6 @@ [ [ 'title', - 'image', 'translit', 'meta_title', 'meta_keywords', @@ -107,15 +110,6 @@ 'max' => 255, ], [ - [ 'imageUpload' ], - 'safe', - ], - [ - [ 'imageUpload' ], - 'file', - 'extensions' => 'jpg, gif, png', - ], - [ [ 'date', 'date_end', @@ -159,16 +153,6 @@ ]; } - public function getImageFile() - { - return empty( $this->image ) ? NULL : Yii::getAlias('@imagesDir/blog/' . $this->image); - } - - public function getImageUrl() - { - return empty( $this->image ) ? NULL : Yii::getAlias('@imagesUrl/blog/' . $this->image); - } - /** * @return ActiveQuery */ diff --git a/common/modules/product/models/Import.php b/common/modules/product/models/Import.php index ab8b4b9..795c79d 100755 --- a/common/modules/product/models/Import.php +++ b/common/modules/product/models/Import.php @@ -207,12 +207,11 @@ class Import extends Model { // 2 Бренд $brand_name = $data[1]; - if (empty ($brand_name)) - { - $result_items[] = "Не указан бренд (строка $j)"; - continue; - } - +// if (empty ($brand_name)) +// { +// $result_items[] = "Не указан бренд (строка $j)"; +// continue; +// } // 3 Название товара $product_name = $data[2]; if (empty ($product_name)) @@ -284,7 +283,7 @@ class Import extends Model { $category_id[] = $category->category_id; } - $_product->_categories = $category_id; + $_product->categories = $category_id; // ===== Set brand ==== if ( $brand_name ) { @@ -366,7 +365,7 @@ class Import extends Model { if (isset($variants_options) && !empty($variants_options)) { - $_productVariant->_options = $variants_options; + $_productVariant->options = $variants_options; } $_productVariant->save(false); @@ -397,7 +396,7 @@ class Import extends Model { if (isset($options) && !empty($options)) { - $_product->_options = $options; + $_product->options = $options; } $_product->save(); diff --git a/common/modules/product/models/Product.php b/common/modules/product/models/Product.php index 2fc0c5d..ba732b6 100755 --- a/common/modules/product/models/Product.php +++ b/common/modules/product/models/Product.php @@ -368,49 +368,98 @@ ->sum('quantity'); } +// public function afterSave($insert, $changedAttributes) +// { +// parent::afterSave($insert, $changedAttributes); +// +// $this->unlinkAll('categories', true); +// $this->unlinkAll('options', true); +// +// if($this->_categories) { +// $categories = Category::findAll($this->_categories); +// foreach($categories as $category) { +// $this->link('categories', $category); +// } +// } +// $options = TaxOption::findAll($this->_options); +// +// foreach($options as $option) { +// $this->link('options', $option); +// } +// +// +// if(!empty( $this->_variants )) { +// $todel = []; +// foreach($this->variants ? : [] as $_variant) { +// $todel[ $_variant->product_variant_id ] = $_variant->product_variant_id; +// } +// foreach($this->_variants as $_variant) { +// if(!is_array($_variant)) { +// return; +// } +// if(!empty( $_variant[ 'product_variant_id' ] )) { +// unset( $todel[ $_variant[ 'product_variant_id' ] ] ); +// $model = ProductVariant::findOne($_variant[ 'product_variant_id' ]); +// } else { +// $model = new ProductVariant(); +// } +// $_variant[ 'product_id' ] = $this->product_id; +// $model->load([ 'ProductVariant' => $_variant ]); +// $model->product_id = $this->product_id; +// $model->save(); +// } +// if(!empty( $todel )) { +// ProductVariant::deleteAll([ 'product_variant_id' => $todel ]); +// } +// } +// } public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); - - $this->unlinkAll('categories', true); - $this->unlinkAll('options', true); - - $categories = Category::findAll($this->_categories); - $options = TaxOption::findAll($this->_options); - - foreach($options as $option) { - $this->link('options', $option); + + + if(!empty($this->categories)){ + $categories = Category::findAll($this->categories); + $this->unlinkAll('categories', true); + foreach($categories as $category){ + $this->link('categories', $category); + } } - foreach($categories as $category) { - $this->link('categories', $category); + + if(!empty($this->options)){ + $options = TaxOption::findAll($this->options); + $this->unlinkAll('options',true); + foreach($options as $option){ + $this->link('options', $option); + } } - - if(!empty( $this->_variants )) { + + + if (!empty($this->_variants)) { $todel = []; - foreach($this->variants ? : [] as $_variant) { - $todel[ $_variant->product_variant_id ] = $_variant->product_variant_id; + foreach ($this->variants ?: [] as $_variant) { + $todel[$_variant->product_variant_id] = $_variant->product_variant_id; } - foreach($this->_variants as $_variant) { - if(!is_array($_variant)) { + foreach ($this->_variants as $_variant) { + if (!is_array($_variant)) { return; } - if(!empty( $_variant[ 'product_variant_id' ] )) { - unset( $todel[ $_variant[ 'product_variant_id' ] ] ); - $model = ProductVariant::findOne($_variant[ 'product_variant_id' ]); + if (!empty($_variant['product_variant_id'])) { + unset($todel[$_variant['product_variant_id']]); + $model = ProductVariant::findOne($_variant['product_variant_id']); } else { $model = new ProductVariant(); } - $_variant[ 'product_id' ] = $this->product_id; - $model->load([ 'ProductVariant' => $_variant ]); + $_variant['product_id'] = $this->product_id; + $model->load(['ProductVariant' => $_variant]); $model->product_id = $this->product_id; $model->save(); } - if(!empty( $todel )) { - ProductVariant::deleteAll([ 'product_variant_id' => $todel ]); + if (!empty($todel)) { + ProductVariant::deleteAll(['product_variant_id' => $todel]); } } } - public function imagesUpload() { if($this->validate()) { -- libgit2 0.21.4