From 0c6fd004d8739f106a58f584661e1e5327958b34 Mon Sep 17 00:00:00 2001 From: Mihail Date: Wed, 28 Oct 2015 11:28:22 +0200 Subject: [PATCH] fixed issue with accidently delete method in xlsx parser --- lib/XlsxParser.php | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 74 insertions(+), 11 deletions(-) diff --git a/lib/XlsxParser.php b/lib/XlsxParser.php index 17eaccc..cf5e88d 100644 --- a/lib/XlsxParser.php +++ b/lib/XlsxParser.php @@ -10,7 +10,6 @@ namespace yii\multiparser; - /** * Class XlsxParser * @package yii\multiparser @@ -48,34 +47,53 @@ class XlsxParser extends TableParser { public function read() { - $this->extractFiles(); + $this->readSheets(); $this->readStrings(); foreach ( $this->sheets_arr as $sheet ) { //проходим по всем файлам из директории /xl/worksheets/ - + $this->current_sheet = $sheet; $sheet_path = $this->path_for_extract_files . '/xl/worksheets/' . $sheet . '.xml'; if ( file_exists( $sheet_path ) && is_readable( $sheet_path ) ) { $xml = simplexml_load_file( $sheet_path, "SimpleXMLIterator" ); $this->current_node = $xml->sheetData->row; $this->current_node->rewind(); + if ( $this->current_node->valid() ) { + + parent::read(); - parent::read(); + } } } - return $this->$result; + + + if ( $this->active_sheet ) { + + // в настройках указан конкретный лист с которогшо будем производить чтение, поэтому и возвращаем подмассив + return $this->result[ $this->current_sheet ]; + }else{ + return $this->result; + } + } protected function extractFiles () { + $this->path_for_extract_files = $this->path_for_extract_files . session_id(); + if ( !mkdir($this->path_for_extract_files) ) + { + throw new \Exception( 'Ошибка создания временного каталога - ' . $this->path_for_extract_files ); + } + + $zip = new \ZipArchive; if ( $zip->open( $this->file->getPathname() ) === TRUE ) { - $zip->extractTo( $this->path_for_extract_files ); + $zip->extractTo( $this->path_for_extract_files . '/' ); $zip->close(); } else { throw new \Exception( 'Ошибка чтения xlsx файла' ); @@ -85,7 +103,7 @@ class XlsxParser extends TableParser { protected function readSheets () { if ( $this->active_sheet ) { - $this->sheets_arr[ $this->active_sheet ] = 'Sheet' . $this->active_sheet; + $this->sheets_arr[ ] = 'Sheet' . $this->active_sheet; return; } @@ -118,11 +136,18 @@ class XlsxParser extends TableParser { } + + // protected function readRow ( $item, $sheet , $current_row ) protected function readRow ( ) { + $this->row = []; $node = $this->current_node->getChildren(); - - foreach ( $node as $child ) { + if ($node === NULL) { + return; + } + //foreach ( $node as $child ) { + for ( $node->rewind(); $node->valid(); $node->next() ) { + $child = $node->current(); $attr = $child->attributes(); if( isset($child->v) ) { @@ -131,14 +156,19 @@ class XlsxParser extends TableParser { $value = ''; } if ( isset( $attr['t'] ) ) { - // $this->result_arr[$sheet][$current_row][$cell] = $this->strings_arr[ $value ]; $this->row[] = $this->strings_arr[ $value ]; }else{ - // $this->result_arr[$sheet][$current_row][$cell] = $value; $this->row[] = $value; } } + // дополним ряд пустыми значениями если у нас ключей больше чем значений + if ( $this->has_header_row && ( count( $this->keys ) > count( $this->row ) ) ) { + $extra_coloumn = count( $this->keys ) - count( $this->row ); + for ( $i = 1; $i <= $extra_coloumn; $i++ ) { + $this->row[] = ''; + } + } $this->current_node->next(); } @@ -169,4 +199,37 @@ class XlsxParser extends TableParser { protected function isEmptyColumn( $val ){ return $val == ''; } + + protected function setResult( ){ + $this->result[ $this->current_sheet ][] = $this->row; + } + + protected function deleteExtractFiles () + { + $this->removeDir( $this->path_for_extract_files ); + + } + + protected function removeDir($dir) { + if (is_dir($dir)) { + $objects = scandir($dir); + foreach ($objects as $object) { + if ($object != "." && $object != "..") { + if (filetype($dir."/".$object) == "dir") + $this->removeDir($dir."/".$object); + else + unlink($dir."/".$object); + } + } + reset($objects); + rmdir($dir); + } + } + + function __destruct() + { + $this->deleteExtractFiles(); + } + + } \ No newline at end of file -- libgit2 0.21.4