Commit 0c6fd004d8739f106a58f584661e1e5327958b34
1 parent
42a252c2
fixed issue with accidently delete method in xlsx parser
Showing
1 changed file
with
74 additions
and
11 deletions
Show diff stats
lib/XlsxParser.php
@@ -10,7 +10,6 @@ namespace yii\multiparser; | @@ -10,7 +10,6 @@ namespace yii\multiparser; | ||
10 | 10 | ||
11 | 11 | ||
12 | 12 | ||
13 | - | ||
14 | /** | 13 | /** |
15 | * Class XlsxParser | 14 | * Class XlsxParser |
16 | * @package yii\multiparser | 15 | * @package yii\multiparser |
@@ -48,34 +47,53 @@ class XlsxParser extends TableParser { | @@ -48,34 +47,53 @@ class XlsxParser extends TableParser { | ||
48 | 47 | ||
49 | public function read() | 48 | public function read() |
50 | { | 49 | { |
51 | - | ||
52 | $this->extractFiles(); | 50 | $this->extractFiles(); |
51 | + | ||
53 | $this->readSheets(); | 52 | $this->readSheets(); |
54 | $this->readStrings(); | 53 | $this->readStrings(); |
55 | 54 | ||
56 | foreach ( $this->sheets_arr as $sheet ) { | 55 | foreach ( $this->sheets_arr as $sheet ) { |
57 | //проходим по всем файлам из директории /xl/worksheets/ | 56 | //проходим по всем файлам из директории /xl/worksheets/ |
58 | - | 57 | + $this->current_sheet = $sheet; |
59 | $sheet_path = $this->path_for_extract_files . '/xl/worksheets/' . $sheet . '.xml'; | 58 | $sheet_path = $this->path_for_extract_files . '/xl/worksheets/' . $sheet . '.xml'; |
60 | if ( file_exists( $sheet_path ) && is_readable( $sheet_path ) ) { | 59 | if ( file_exists( $sheet_path ) && is_readable( $sheet_path ) ) { |
61 | 60 | ||
62 | $xml = simplexml_load_file( $sheet_path, "SimpleXMLIterator" ); | 61 | $xml = simplexml_load_file( $sheet_path, "SimpleXMLIterator" ); |
63 | $this->current_node = $xml->sheetData->row; | 62 | $this->current_node = $xml->sheetData->row; |
64 | $this->current_node->rewind(); | 63 | $this->current_node->rewind(); |
64 | + if ( $this->current_node->valid() ) { | ||
65 | + | ||
66 | + parent::read(); | ||
65 | 67 | ||
66 | - parent::read(); | 68 | + } |
67 | 69 | ||
68 | } | 70 | } |
69 | 71 | ||
70 | } | 72 | } |
71 | - return $this->$result; | 73 | + |
74 | + | ||
75 | + if ( $this->active_sheet ) { | ||
76 | + | ||
77 | + // в настройках указан конкретный лист с которогшо будем производить чтение, поэтому и возвращаем подмассив | ||
78 | + return $this->result[ $this->current_sheet ]; | ||
79 | + }else{ | ||
80 | + return $this->result; | ||
81 | + } | ||
82 | + | ||
72 | } | 83 | } |
73 | 84 | ||
74 | protected function extractFiles () | 85 | protected function extractFiles () |
75 | { | 86 | { |
87 | + $this->path_for_extract_files = $this->path_for_extract_files . session_id(); | ||
88 | + if ( !mkdir($this->path_for_extract_files) ) | ||
89 | + { | ||
90 | + throw new \Exception( 'Ошибка создания временного каталога - ' . $this->path_for_extract_files ); | ||
91 | + } | ||
92 | + | ||
93 | + | ||
76 | $zip = new \ZipArchive; | 94 | $zip = new \ZipArchive; |
77 | if ( $zip->open( $this->file->getPathname() ) === TRUE ) { | 95 | if ( $zip->open( $this->file->getPathname() ) === TRUE ) { |
78 | - $zip->extractTo( $this->path_for_extract_files ); | 96 | + $zip->extractTo( $this->path_for_extract_files . '/' ); |
79 | $zip->close(); | 97 | $zip->close(); |
80 | } else { | 98 | } else { |
81 | throw new \Exception( 'Ошибка чтения xlsx файла' ); | 99 | throw new \Exception( 'Ошибка чтения xlsx файла' ); |
@@ -85,7 +103,7 @@ class XlsxParser extends TableParser { | @@ -85,7 +103,7 @@ class XlsxParser extends TableParser { | ||
85 | protected function readSheets () | 103 | protected function readSheets () |
86 | { | 104 | { |
87 | if ( $this->active_sheet ) { | 105 | if ( $this->active_sheet ) { |
88 | - $this->sheets_arr[ $this->active_sheet ] = 'Sheet' . $this->active_sheet; | 106 | + $this->sheets_arr[ ] = 'Sheet' . $this->active_sheet; |
89 | return; | 107 | return; |
90 | } | 108 | } |
91 | 109 | ||
@@ -118,11 +136,18 @@ class XlsxParser extends TableParser { | @@ -118,11 +136,18 @@ class XlsxParser extends TableParser { | ||
118 | } | 136 | } |
119 | 137 | ||
120 | 138 | ||
139 | + | ||
140 | + // protected function readRow ( $item, $sheet , $current_row ) | ||
121 | protected function readRow ( ) | 141 | protected function readRow ( ) |
122 | { | 142 | { |
143 | + $this->row = []; | ||
123 | $node = $this->current_node->getChildren(); | 144 | $node = $this->current_node->getChildren(); |
124 | - | ||
125 | - foreach ( $node as $child ) { | 145 | + if ($node === NULL) { |
146 | + return; | ||
147 | + } | ||
148 | + //foreach ( $node as $child ) { | ||
149 | + for ( $node->rewind(); $node->valid(); $node->next() ) { | ||
150 | + $child = $node->current(); | ||
126 | $attr = $child->attributes(); | 151 | $attr = $child->attributes(); |
127 | 152 | ||
128 | if( isset($child->v) ) { | 153 | if( isset($child->v) ) { |
@@ -131,14 +156,19 @@ class XlsxParser extends TableParser { | @@ -131,14 +156,19 @@ class XlsxParser extends TableParser { | ||
131 | $value = ''; | 156 | $value = ''; |
132 | } | 157 | } |
133 | if ( isset( $attr['t'] ) ) { | 158 | if ( isset( $attr['t'] ) ) { |
134 | - // $this->result_arr[$sheet][$current_row][$cell] = $this->strings_arr[ $value ]; | ||
135 | $this->row[] = $this->strings_arr[ $value ]; | 159 | $this->row[] = $this->strings_arr[ $value ]; |
136 | }else{ | 160 | }else{ |
137 | - // $this->result_arr[$sheet][$current_row][$cell] = $value; | ||
138 | $this->row[] = $value; | 161 | $this->row[] = $value; |
139 | } | 162 | } |
140 | 163 | ||
141 | } | 164 | } |
165 | + // дополним ряд пустыми значениями если у нас ключей больше чем значений | ||
166 | + if ( $this->has_header_row && ( count( $this->keys ) > count( $this->row ) ) ) { | ||
167 | + $extra_coloumn = count( $this->keys ) - count( $this->row ); | ||
168 | + for ( $i = 1; $i <= $extra_coloumn; $i++ ) { | ||
169 | + $this->row[] = ''; | ||
170 | + } | ||
171 | + } | ||
142 | $this->current_node->next(); | 172 | $this->current_node->next(); |
143 | } | 173 | } |
144 | 174 | ||
@@ -169,4 +199,37 @@ class XlsxParser extends TableParser { | @@ -169,4 +199,37 @@ class XlsxParser extends TableParser { | ||
169 | protected function isEmptyColumn( $val ){ | 199 | protected function isEmptyColumn( $val ){ |
170 | return $val == ''; | 200 | return $val == ''; |
171 | } | 201 | } |
202 | + | ||
203 | + protected function setResult( ){ | ||
204 | + $this->result[ $this->current_sheet ][] = $this->row; | ||
205 | + } | ||
206 | + | ||
207 | + protected function deleteExtractFiles () | ||
208 | + { | ||
209 | + $this->removeDir( $this->path_for_extract_files ); | ||
210 | + | ||
211 | + } | ||
212 | + | ||
213 | + protected function removeDir($dir) { | ||
214 | + if (is_dir($dir)) { | ||
215 | + $objects = scandir($dir); | ||
216 | + foreach ($objects as $object) { | ||
217 | + if ($object != "." && $object != "..") { | ||
218 | + if (filetype($dir."/".$object) == "dir") | ||
219 | + $this->removeDir($dir."/".$object); | ||
220 | + else | ||
221 | + unlink($dir."/".$object); | ||
222 | + } | ||
223 | + } | ||
224 | + reset($objects); | ||
225 | + rmdir($dir); | ||
226 | + } | ||
227 | + } | ||
228 | + | ||
229 | + function __destruct() | ||
230 | + { | ||
231 | + $this->deleteExtractFiles(); | ||
232 | + } | ||
233 | + | ||
234 | + | ||
172 | } | 235 | } |
173 | \ No newline at end of file | 236 | \ No newline at end of file |