Commit 4380a807f51ec488a2c4c6475c22339986c8d4ad
Merge remote-tracking branch 'origin/master'
Showing
5 changed files
with
84 additions
and
42 deletions
Show diff stats
backend/components/parsers/CsvParser.php
... | ... | @@ -27,36 +27,18 @@ class CsvParser implements \IteratorAggregate { |
27 | 27 | /** @var out encoding charset */ |
28 | 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 | 35 | $this->file = $file; |
54 | 36 | |
55 | 37 | $this->file->setCsvControl($delimiter); |
56 | 38 | $this->file->setFlags(\SplFileObject::READ_CSV); |
57 | 39 | $this->file->seek( $this->first_line ); |
58 | 40 | |
59 | - $this->first_line = $first_line; | |
41 | + | |
60 | 42 | $this->in_charset = 'windows-1251'; |
61 | 43 | $this->hasHeaderRow = $hasHeaderRow; |
62 | 44 | } |
... | ... | @@ -82,6 +64,7 @@ class CsvParser implements \IteratorAggregate { |
82 | 64 | */ |
83 | 65 | public function read() |
84 | 66 | { |
67 | + // @todo add comments | |
85 | 68 | $return = []; |
86 | 69 | |
87 | 70 | $line = 0; |
... | ... | @@ -120,21 +103,22 @@ class CsvParser implements \IteratorAggregate { |
120 | 103 | } |
121 | 104 | |
122 | 105 | private function readRow() |
106 | + // @todo add comments | |
123 | 107 | { |
124 | 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 | 125 | \ No newline at end of file | ... | ... |
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 | 51 | \ No newline at end of file | ... | ... |
backend/controllers/ParserController.php
... | ... | @@ -5,7 +5,7 @@ use Yii; |
5 | 5 | use yii\filters\AccessControl; |
6 | 6 | use yii\web\Controller; |
7 | 7 | use yii\filters\VerbFilter; |
8 | -use app\models\UploadForm; | |
8 | +use app\models\UploadFileParsingForm; | |
9 | 9 | use yii\web\UploadedFile; |
10 | 10 | use yii\data\ArrayDataProvider; |
11 | 11 | use app\components\parsers\ParserHandler; |
... | ... | @@ -54,7 +54,7 @@ class ParserController extends Controller |
54 | 54 | |
55 | 55 | public function actionIndex() |
56 | 56 | { |
57 | - $model = new UploadForm(); | |
57 | + $model = new UploadFileParsingForm(); | |
58 | 58 | |
59 | 59 | if (Yii::$app->request->isPost) { |
60 | 60 | $model->file = UploadedFile::getInstance($model, 'file'); |
... | ... | @@ -63,7 +63,7 @@ class ParserController extends Controller |
63 | 63 | $filePath = Yii::getAlias('@webroot') . '/uploads/' . $model->file->baseName . '.' . $model->file->extension; |
64 | 64 | $model->file->saveAs( $filePath ); |
65 | 65 | |
66 | - $parser = new ParserHandler( $filePath, 0 ); | |
66 | + $parser = new ParserHandler( $filePath, 1 ); | |
67 | 67 | $data = $parser->run(); |
68 | 68 | |
69 | 69 | if( !is_array($data) ){ | ... | ... |
backend/models/UploadForm.php renamed to backend/models/UploadFileParsingForm.php
backend/views/parser/index.php
1 | 1 | <?php |
2 | 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 | 18 | \ No newline at end of file |
19 | + <?php ActiveForm::end() ?> | |
20 | + </div> | |
21 | +</div> | |
14 | 22 | \ No newline at end of file | ... | ... |