Commit febcec0b7e76290716c25781628f83210eacf965

Authored by Mihail
1 parent 5be26bf2

final version parser upload form and add custom vardumper

backend/components/parsers/CsvParser.php
... ... @@ -6,7 +6,7 @@
6 6 * Time: 17:00
7 7 */
8 8  
9   -namespace app\components\parsers;
  9 +namespace backend\components\parsers;
10 10  
11 11 use Yii;
12 12 use yii\base\ErrorException;
... ... @@ -24,13 +24,17 @@ class CsvParser implements \IteratorAggregate {
24 24 private $out_charset = 'UTF-8';
25 25 /** @var out encoding charset */
26 26 private $in_charset;
27   - /** @var out encoding charset */
  27 + /** @var int - first line for parsing */
28 28 private $first_line;
29 29  
30   - public function setup( $file, $first_line, $hasHeaderRow = TRUE, $delimiter = ';')
  30 + /** @var int - first column for parsing */
  31 + private $first_column;
  32 +
  33 + public function setup( $file, $first_line, $first_column, $hasHeaderRow = TRUE, $delimiter = ';')
31 34 {
32 35  
33 36 $this->first_line = $first_line;
  37 + $this->first_column = $first_column;
34 38  
35 39 $this->file = $file;
36 40  
... ... @@ -106,7 +110,7 @@ class CsvParser implements \IteratorAggregate {
106 110 // @todo add comments
107 111 {
108 112 $dirt_value_arr = $this->file->fgetcsv( );
109   - $dirt_value_arr = array_slice( $dirt_value_arr, 2 );
  113 + $dirt_value_arr = array_slice( $dirt_value_arr, $this->first_column );
110 114 $clear_arr = Encoder::encodeArray( $this->in_charset, $this->out_charset, $dirt_value_arr );
111 115 return $clear_arr;
112 116  
... ...
backend/components/parsers/Encoder.php
... ... @@ -6,7 +6,7 @@
6 6 * Time: 13:36
7 7 */
8 8  
9   -namespace app\components\parsers;
  9 +namespace backend\components\parsers;
10 10  
11 11 // @todo add comments
12 12 class Encoder
... ...
backend/components/parsers/ParserHandler.php
1 1 <?php
2   -namespace app\components\parsers;
  2 +namespace backend\components\parsers;
  3 +
  4 +use Yii;
3 5  
4   -use app\components\parsers\CsvParser;
5 6  
6 7 class ParserHandler {
7 8  
... ... @@ -15,19 +16,19 @@ class ParserHandler {
15 16 private $extension;
16 17  
17 18 /** @var string - extension of file $filePath */
18   - private $first_line;
  19 + private $options;
19 20  
20 21 /**
21 22 * @param string first line in file for parsing
22 23 */
23   - public function __construct( $filePath, $first_line )
  24 + public function __construct( $filePath, $options )
24 25 {
25 26 $this->filePath = $filePath;
26   - $this->first_line = $first_line;
  27 + $this->options = $options;
27 28  
28 29 try {
29 30 $this->fileObject = new \SplFileObject( $this->filePath , 'r' );;
30   - } catch (ErrorException $e) {
  31 + } catch (\ErrorException $e) {
31 32 Yii::warning("Ошибка открытия файла {$this->filePath}");
32 33 }
33 34  
... ... @@ -38,9 +39,12 @@ class ParserHandler {
38 39  
39 40 public function run(){
40 41 if ($this->extension = 'csv'){
  42 + $first_line = isset( $this->options->first_line )? $this->options->first_line : 0;
  43 + $first_column = isset( $this->options->first_column )? $this->options->first_column : 0;
41 44  
42 45 $csvParser = new CsvParser( );
43   - $csvParser->setup( $this->fileObject, $this->first_line );
  46 + $csvParser->setup( $this->fileObject, $first_line, $first_column );
  47 +
44 48 return $csvParser->read();
45 49 };
46 50 }
... ...
backend/controllers/ParserController.php
... ... @@ -5,16 +5,19 @@ use Yii;
5 5 use yii\filters\AccessControl;
6 6 use yii\web\Controller;
7 7 use yii\filters\VerbFilter;
8   -use app\models\UploadFileParsingForm;
  8 +use backend\models\UploadFileParsingForm;
9 9 use yii\web\UploadedFile;
10 10 use yii\data\ArrayDataProvider;
11   -use app\components\parsers\ParserHandler;
  11 +use backend\components\parsers\ParserHandler;
  12 +use common\components\debug\CustomVarDamp;
12 13  
13 14 /**
14   - * Site controller
  15 + * Parser controller
15 16 */
  17 +
16 18 class ParserController extends Controller
17 19 {
  20 + public $enableCsrfValidation = false;
18 21 /**
19 22 * @inheritdoc
20 23 */
... ... @@ -56,14 +59,13 @@ class ParserController extends Controller
56 59 {
57 60 $model = new UploadFileParsingForm();
58 61  
59   - if (Yii::$app->request->isPost) {
  62 + if ($model->load(Yii::$app->request->post())) {
60 63 $model->file = UploadedFile::getInstance($model, 'file');
61 64  
62 65 if ($model->file && $model->validate()) {
63 66 $filePath = Yii::getAlias('@webroot') . '/uploads/' . $model->file->baseName . '.' . $model->file->extension;
64 67 $model->file->saveAs( $filePath );
65   -
66   - $parser = new ParserHandler( $filePath, 1 );
  68 + $parser = new ParserHandler( $filePath, $model );
67 69 $data = $parser->run();
68 70  
69 71 if( !is_array($data) ){
... ...
backend/models/UploadFileParsingForm.php
1 1 <?php
2   -namespace app\models;
  2 +namespace backend\models;
3 3  
4 4 use yii\base\Model;
5 5 use yii\web\UploadedFile;
  6 +use Yii;
6 7  
7 8 /**
8 9 * UploadForm is the model behind the upload form.
... ... @@ -13,6 +14,8 @@ class UploadFileParsingForm extends Model
13 14 * @var UploadedFile file attribute
14 15 */
15 16 public $file;
  17 + public $first_line;
  18 + public $first_column;
16 19  
17 20 /**
18 21 * @return array the validation rules.
... ... @@ -20,7 +23,18 @@ class UploadFileParsingForm extends Model
20 23 public function rules()
21 24 {
22 25 return [
23   - [['file'], 'file', ] //'extensions' => ['csv', 'xml'] ],
  26 + [['file'], 'file'], //'extensions' => ['csv', 'xml'] ],
  27 + ['first_line', 'integer'],
  28 + ['first_column', 'integer']
  29 + ];
  30 + }
  31 +
  32 + public function attributeLabels()
  33 + {
  34 + return [
  35 + 'file' => Yii::t('app', 'Источник'),
  36 + 'first_line' => Yii::t('app', 'Первая значимая строка'),
  37 + 'first_column' => Yii::t('app', 'Первый значимый столбец'),
24 38 ];
25 39 }
26 40 }
27 41 \ No newline at end of file
... ...
backend/views/parser/index.php
... ... @@ -6,13 +6,15 @@ use yii\helpers\Html;
6 6 <div class="row">
7 7 <div class="col-lg-5">
8 8 <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
  9 + <h3>Загрузка прайсов поставщиков</h3>
  10 +
9 11 <?= $form->field($model, 'first_line') ?>
10 12 <?= $form->field($model, 'first_column') ?>
11 13  
12 14 <?= $form->field($model, 'file')->fileInput() ?>
13 15  
14 16 <div class="form-group">
15   - <?= Html::submitButton(Yii::t('app', 'Submit'), ['class' => 'btn btn-primary']) ?>
  17 + <?= Html::submitButton(Yii::t('app', 'Прочитать'), ['class' => 'btn btn-primary']) ?>
16 18 </div>
17 19  
18 20 <?php ActiveForm::end() ?>
... ...
common/components/debug/CustomVarDamp.php 0 → 100644
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: Cibermag
  5 + * Date: 27.08.2015
  6 + * Time: 16:47
  7 + */
  8 +namespace common\components\debug;
  9 +use yii\helpers\BaseVarDumper;
  10 +
  11 +class CustomVarDamp extends BaseVarDumper {
  12 +
  13 + public static function dumpAndDie($var, $depth = 10, $highlight = false)
  14 + {
  15 + echo "<pre>";
  16 + echo static::dumpAsString($var, $depth, $highlight);
  17 + echo "</pre>";
  18 + die;
  19 + }
  20 +}
0 21 \ No newline at end of file
... ...