Commit 45c41a76e504022a078335eb0a3a0357f395ac81
1 parent
7a80e74c
edit createAssocArray function
Showing
11 changed files
with
163 additions
and
73 deletions
Show diff stats
backend/components/parsers/CustomCsvParser.php
... | ... | @@ -11,23 +11,20 @@ namespace backend\components\parsers; |
11 | 11 | |
12 | 12 | class CustomCsvParser extends \yii\multiparser\CsvParser { |
13 | 13 | |
14 | - //public $last_line = 10; | |
14 | + public $last_line = 10; | |
15 | 15 | //public $hasHeaderRow = true; |
16 | 16 | // public $keys = ['first','second', 'third', 'forth', 'fifth']; |
17 | - | |
18 | - | |
19 | - protected function readRow() | |
17 | + public function setupConverter() | |
20 | 18 | { |
21 | 19 | |
22 | - $row = parent::readRow(); | |
23 | - | |
24 | - if (is_array($row)) { | |
25 | - | |
26 | - $row = Encoder::encodeArray( Encoder::$in_charset, Encoder::$out_charset, $row ); | |
20 | + if ($this->hasHeaderRow) { | |
21 | + // если у файла есть заголовок, то в результате имеем ассоциативный массив | |
22 | + $this->converter_conf['hasKey'] = 1; | |
27 | 23 | } |
28 | 24 | |
29 | - return $row; | |
25 | + $this->converter = \Yii::createObject($this->converter_conf); | |
30 | 26 | |
31 | 27 | } |
32 | 28 | |
29 | + | |
33 | 30 | } |
34 | 31 | \ No newline at end of file | ... | ... |
1 | +<?php | |
2 | + return [ | |
3 | + 'csv' => | |
4 | + ['web' => | |
5 | + ['class' => 'backend\components\parsers\CustomCsvParser', | |
6 | + 'auto_detect_first_line' => true, | |
7 | + 'converter_conf' => ['class' => 'yii\multiparser\Converter', | |
8 | + 'configuration' => [ | |
9 | + "string" => 'DESCR' | |
10 | + ] | |
11 | + ,]], | |
12 | + | |
13 | + 'basic_column' => [ | |
14 | + Null => 'Пусто', | |
15 | + "BRAND" => 'Бренд', | |
16 | + "ARTICLE"=> 'Артикул', | |
17 | + "PRICE" => 'Цена', | |
18 | + "DESCR" => 'Наименование', | |
19 | + "BOX" => 'Колво', | |
20 | + "ADD_BOX"=> 'В пути', | |
21 | + "GROUP" => 'Группа RG' | |
22 | + ], | |
23 | + ], | |
24 | + 'xml' => | |
25 | + ['web' => | |
26 | + ['class' => 'yii\multiparser\XmlParser', | |
27 | + 'node' => 'Товар',], | |
28 | + | |
29 | + 'basic_column' => [ | |
30 | + "BRAND" => 'Производитель', | |
31 | + "ARTICLE"=> 'Код', | |
32 | + "PRICE" => 'Розница', | |
33 | + "DESCR" => 'Наименование', | |
34 | + "BOX" => 'Колво', | |
35 | + "ADD_BOX"=> 'Ожидаемое', | |
36 | + "GROUP" => 'Группа' | |
37 | + ], | |
38 | + ] | |
39 | + ]; | |
40 | + | |
41 | + | |
42 | +//[ | |
43 | +// "float" => 'PRICE', | |
44 | +// "integer" => ['BOX' , 'ADD_BOX' ], | |
45 | +// "prefix" => 'ARTICLE' | |
46 | +//] | |
0 | 47 | \ No newline at end of file | ... | ... |
backend/config/main.php
... | ... | @@ -5,6 +5,7 @@ $params = array_merge( |
5 | 5 | require(__DIR__ . '/params.php'), |
6 | 6 | require(__DIR__ . '/params-local.php') |
7 | 7 | ); |
8 | +$mp_configuration = require( __DIR__ . '/../components/parsers/config.php'); | |
8 | 9 | |
9 | 10 | return [ |
10 | 11 | 'id' => 'app-backend', |
... | ... | @@ -41,6 +42,7 @@ return [ |
41 | 42 | 'multiparser'=>[ |
42 | 43 | |
43 | 44 | 'class' => 'yii\multiparser\YiiMultiparser', |
45 | + 'configuration' => $mp_configuration, | |
44 | 46 | |
45 | 47 | ], |
46 | 48 | ], | ... | ... |
backend/controllers/ParserController.php
... | ... | @@ -8,7 +8,7 @@ use yii\filters\VerbFilter; |
8 | 8 | use backend\models\UploadFileParsingForm; |
9 | 9 | use yii\web\UploadedFile; |
10 | 10 | use yii\data\ArrayDataProvider; |
11 | -use backend\components\parsers\DynamicFormHelper; | |
11 | +use yii\multiparser\DynamicFormHelper; | |
12 | 12 | use backend\components\parsers\CustomParserConfigurator; |
13 | 13 | |
14 | 14 | use common\components\CustomVarDamp; |
... | ... | @@ -96,13 +96,14 @@ class ParserController extends BaseController |
96 | 96 | ], |
97 | 97 | ]); |
98 | 98 | |
99 | + // CustomVarDamp::dumpAndDie($data); | |
99 | 100 | $header_model = DynamicFormHelper::CreateDynamicModel( count( $data[0] ) ); |
100 | 101 | |
101 | - //CustomVarDamp::dumpAndDie($header_model); | |
102 | + // CustomVarDamp::dumpAndDie(Yii::$app->multiparser->getConfiguration('csv','basic_column')); | |
102 | 103 | return $this->render('results', |
103 | 104 | ['model' => $data, |
104 | 105 | 'header_model' => $header_model, |
105 | - 'basic_column' => CustomParserConfigurator::$basic_column, | |
106 | + 'basic_column' => Yii::$app->multiparser->getConfiguration('csv','basic_column'), | |
106 | 107 | 'dataProvider' => $provider]); |
107 | 108 | } |
108 | 109 | |
... | ... | @@ -113,12 +114,15 @@ public function actionWrite() |
113 | 114 | $arr_attributes = Yii::$app->request->post()['DynamicModel']; |
114 | 115 | $model = DynamicFormHelper::CreateDynamicModel( $arr_attributes ); |
115 | 116 | foreach ($arr_attributes as $key => $value) { |
116 | - $model->addRule($key, 'in', ['range' => array_keys( CustomParserConfigurator::$basic_column )]); | |
117 | + $model->addRule($key, 'in', ['range' => array_keys( Yii::$app->multiparser->getConfiguration('csv','basic_column') )]); | |
117 | 118 | } |
118 | 119 | |
119 | 120 | //CustomVarDamp::dumpAndDie($model); |
120 | 121 | if ($model->validate()) { |
121 | 122 | $arr = $model->toArray(); |
123 | + $data = json_decode( Yii::$app->getCache()->get( 'parser_data' ),true ); | |
124 | + | |
125 | + // CustomVarDamp::dumpAndDie(DynamicFormHelper::CreateAssocArray($data, $arr)); | |
122 | 126 | CustomVarDamp::dumpAndDie($arr); |
123 | 127 | } |
124 | 128 | ... | ... |
backend/models/UploadFileParsingForm.php
... | ... | @@ -52,7 +52,6 @@ class UploadFileParsingForm extends Model |
52 | 52 | public function readFile($filePath){ |
53 | 53 | |
54 | 54 | $data = Yii::$app->multiparser->parse($filePath); |
55 | - // \common\components\CustomVarDamp::dumpAndDie($data); | |
56 | 55 | if( !is_array($data) ){ |
57 | 56 | $data = ['No results']; |
58 | 57 | } | ... | ... |
backend/views/parser/results.php
1 | 1 | <?php |
2 | 2 | |
3 | 3 | use yii\helpers\Html; |
4 | -use backend\components\parsers\DynamicFormHelper; | |
4 | +use yii\multiparser\DynamicFormHelper; | |
5 | 5 | use yii\widgets\ActiveForm; |
6 | 6 | |
7 | 7 | |
... | ... | @@ -20,7 +20,7 @@ $this->params['breadcrumbs'][] = $this->title; |
20 | 20 | |
21 | 21 | $form = ActiveForm::begin(['action' => 'write']); |
22 | 22 | ?> |
23 | - <?= DynamicFormHelper::CreateDynamicGridViewWithDropDownListHeader( $dataProvider, $form, $header_model, $basic_column )?> | |
23 | + <?= DynamicFormHelper::CreateGridWithDropDownListHeader( $dataProvider, $form, $header_model, $basic_column )?> | |
24 | 24 | |
25 | 25 | <div class="form-group"> |
26 | 26 | <?= Html::submitButton(Yii::t('app', 'Записать в БД'), ['class' => 'btn btn-primary']) ?> | ... | ... |
common/components/CustomVarDamp.php
... | ... | @@ -17,10 +17,16 @@ class CustomVarDamp extends BaseVarDumper { |
17 | 17 | echo "</pre>"; |
18 | 18 | die; |
19 | 19 | } |
20 | - public static function dump($var, $depth = 10, $highlight = false) | |
20 | + public static function dump($var, $step = '', $depth = 10, $highlight = false) | |
21 | 21 | { |
22 | 22 | echo "<pre>"; |
23 | + if ($step) { | |
24 | + echo "-------------- {$step} -------------"; | |
25 | + } | |
23 | 26 | echo static::dumpAsString($var, $depth, $highlight); |
27 | + if ($step) { | |
28 | + echo "-------------- {$step} -------------"; | |
29 | + } | |
24 | 30 | echo "</pre>"; |
25 | 31 | |
26 | 32 | } | ... | ... |
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 | ... | ... |
framework/core/Convert.php
... | ... | @@ -166,63 +166,63 @@ class Convert { |
166 | 166 | } |
167 | 167 | |
168 | 168 | /** |
169 | - * Converts an XML string to a PHP array | |
170 | - * See http://phpsecurity.readthedocs.org/en/latest/Injection-Attacks.html#xml-external-entity-injection | |
171 | - * | |
172 | - * @uses recursiveXMLToArray() | |
173 | - * @param string $val | |
174 | - * @param boolean $disableDoctypes Disables the use of DOCTYPE, and will trigger an error if encountered. | |
175 | - * false by default. | |
176 | - * @param boolean $disableExternals Disables the loading of external entities. false by default. | |
177 | - * @return array | |
178 | - */ | |
179 | - public static function xml2array($val, $disableDoctypes = false, $disableExternals = false) { | |
180 | - // Check doctype | |
181 | - if($disableDoctypes && preg_match('/\<\!DOCTYPE.+]\>/', $val)) { | |
182 | - throw new InvalidArgumentException('XML Doctype parsing disabled'); | |
183 | - } | |
169 | + * Converts an XML string to a PHP array | |
170 | + * See http://phpsecurity.readthedocs.org/en/latest/Injection-Attacks.html#xml-external-entity-injection | |
171 | + * | |
172 | + * @uses recursiveXMLToArray() | |
173 | + * @param string $val | |
174 | + * @param boolean $disableDoctypes Disables the use of DOCTYPE, and will trigger an error if encountered. | |
175 | + * false by default. | |
176 | + * @param boolean $disableExternals Disables the loading of external entities. false by default. | |
177 | + * @return array | |
178 | + */ | |
179 | + public static function xml2array($val, $disableDoctypes = false, $disableExternals = false) { | |
180 | + // Check doctype | |
181 | + if($disableDoctypes && preg_match('/\<\!DOCTYPE.+]\>/', $val)) { | |
182 | + throw new InvalidArgumentException('XML Doctype parsing disabled'); | |
183 | + } | |
184 | 184 | |
185 | - // Disable external entity loading | |
186 | - if($disableExternals) $oldVal = libxml_disable_entity_loader($disableExternals); | |
187 | - try { | |
188 | - $xml = new SimpleXMLElement($val); | |
189 | - $result = self::recursiveXMLToArray($xml); | |
190 | - } catch(Exception $ex) { | |
191 | - if($disableExternals) libxml_disable_entity_loader($oldVal); | |
192 | - throw $ex; | |
193 | - } | |
194 | - if($disableExternals) libxml_disable_entity_loader($oldVal); | |
195 | - return $result; | |
196 | - } | |
185 | + // Disable external entity loading | |
186 | + if($disableExternals) $oldVal = libxml_disable_entity_loader($disableExternals); | |
187 | + try { | |
188 | + $xml = new SimpleXMLElement($val); | |
189 | + $result = self::recursiveXMLToArray($xml); | |
190 | + } catch(Exception $ex) { | |
191 | + if($disableExternals) libxml_disable_entity_loader($oldVal); | |
192 | + throw $ex; | |
193 | + } | |
194 | + if($disableExternals) libxml_disable_entity_loader($oldVal); | |
195 | + return $result; | |
196 | + } | |
197 | 197 | |
198 | - /** | |
199 | - * Convert a XML string to a PHP array recursively. Do not | |
200 | - * call this function directly, Please use {@link Convert::xml2array()} | |
201 | - * | |
202 | - * @param SimpleXMLElement | |
203 | - * | |
204 | - * @return mixed | |
205 | - */ | |
206 | - protected static function recursiveXMLToArray($xml) { | |
207 | - if(is_object($xml) && get_class($xml) == 'SimpleXMLElement') { | |
208 | - $attributes = $xml->attributes(); | |
209 | - foreach($attributes as $k => $v) { | |
210 | - if($v) $a[$k] = (string) $v; | |
211 | - } | |
212 | - $x = $xml; | |
213 | - $xml = get_object_vars($xml); | |
214 | - } | |
215 | - if(is_array($xml)) { | |
216 | - if(count($xml) == 0) return (string) $x; // for CDATA | |
217 | - foreach($xml as $key => $value) { | |
218 | - $r[$key] = self::recursiveXMLToArray($value); | |
219 | - } | |
220 | - if(isset($a)) $r['@'] = $a; // Attributes | |
221 | - return $r; | |
222 | - } | |
223 | - | |
224 | - return (string) $xml; | |
225 | - } | |
198 | + /** | |
199 | + * Convert a XML string to a PHP array recursively. Do not | |
200 | + * call this function directly, Please use {@link Convert::xml2array()} | |
201 | + * | |
202 | + * @param SimpleXMLElement | |
203 | + * | |
204 | + * @return mixed | |
205 | + */ | |
206 | + protected static function recursiveXMLToArray($xml) { | |
207 | + if(is_object($xml) && get_class($xml) == 'SimpleXMLElement') { | |
208 | + $attributes = $xml->attributes(); | |
209 | + foreach($attributes as $k => $v) { | |
210 | + if($v) $a[$k] = (string) $v; | |
211 | + } | |
212 | + $x = $xml; | |
213 | + $xml = get_object_vars($xml); | |
214 | + } | |
215 | + if(is_array($xml)) { | |
216 | + if(count($xml) == 0) return (string) $x; // for CDATA | |
217 | + foreach($xml as $key => $value) { | |
218 | + $r[$key] = self::recursiveXMLToArray($value); | |
219 | + } | |
220 | + if(isset($a)) $r['@'] = $a; // Attributes | |
221 | + return $r; | |
222 | + } | |
223 | + | |
224 | + return (string) $xml; | |
225 | + } | |
226 | 226 | |
227 | 227 | /** |
228 | 228 | * Create a link if the string is a valid URL | ... | ... |