From 65b3647f5353ffce7b030ecebb01cc08cc66651c Mon Sep 17 00:00:00 2001 From: stes Date: Wed, 8 Nov 2017 17:14:30 +0200 Subject: [PATCH] event, blog, social --- controllers/BlogArticleController.php | 25 ++++++++++++++++++------- models/BlogArticle.php | 24 ++++++++++++++++++++++++ views/blog-article/_form.php | 4 +++- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/controllers/BlogArticleController.php b/controllers/BlogArticleController.php index 2fd9863..bfa883f 100755 --- a/controllers/BlogArticleController.php +++ b/controllers/BlogArticleController.php @@ -13,7 +13,8 @@ use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use yii\web\Response; - + use yii\web\UploadedFile; + /** * BlogArticleController implements the CRUD actions for BlogArticle model. */ @@ -116,15 +117,19 @@ } } } - - if (!empty( \Yii::$app->request->post('BlogArticle')[ 'products' ] )) { + if ( ($file = UploadedFile::getInstance($model, 'products_file')) ) { + if(!empty($file)){ + $file->saveAs(Yii::getAlias('@uploadDir/' . $file->name)); + $model->saveProducts(Yii::getAlias('@uploadDir/' . $file->name)); + } + + }elseif (!empty( \Yii::$app->request->post('BlogArticle')[ 'products' ] )) { foreach (\Yii::$app->request->post('BlogArticle')[ 'products' ] as $item) { if ($product = Product::findOne($item)) { $model->link('products', $product); } } } - if (!empty( \Yii::$app->request->post('BlogArticle')[ 'blogArticles' ] )) { foreach (\Yii::$app->request->post('BlogArticle')[ 'blogArticles' ] as $item) { if ($article = Product::findOne($item)) { @@ -223,8 +228,14 @@ } } } - - if (!empty( \Yii::$app->request->post('BlogArticle')[ 'products' ] )) { + + if ( ($file = UploadedFile::getInstance($model, 'products_file')) ) { + if(!empty($file)){ + $file->saveAs(Yii::getAlias('@uploadDir/' . $file->name)); + $model->saveProducts(Yii::getAlias('@uploadDir/' . $file->name)); + } + + }elseif (!empty( \Yii::$app->request->post('BlogArticle')[ 'products' ] )) { $model->unlinkAll('products', true); foreach (\Yii::$app->request->post('BlogArticle')[ 'products' ] as $item) { if ($product = Product::findOne($item)) { @@ -326,7 +337,7 @@ ->joinWith('lang') ->select( [ - 'id', + 'id' => 'product.id', 'product_lang.title as text', ] ) diff --git a/models/BlogArticle.php b/models/BlogArticle.php index 5c157cc..9578376 100755 --- a/models/BlogArticle.php +++ b/models/BlogArticle.php @@ -3,6 +3,7 @@ namespace artweb\artbox\blog\models; use artweb\artbox\behaviors\SaveImgBehavior; + use artweb\artbox\ecommerce\models\ProductVariant; use yii\behaviors\TimestampBehavior; use yii\db\ActiveRecord; use artweb\artbox\language\behaviors\LanguageBehavior; @@ -59,6 +60,7 @@ */ class BlogArticle extends ActiveRecord { + public $products_file; /** * @inheritdoc */ @@ -112,6 +114,7 @@ 'string', 'max' => 255, ], + ['products_file', 'file'] ]; } @@ -185,4 +188,25 @@ return $this->hasMany(BlogTag::className(), [ 'id' => 'blog_tag_id' ]) ->viaTable('blog_article_to_tag', [ 'blog_article_id' => 'id' ]); } + + public function saveProducts($file){ + set_time_limit(0); + + + $handle = fopen($file, 'r'); + + + while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { + if(isset($data[0])){ + $product = ProductVariant::find()->where(['sku' => $data[0]])->joinWith('product')->one(); + if($product instanceof ProductVariant){ + $this->link('products', $product); + } + } + + } + fclose($handle); + unlink($file); + + } } diff --git a/views/blog-article/_form.php b/views/blog-article/_form.php index 964c2f1..6b5a8c3 100755 --- a/views/blog-article/_form.php +++ b/views/blog-article/_form.php @@ -138,7 +138,9 @@ ] ); ?> - + field($model, 'products_file')->widget(\kartik\file\FileInput::className(), [ + 'language' => 'ru' + ]); ?> id )) { $data = 'function(params) { return {q:params.term}; }'; -- libgit2 0.21.4