diff --git a/backend/components/parsers/CustomCsvParser.php b/backend/components/parsers/CustomCsvParser.php index ede9d60..5c45f7a 100644 --- a/backend/components/parsers/CustomCsvParser.php +++ b/backend/components/parsers/CustomCsvParser.php @@ -11,9 +11,9 @@ namespace backend\components\parsers; class CustomCsvParser extends \yii\multiparser\CsvParser { - public $last_line = 10; - public $hasHeaderRow = true; - public $keys = ['first','second', 'third', 'forth', 'fifth']; + //public $last_line = 10; + //public $hasHeaderRow = true; + // public $keys = ['first','second', 'third', 'forth', 'fifth']; protected function readRow() diff --git a/backend/components/parsers/CustomParserConfigurator.php b/backend/components/parsers/CustomParserConfigurator.php new file mode 100644 index 0000000..18b08e8 --- /dev/null +++ b/backend/components/parsers/CustomParserConfigurator.php @@ -0,0 +1,28 @@ + + ['web' => + ['class' => 'backend\components\parsers\CustomCsvParser', + 'auto_detect_first_line' => true,]]]; + + + + public static $basic_column = [ + "BRAND" => 'Бренд', + "ARTICLE"=> 'Артикул', + "PRICE" => 'Цена', + "DESCR" => 'Наименование', + "BOX" => 'Колво', + "ADD_BOX"=> 'В пути', + "GROUP" => 'Группа RG' + ]; + + +} \ No newline at end of file diff --git a/backend/components/parsers/DynamicFormHelper.php b/backend/components/parsers/DynamicFormHelper.php new file mode 100644 index 0000000..675a0fb --- /dev/null +++ b/backend/components/parsers/DynamicFormHelper.php @@ -0,0 +1,73 @@ + $i) { + $arr_keys[] = "attr_{$i}"; + $i++; + } + array_flip($arr_keys); + + } + + $model = new DynamicModel($arr_keys); + + return $model; + } + + // @todo - rewrite on two functions, add comments + public static function CreateDynamicGridViewWithDropDownListHeader( $dataProvider, $form, $header_model, $arr_header_values ) + { + $columns_config = [['class' => SerialColumn::className()]]; + $i = 0; + foreach( $header_model as $key => $value ) { + + $columns_config[] = ['header' => $form->field($header_model, $key, ['inputOptions' => ['label' => '']])->dropDownList($arr_header_values), 'attribute' => $i]; + $i++; + } + + //\common\components\CustomVarDamp::dumpAndDie($columns_config); + + $dynamic_grid_view = GridView::widget( ['dataProvider' => $dataProvider, + 'columns' => $columns_config ] ); + + return $dynamic_grid_view; + + } + + + +} \ No newline at end of file diff --git a/backend/components/widgets/InputHeaderDataColumn.php b/backend/components/widgets/InputHeaderDataColumn.php deleted file mode 100644 index d11fe00..0000000 --- a/backend/components/widgets/InputHeaderDataColumn.php +++ /dev/null @@ -1,14 +0,0 @@ -load(Yii::$app->request->post())) { $model->file = UploadedFile::getInstance($model, 'file'); if ($model->validate()) { - //CustomVarDamp::dumpAndDie(Yii::$app->request->post()); $filePath = Yii::getAlias('@webroot') . '/uploads/' . $model->file->baseName . '.' . $model->file->extension; $model->file->saveAs( $filePath ); $data = $model->readFile($filePath); - Yii::$app->getCache()->set( 'parser_data', json_encode($data), 200 ); + Yii::$app->getCache()->set( 'parser_data', json_encode($data) ); - $provider = new ArrayDataProvider([ - 'allModels' => $data, - 'pagination' => [ - 'pageSize' => 10, - ], -// 'sort' => [ -// 'attributes' => ['id', 'name'], -// ], - ]); - - return $this->render('results', - ['model' => $data, - 'imp' => $model, - 'dataProvider' => $provider]); } } else if( Yii::$app->getCache()->get( 'parser_data' )) { - $data = json_decode(Yii::$app->getCache()->get( 'parser_data' ),true); + $data = json_decode( Yii::$app->getCache()->get( 'parser_data' ),true ); - $provider = new ArrayDataProvider([ - 'allModels' => $data, - 'pagination' => [ - 'pageSize' => 10, - ], -// 'sort' => [ -// 'attributes' => ['id', 'name'], -// ], - ]); - - return $this->render('results', - ['model' => $data, - 'imp' => $model, - 'dataProvider' => $provider]); } - return $this->render('index', ['model' => $model]); + $provider = new ArrayDataProvider([ + 'allModels' => $data, + 'pagination' => [ + 'pageSize' => 10, + ], + ]); + + $header_model = DynamicFormHelper::CreateDynamicModel( count( $data[0] ) ); + + //CustomVarDamp::dumpAndDie($header_model); + return $this->render('results', + ['model' => $data, + 'header_model' => $header_model, + 'basic_column' => CustomParserConfigurator::$basic_column, + 'dataProvider' => $provider]); } + public function actionWrite() { - CustomVarDamp::dumpAndDie(Yii::$app->request->post()); + //CustomVarDamp::dumpAndDie(Yii::$app->request->post()); + + $arr_attributes = Yii::$app->request->post()['DynamicModel']; + $model = DynamicFormHelper::CreateDynamicModel( $arr_attributes ); + foreach ($arr_attributes as $key => $value) { + $model->addRule($key, 'in', ['range' => array_keys( CustomParserConfigurator::$basic_column )]); + } + + //CustomVarDamp::dumpAndDie($model); + if ($model->validate()) { + $arr = $model->toArray(); + CustomVarDamp::dumpAndDie($arr); + } + + } diff --git a/backend/models/UploadFileParsingForm.php b/backend/models/UploadFileParsingForm.php index b218571..9377a59 100644 --- a/backend/models/UploadFileParsingForm.php +++ b/backend/models/UploadFileParsingForm.php @@ -3,9 +3,8 @@ namespace backend\models; use yii\base\Model; use yii\web\UploadedFile; -use yii\multiparser\ParserHandler; use Yii; -use common\components\debug\CustomVarDamp; +use common\components\CustomVarDamp; /** * UploadForm is the model behind the upload form. @@ -21,7 +20,6 @@ class UploadFileParsingForm extends Model public $delimiter; public $delete_price; public $delete_prefix; - public $first; /** * @return array the validation rules. @@ -37,8 +35,7 @@ class UploadFileParsingForm extends Model ['importer', 'integer','max' => 999999, 'min' => 0 ], [['action','delete_prefix', 'delete_price'], 'boolean'], ['delimiter', 'string', 'max' => 1], - ['delimiter', 'default', 'value' => ';'], - ['first', 'safe'] + ['delimiter', 'default', 'value' => ';'] ]; } diff --git a/backend/views/parser/results.php b/backend/views/parser/results.php index b74dfd3..2f3c7d0 100644 --- a/backend/views/parser/results.php +++ b/backend/views/parser/results.php @@ -1,10 +1,10 @@ params['breadcrumbs'][] = $this->title; $form = ActiveForm::begin(['action' => 'write']); ?> - $dataProvider, - 'columns' => [['class' => SerialColumn::className()], - ['header' => $form->field($imp, 'first')->dropDownList(['1'=>'001', '2'=>'002']), - 'attribute' => 'first' ], - 'second', - '3', - '4', - '5',] - ]); ?> +
'btn btn-primary']) ?> diff --git a/vendor/yiisoft/multiparser/CsvParser.php b/vendor/yiisoft/multiparser/CsvParser.php index 41e6831..cde4650 100644 --- a/vendor/yiisoft/multiparser/CsvParser.php +++ b/vendor/yiisoft/multiparser/CsvParser.php @@ -5,6 +5,10 @@ namespace yii\multiparser; +/** + * Class CsvParser + * @package yii\multiparser + */ class CsvParser implements ParserInterface { @@ -42,6 +46,7 @@ class CsvParser implements ParserInterface public $min_column_quantity = 5; + /** метод устанвливает нужные настройки объекта SplFileObject, для работы с csv */ @@ -129,6 +134,7 @@ class CsvParser implements ParserInterface } + protected function closeHandler() { $this->file = NULL; diff --git a/vendor/yiisoft/multiparser/ParserConfigurator.php b/vendor/yiisoft/multiparser/ParserConfigurator.php index fef137b..14f4f5e 100644 --- a/vendor/yiisoft/multiparser/ParserConfigurator.php +++ b/vendor/yiisoft/multiparser/ParserConfigurator.php @@ -4,7 +4,7 @@ namespace yii\multiparser; class ParserConfigurator { - private static $configuration = [ + protected static $configuration = [ 'csv' => ['web' => ['class' => 'backend\components\parsers\CustomCsvParser', -- libgit2 0.21.4