-
Notifications
You must be signed in to change notification settings - Fork 498
/
SpreadsheetReader_ODS.php
339 lines (296 loc) · 8 KB
/
SpreadsheetReader_ODS.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* Class for parsing ODS files
*
* @author Martins Pilsetnieks
*/
class SpreadsheetReader_ODS implements Iterator, Countable
{
private $Options = array(
'TempDir' => '',
'ReturnDateTimeObjects' => false
);
/**
* @var string Path to temporary content file
*/
private $ContentPath = '';
/**
* @var XMLReader XML reader object
*/
private $Content = false;
/**
* @var array Data about separate sheets in the file
*/
private $Sheets = false;
private $CurrentRow = null;
/**
* @var int Number of the sheet we're currently reading
*/
private $CurrentSheet = 0;
private $Index = 0;
private $TableOpen = false;
private $RowOpen = false;
/**
* @param string Path to file
* @param array Options:
* TempDir => string Temporary directory path
* ReturnDateTimeObjects => bool True => dates and times will be returned as PHP DateTime objects, false => as strings
*/
public function __construct($Filepath, array $Options = null)
{
if (!is_readable($Filepath))
{
throw new Exception('SpreadsheetReader_ODS: File not readable ('.$Filepath.')');
}
$this -> TempDir = isset($Options['TempDir']) && is_writable($Options['TempDir']) ?
$Options['TempDir'] :
sys_get_temp_dir();
$this -> TempDir = rtrim($this -> TempDir, DIRECTORY_SEPARATOR);
$this -> TempDir = $this -> TempDir.DIRECTORY_SEPARATOR.uniqid().DIRECTORY_SEPARATOR;
$Zip = new ZipArchive;
$Status = $Zip -> open($Filepath);
if ($Status !== true)
{
throw new Exception('SpreadsheetReader_ODS: File not readable ('.$Filepath.') (Error '.$Status.')');
}
if ($Zip -> locateName('content.xml') !== false)
{
$Zip -> extractTo($this -> TempDir, 'content.xml');
$this -> ContentPath = $this -> TempDir.'content.xml';
}
$Zip -> close();
if ($this -> ContentPath && is_readable($this -> ContentPath))
{
$this -> Content = new XMLReader;
$this -> Content -> open($this -> ContentPath);
$this -> Valid = true;
}
}
/**
* Destructor, destroys all that remains (closes and deletes temp files)
*/
public function __destruct()
{
if ($this -> Content && $this -> Content instanceof XMLReader)
{
$this -> Content -> close();
unset($this -> Content);
}
if (file_exists($this -> ContentPath))
{
@unlink($this -> ContentPath);
unset($this -> ContentPath);
}
}
/**
* Retrieves an array with information about sheets in the current file
*
* @return array List of sheets (key is sheet index, value is name)
*/
public function Sheets()
{
if ($this -> Sheets === false)
{
$this -> Sheets = array();
if ($this -> Valid)
{
$this -> SheetReader = new XMLReader;
$this -> SheetReader -> open($this -> ContentPath);
while ($this -> SheetReader -> read())
{
if ($this -> SheetReader -> name == 'table:table')
{
$this -> Sheets[] = $this -> SheetReader -> getAttribute('table:name');
$this -> SheetReader -> next();
}
}
$this -> SheetReader -> close();
}
}
return $this -> Sheets;
}
/**
* Changes the current sheet in the file to another
*
* @param int Sheet index
*
* @return bool True if sheet was successfully changed, false otherwise.
*/
public function ChangeSheet($Index)
{
$Index = (int)$Index;
$Sheets = $this -> Sheets();
if (isset($Sheets[$Index]))
{
$this -> CurrentSheet = $Index;
$this -> rewind();
return true;
}
return false;
}
// !Iterator interface methods
/**
* Rewind the Iterator to the first element.
* Similar to the reset() function for arrays in PHP
*/
public function rewind()
{
if ($this -> Index > 0)
{
// If the worksheet was already iterated, XML file is reopened.
// Otherwise it should be at the beginning anyway
$this -> Content -> close();
$this -> Content -> open($this -> ContentPath);
$this -> Valid = true;
$this -> TableOpen = false;
$this -> RowOpen = false;
$this -> CurrentRow = null;
}
$this -> Index = 0;
}
/**
* Return the current element.
* Similar to the current() function for arrays in PHP
*
* @return mixed current element from the collection
*/
public function current()
{
if ($this -> Index == 0 && is_null($this -> CurrentRow))
{
$this -> next();
$this -> Index--;
}
return $this -> CurrentRow;
}
/**
* Move forward to next element.
* Similar to the next() function for arrays in PHP
*/
public function next()
{
$this -> Index++;
$this -> CurrentRow = array();
if (!$this -> TableOpen)
{
$TableCounter = 0;
$SkipRead = false;
while ($this -> Valid = ($SkipRead || $this -> Content -> read()))
{
if ($SkipRead)
{
$SkipRead = false;
}
if ($this -> Content -> name == 'table:table' && $this -> Content -> nodeType != XMLReader::END_ELEMENT)
{
if ($TableCounter == $this -> CurrentSheet)
{
$this -> TableOpen = true;
break;
}
$TableCounter++;
$this -> Content -> next();
$SkipRead = true;
}
}
}
if ($this -> TableOpen && !$this -> RowOpen)
{
while ($this -> Valid = $this -> Content -> read())
{
switch ($this -> Content -> name)
{
case 'table:table':
$this -> TableOpen = false;
$this -> Content -> next('office:document-content');
$this -> Valid = false;
break 2;
case 'table:table-row':
if ($this -> Content -> nodeType != XMLReader::END_ELEMENT)
{
$this -> RowOpen = true;
break 2;
}
break;
}
}
}
if ($this -> RowOpen)
{
$LastCellContent = '';
while ($this -> Valid = $this -> Content -> read())
{
switch ($this -> Content -> name)
{
case 'table:table-cell':
if ($this -> Content -> nodeType == XMLReader::END_ELEMENT || $this -> Content -> isEmptyElement)
{
if ($this -> Content -> nodeType == XMLReader::END_ELEMENT)
{
$CellValue = $LastCellContent;
}
elseif ($this -> Content -> isEmptyElement)
{
$LastCellContent = '';
$CellValue = $LastCellContent;
}
$this -> CurrentRow[] = $LastCellContent;
if ($this -> Content -> getAttribute('table:number-columns-repeated') !== null)
{
$RepeatedColumnCount = $this -> Content -> getAttribute('table:number-columns-repeated');
// Checking if larger than one because the value is already added to the row once before
if ($RepeatedColumnCount > 1)
{
$this -> CurrentRow = array_pad($this -> CurrentRow, count($this -> CurrentRow) + $RepeatedColumnCount - 1, $LastCellContent);
}
}
}
else
{
$LastCellContent = '';
}
case 'text:p':
if ($this -> Content -> nodeType != XMLReader::END_ELEMENT)
{
$LastCellContent = $this -> Content -> readString();
}
break;
case 'table:table-row':
$this -> RowOpen = false;
break 2;
}
}
}
return $this -> CurrentRow;
}
/**
* Return the identifying key of the current element.
* Similar to the key() function for arrays in PHP
*
* @return mixed either an integer or a string
*/
public function key()
{
return $this -> Index;
}
/**
* Check if there is a current element after calls to rewind() or next().
* Used to check if we've iterated to the end of the collection
*
* @return boolean FALSE if there's nothing more to iterate over
*/
public function valid()
{
return $this -> Valid;
}
// !Countable interface method
/**
* Ostensibly should return the count of the contained items but this just returns the number
* of rows read so far. It's not really correct but at least coherent.
*/
public function count()
{
return $this -> Index + 1;
}
}
?>