Blame view

vendor/yiisoft/multiparser/XlsxParser.php 4.74 KB
3c4b566f   Mihail   add XlsxParser
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  <?php
  /**
   * Created by PhpStorm.
   * User: Tsurkanov
   * Date: 21.10.2015
   * Time: 15:44
   */
  
  namespace yii\multiparser;
  
  
  use common\components\CustomVarDamp;
  
  
95c167d6   Mihail   add managing with...
15
16
17
18
  /**
   * Class XlsxParser
   * @package yii\multiparser
   */
9d9d876f   Mihail   add xlsx parser i...
19
  class XlsxParser extends TableParser {
95c167d6   Mihail   add managing with...
20
21
22
23
  
      /**
       * @var string - путь куда будут распаковываться файлы, если не указанно - во временный каталог сервера
       */
3c4b566f   Mihail   add XlsxParser
24
      public $path_for_extract_files = '';
9d9d876f   Mihail   add xlsx parser i...
25
  
95c167d6   Mihail   add managing with...
26
27
28
29
30
31
32
33
34
  
      /**
       * @var int - если указано то считывание будет производиться с этого листа, иначе со всех листов
       * при чтении со всех листов - выходной массив будет иметь номера листов первыми элементами
       */
      public $active_sheet = 0;
  
      protected $strings_arr = [];
      protected $sheets_arr = [];
9d9d876f   Mihail   add xlsx parser i...
35
36
37
  
      protected $current_node;
      protected $current_sheet;
3c4b566f   Mihail   add XlsxParser
38
39
40
  
      public function setup()
      {
9d9d876f   Mihail   add xlsx parser i...
41
  
3c4b566f   Mihail   add XlsxParser
42
          parent::setup();
9d9d876f   Mihail   add xlsx parser i...
43
  
3c4b566f   Mihail   add XlsxParser
44
45
46
47
48
49
50
51
          if ( $this->path_for_extract_files == '' ) {
              $this->path_for_extract_files = sys_get_temp_dir();
          }
      }
  
  
      public function read()
      {
3c4b566f   Mihail   add XlsxParser
52
  
95c167d6   Mihail   add managing with...
53
54
55
56
   //       $this->extractFiles();
  
          $this->readSheets();
          $this->readStrings();
95c167d6   Mihail   add managing with...
57
  
9d9d876f   Mihail   add xlsx parser i...
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
          foreach ( $this->sheets_arr  as $sheet ) {
              //проходим по всем файлам из директории /xl/worksheets/
  
              $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();
  
                  parent::read();
  
              }
  
          }
  CustomVarDamp::dumpAndDie($this->$result);
95c167d6   Mihail   add managing with...
74
         // return $this->$result_arr;
3c4b566f   Mihail   add XlsxParser
75
76
77
78
79
80
81
82
83
      }
  
      protected function extractFiles ()
      {
          $zip = new \ZipArchive;
          if ( $zip->open( $this->file->getPathname() ) === TRUE ) {
              $zip->extractTo( $this->path_for_extract_files );
              $zip->close();
          } else {
95c167d6   Mihail   add managing with...
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
              throw new \Exception( 'Ошибка чтения xlsx файла' );
          }
      }
  
      protected function readSheets ()
      {
          if ( $this->active_sheet ) {
              $this->sheets_arr[ $this->active_sheet ] = 'Sheet' . $this->active_sheet;
              return;
          }
  
          $xml = simplexml_load_file( $this->path_for_extract_files . '/xl/workbook.xml' );
          foreach ( $xml->sheets->children() as $sheet ) {
              $sheet_name = '';
              $sheet_id = 0;
              $attr = $sheet->attributes();
              foreach ( $attr as $name => $value ) {
                  if ($name == 'name')
                      $sheet_name = (string)$value;
  
                  if ($name == 'sheetId')
                      $sheet_id = $value;
  
              }
              if ( $sheet_name && $sheet_id ) {
                  $this->sheets_arr[$sheet_name] = 'Sheet' . $sheet_id;
              }
  //
          }
      }
  
      protected function readStrings ()
      {
          $xml = simplexml_load_file( $this->path_for_extract_files . '/xl/sharedStrings.xml' );
          foreach ( $xml->children() as $item ) {
              $this->strings_arr[] = (string)$item->t;
3c4b566f   Mihail   add XlsxParser
120
121
122
          }
      }
  
95c167d6   Mihail   add managing with...
123
  
9d9d876f   Mihail   add xlsx parser i...
124
125
126
  
     // protected function readRow ( $item, $sheet , $current_row )
      protected function readRow ( )
95c167d6   Mihail   add managing with...
127
      {
9d9d876f   Mihail   add xlsx parser i...
128
129
130
          $node = $this->current_node->getChildren();
  
          foreach ( $node as $child ) {
95c167d6   Mihail   add managing with...
131
132
133
134
135
              $attr = $child->attributes();
  
              if( isset($child->v) ) {
                  $value = (string)$child->v;
              }else{
9d9d876f   Mihail   add xlsx parser i...
136
                  $value = '';
95c167d6   Mihail   add managing with...
137
138
              }
              if ( isset( $attr['t'] ) ) {
9d9d876f   Mihail   add xlsx parser i...
139
140
                //  $this->result_arr[$sheet][$current_row][$cell] =  $this->strings_arr[ $value ];
                  $this->row[] =  $this->strings_arr[ $value ];
95c167d6   Mihail   add managing with...
141
              }else{
9d9d876f   Mihail   add xlsx parser i...
142
143
               //   $this->result_arr[$sheet][$current_row][$cell] =  $value;
                  $this->row[] =  $value;
95c167d6   Mihail   add managing with...
144
              }
9d9d876f   Mihail   add xlsx parser i...
145
  
95c167d6   Mihail   add managing with...
146
          }
9d9d876f   Mihail   add xlsx parser i...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
          $this->current_node->next();
          CustomVarDamp::dump($this->row);
      }
  
      protected  function isEmptyRow(){
  
          $is_empty = false;
  
          if ( !count( $this->row ) || !$this->current_node->valid() ) {
              return true;
          }
  
          $j = 0;
          for ($i = 1; $i <= count( $this->row ); $i++) {
  
              if ( $this->isEmptyColumn( $this->row[$i - 1] ) ) {
                  $j++;
              }
  
              if ( $j >= $this->min_column_quantity ) {
                  $is_empty = true;
                  break;
              }
          }
  
          return $is_empty;
      }
95c167d6   Mihail   add managing with...
174
  
9d9d876f   Mihail   add xlsx parser i...
175
176
      protected  function isEmptyColumn( $val ){
          return $val == '';
3c4b566f   Mihail   add XlsxParser
177
178
      }
  }