Commit 65b3647f5353ffce7b030ecebb01cc08cc66651c
1 parent
5d36359e
event, blog, social
Showing
3 changed files
with
45 additions
and
8 deletions
Show diff stats
controllers/BlogArticleController.php
| @@ -13,7 +13,8 @@ | @@ -13,7 +13,8 @@ | ||
| 13 | use yii\web\NotFoundHttpException; | 13 | use yii\web\NotFoundHttpException; |
| 14 | use yii\filters\VerbFilter; | 14 | use yii\filters\VerbFilter; |
| 15 | use yii\web\Response; | 15 | use yii\web\Response; |
| 16 | - | 16 | + use yii\web\UploadedFile; |
| 17 | + | ||
| 17 | /** | 18 | /** |
| 18 | * BlogArticleController implements the CRUD actions for BlogArticle model. | 19 | * BlogArticleController implements the CRUD actions for BlogArticle model. |
| 19 | */ | 20 | */ |
| @@ -116,15 +117,19 @@ | @@ -116,15 +117,19 @@ | ||
| 116 | } | 117 | } |
| 117 | } | 118 | } |
| 118 | } | 119 | } |
| 119 | - | ||
| 120 | - if (!empty( \Yii::$app->request->post('BlogArticle')[ 'products' ] )) { | 120 | + if ( ($file = UploadedFile::getInstance($model, 'products_file')) ) { |
| 121 | + if(!empty($file)){ | ||
| 122 | + $file->saveAs(Yii::getAlias('@uploadDir/' . $file->name)); | ||
| 123 | + $model->saveProducts(Yii::getAlias('@uploadDir/' . $file->name)); | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + }elseif (!empty( \Yii::$app->request->post('BlogArticle')[ 'products' ] )) { | ||
| 121 | foreach (\Yii::$app->request->post('BlogArticle')[ 'products' ] as $item) { | 127 | foreach (\Yii::$app->request->post('BlogArticle')[ 'products' ] as $item) { |
| 122 | if ($product = Product::findOne($item)) { | 128 | if ($product = Product::findOne($item)) { |
| 123 | $model->link('products', $product); | 129 | $model->link('products', $product); |
| 124 | } | 130 | } |
| 125 | } | 131 | } |
| 126 | } | 132 | } |
| 127 | - | ||
| 128 | if (!empty( \Yii::$app->request->post('BlogArticle')[ 'blogArticles' ] )) { | 133 | if (!empty( \Yii::$app->request->post('BlogArticle')[ 'blogArticles' ] )) { |
| 129 | foreach (\Yii::$app->request->post('BlogArticle')[ 'blogArticles' ] as $item) { | 134 | foreach (\Yii::$app->request->post('BlogArticle')[ 'blogArticles' ] as $item) { |
| 130 | if ($article = Product::findOne($item)) { | 135 | if ($article = Product::findOne($item)) { |
| @@ -223,8 +228,14 @@ | @@ -223,8 +228,14 @@ | ||
| 223 | } | 228 | } |
| 224 | } | 229 | } |
| 225 | } | 230 | } |
| 226 | - | ||
| 227 | - if (!empty( \Yii::$app->request->post('BlogArticle')[ 'products' ] )) { | 231 | + |
| 232 | + if ( ($file = UploadedFile::getInstance($model, 'products_file')) ) { | ||
| 233 | + if(!empty($file)){ | ||
| 234 | + $file->saveAs(Yii::getAlias('@uploadDir/' . $file->name)); | ||
| 235 | + $model->saveProducts(Yii::getAlias('@uploadDir/' . $file->name)); | ||
| 236 | + } | ||
| 237 | + | ||
| 238 | + }elseif (!empty( \Yii::$app->request->post('BlogArticle')[ 'products' ] )) { | ||
| 228 | $model->unlinkAll('products', true); | 239 | $model->unlinkAll('products', true); |
| 229 | foreach (\Yii::$app->request->post('BlogArticle')[ 'products' ] as $item) { | 240 | foreach (\Yii::$app->request->post('BlogArticle')[ 'products' ] as $item) { |
| 230 | if ($product = Product::findOne($item)) { | 241 | if ($product = Product::findOne($item)) { |
| @@ -326,7 +337,7 @@ | @@ -326,7 +337,7 @@ | ||
| 326 | ->joinWith('lang') | 337 | ->joinWith('lang') |
| 327 | ->select( | 338 | ->select( |
| 328 | [ | 339 | [ |
| 329 | - 'id', | 340 | + 'id' => 'product.id', |
| 330 | 'product_lang.title as text', | 341 | 'product_lang.title as text', |
| 331 | ] | 342 | ] |
| 332 | ) | 343 | ) |
models/BlogArticle.php
| @@ -3,6 +3,7 @@ | @@ -3,6 +3,7 @@ | ||
| 3 | namespace artweb\artbox\blog\models; | 3 | namespace artweb\artbox\blog\models; |
| 4 | 4 | ||
| 5 | use artweb\artbox\behaviors\SaveImgBehavior; | 5 | use artweb\artbox\behaviors\SaveImgBehavior; |
| 6 | + use artweb\artbox\ecommerce\models\ProductVariant; | ||
| 6 | use yii\behaviors\TimestampBehavior; | 7 | use yii\behaviors\TimestampBehavior; |
| 7 | use yii\db\ActiveRecord; | 8 | use yii\db\ActiveRecord; |
| 8 | use artweb\artbox\language\behaviors\LanguageBehavior; | 9 | use artweb\artbox\language\behaviors\LanguageBehavior; |
| @@ -59,6 +60,7 @@ | @@ -59,6 +60,7 @@ | ||
| 59 | */ | 60 | */ |
| 60 | class BlogArticle extends ActiveRecord | 61 | class BlogArticle extends ActiveRecord |
| 61 | { | 62 | { |
| 63 | + public $products_file; | ||
| 62 | /** | 64 | /** |
| 63 | * @inheritdoc | 65 | * @inheritdoc |
| 64 | */ | 66 | */ |
| @@ -112,6 +114,7 @@ | @@ -112,6 +114,7 @@ | ||
| 112 | 'string', | 114 | 'string', |
| 113 | 'max' => 255, | 115 | 'max' => 255, |
| 114 | ], | 116 | ], |
| 117 | + ['products_file', 'file'] | ||
| 115 | ]; | 118 | ]; |
| 116 | } | 119 | } |
| 117 | 120 | ||
| @@ -185,4 +188,25 @@ | @@ -185,4 +188,25 @@ | ||
| 185 | return $this->hasMany(BlogTag::className(), [ 'id' => 'blog_tag_id' ]) | 188 | return $this->hasMany(BlogTag::className(), [ 'id' => 'blog_tag_id' ]) |
| 186 | ->viaTable('blog_article_to_tag', [ 'blog_article_id' => 'id' ]); | 189 | ->viaTable('blog_article_to_tag', [ 'blog_article_id' => 'id' ]); |
| 187 | } | 190 | } |
| 191 | + | ||
| 192 | + public function saveProducts($file){ | ||
| 193 | + set_time_limit(0); | ||
| 194 | + | ||
| 195 | + | ||
| 196 | + $handle = fopen($file, 'r'); | ||
| 197 | + | ||
| 198 | + | ||
| 199 | + while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { | ||
| 200 | + if(isset($data[0])){ | ||
| 201 | + $product = ProductVariant::find()->where(['sku' => $data[0]])->joinWith('product')->one(); | ||
| 202 | + if($product instanceof ProductVariant){ | ||
| 203 | + $this->link('products', $product); | ||
| 204 | + } | ||
| 205 | + } | ||
| 206 | + | ||
| 207 | + } | ||
| 208 | + fclose($handle); | ||
| 209 | + unlink($file); | ||
| 210 | + | ||
| 211 | + } | ||
| 188 | } | 212 | } |
views/blog-article/_form.php
| @@ -138,7 +138,9 @@ | @@ -138,7 +138,9 @@ | ||
| 138 | ] | 138 | ] |
| 139 | ); | 139 | ); |
| 140 | ?> | 140 | ?> |
| 141 | - | 141 | + <?= $form->field($model, 'products_file')->widget(\kartik\file\FileInput::className(), [ |
| 142 | + 'language' => 'ru' | ||
| 143 | + ]); ?> | ||
| 142 | <?php | 144 | <?php |
| 143 | if (empty( $model->id )) { | 145 | if (empty( $model->id )) { |
| 144 | $data = 'function(params) { return {q:params.term}; }'; | 146 | $data = 'function(params) { return {q:params.term}; }'; |