From febcec0b7e76290716c25781628f83210eacf965 Mon Sep 17 00:00:00 2001 From: Mihail Date: Thu, 27 Aug 2015 17:22:57 +0300 Subject: [PATCH] final version parser upload form and add custom vardumper --- backend/components/parsers/CsvParser.php | 12 ++++++++---- backend/components/parsers/Encoder.php | 2 +- backend/components/parsers/ParserHandler.php | 18 +++++++++++------- backend/controllers/ParserController.php | 14 ++++++++------ backend/models/UploadFileParsingForm.php | 18 ++++++++++++++++-- backend/views/parser/index.php | 4 +++- common/components/debug/CustomVarDamp.php | 20 ++++++++++++++++++++ 7 files changed, 67 insertions(+), 21 deletions(-) create mode 100644 common/components/debug/CustomVarDamp.php diff --git a/backend/components/parsers/CsvParser.php b/backend/components/parsers/CsvParser.php index af6322b..01c07a4 100644 --- a/backend/components/parsers/CsvParser.php +++ b/backend/components/parsers/CsvParser.php @@ -6,7 +6,7 @@ * Time: 17:00 */ -namespace app\components\parsers; +namespace backend\components\parsers; use Yii; use yii\base\ErrorException; @@ -24,13 +24,17 @@ class CsvParser implements \IteratorAggregate { private $out_charset = 'UTF-8'; /** @var out encoding charset */ private $in_charset; - /** @var out encoding charset */ + /** @var int - first line for parsing */ private $first_line; - public function setup( $file, $first_line, $hasHeaderRow = TRUE, $delimiter = ';') + /** @var int - first column for parsing */ + private $first_column; + + public function setup( $file, $first_line, $first_column, $hasHeaderRow = TRUE, $delimiter = ';') { $this->first_line = $first_line; + $this->first_column = $first_column; $this->file = $file; @@ -106,7 +110,7 @@ class CsvParser implements \IteratorAggregate { // @todo add comments { $dirt_value_arr = $this->file->fgetcsv( ); - $dirt_value_arr = array_slice( $dirt_value_arr, 2 ); + $dirt_value_arr = array_slice( $dirt_value_arr, $this->first_column ); $clear_arr = Encoder::encodeArray( $this->in_charset, $this->out_charset, $dirt_value_arr ); return $clear_arr; diff --git a/backend/components/parsers/Encoder.php b/backend/components/parsers/Encoder.php index 8ae2788..94fc76e 100644 --- a/backend/components/parsers/Encoder.php +++ b/backend/components/parsers/Encoder.php @@ -6,7 +6,7 @@ * Time: 13:36 */ -namespace app\components\parsers; +namespace backend\components\parsers; // @todo add comments class Encoder diff --git a/backend/components/parsers/ParserHandler.php b/backend/components/parsers/ParserHandler.php index d66836a..d244361 100644 --- a/backend/components/parsers/ParserHandler.php +++ b/backend/components/parsers/ParserHandler.php @@ -1,7 +1,8 @@ filePath = $filePath; - $this->first_line = $first_line; + $this->options = $options; try { $this->fileObject = new \SplFileObject( $this->filePath , 'r' );; - } catch (ErrorException $e) { + } catch (\ErrorException $e) { Yii::warning("Ошибка открытия файла {$this->filePath}"); } @@ -38,9 +39,12 @@ class ParserHandler { public function run(){ if ($this->extension = 'csv'){ + $first_line = isset( $this->options->first_line )? $this->options->first_line : 0; + $first_column = isset( $this->options->first_column )? $this->options->first_column : 0; $csvParser = new CsvParser( ); - $csvParser->setup( $this->fileObject, $this->first_line ); + $csvParser->setup( $this->fileObject, $first_line, $first_column ); + return $csvParser->read(); }; } diff --git a/backend/controllers/ParserController.php b/backend/controllers/ParserController.php index 6a36cc1..9c24116 100644 --- a/backend/controllers/ParserController.php +++ b/backend/controllers/ParserController.php @@ -5,16 +5,19 @@ use Yii; use yii\filters\AccessControl; use yii\web\Controller; use yii\filters\VerbFilter; -use app\models\UploadFileParsingForm; +use backend\models\UploadFileParsingForm; use yii\web\UploadedFile; use yii\data\ArrayDataProvider; -use app\components\parsers\ParserHandler; +use backend\components\parsers\ParserHandler; +use common\components\debug\CustomVarDamp; /** - * Site controller + * Parser controller */ + class ParserController extends Controller { + public $enableCsrfValidation = false; /** * @inheritdoc */ @@ -56,14 +59,13 @@ class ParserController extends Controller { $model = new UploadFileParsingForm(); - if (Yii::$app->request->isPost) { + if ($model->load(Yii::$app->request->post())) { $model->file = UploadedFile::getInstance($model, 'file'); if ($model->file && $model->validate()) { $filePath = Yii::getAlias('@webroot') . '/uploads/' . $model->file->baseName . '.' . $model->file->extension; $model->file->saveAs( $filePath ); - - $parser = new ParserHandler( $filePath, 1 ); + $parser = new ParserHandler( $filePath, $model ); $data = $parser->run(); if( !is_array($data) ){ diff --git a/backend/models/UploadFileParsingForm.php b/backend/models/UploadFileParsingForm.php index c4a5ea4..e035177 100644 --- a/backend/models/UploadFileParsingForm.php +++ b/backend/models/UploadFileParsingForm.php @@ -1,8 +1,9 @@ ['csv', 'xml'] ], + [['file'], 'file'], //'extensions' => ['csv', 'xml'] ], + ['first_line', 'integer'], + ['first_column', 'integer'] + ]; + } + + public function attributeLabels() + { + return [ + 'file' => Yii::t('app', 'Источник'), + 'first_line' => Yii::t('app', 'Первая значимая строка'), + 'first_column' => Yii::t('app', 'Первый значимый столбец'), ]; } } \ No newline at end of file diff --git a/backend/views/parser/index.php b/backend/views/parser/index.php index 2d27e1d..eaba348 100644 --- a/backend/views/parser/index.php +++ b/backend/views/parser/index.php @@ -6,13 +6,15 @@ use yii\helpers\Html;
['enctype' => 'multipart/form-data']]) ?> +

Загрузка прайсов поставщиков

+ field($model, 'first_line') ?> field($model, 'first_column') ?> field($model, 'file')->fileInput() ?>
- 'btn btn-primary']) ?> + 'btn btn-primary']) ?>
diff --git a/common/components/debug/CustomVarDamp.php b/common/components/debug/CustomVarDamp.php new file mode 100644 index 0000000..76d1a19 --- /dev/null +++ b/common/components/debug/CustomVarDamp.php @@ -0,0 +1,20 @@ +"; + echo static::dumpAsString($var, $depth, $highlight); + echo ""; + die; + } +} \ No newline at end of file -- libgit2 0.21.4