1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MewesK\TwigExcelBundle\Wrapper; |
4
|
|
|
|
5
|
|
|
use PHPExcel_IOFactory; |
6
|
|
|
use PHPExcel_Settings; |
7
|
|
|
use PHPExcel_Writer_Abstract; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
use Symfony\Bridge\Twig\AppVariable; |
10
|
|
|
use Twig_Environment; |
11
|
|
|
use Twig_Loader_Filesystem; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class XlsDocumentWrapper |
15
|
|
|
* |
16
|
|
|
* @package MewesK\TwigExcelBundle\Wrapper |
17
|
|
|
*/ |
18
|
|
|
class XlsDocumentWrapper extends AbstractWrapper |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $context; |
24
|
|
|
/** |
25
|
|
|
* @var Twig_Environment |
26
|
|
|
*/ |
27
|
|
|
protected $environment; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \PHPExcel |
31
|
|
|
*/ |
32
|
|
|
protected $object; |
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $attributes; |
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $mappings; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* XlsDocumentWrapper constructor. |
44
|
|
|
* @param array $context |
45
|
|
|
* @param Twig_Environment $environment |
46
|
|
|
*/ |
47
|
|
|
public function __construct(array $context, Twig_Environment $environment) |
48
|
|
|
{ |
49
|
|
|
$this->context = $context; |
50
|
|
|
$this->environment = $environment; |
51
|
|
|
|
52
|
|
|
$this->object = null; |
53
|
|
|
$this->attributes = []; |
54
|
|
|
$this->mappings = []; |
55
|
|
|
|
56
|
|
|
$this->initializeMappings(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function initializeMappings() |
60
|
|
|
{ |
61
|
|
|
$this->mappings['category'] = function ($value) { |
62
|
|
|
$this->object->getProperties()->setCategory($value); |
63
|
|
|
}; |
64
|
|
|
$this->mappings['company'] = function ($value) { |
65
|
|
|
$this->object->getProperties()->setCompany($value); |
66
|
|
|
}; |
67
|
|
|
$this->mappings['created'] = function ($value) { |
68
|
|
|
$this->object->getProperties()->setCreated($value); |
69
|
|
|
}; |
70
|
|
|
$this->mappings['creator'] = function ($value) { |
71
|
|
|
$this->object->getProperties()->setCreator($value); |
72
|
|
|
}; |
73
|
|
|
$this->mappings['defaultStyle'] = function ($value) { |
74
|
|
|
$this->object->getDefaultStyle()->applyFromArray($value); |
75
|
|
|
}; |
76
|
|
|
$this->mappings['description'] = function ($value) { |
77
|
|
|
$this->object->getProperties()->setDescription($value); |
78
|
|
|
}; |
79
|
|
|
$this->mappings['format'] = function ($value) { |
80
|
|
|
$this->attributes['format'] = $value; |
81
|
|
|
}; |
82
|
|
|
$this->mappings['keywords'] = function ($value) { |
83
|
|
|
$this->object->getProperties()->setKeywords($value); |
84
|
|
|
}; |
85
|
|
|
$this->mappings['lastModifiedBy'] = function ($value) { |
86
|
|
|
$this->object->getProperties()->setLastModifiedBy($value); |
87
|
|
|
}; |
88
|
|
|
$this->mappings['manager'] = function ($value) { |
89
|
|
|
$this->object->getProperties()->setManager($value); |
90
|
|
|
}; |
91
|
|
|
$this->mappings['modified'] = function ($value) { |
92
|
|
|
$this->object->getProperties()->setModified($value); |
93
|
|
|
}; |
94
|
|
|
$this->mappings['security']['lockRevision'] = function ($value) { |
95
|
|
|
$this->object->getSecurity()->setLockRevision($value); |
96
|
|
|
}; |
97
|
|
|
$this->mappings['security']['lockStructure'] = function ($value) { |
98
|
|
|
$this->object->getSecurity()->setLockStructure($value); |
99
|
|
|
}; |
100
|
|
|
$this->mappings['security']['lockWindows'] = function ($value) { |
101
|
|
|
$this->object->getSecurity()->setLockWindows($value); |
102
|
|
|
}; |
103
|
|
|
$this->mappings['security']['revisionsPassword'] = function ($value) { |
104
|
|
|
$this->object->getSecurity()->setRevisionsPassword($value); |
105
|
|
|
}; |
106
|
|
|
$this->mappings['security']['workbookPassword'] = function ($value) { |
107
|
|
|
$this->object->getSecurity()->setWorkbookPassword($value); |
108
|
|
|
}; |
109
|
|
|
$this->mappings['subject'] = function ($value) { |
110
|
|
|
$this->object->getProperties()->setSubject($value); |
111
|
|
|
}; |
112
|
|
|
$this->mappings['template'] = function ($value) { |
113
|
|
|
$this->attributes['template'] = $value; |
114
|
|
|
}; |
115
|
|
|
$this->mappings['title'] = function ($value) { |
116
|
|
|
$this->object->getProperties()->setTitle($value); |
117
|
|
|
}; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param null|array $properties |
122
|
|
|
* |
123
|
|
|
* @throws \PHPExcel_Exception |
124
|
|
|
*/ |
125
|
|
|
public function start(array $properties = null) |
126
|
|
|
{ |
127
|
|
|
// load template |
128
|
|
|
if (array_key_exists('template', $properties)) { |
129
|
|
|
$templatePath = $this->expandPath($properties['template']); |
130
|
|
|
$reader = PHPExcel_IOFactory::createReaderForFile($templatePath); |
131
|
|
|
$this->object = $reader->load($templatePath); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// create new |
135
|
|
|
else { |
136
|
|
|
$this->object = new \PHPExcel(); |
137
|
|
|
$this->object->removeSheetByIndex(0); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$this->attributes['properties'] = $properties ?: []; |
141
|
|
|
|
142
|
|
|
if ($properties !== null) { |
143
|
|
|
$this->setProperties($properties, $this->mappings); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param bool $preCalculateFormulas |
149
|
|
|
* @param null|string $diskCachingDirectory |
150
|
|
|
* |
151
|
|
|
* @throws \InvalidArgumentException |
152
|
|
|
* @throws \PHPExcel_Exception |
153
|
|
|
* @throws \PHPExcel_Reader_Exception |
154
|
|
|
* @throws \PHPExcel_Writer_Exception |
155
|
|
|
*/ |
156
|
|
|
public function end($preCalculateFormulas = true, $diskCachingDirectory = null) |
157
|
|
|
{ |
158
|
|
|
$format = null; |
159
|
|
|
|
160
|
|
|
// try document property |
161
|
|
|
if (array_key_exists('format', $this->attributes)) { |
162
|
|
|
$format = $this->attributes['format']; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// try Symfony request |
166
|
|
|
else if (array_key_exists('app', $this->context)) { |
167
|
|
|
/** |
168
|
|
|
* @var $appVariable AppVariable |
169
|
|
|
*/ |
170
|
|
|
$appVariable = $this->context['app']; |
171
|
|
|
if ($appVariable instanceof AppVariable && $appVariable->getRequest() !== null) { |
172
|
|
|
$format = $appVariable->getRequest()->getRequestFormat(); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// set default |
177
|
|
|
if ($format === null || !is_string($format)) { |
178
|
|
|
$format = 'xlsx'; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
switch (strtolower($format)) { |
182
|
|
|
case 'csv': |
183
|
|
|
$writerType = 'CSV'; |
184
|
|
|
break; |
185
|
|
|
case 'ods': |
186
|
|
|
$writerType = 'OpenDocument'; |
187
|
|
|
break; |
188
|
|
|
case 'pdf': |
189
|
|
|
$writerType = 'PDF'; |
190
|
|
|
try { |
191
|
|
|
$reflectionClass = new ReflectionClass('mPDF'); |
192
|
|
|
$path = dirname($reflectionClass->getFileName()); |
193
|
|
|
if (!PHPExcel_Settings::setPdfRenderer(PHPExcel_Settings::PDF_RENDERER_MPDF, $path)) { |
194
|
|
|
throw new \PHPExcel_Exception(); |
195
|
|
|
} |
196
|
|
|
} catch (\Exception $e) { |
197
|
|
|
throw new \PHPExcel_Exception('Error loading mPDF. Is mPDF correctly installed?', $e->getCode(), $e); |
198
|
|
|
} |
199
|
|
|
break; |
200
|
|
|
case 'xls': |
201
|
|
|
$writerType = 'Excel5'; |
202
|
|
|
break; |
203
|
|
|
case 'xlsx': |
204
|
|
|
$writerType = 'Excel2007'; |
205
|
|
|
break; |
206
|
|
|
default: |
207
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown format "%s"', $format)); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @var $writer PHPExcel_Writer_Abstract |
212
|
|
|
*/ |
213
|
|
|
$writer = \PHPExcel_IOFactory::createWriter($this->object, $writerType); |
214
|
|
|
$writer->setPreCalculateFormulas($preCalculateFormulas); |
215
|
|
|
$writer->setUseDiskCaching($diskCachingDirectory !== null, $diskCachingDirectory); |
216
|
|
|
$writer->save('php://output'); |
217
|
|
|
|
218
|
|
|
$this->object = null; |
219
|
|
|
$this->attributes = []; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
// |
223
|
|
|
// Helpers |
224
|
|
|
// |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Resolves properties containing paths using namespaces. |
228
|
|
|
* |
229
|
|
|
* @param string $path |
230
|
|
|
* @return bool |
231
|
|
|
*/ |
232
|
|
|
private function expandPath($path) |
233
|
|
|
{ |
234
|
|
|
$loader = $this->environment->getLoader(); |
235
|
|
|
if ($loader instanceof Twig_Loader_Filesystem) { |
236
|
|
|
/** |
237
|
|
|
* @var Twig_Loader_Filesystem $loader |
238
|
|
|
*/ |
239
|
|
|
foreach ($loader->getNamespaces() as $namespace) { |
240
|
|
|
if (strpos($path, $namespace) === 1) { |
241
|
|
|
foreach ($loader->getPaths($namespace) as $namespacePath) { |
242
|
|
|
$expandedPathAttribute = str_replace('@' . $namespace, $namespacePath, $path); |
243
|
|
|
if (file_exists($expandedPathAttribute)) { |
244
|
|
|
return $expandedPathAttribute; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
return $path; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
// |
254
|
|
|
// Getters/Setters |
255
|
|
|
// |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @return \PHPExcel |
259
|
|
|
*/ |
260
|
|
|
public function getObject() |
261
|
|
|
{ |
262
|
|
|
return $this->object; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @param \PHPExcel $object |
267
|
|
|
*/ |
268
|
|
|
public function setObject($object) |
269
|
|
|
{ |
270
|
|
|
$this->object = $object; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @return array |
275
|
|
|
*/ |
276
|
|
|
public function getAttributes() |
277
|
|
|
{ |
278
|
|
|
return $this->attributes; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @param array $attributes |
283
|
|
|
*/ |
284
|
|
|
public function setAttributes($attributes) |
285
|
|
|
{ |
286
|
|
|
$this->attributes = $attributes; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @return array |
291
|
|
|
*/ |
292
|
|
|
public function getMappings() |
293
|
|
|
{ |
294
|
|
|
return $this->mappings; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @param array $mappings |
299
|
|
|
*/ |
300
|
|
|
public function setMappings($mappings) |
301
|
|
|
{ |
302
|
|
|
$this->mappings = $mappings; |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|