diff --git a/backend/components/parsers/CsvParser.php b/backend/components/parsers/CsvParser.php index b024550..1ea01c6 100644 --- a/backend/components/parsers/CsvParser.php +++ b/backend/components/parsers/CsvParser.php @@ -13,7 +13,8 @@ use Yii; use yii\base\ErrorException; use common\components\debug\CustomVarDamp; -class CsvParser { +class CsvParser +{ /** @var bool */ @@ -28,6 +29,7 @@ class CsvParser { public $in_charset = 'windows-1251'; /** @var int - first line for parsing */ public $first_line = 0; + public $last_line = 10; /** @var int - first column for parsing */ public $first_column = 0; @@ -42,40 +44,51 @@ class CsvParser { public function setup() { + $this->file->setCsvControl($this->delimiter); + $this->file->setFlags(\SplFileObject::READ_CSV); + $this->file->setFlags(\SplFileObject::SKIP_EMPTY); +// $this->file->setFlags(\SplFileObject::READ_AHEAD); + if ($this->auto_detect_start_position) { $this->first_line = $this->detectStartPosition(); } - CustomVarDamp::dumpAndDie( $this->first_line ); - $this->file->setCsvControl($this->$delimiter); - $this->file->setFlags(\SplFileObject::READ_CSV); - $this->file->seek( $this->first_line ); + // CustomVarDamp::dumpAndDie($this); +// echo $this->file->key(); +// $this->file->seek($this->first_line + 1); +// echo $this->file->key(); } - - protected function detectStartPosition () + protected function detectStartPosition() { - $first_column = 0; + $first_line = 0; $find = false; while (!$find) { - $j = 0; - $row = $this->readRow(); - $first_column++; - for ($i = 1; $i<=count($row); $i++) { - if (!$row[$i]) { - $j++; + $j = 0; + $row = $this->readRow(); + + if ($row === false) { + continue; } - if ( $j >= $this->min_column_quantity ) { - $find = true; - break; + + $first_line++; + for ($i = 1; $i <= count($row); $i++) { + + if ($row[$i - 1] <> '') { + $j++; + } + + if ($j >= $this->min_column_quantity) { + $find = true; + break; + } } } - } - return $first_column; + return $first_line; } @@ -87,10 +100,10 @@ class CsvParser { { // @todo add comments $return = []; - + //CustomVarDamp::dump(debug_print_backtrace(1,2)); $line = 0; $this->keys = NULL; - + CustomVarDamp::dump($this->file->key()); while (($row = $this->readRow()) !== FALSE) { $line++; @@ -110,10 +123,16 @@ class CsvParser { } else { $return[] = $row; } + if(($this->last_line) && ($line > $this->last_line)){ +// CustomVarDamp::dump($this->last_line); +// CustomVarDamp::dump($line); + break; + } + } $this->closeHandler(); - + //CustomVarDamp::dumpAndDie($return); return $return; } @@ -127,16 +146,14 @@ class CsvParser { // @todo add comments { - $row = $this->file->fgetcsv( ); + $row = $this->file->fgetcsv(); // if (is_array($row)) { - $row = array_slice( $row, $this->first_column ); - $row = Encoder::encodeArray( $this->in_charset, $this->out_charset, $row ); - } else{ - $row = false; + // $row = array_slice( $row, $this->first_column ); + $row = Encoder::encodeArray($this->in_charset, $this->out_charset, $row); } - - + if (is_null($row)) + $row = false; // if ($this->keys !== NULL) // @$clear_arr[3] = ValueFilter::pricefilter($clear_arr[3]);{}{}{} diff --git a/backend/components/parsers/ParserHandler.php b/backend/components/parsers/ParserHandler.php index 1cbea46..86d6859 100644 --- a/backend/components/parsers/ParserHandler.php +++ b/backend/components/parsers/ParserHandler.php @@ -35,7 +35,6 @@ class ParserHandler { //preg_match( '/\.[^\.]+$/i',$filePath, $resultArray ); $this->extension = $this->fileObject->getExtension(); - $this->run(); } public function run(){ @@ -46,12 +45,14 @@ class ParserHandler { $csvParser = Yii::createObject([ 'class' => 'backend\components\parsers\CsvParser', 'file' => $this->fileObject, + 'auto_detect_start_position' => true, ]); //CustomVarDamp::dumpAndDie($csvParser); - $csvParser = new CsvParser( ); - $csvParser->setup( $this->fileObject, $first_line, $first_column ); + // $csvParser = new CsvParser( ); + $csvParser->setup( ); - return $csvParser->read(); +// CustomVarDamp::dumpAndDie($data); + return $csvParser->read();;// }; } } diff --git a/backend/views/parser/results.php b/backend/views/parser/results.php index ac8e56d..82d9531 100644 --- a/backend/views/parser/results.php +++ b/backend/views/parser/results.php @@ -22,9 +22,14 @@ $this->params['breadcrumbs'][] = $this->title; 'dataProvider' => $dataProvider, 'columns' => [['class' => SerialColumn::className()], '1', - '2',] + '2', + '3', + '4', + '5',] ]); ?> + + 'btn btn-primary', 'name' => 'Return',]) ?> \ No newline at end of file diff --git a/common/components/debug/CustomVarDamp.php b/common/components/debug/CustomVarDamp.php index 76d1a19..7bdcb78 100644 --- a/common/components/debug/CustomVarDamp.php +++ b/common/components/debug/CustomVarDamp.php @@ -17,4 +17,11 @@ class CustomVarDamp extends BaseVarDumper { echo ""; die; } + public static function dump($var, $depth = 10, $highlight = false) + { + echo "
";
+        echo static::dumpAsString($var, $depth, $highlight);
+        echo "
"; + + } } \ No newline at end of file -- libgit2 0.21.4