diff --git a/common/modules/comment/readme b/common/modules/comment/readme new file mode 100644 index 0000000..5a40957 --- /dev/null +++ b/common/modules/comment/readme @@ -0,0 +1,64 @@ +1. Запускаем миграцию: +php yii migrate --migrationPath=common/modules/comment/migrations +2. Добавляем модуль в конфиг модуль: +'modules' => [ + ... + 'artbox-comment' => [ + 'class' => 'common\modules\comment\Module', + ], +], +3. Добавляем в конфиг переводы: +'i18n' => [ + 'translations' => [ + ... + 'artbox-comment' => [ + 'class' => 'yii\i18n\PhpMessageSource', + 'basePath' => '@common/modules/comment/messages', + ], + ], +], +4. Для управления добавляем в конфиги админки карту контроллера: +'controllerMap' => [ + ... + 'artbox-comments' => [ + 'class' => 'common\modules\comment\controllers\ManageController', + 'viewPath' => '@common/modules/comment/views/manage', + ], +], +5. В конфиге админке поменять пользователя на покупателя: +'modules' => [ + ... + 'artbox-comment' => [ + 'class' => 'common\modules\comment\Module', + 'userIdentityClass' => 'common\models\Customer', + ], +], +6. Вывод виджета: +echo CommentWidget::widget([ + 'model' => $product, +]); +7. Добавляем в нужную модель методы: +public function getComments() { + return $this->hasMany(CommentModel::className(), ['entity_id' => 'product_id'])->where(['artbox_comment.entity' => self::className(), 'artbox_comment.status' => CommentModel::STATUS_ACTIVE, 'artbox_comment.artbox_comment_pid' => NULL]); +} +/** Не обязательно для рейтинга PG ONLY **/ + public function recalculateRating() { + $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 ProductToRating(['product_id' => $this->product_id, 'value' => $average]); // Заменить модель + } + if($averageRating->save()) { + return true; + } else { + return false; + } + } + public function getAverageRating() { + return $this->hasOne(ProductToRating::className(), ['product_id' => 'product_id']); // Заменить модель + } \ No newline at end of file diff --git a/common/modules/product/controllers/ManageController.php b/common/modules/product/controllers/ManageController.php index bf5864b..558d576 100755 --- a/common/modules/product/controllers/ManageController.php +++ b/common/modules/product/controllers/ManageController.php @@ -225,13 +225,38 @@ class ManageController extends Controller return $model->goPrices($from, 10); } } - - public function actionExport() { + + public function actionExportProcess($from, $filename) + { + $model = new Export(); - if (($file = $model->process(Yii::getAlias('@uploadDir')))) { - return Yii::$app->response->sendFile($file)->send(); + if(Yii::$app->request->isAjax) { + Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; + return $model->process($filename,$from); } - throw new NotFoundHttpException('Error'); + } + + public function actionExport() + { + $model = new Export(); + + if($model->load(Yii::$app->request->post())) { + // PROCESS PAGE + return $this->render('export-process', [ + 'model' => $model, + 'method' => 'export', + ]); + } + + return $this->render('export', [ + 'model' => $model, + ]); + // $model = new Export(); + // if(( $file = $model->process(Yii::getAlias('@uploadDir')) )) { + // return Yii::$app->response->sendFile($file) + // ->send(); + // } + // throw new NotFoundHttpException('Error'); } /** diff --git a/common/modules/product/models/Export.php b/common/modules/product/models/Export.php old mode 100755 new mode 100644 index 65e7edb..3865054 --- a/common/modules/product/models/Export.php +++ b/common/modules/product/models/Export.php @@ -1,120 +1,119 @@ joinWith(['variants'])->where(['!=', ProductVariant::tableName() .'.stock', 0])->select('product.product_id')->all(); - $products = Product::find() - ->joinWith(['variantsWithFilters','brand','categories'])->with('filters')->all(); - - foreach ($products as $product) + + namespace common\modules\product\models; + + use yii\base\Model; + + class Export extends Model + { + + public $lang; + + public $file; + + public $errors = []; + + public $output = []; + + public function process($filename = NULL, $from = 0) { - - - - /*if ($i>1e2) { - break; - }*/ - $mods = []; - - $filterString = $this->convertFilterToString($product->filters); - - foreach ($product->variantsWithFilters as $variant) - { - - $color = $variant->name; - - $mods[] = $variant->sku . - '=' . $this->convertFilterToString($variant->filters) . - '=' . $color . - '=' . ((!empty($variant->image)) ? $variant->image->image: ''). - '=' . $variant->stock; + if(empty( $filename )) { + $filename = 'products_' . date('d_m_Y_H_i') . '.csv'; + $handle = fopen(\Yii::getAlias('@storage/sync/') . $filename, "w"); + } else { + $handle = fopen(\Yii::getAlias('@storage/sync/') . $filename, "a"); + } + + + $products = Product::find() + ->joinWith([ + 'variantsWithFilters', + 'brand', + 'categories', + ]) + ->with('filters') + ->limit(1000) + ->offset($from) + ->all(); + $filesize = Product::find() + ->count(); + foreach($products as $product) { + + $mods = []; + $filterString = $this->convertFilterToString($product->filters); + + foreach($product->variantsWithFilters as $variant) { + $color = $variant->name; + $mods[] = $variant->sku . '=' . $this->convertFilterToString($variant->filters) . '=' . $color . '=' . ( ( !empty( $variant->image ) ) ? $variant->image->image : '' ) . '=' . $variant->stock; + } + + $fotos = []; + + $categories = []; + foreach($product->categories as $value) { + $categories[] = $value->name; + } + + $categories = implode(',', $categories); + + $list = [ + $categories, + $product->brand->name, + $product->name, + '', + ( ( !empty( $product->description ) ) ? $product->description : '' ), + $filterString, + ( !empty( $product->variant ) ) ? $product->variant->price_old : '', + ( !empty( $product->variant ) ) ? $product->variant->price : '', + intval($product->akciya), + '', + intval($product->is_new), + intval($product->is_top), + $product->video, + implode(',', $fotos), + ]; + $to_write = array_merge($list, $mods); + fputcsv($handle, $to_write, ';'); + unset( $product ); } - - - $fotos = []; - -// foreach ($product->images as $image) -// { -// $fotos[] = $image->imageUrl; -// } - -// $filters = $product->properties; - $categories = []; - foreach($product->categories as $value){ - - $categories[] = $value->name; - + + fclose($handle); + + $from += 1000; + $end = false; + if($from > $filesize) { + $end = true; } - - - $categories = implode(',',$categories); - - $list = [ - - $categories, - $product->brand->name, - $product->name, - '', - ((! empty($product->description)) ? $product->description : ''), - $filterString, - (!empty($product->variant)) ? $product->variant->price_old : '', - (!empty($product->variant)) ? $product->variant->price : '', - intval($product->akciya), - '', - intval($product->is_new), - intval($product->is_top), - $product->video, - implode (',', $fotos), + + $result = [ + 'end' => $end, + 'from' => $from, + 'totalsize' => $filesize, + 'filename' => $filename, ]; - - $to_write = array_merge ($list, $mods); - foreach($to_write as &$cell) { - $cell = iconv("UTF-8", "WINDOWS-1251", $cell); + + if($end) { + $result = array_merge($result, [ + 'link' => '/storage/sync/'.$filename, + ]); } - - - fputcsv($handle, $to_write, ';'); - unset($product); - - } - - - - fclose ($handle); - - return $dirName .'/'. $filename; - } - - - public function convertFilterToString($filters){ - $fittersArray = []; - foreach($filters as $filter){ - $fittersArray[$filter->taxGroup->alias][] = $filter->name; + + return $result; + } - $filterString=[]; - - foreach($fittersArray as $filterName =>$filterRows ){ - $row = implode(',',$filterRows); - $filterString[] = "[{$filterName}:{$row}]"; - + + public function convertFilterToString($filters) + { + $fittersArray = []; + foreach($filters as $filter) { + $fittersArray[ $filter->taxGroup->alias ][] = $filter->value; + } + $filterString = []; + + foreach($fittersArray as $filterName => $filterRows) { + $row = implode(',', $filterRows); + $filterString[] = "[{$filterName}:{$row}]"; + } + return implode('*', $filterString); } - return implode('*',$filterString); - } -} \ No newline at end of file + } \ No newline at end of file diff --git a/common/modules/product/views/manage/export-process.php b/common/modules/product/views/manage/export-process.php new file mode 100644 index 0000000..48f6372 --- /dev/null +++ b/common/modules/product/views/manage/export-process.php @@ -0,0 +1,106 @@ + +registerJs("var in_process=true; + var count=1; + var filename = null; + + doExport(0,filename); + + function doExport(from,filename) { + from = typeof(from) != 'undefined' ? from : 0; + + $.ajax({ + method: 'get', + url: '".Yii::$app->request->baseUrl .'/product/manage/export-process'."', + data: { + from:from, + filename: filename + }, + dataType: 'json', + success: function(data){ + + var per = Math.round(100*data.from/data.totalsize)+'%'; + $('#progressbar div').css({width: per}); + + if(data != false && !data.end) + { + doExport(data.from,data.filename); + } + else + { + console.log(data.link); + $(progressbar).hide('fast'); + $('#result_link').attr('href', data.link).removeClass('hidden'); + in_process = false; + } + }, + error: function(xhr, status, errorThrown) { + } + }); + }"); +?> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+

Экспорт данных товаров

+ + [ + 'value' => 100, + 'label' => '' + ], + 'options' => [ + 'id' => 'progressbar' + ], + ]);?> + + +
diff --git a/common/modules/product/views/manage/export.php b/common/modules/product/views/manage/export.php new file mode 100644 index 0000000..d18b220 --- /dev/null +++ b/common/modules/product/views/manage/export.php @@ -0,0 +1,55 @@ + + +
+ false, + 'options' => [ 'enctype' => 'multipart/form-data' ], + ]); ?> + + errors) : ?> +
+ \n", $model->errors); ?> +
+ + + output) : ?> +

Лог операции

+
+ \n", $model->output); ?> +
+ + + field($model, 'lang') + ->hiddenInput([ + 'options' => [ + 'value' => 1, + ], + ])->label(false); ?> + + + + + field($model, 'file')->widget(\kartik\file\FileInput::classname(), [ + 'language' => 'ru', + 'options' => [ + 'multiple' => false, + ], + 'pluginOptions' => [ + 'allowedFileExtensions' => ['csv'], + 'overwriteInitial' => true, + 'showRemove' => false, + 'showUpload' => false, + ], + ])*/ ?> + +
+ 'btn btn-primary' ]) ?> +
+ + +
\ No newline at end of file -- libgit2 0.21.4