Commit 474f35bfc3b965c60c2a0405554cc63b5b4895d8
1 parent
e774f057
add DynamicFormHelper, and finish with result form (dynamic)
Showing
9 changed files
with
152 additions
and
69 deletions
Show diff stats
backend/components/parsers/CustomCsvParser.php
| @@ -11,9 +11,9 @@ namespace backend\components\parsers; | @@ -11,9 +11,9 @@ namespace backend\components\parsers; | ||
| 11 | 11 | ||
| 12 | class CustomCsvParser extends \yii\multiparser\CsvParser { | 12 | class CustomCsvParser extends \yii\multiparser\CsvParser { |
| 13 | 13 | ||
| 14 | - public $last_line = 10; | ||
| 15 | - public $hasHeaderRow = true; | ||
| 16 | - public $keys = ['first','second', 'third', 'forth', 'fifth']; | 14 | + //public $last_line = 10; |
| 15 | + //public $hasHeaderRow = true; | ||
| 16 | + // public $keys = ['first','second', 'third', 'forth', 'fifth']; | ||
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | protected function readRow() | 19 | protected function readRow() |
backend/components/parsers/CustomParserConfigurator.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace backend\components\parsers; | ||
| 4 | +use yii\multiparser\ParserConfigurator; | ||
| 5 | + | ||
| 6 | +class CustomParserConfigurator extends ParserConfigurator | ||
| 7 | +{ | ||
| 8 | + | ||
| 9 | + protected static $configuration = [ | ||
| 10 | + 'csv' => | ||
| 11 | + ['web' => | ||
| 12 | + ['class' => 'backend\components\parsers\CustomCsvParser', | ||
| 13 | + 'auto_detect_first_line' => true,]]]; | ||
| 14 | + | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + public static $basic_column = [ | ||
| 18 | + "BRAND" => 'Бренд', | ||
| 19 | + "ARTICLE"=> 'Артикул', | ||
| 20 | + "PRICE" => 'Цена', | ||
| 21 | + "DESCR" => 'Наименование', | ||
| 22 | + "BOX" => 'Колво', | ||
| 23 | + "ADD_BOX"=> 'В пути', | ||
| 24 | + "GROUP" => 'Группа RG' | ||
| 25 | + ]; | ||
| 26 | + | ||
| 27 | + | ||
| 28 | +} | ||
| 0 | \ No newline at end of file | 29 | \ No newline at end of file |
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * Created by PhpStorm. | ||
| 4 | + * User: Cibermag | ||
| 5 | + * Date: 08.09.2015 | ||
| 6 | + * Time: 14:50 | ||
| 7 | + */ | ||
| 8 | + | ||
| 9 | +namespace backend\components\parsers; | ||
| 10 | + | ||
| 11 | +use yii\base\DynamicModel; | ||
| 12 | +use yii\grid\GridView; | ||
| 13 | +use yii\grid\SerialColumn; | ||
| 14 | + | ||
| 15 | +/** | ||
| 16 | + * Class DynamicFormHelper | ||
| 17 | + * @package backend\components\parsers | ||
| 18 | + * Содержит процедуры генерации компонентов с динамическим количеством аттрибутов | ||
| 19 | + */ | ||
| 20 | +class DynamicFormHelper | ||
| 21 | +{ | ||
| 22 | + | ||
| 23 | + | ||
| 24 | + /** | ||
| 25 | + * @param $source - int or array | ||
| 26 | + * если передан массив, то создается модель с атрибутами переданными в массиве, | ||
| 27 | + * ключ - имя, значение - значение аттрибута | ||
| 28 | + * если передано число, то создается переданное количество аттрибутов с именами - attr_0, attr_1... | ||
| 29 | + */ | ||
| 30 | + public static function CreateDynamicModel($source) | ||
| 31 | + { | ||
| 32 | + $arr_keys = []; | ||
| 33 | + if (is_array($source)) { | ||
| 34 | + $arr_keys = $source; | ||
| 35 | + } elseif (is_int($source)) { | ||
| 36 | + | ||
| 37 | + $i = 0; | ||
| 38 | + while ($source > $i) { | ||
| 39 | + $arr_keys[] = "attr_{$i}"; | ||
| 40 | + $i++; | ||
| 41 | + } | ||
| 42 | + array_flip($arr_keys); | ||
| 43 | + | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + $model = new DynamicModel($arr_keys); | ||
| 47 | + | ||
| 48 | + return $model; | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + // @todo - rewrite on two functions, add comments | ||
| 52 | + public static function CreateDynamicGridViewWithDropDownListHeader( $dataProvider, $form, $header_model, $arr_header_values ) | ||
| 53 | + { | ||
| 54 | + $columns_config = [['class' => SerialColumn::className()]]; | ||
| 55 | + $i = 0; | ||
| 56 | + foreach( $header_model as $key => $value ) { | ||
| 57 | + | ||
| 58 | + $columns_config[] = ['header' => $form->field($header_model, $key, ['inputOptions' => ['label' => '']])->dropDownList($arr_header_values), 'attribute' => $i]; | ||
| 59 | + $i++; | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + //\common\components\CustomVarDamp::dumpAndDie($columns_config); | ||
| 63 | + | ||
| 64 | + $dynamic_grid_view = GridView::widget( ['dataProvider' => $dataProvider, | ||
| 65 | + 'columns' => $columns_config ] ); | ||
| 66 | + | ||
| 67 | + return $dynamic_grid_view; | ||
| 68 | + | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + | ||
| 72 | + | ||
| 73 | +} | ||
| 0 | \ No newline at end of file | 74 | \ No newline at end of file |
backend/components/widgets/InputHeaderDataColumn.php deleted
| 1 | -<?php | ||
| 2 | -/** | ||
| 3 | - * Created by PhpStorm. | ||
| 4 | - * User: Cibermag | ||
| 5 | - * Date: 07.09.2015 | ||
| 6 | - * Time: 18:04 | ||
| 7 | - */ | ||
| 8 | -use yii\grid\DataColumn ; | ||
| 9 | -class InputHeaderDataColumn extends DataColumn { | ||
| 10 | - | ||
| 11 | - protected function renderHeaderCellContent(){ | ||
| 12 | - | ||
| 13 | - } | ||
| 14 | -} | ||
| 15 | \ No newline at end of file | 0 | \ No newline at end of file |
backend/controllers/ParserController.php
| @@ -8,6 +8,8 @@ use yii\filters\VerbFilter; | @@ -8,6 +8,8 @@ use yii\filters\VerbFilter; | ||
| 8 | use backend\models\UploadFileParsingForm; | 8 | use backend\models\UploadFileParsingForm; |
| 9 | use yii\web\UploadedFile; | 9 | use yii\web\UploadedFile; |
| 10 | use yii\data\ArrayDataProvider; | 10 | use yii\data\ArrayDataProvider; |
| 11 | +use backend\components\parsers\DynamicFormHelper; | ||
| 12 | +use backend\components\parsers\CustomParserConfigurator; | ||
| 11 | 13 | ||
| 12 | use common\components\CustomVarDamp; | 14 | use common\components\CustomVarDamp; |
| 13 | 15 | ||
| @@ -67,60 +69,60 @@ class ParserController extends BaseController | @@ -67,60 +69,60 @@ class ParserController extends BaseController | ||
| 67 | public function actionResults(){ | 69 | public function actionResults(){ |
| 68 | 70 | ||
| 69 | $model = new UploadFileParsingForm(); | 71 | $model = new UploadFileParsingForm(); |
| 70 | - | 72 | + $data = []; |
| 71 | if ($model->load(Yii::$app->request->post())) { | 73 | if ($model->load(Yii::$app->request->post())) { |
| 72 | $model->file = UploadedFile::getInstance($model, 'file'); | 74 | $model->file = UploadedFile::getInstance($model, 'file'); |
| 73 | 75 | ||
| 74 | if ($model->validate()) { | 76 | if ($model->validate()) { |
| 75 | - //CustomVarDamp::dumpAndDie(Yii::$app->request->post()); | ||
| 76 | $filePath = Yii::getAlias('@webroot') . '/uploads/' . $model->file->baseName . '.' . $model->file->extension; | 77 | $filePath = Yii::getAlias('@webroot') . '/uploads/' . $model->file->baseName . '.' . $model->file->extension; |
| 77 | 78 | ||
| 78 | $model->file->saveAs( $filePath ); | 79 | $model->file->saveAs( $filePath ); |
| 79 | $data = $model->readFile($filePath); | 80 | $data = $model->readFile($filePath); |
| 80 | 81 | ||
| 81 | - Yii::$app->getCache()->set( 'parser_data', json_encode($data), 200 ); | 82 | + Yii::$app->getCache()->set( 'parser_data', json_encode($data) ); |
| 82 | 83 | ||
| 83 | - $provider = new ArrayDataProvider([ | ||
| 84 | - 'allModels' => $data, | ||
| 85 | - 'pagination' => [ | ||
| 86 | - 'pageSize' => 10, | ||
| 87 | - ], | ||
| 88 | -// 'sort' => [ | ||
| 89 | -// 'attributes' => ['id', 'name'], | ||
| 90 | -// ], | ||
| 91 | - ]); | ||
| 92 | - | ||
| 93 | - return $this->render('results', | ||
| 94 | - ['model' => $data, | ||
| 95 | - 'imp' => $model, | ||
| 96 | - 'dataProvider' => $provider]); | ||
| 97 | } | 84 | } |
| 98 | 85 | ||
| 99 | } else if( Yii::$app->getCache()->get( 'parser_data' )) { | 86 | } else if( Yii::$app->getCache()->get( 'parser_data' )) { |
| 100 | 87 | ||
| 101 | - $data = json_decode(Yii::$app->getCache()->get( 'parser_data' ),true); | 88 | + $data = json_decode( Yii::$app->getCache()->get( 'parser_data' ),true ); |
| 102 | 89 | ||
| 103 | - $provider = new ArrayDataProvider([ | ||
| 104 | - 'allModels' => $data, | ||
| 105 | - 'pagination' => [ | ||
| 106 | - 'pageSize' => 10, | ||
| 107 | - ], | ||
| 108 | -// 'sort' => [ | ||
| 109 | -// 'attributes' => ['id', 'name'], | ||
| 110 | -// ], | ||
| 111 | - ]); | ||
| 112 | - | ||
| 113 | - return $this->render('results', | ||
| 114 | - ['model' => $data, | ||
| 115 | - 'imp' => $model, | ||
| 116 | - 'dataProvider' => $provider]); | ||
| 117 | } | 90 | } |
| 118 | 91 | ||
| 119 | - return $this->render('index', ['model' => $model]); | 92 | + $provider = new ArrayDataProvider([ |
| 93 | + 'allModels' => $data, | ||
| 94 | + 'pagination' => [ | ||
| 95 | + 'pageSize' => 10, | ||
| 96 | + ], | ||
| 97 | + ]); | ||
| 98 | + | ||
| 99 | + $header_model = DynamicFormHelper::CreateDynamicModel( count( $data[0] ) ); | ||
| 100 | + | ||
| 101 | + //CustomVarDamp::dumpAndDie($header_model); | ||
| 102 | + return $this->render('results', | ||
| 103 | + ['model' => $data, | ||
| 104 | + 'header_model' => $header_model, | ||
| 105 | + 'basic_column' => CustomParserConfigurator::$basic_column, | ||
| 106 | + 'dataProvider' => $provider]); | ||
| 120 | } | 107 | } |
| 108 | + | ||
| 121 | public function actionWrite() | 109 | public function actionWrite() |
| 122 | { | 110 | { |
| 123 | - CustomVarDamp::dumpAndDie(Yii::$app->request->post()); | 111 | + //CustomVarDamp::dumpAndDie(Yii::$app->request->post()); |
| 112 | + | ||
| 113 | + $arr_attributes = Yii::$app->request->post()['DynamicModel']; | ||
| 114 | + $model = DynamicFormHelper::CreateDynamicModel( $arr_attributes ); | ||
| 115 | + foreach ($arr_attributes as $key => $value) { | ||
| 116 | + $model->addRule($key, 'in', ['range' => array_keys( CustomParserConfigurator::$basic_column )]); | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + //CustomVarDamp::dumpAndDie($model); | ||
| 120 | + if ($model->validate()) { | ||
| 121 | + $arr = $model->toArray(); | ||
| 122 | + CustomVarDamp::dumpAndDie($arr); | ||
| 123 | + } | ||
| 124 | + | ||
| 125 | + | ||
| 124 | 126 | ||
| 125 | } | 127 | } |
| 126 | 128 |
backend/models/UploadFileParsingForm.php
| @@ -3,9 +3,8 @@ namespace backend\models; | @@ -3,9 +3,8 @@ namespace backend\models; | ||
| 3 | 3 | ||
| 4 | use yii\base\Model; | 4 | use yii\base\Model; |
| 5 | use yii\web\UploadedFile; | 5 | use yii\web\UploadedFile; |
| 6 | -use yii\multiparser\ParserHandler; | ||
| 7 | use Yii; | 6 | use Yii; |
| 8 | -use common\components\debug\CustomVarDamp; | 7 | +use common\components\CustomVarDamp; |
| 9 | 8 | ||
| 10 | /** | 9 | /** |
| 11 | * UploadForm is the model behind the upload form. | 10 | * UploadForm is the model behind the upload form. |
| @@ -21,7 +20,6 @@ class UploadFileParsingForm extends Model | @@ -21,7 +20,6 @@ class UploadFileParsingForm extends Model | ||
| 21 | public $delimiter; | 20 | public $delimiter; |
| 22 | public $delete_price; | 21 | public $delete_price; |
| 23 | public $delete_prefix; | 22 | public $delete_prefix; |
| 24 | - public $first; | ||
| 25 | 23 | ||
| 26 | /** | 24 | /** |
| 27 | * @return array the validation rules. | 25 | * @return array the validation rules. |
| @@ -37,8 +35,7 @@ class UploadFileParsingForm extends Model | @@ -37,8 +35,7 @@ class UploadFileParsingForm extends Model | ||
| 37 | ['importer', 'integer','max' => 999999, 'min' => 0 ], | 35 | ['importer', 'integer','max' => 999999, 'min' => 0 ], |
| 38 | [['action','delete_prefix', 'delete_price'], 'boolean'], | 36 | [['action','delete_prefix', 'delete_price'], 'boolean'], |
| 39 | ['delimiter', 'string', 'max' => 1], | 37 | ['delimiter', 'string', 'max' => 1], |
| 40 | - ['delimiter', 'default', 'value' => ';'], | ||
| 41 | - ['first', 'safe'] | 38 | + ['delimiter', 'default', 'value' => ';'] |
| 42 | 39 | ||
| 43 | ]; | 40 | ]; |
| 44 | } | 41 | } |
backend/views/parser/results.php
| 1 | <?php | 1 | <?php |
| 2 | 2 | ||
| 3 | use yii\helpers\Html; | 3 | use yii\helpers\Html; |
| 4 | -use yii\grid\GridView; | ||
| 5 | -use yii\grid\SerialColumn; | 4 | +use backend\components\parsers\DynamicFormHelper; |
| 6 | use yii\widgets\ActiveForm; | 5 | use yii\widgets\ActiveForm; |
| 7 | 6 | ||
| 7 | + | ||
| 8 | /* @var $this yii\web\View */ | 8 | /* @var $this yii\web\View */ |
| 9 | /* @var $searchModel backend\models\CatalogSearch */ | 9 | /* @var $searchModel backend\models\CatalogSearch */ |
| 10 | /* @var $dataProvider yii\data\ActiveDataProvider */ | 10 | /* @var $dataProvider yii\data\ActiveDataProvider */ |
| @@ -20,16 +20,7 @@ $this->params['breadcrumbs'][] = $this->title; | @@ -20,16 +20,7 @@ $this->params['breadcrumbs'][] = $this->title; | ||
| 20 | 20 | ||
| 21 | $form = ActiveForm::begin(['action' => 'write']); | 21 | $form = ActiveForm::begin(['action' => 'write']); |
| 22 | ?> | 22 | ?> |
| 23 | - <?= GridView::widget([ | ||
| 24 | - 'dataProvider' => $dataProvider, | ||
| 25 | - 'columns' => [['class' => SerialColumn::className()], | ||
| 26 | - ['header' => $form->field($imp, 'first')->dropDownList(['1'=>'001', '2'=>'002']), | ||
| 27 | - 'attribute' => 'first' ], | ||
| 28 | - 'second', | ||
| 29 | - '3', | ||
| 30 | - '4', | ||
| 31 | - '5',] | ||
| 32 | - ]); ?> | 23 | + <?= DynamicFormHelper::CreateDynamicGridViewWithDropDownListHeader( $dataProvider, $form, $header_model, $basic_column )?> |
| 33 | 24 | ||
| 34 | <div class="form-group"> | 25 | <div class="form-group"> |
| 35 | <?= Html::submitButton(Yii::t('app', 'Записать в БД'), ['class' => 'btn btn-primary']) ?> | 26 | <?= Html::submitButton(Yii::t('app', 'Записать в БД'), ['class' => 'btn btn-primary']) ?> |
vendor/yiisoft/multiparser/CsvParser.php
| @@ -5,6 +5,10 @@ | @@ -5,6 +5,10 @@ | ||
| 5 | namespace yii\multiparser; | 5 | namespace yii\multiparser; |
| 6 | 6 | ||
| 7 | 7 | ||
| 8 | +/** | ||
| 9 | + * Class CsvParser | ||
| 10 | + * @package yii\multiparser | ||
| 11 | + */ | ||
| 8 | class CsvParser implements ParserInterface | 12 | class CsvParser implements ParserInterface |
| 9 | { | 13 | { |
| 10 | 14 | ||
| @@ -42,6 +46,7 @@ class CsvParser implements ParserInterface | @@ -42,6 +46,7 @@ class CsvParser implements ParserInterface | ||
| 42 | public $min_column_quantity = 5; | 46 | public $min_column_quantity = 5; |
| 43 | 47 | ||
| 44 | 48 | ||
| 49 | + | ||
| 45 | /** | 50 | /** |
| 46 | метод устанвливает нужные настройки объекта SplFileObject, для работы с csv | 51 | метод устанвливает нужные настройки объекта SplFileObject, для работы с csv |
| 47 | */ | 52 | */ |
| @@ -129,6 +134,7 @@ class CsvParser implements ParserInterface | @@ -129,6 +134,7 @@ class CsvParser implements ParserInterface | ||
| 129 | } | 134 | } |
| 130 | 135 | ||
| 131 | 136 | ||
| 137 | + | ||
| 132 | protected function closeHandler() | 138 | protected function closeHandler() |
| 133 | { | 139 | { |
| 134 | $this->file = NULL; | 140 | $this->file = NULL; |
vendor/yiisoft/multiparser/ParserConfigurator.php
| @@ -4,7 +4,7 @@ namespace yii\multiparser; | @@ -4,7 +4,7 @@ namespace yii\multiparser; | ||
| 4 | class ParserConfigurator | 4 | class ParserConfigurator |
| 5 | { | 5 | { |
| 6 | 6 | ||
| 7 | - private static $configuration = [ | 7 | + protected static $configuration = [ |
| 8 | 'csv' => | 8 | 'csv' => |
| 9 | ['web' => | 9 | ['web' => |
| 10 | ['class' => 'backend\components\parsers\CustomCsvParser', | 10 | ['class' => 'backend\components\parsers\CustomCsvParser', |