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 | 13 | use yii\web\NotFoundHttpException; |
14 | 14 | use yii\filters\VerbFilter; |
15 | 15 | use yii\web\Response; |
16 | - | |
16 | + use yii\web\UploadedFile; | |
17 | + | |
17 | 18 | /** |
18 | 19 | * BlogArticleController implements the CRUD actions for BlogArticle model. |
19 | 20 | */ |
... | ... | @@ -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 | 127 | foreach (\Yii::$app->request->post('BlogArticle')[ 'products' ] as $item) { |
122 | 128 | if ($product = Product::findOne($item)) { |
123 | 129 | $model->link('products', $product); |
124 | 130 | } |
125 | 131 | } |
126 | 132 | } |
127 | - | |
128 | 133 | if (!empty( \Yii::$app->request->post('BlogArticle')[ 'blogArticles' ] )) { |
129 | 134 | foreach (\Yii::$app->request->post('BlogArticle')[ 'blogArticles' ] as $item) { |
130 | 135 | if ($article = Product::findOne($item)) { |
... | ... | @@ -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 | 239 | $model->unlinkAll('products', true); |
229 | 240 | foreach (\Yii::$app->request->post('BlogArticle')[ 'products' ] as $item) { |
230 | 241 | if ($product = Product::findOne($item)) { |
... | ... | @@ -326,7 +337,7 @@ |
326 | 337 | ->joinWith('lang') |
327 | 338 | ->select( |
328 | 339 | [ |
329 | - 'id', | |
340 | + 'id' => 'product.id', | |
330 | 341 | 'product_lang.title as text', |
331 | 342 | ] |
332 | 343 | ) | ... | ... |
models/BlogArticle.php
... | ... | @@ -3,6 +3,7 @@ |
3 | 3 | namespace artweb\artbox\blog\models; |
4 | 4 | |
5 | 5 | use artweb\artbox\behaviors\SaveImgBehavior; |
6 | + use artweb\artbox\ecommerce\models\ProductVariant; | |
6 | 7 | use yii\behaviors\TimestampBehavior; |
7 | 8 | use yii\db\ActiveRecord; |
8 | 9 | use artweb\artbox\language\behaviors\LanguageBehavior; |
... | ... | @@ -59,6 +60,7 @@ |
59 | 60 | */ |
60 | 61 | class BlogArticle extends ActiveRecord |
61 | 62 | { |
63 | + public $products_file; | |
62 | 64 | /** |
63 | 65 | * @inheritdoc |
64 | 66 | */ |
... | ... | @@ -112,6 +114,7 @@ |
112 | 114 | 'string', |
113 | 115 | 'max' => 255, |
114 | 116 | ], |
117 | + ['products_file', 'file'] | |
115 | 118 | ]; |
116 | 119 | } |
117 | 120 | |
... | ... | @@ -185,4 +188,25 @@ |
185 | 188 | return $this->hasMany(BlogTag::className(), [ 'id' => 'blog_tag_id' ]) |
186 | 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 | 138 | ] |
139 | 139 | ); |
140 | 140 | ?> |
141 | - | |
141 | + <?= $form->field($model, 'products_file')->widget(\kartik\file\FileInput::className(), [ | |
142 | + 'language' => 'ru' | |
143 | + ]); ?> | |
142 | 144 | <?php |
143 | 145 | if (empty( $model->id )) { |
144 | 146 | $data = 'function(params) { return {q:params.term}; }'; | ... | ... |