Commit 83f0c6d892edef906ef7935ff973f0e2a98d0f10

Authored by Administrator
2 parents 6a016cfa 77422ce3

Merge remote-tracking branch 'origin/master'

backend/components/parsers/CsvParser.php
@@ -27,36 +27,18 @@ class CsvParser implements \IteratorAggregate { @@ -27,36 +27,18 @@ class CsvParser implements \IteratorAggregate {
27 /** @var out encoding charset */ 27 /** @var out encoding charset */
28 private $first_line; 28 private $first_line;
29 29
30 -  
31 -  
32 -  
33 -// public function encodeFile( $in_charset, $out_charset, $filePath ){  
34 -//  
35 -// $old_content = file_get_contents( $filePath );  
36 -// $encode_content = iconv( $in_charset, $out_charset, $old_content );  
37 -// $file = @fopen( $filePath, "w" );  
38 -// fwrite( $file, $encode_content );  
39 -// @fclose( $file );  
40 -//  
41 -// }  
42 -  
43 -  
44 - /**  
45 - * @param string $delimiter  
46 - * @param string $enclosure  
47 - * @param string $escape  
48 - * @return $this  
49 - */  
50 - public function setup( $file, $first_line = 1, $hasHeaderRow = TRUE, $delimiter = ';') 30 + public function setup( $file, $first_line, $hasHeaderRow = TRUE, $delimiter = ';')
51 { 31 {
52 32
  33 + $this->first_line = $first_line;
  34 +
53 $this->file = $file; 35 $this->file = $file;
54 36
55 $this->file->setCsvControl($delimiter); 37 $this->file->setCsvControl($delimiter);
56 $this->file->setFlags(\SplFileObject::READ_CSV); 38 $this->file->setFlags(\SplFileObject::READ_CSV);
57 $this->file->seek( $this->first_line ); 39 $this->file->seek( $this->first_line );
58 40
59 - $this->first_line = $first_line; 41 +
60 $this->in_charset = 'windows-1251'; 42 $this->in_charset = 'windows-1251';
61 $this->hasHeaderRow = $hasHeaderRow; 43 $this->hasHeaderRow = $hasHeaderRow;
62 } 44 }
@@ -82,6 +64,7 @@ class CsvParser implements \IteratorAggregate { @@ -82,6 +64,7 @@ class CsvParser implements \IteratorAggregate {
82 */ 64 */
83 public function read() 65 public function read()
84 { 66 {
  67 + // @todo add comments
85 $return = []; 68 $return = [];
86 69
87 $line = 0; 70 $line = 0;
@@ -120,21 +103,22 @@ class CsvParser implements \IteratorAggregate { @@ -120,21 +103,22 @@ class CsvParser implements \IteratorAggregate {
120 } 103 }
121 104
122 private function readRow() 105 private function readRow()
  106 + // @todo add comments
123 { 107 {
124 $dirt_value_arr = $this->file->fgetcsv( ); 108 $dirt_value_arr = $this->file->fgetcsv( );
125 - $encode_arr = $this->encodeArray( $dirt_value_arr );  
126 -  
127 - return $encode_arr; 109 + $dirt_value_arr = array_slice( $dirt_value_arr, 2 );
  110 + $clear_arr = Encoder::encodeArray( $this->in_charset, $this->out_charset, $dirt_value_arr );
  111 + return $clear_arr;
128 112
129 } 113 }
130 114
131 - private function encodeArray( $array_to_encode )  
132 - {  
133 - return array_map(function($array_to_encode) {  
134 - return iconv( $this->in_charset, $this->out_charset, $array_to_encode );  
135 - }, $array_to_encode);  
136 -  
137 - } 115 +// private function encodeArray( $array_to_encode )
  116 +// {
  117 +// return array_map(function($array_to_encode) {
  118 +// return iconv( $this->in_charset, $this->out_charset, $array_to_encode );
  119 +// }, $array_to_encode);
  120 +//
  121 +// }
138 122
139 123
140 } 124 }
141 \ No newline at end of file 125 \ No newline at end of file
backend/components/parsers/Encoder.php 0 → 100644
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: Cibermag
  5 + * Date: 27.08.2015
  6 + * Time: 13:36
  7 + */
  8 +
  9 +namespace app\components\parsers;
  10 +
  11 +// @todo add comments
  12 +class Encoder
  13 +{
  14 + public static $in_charset;
  15 + public static $out_charset;
  16 +
  17 +
  18 + public static function encodeFile($in_charset, $out_charset, $filePath)
  19 + {
  20 +
  21 + $old_content = file_get_contents($filePath);
  22 + $encode_content = self::encode( $in_charset, $out_charset, $old_content );
  23 + $file = @fopen($filePath, "w");
  24 + fwrite($file, $encode_content);
  25 + @fclose($file);
  26 + }
  27 +
  28 + public static function encodeArray($in_charset, $out_charset, $array)
  29 + {
  30 +
  31 + self::$in_charset = $in_charset;
  32 + self::$out_charset = $out_charset;
  33 +
  34 + $result = array_map(
  35 + function ( $array) {
  36 +
  37 + return self::encode( self::$in_charset, self::$out_charset, $array );
  38 +
  39 + },
  40 + $array);
  41 +
  42 + return $result;
  43 + }
  44 +
  45 + private static function encode( $in_charset, $out_charset, $source ){
  46 +
  47 + return iconv($in_charset, $out_charset, $source);
  48 +
  49 + }
  50 +}
0 \ No newline at end of file 51 \ No newline at end of file
backend/controllers/ParserController.php
@@ -5,7 +5,7 @@ use Yii; @@ -5,7 +5,7 @@ use Yii;
5 use yii\filters\AccessControl; 5 use yii\filters\AccessControl;
6 use yii\web\Controller; 6 use yii\web\Controller;
7 use yii\filters\VerbFilter; 7 use yii\filters\VerbFilter;
8 -use app\models\UploadForm; 8 +use app\models\UploadFileParsingForm;
9 use yii\web\UploadedFile; 9 use yii\web\UploadedFile;
10 use yii\data\ArrayDataProvider; 10 use yii\data\ArrayDataProvider;
11 use app\components\parsers\ParserHandler; 11 use app\components\parsers\ParserHandler;
@@ -54,7 +54,7 @@ class ParserController extends Controller @@ -54,7 +54,7 @@ class ParserController extends Controller
54 54
55 public function actionIndex() 55 public function actionIndex()
56 { 56 {
57 - $model = new UploadForm(); 57 + $model = new UploadFileParsingForm();
58 58
59 if (Yii::$app->request->isPost) { 59 if (Yii::$app->request->isPost) {
60 $model->file = UploadedFile::getInstance($model, 'file'); 60 $model->file = UploadedFile::getInstance($model, 'file');
@@ -63,7 +63,7 @@ class ParserController extends Controller @@ -63,7 +63,7 @@ class ParserController extends Controller
63 $filePath = Yii::getAlias('@webroot') . '/uploads/' . $model->file->baseName . '.' . $model->file->extension; 63 $filePath = Yii::getAlias('@webroot') . '/uploads/' . $model->file->baseName . '.' . $model->file->extension;
64 $model->file->saveAs( $filePath ); 64 $model->file->saveAs( $filePath );
65 65
66 - $parser = new ParserHandler( $filePath, 0 ); 66 + $parser = new ParserHandler( $filePath, 1 );
67 $data = $parser->run(); 67 $data = $parser->run();
68 68
69 if( !is_array($data) ){ 69 if( !is_array($data) ){
backend/models/UploadForm.php renamed to backend/models/UploadFileParsingForm.php
@@ -7,7 +7,7 @@ use yii\web\UploadedFile; @@ -7,7 +7,7 @@ use yii\web\UploadedFile;
7 /** 7 /**
8 * UploadForm is the model behind the upload form. 8 * UploadForm is the model behind the upload form.
9 */ 9 */
10 -class UploadForm extends Model 10 +class UploadFileParsingForm extends Model
11 { 11 {
12 /** 12 /**
13 * @var UploadedFile file attribute 13 * @var UploadedFile file attribute
backend/views/parser/index.php
1 <?php 1 <?php
2 use yii\widgets\ActiveForm; 2 use yii\widgets\ActiveForm;
  3 +use yii\helpers\Html;
  4 +
3 ?> 5 ?>
4 - <div class="catalog-view">  
5 -<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?> 6 +<div class="row">
  7 + <div class="col-lg-5">
  8 + <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
  9 + <?= $form->field($model, 'first_line') ?>
  10 + <?= $form->field($model, 'first_column') ?>
6 11
7 -<?= $form->field($model, 'file')->fileInput() ?> 12 + <?= $form->field($model, 'file')->fileInput() ?>
8 13
9 -<button>Прочитать</button> 14 + <div class="form-group">
  15 + <?= Html::submitButton(Yii::t('app', 'Submit'), ['class' => 'btn btn-primary']) ?>
  16 + </div>
10 17
11 -<?php ActiveForm::end() ?>  
12 - </div>  
13 \ No newline at end of file 18 \ No newline at end of file
  19 + <?php ActiveForm::end() ?>
  20 + </div>
  21 +</div>
14 \ No newline at end of file 22 \ No newline at end of file