1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://www.gnu.org/licenses/lgpl.html |
5
|
|
|
* @copyright Metaways Infosystems GmbH, 2013 |
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2018 |
7
|
|
|
* @package MW |
8
|
|
|
* @subpackage Container |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Aimeos\MW\Container; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Implementation of PHPExcel containers. |
17
|
|
|
* |
18
|
|
|
* @package MW |
19
|
|
|
* @subpackage Container |
20
|
|
|
*/ |
21
|
|
|
class PHPExcel |
22
|
|
|
extends \Aimeos\MW\Container\Base |
23
|
|
|
implements \Aimeos\MW\Container\Iface |
24
|
|
|
{ |
25
|
|
|
private $container; |
26
|
|
|
private $format; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Opens an existing container or creates a new one. |
31
|
|
|
* |
32
|
|
|
* @param string $resourcepath Path to the resource like a file |
33
|
|
|
* @param string $format Format of the content objects inside the container |
34
|
|
|
* @param array $options Associative list of key/value pairs for configuration |
35
|
|
|
*/ |
36
|
|
|
public function __construct( $resourcepath, $format, array $options = [] ) |
37
|
|
|
{ |
38
|
|
|
if( file_exists( $resourcepath ) ) |
39
|
|
|
{ |
40
|
|
|
$type = \PHPExcel_IOFactory::identify( $resourcepath ); |
41
|
|
|
$reader = \PHPExcel_IOFactory::createReader( $type ); |
42
|
|
|
$this->container = $reader->load( $resourcepath ); |
43
|
|
|
} |
44
|
|
|
else |
45
|
|
|
{ |
46
|
|
|
$this->container = new \PHPExcel(); |
47
|
|
|
$this->container->removeSheetByIndex( 0 ); |
48
|
|
|
|
49
|
|
|
switch( $format ) |
50
|
|
|
{ |
51
|
|
|
case 'Excel5': |
52
|
|
|
$resourcepath .= '.xls'; |
53
|
|
|
break; |
54
|
|
|
case 'Excel2003XML': |
55
|
|
|
$resourcepath .= '.xml'; |
56
|
|
|
break; |
57
|
|
|
case 'Excel2007': |
58
|
|
|
$resourcepath .= '.xlsx'; |
59
|
|
|
break; |
60
|
|
|
case 'OOCalc': |
61
|
|
|
$resourcepath .= '.ods'; |
62
|
|
|
break; |
63
|
|
|
case 'SYLK': |
64
|
|
|
$resourcepath .= '.slk'; |
65
|
|
|
break; |
66
|
|
|
case 'Gnumeric': |
67
|
|
|
$resourcepath .= '.gnumeric'; |
68
|
|
|
break; |
69
|
|
|
case 'CSV': |
70
|
|
|
$resourcepath .= '.csv'; |
71
|
|
|
break; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
parent::__construct( $resourcepath, $options ); |
76
|
|
|
|
77
|
|
|
$this->iterator = $this->container->getWorksheetIterator(); |
|
|
|
|
78
|
|
|
|
79
|
|
|
$this->resourcepath = $resourcepath; |
|
|
|
|
80
|
|
|
$this->format = $format; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Creates a new content object. |
86
|
|
|
* |
87
|
|
|
* @param string $name Name of the content |
88
|
|
|
* @return \Aimeos\MW\Container\Content\Iface New content object |
89
|
|
|
*/ |
90
|
|
|
public function create( $name ) |
91
|
|
|
{ |
92
|
|
|
$sheet = $this->container->createSheet(); |
93
|
|
|
$sheet->setTitle( $name ); |
94
|
|
|
|
95
|
|
|
return new \Aimeos\MW\Container\Content\PHPExcel( $sheet, $name, $this->getOptions() ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Adds content data to the container. |
101
|
|
|
* |
102
|
|
|
* @param \Aimeos\MW\Container\Content\Iface $content Content object |
103
|
|
|
*/ |
104
|
|
|
public function add( \Aimeos\MW\Container\Content\Iface $content ) |
105
|
|
|
{ |
106
|
|
|
// was already added to the PHPExcel object by createSheet() |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns the element specified by its name. |
112
|
|
|
* |
113
|
|
|
* @param string $name Name of the content object that should be returned |
114
|
|
|
* @return \Aimeos\MW\Container\Content\Iface Content object |
115
|
|
|
*/ |
116
|
|
|
public function get( $name ) |
117
|
|
|
{ |
118
|
|
|
if( ( $sheet = $this->container->getSheetByName( $name ) ) === null ) { |
119
|
|
|
throw new \Aimeos\MW\Container\Exception( sprintf( 'No sheet "%1$s" available', $name ) ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return new \Aimeos\MW\Container\Content\PHPExcel( $sheet, $sheet->getTitle(), $this->getOptions() ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Cleans up and saves the container. |
128
|
|
|
*/ |
129
|
|
|
public function close() |
130
|
|
|
{ |
131
|
|
|
$writer = \PHPExcel_IOFactory::createWriter( $this->container, $this->format ); |
132
|
|
|
$writer->save( $this->resourcepath ); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Return the current element. |
138
|
|
|
* |
139
|
|
|
* @return \Aimeos\MW\Container\Content\Iface Content object with PHPExcel sheet |
140
|
|
|
*/ |
141
|
|
|
public function current() |
142
|
|
|
{ |
143
|
|
|
$sheet = $this->iterator->current(); |
144
|
|
|
|
145
|
|
|
return new \Aimeos\MW\Container\Content\PHPExcel( $sheet, $sheet->getTitle(), $this->getOptions() ); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Returns the key of the current element. |
151
|
|
|
* |
152
|
|
|
* @return integer Index of the PHPExcel sheet |
153
|
|
|
*/ |
154
|
|
|
public function key() |
155
|
|
|
{ |
156
|
|
|
return $this->iterator->key(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Moves forward to next PHPExcel sheet. |
162
|
|
|
*/ |
163
|
|
|
public function next() |
164
|
|
|
{ |
165
|
|
|
return $this->iterator->next(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Rewinds to the first PHPExcel sheet. |
171
|
|
|
*/ |
172
|
|
|
public function rewind() |
173
|
|
|
{ |
174
|
|
|
return $this->iterator->rewind(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Checks if the current position is valid. |
180
|
|
|
* |
181
|
|
|
* @return boolean True on success or false on failure |
182
|
|
|
*/ |
183
|
|
|
public function valid() |
184
|
|
|
{ |
185
|
|
|
return $this->iterator->valid(); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|