From 45c41a76e504022a078335eb0a3a0357f395ac81 Mon Sep 17 00:00:00 2001 From: tsurkanovm Date: Sun, 13 Sep 2015 21:11:39 +0300 Subject: [PATCH] edit createAssocArray function --- backend/components/parsers/CustomCsvParser.php | 17 +++++++---------- backend/components/parsers/config.php | 46 ++++++++++++++++++++++++++++++++++++++++++++++ backend/config/main.php | 2 ++ backend/controllers/ParserController.php | 12 ++++++++---- backend/models/UploadFileParsingForm.php | 1 - backend/views/parser/results.php | 4 ++-- backend/web/.htaccess | 8 ++++++++ common/components/CustomVarDamp.php | 8 +++++++- common/components/debug/CustomVarDamp.php | 20 ++++++++++++++++++++ framework/core/Convert.php | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------- frontend/web/.htaccess | 8 ++++++++ 11 files changed, 163 insertions(+), 73 deletions(-) create mode 100644 backend/components/parsers/config.php create mode 100644 backend/web/.htaccess create mode 100644 common/components/debug/CustomVarDamp.php create mode 100644 frontend/web/.htaccess diff --git a/backend/components/parsers/CustomCsvParser.php b/backend/components/parsers/CustomCsvParser.php index 5c45f7a..5674511 100644 --- a/backend/components/parsers/CustomCsvParser.php +++ b/backend/components/parsers/CustomCsvParser.php @@ -11,23 +11,20 @@ namespace backend\components\parsers; class CustomCsvParser extends \yii\multiparser\CsvParser { - //public $last_line = 10; + public $last_line = 10; //public $hasHeaderRow = true; // public $keys = ['first','second', 'third', 'forth', 'fifth']; - - - protected function readRow() + public function setupConverter() { - $row = parent::readRow(); - - if (is_array($row)) { - - $row = Encoder::encodeArray( Encoder::$in_charset, Encoder::$out_charset, $row ); + if ($this->hasHeaderRow) { + // если у файла есть заголовок, то в результате имеем ассоциативный массив + $this->converter_conf['hasKey'] = 1; } - return $row; + $this->converter = \Yii::createObject($this->converter_conf); } + } \ No newline at end of file diff --git a/backend/components/parsers/config.php b/backend/components/parsers/config.php new file mode 100644 index 0000000..6e14618 --- /dev/null +++ b/backend/components/parsers/config.php @@ -0,0 +1,46 @@ + + ['web' => + ['class' => 'backend\components\parsers\CustomCsvParser', + 'auto_detect_first_line' => true, + 'converter_conf' => ['class' => 'yii\multiparser\Converter', + 'configuration' => [ + "string" => 'DESCR' + ] + ,]], + + 'basic_column' => [ + Null => 'Пусто', + "BRAND" => 'Бренд', + "ARTICLE"=> 'Артикул', + "PRICE" => 'Цена', + "DESCR" => 'Наименование', + "BOX" => 'Колво', + "ADD_BOX"=> 'В пути', + "GROUP" => 'Группа RG' + ], + ], + 'xml' => + ['web' => + ['class' => 'yii\multiparser\XmlParser', + 'node' => 'Товар',], + + 'basic_column' => [ + "BRAND" => 'Производитель', + "ARTICLE"=> 'Код', + "PRICE" => 'Розница', + "DESCR" => 'Наименование', + "BOX" => 'Колво', + "ADD_BOX"=> 'Ожидаемое', + "GROUP" => 'Группа' + ], + ] + ]; + + +//[ +// "float" => 'PRICE', +// "integer" => ['BOX' , 'ADD_BOX' ], +// "prefix" => 'ARTICLE' +//] \ No newline at end of file diff --git a/backend/config/main.php b/backend/config/main.php index fcac464..386e300 100644 --- a/backend/config/main.php +++ b/backend/config/main.php @@ -5,6 +5,7 @@ $params = array_merge( require(__DIR__ . '/params.php'), require(__DIR__ . '/params-local.php') ); +$mp_configuration = require( __DIR__ . '/../components/parsers/config.php'); return [ 'id' => 'app-backend', @@ -41,6 +42,7 @@ return [ 'multiparser'=>[ 'class' => 'yii\multiparser\YiiMultiparser', + 'configuration' => $mp_configuration, ], ], diff --git a/backend/controllers/ParserController.php b/backend/controllers/ParserController.php index 310c228..eb6dab9 100644 --- a/backend/controllers/ParserController.php +++ b/backend/controllers/ParserController.php @@ -8,7 +8,7 @@ use yii\filters\VerbFilter; use backend\models\UploadFileParsingForm; use yii\web\UploadedFile; use yii\data\ArrayDataProvider; -use backend\components\parsers\DynamicFormHelper; +use yii\multiparser\DynamicFormHelper; use backend\components\parsers\CustomParserConfigurator; use common\components\CustomVarDamp; @@ -96,13 +96,14 @@ class ParserController extends BaseController ], ]); + // CustomVarDamp::dumpAndDie($data); $header_model = DynamicFormHelper::CreateDynamicModel( count( $data[0] ) ); - //CustomVarDamp::dumpAndDie($header_model); + // CustomVarDamp::dumpAndDie(Yii::$app->multiparser->getConfiguration('csv','basic_column')); return $this->render('results', ['model' => $data, 'header_model' => $header_model, - 'basic_column' => CustomParserConfigurator::$basic_column, + 'basic_column' => Yii::$app->multiparser->getConfiguration('csv','basic_column'), 'dataProvider' => $provider]); } @@ -113,12 +114,15 @@ public function actionWrite() $arr_attributes = Yii::$app->request->post()['DynamicModel']; $model = DynamicFormHelper::CreateDynamicModel( $arr_attributes ); foreach ($arr_attributes as $key => $value) { - $model->addRule($key, 'in', ['range' => array_keys( CustomParserConfigurator::$basic_column )]); + $model->addRule($key, 'in', ['range' => array_keys( Yii::$app->multiparser->getConfiguration('csv','basic_column') )]); } //CustomVarDamp::dumpAndDie($model); if ($model->validate()) { $arr = $model->toArray(); + $data = json_decode( Yii::$app->getCache()->get( 'parser_data' ),true ); + + // CustomVarDamp::dumpAndDie(DynamicFormHelper::CreateAssocArray($data, $arr)); CustomVarDamp::dumpAndDie($arr); } diff --git a/backend/models/UploadFileParsingForm.php b/backend/models/UploadFileParsingForm.php index 9377a59..ef94bc4 100644 --- a/backend/models/UploadFileParsingForm.php +++ b/backend/models/UploadFileParsingForm.php @@ -52,7 +52,6 @@ class UploadFileParsingForm extends Model public function readFile($filePath){ $data = Yii::$app->multiparser->parse($filePath); - // \common\components\CustomVarDamp::dumpAndDie($data); if( !is_array($data) ){ $data = ['No results']; } diff --git a/backend/views/parser/results.php b/backend/views/parser/results.php index 2f3c7d0..e0c6460 100644 --- a/backend/views/parser/results.php +++ b/backend/views/parser/results.php @@ -1,7 +1,7 @@ params['breadcrumbs'][] = $this->title; $form = ActiveForm::begin(['action' => 'write']); ?> - +
'btn btn-primary']) ?> diff --git a/backend/web/.htaccess b/backend/web/.htaccess new file mode 100644 index 0000000..fa96d7a --- /dev/null +++ b/backend/web/.htaccess @@ -0,0 +1,8 @@ +RewriteEngine on + +RewriteBase / + +RewriteCond %{REQUEST_FILENAME} !-d +RewriteCond %{REQUEST_FILENAME} !-f + +RewriteRule . index.php \ No newline at end of file diff --git a/common/components/CustomVarDamp.php b/common/components/CustomVarDamp.php index adb1aff..4d45831 100644 --- a/common/components/CustomVarDamp.php +++ b/common/components/CustomVarDamp.php @@ -17,10 +17,16 @@ class CustomVarDamp extends BaseVarDumper { echo ""; die; } - public static function dump($var, $depth = 10, $highlight = false) + public static function dump($var, $step = '', $depth = 10, $highlight = false) { echo "
";
+        if ($step) {
+            echo "-------------- {$step} -------------";
+        }
         echo static::dumpAsString($var, $depth, $highlight);
+        if ($step) {
+            echo "-------------- {$step} -------------";
+        }
         echo "
"; } 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 diff --git a/framework/core/Convert.php b/framework/core/Convert.php index 89a4367..bd1f645 100644 --- a/framework/core/Convert.php +++ b/framework/core/Convert.php @@ -166,63 +166,63 @@ class Convert { } /** - * Converts an XML string to a PHP array - * See http://phpsecurity.readthedocs.org/en/latest/Injection-Attacks.html#xml-external-entity-injection - * - * @uses recursiveXMLToArray() - * @param string $val - * @param boolean $disableDoctypes Disables the use of DOCTYPE, and will trigger an error if encountered. - * false by default. - * @param boolean $disableExternals Disables the loading of external entities. false by default. - * @return array - */ - public static function xml2array($val, $disableDoctypes = false, $disableExternals = false) { - // Check doctype - if($disableDoctypes && preg_match('/\<\!DOCTYPE.+]\>/', $val)) { - throw new InvalidArgumentException('XML Doctype parsing disabled'); - } + * Converts an XML string to a PHP array + * See http://phpsecurity.readthedocs.org/en/latest/Injection-Attacks.html#xml-external-entity-injection + * + * @uses recursiveXMLToArray() + * @param string $val + * @param boolean $disableDoctypes Disables the use of DOCTYPE, and will trigger an error if encountered. + * false by default. + * @param boolean $disableExternals Disables the loading of external entities. false by default. + * @return array + */ + public static function xml2array($val, $disableDoctypes = false, $disableExternals = false) { + // Check doctype + if($disableDoctypes && preg_match('/\<\!DOCTYPE.+]\>/', $val)) { + throw new InvalidArgumentException('XML Doctype parsing disabled'); + } - // Disable external entity loading - if($disableExternals) $oldVal = libxml_disable_entity_loader($disableExternals); - try { - $xml = new SimpleXMLElement($val); - $result = self::recursiveXMLToArray($xml); - } catch(Exception $ex) { - if($disableExternals) libxml_disable_entity_loader($oldVal); - throw $ex; - } - if($disableExternals) libxml_disable_entity_loader($oldVal); - return $result; - } + // Disable external entity loading + if($disableExternals) $oldVal = libxml_disable_entity_loader($disableExternals); + try { + $xml = new SimpleXMLElement($val); + $result = self::recursiveXMLToArray($xml); + } catch(Exception $ex) { + if($disableExternals) libxml_disable_entity_loader($oldVal); + throw $ex; + } + if($disableExternals) libxml_disable_entity_loader($oldVal); + return $result; + } - /** - * Convert a XML string to a PHP array recursively. Do not - * call this function directly, Please use {@link Convert::xml2array()} - * - * @param SimpleXMLElement - * - * @return mixed - */ - protected static function recursiveXMLToArray($xml) { - if(is_object($xml) && get_class($xml) == 'SimpleXMLElement') { - $attributes = $xml->attributes(); - foreach($attributes as $k => $v) { - if($v) $a[$k] = (string) $v; - } - $x = $xml; - $xml = get_object_vars($xml); - } - if(is_array($xml)) { - if(count($xml) == 0) return (string) $x; // for CDATA - foreach($xml as $key => $value) { - $r[$key] = self::recursiveXMLToArray($value); - } - if(isset($a)) $r['@'] = $a; // Attributes - return $r; - } - - return (string) $xml; - } + /** + * Convert a XML string to a PHP array recursively. Do not + * call this function directly, Please use {@link Convert::xml2array()} + * + * @param SimpleXMLElement + * + * @return mixed + */ + protected static function recursiveXMLToArray($xml) { + if(is_object($xml) && get_class($xml) == 'SimpleXMLElement') { + $attributes = $xml->attributes(); + foreach($attributes as $k => $v) { + if($v) $a[$k] = (string) $v; + } + $x = $xml; + $xml = get_object_vars($xml); + } + if(is_array($xml)) { + if(count($xml) == 0) return (string) $x; // for CDATA + foreach($xml as $key => $value) { + $r[$key] = self::recursiveXMLToArray($value); + } + if(isset($a)) $r['@'] = $a; // Attributes + return $r; + } + + return (string) $xml; + } /** * Create a link if the string is a valid URL diff --git a/frontend/web/.htaccess b/frontend/web/.htaccess new file mode 100644 index 0000000..35a2d28 --- /dev/null +++ b/frontend/web/.htaccess @@ -0,0 +1,8 @@ +RewriteEngine on +RewriteBase / +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d + +RewriteRule . index.php + +Options -Indexes \ No newline at end of file -- libgit2 0.21.4