1
|
|
|
<?php |
2
|
|
|
/*************************************************************************************/ |
3
|
|
|
/* This file is part of the Thelia package. */ |
4
|
|
|
/* */ |
5
|
|
|
/* Copyright (c) OpenStudio */ |
6
|
|
|
/* email : [email protected] */ |
7
|
|
|
/* web : http://www.thelia.net */ |
8
|
|
|
/* */ |
9
|
|
|
/* For the full copyright and license information, please view the LICENSE.txt */ |
10
|
|
|
/* file that was distributed with this source code. */ |
11
|
|
|
/*************************************************************************************/ |
12
|
|
|
|
13
|
|
|
namespace Thelia\ImportExport\Export; |
14
|
|
|
|
15
|
|
|
use Propel\Runtime\ActiveQuery\ModelCriteria; |
16
|
|
|
use Propel\Runtime\Map\TableMap; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
18
|
|
|
use Thelia\Core\Translation\Translator; |
19
|
|
|
use Thelia\Model\Lang; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class AbstractExport |
23
|
|
|
* @author Jérôme Billiras <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractExport implements \Iterator |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string Default file name |
29
|
|
|
*/ |
30
|
|
|
const FILE_NAME = 'export'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var boolean Export images with data |
34
|
|
|
*/ |
35
|
|
|
const EXPORT_IMAGE = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var boolean Export documents with data |
39
|
|
|
*/ |
40
|
|
|
const EXPORT_DOCUMENT = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var boolean Use range date |
44
|
|
|
*/ |
45
|
|
|
const USE_RANGE_DATE = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array|\Propel\Runtime\Util\PropelModelPager Data to export |
49
|
|
|
*/ |
50
|
|
|
private $data; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var boolean True if data is array, false otherwise |
54
|
|
|
*/ |
55
|
|
|
private $dataIsArray; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var ContainerInterface |
59
|
|
|
*/ |
60
|
|
|
protected $container; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var \Thelia\Model\Lang A language model |
64
|
|
|
*/ |
65
|
|
|
protected $language; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var null|array List of fields in order in which they must be exported and there alias name |
69
|
|
|
*/ |
70
|
|
|
protected $orderAndAliases; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var null|array Keep untranslated $orderAndAliases |
74
|
|
|
*/ |
75
|
|
|
private $originalOrderAndAliases; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var boolean Whether to export images or not |
79
|
|
|
*/ |
80
|
|
|
protected $exportImages = false; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var array Images paths list |
84
|
|
|
*/ |
85
|
|
|
protected $imagesPaths = []; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var boolean Whether to export documents or not |
89
|
|
|
*/ |
90
|
|
|
protected $exportDocuments = false; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var array Documents paths list |
94
|
|
|
*/ |
95
|
|
|
protected $documentsPaths = []; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @var null|array Export date range |
99
|
|
|
*/ |
100
|
|
|
protected $rangeDate; |
101
|
|
|
|
102
|
|
|
public function current() |
103
|
|
|
{ |
104
|
|
|
if ($this->dataIsArray) { |
105
|
|
|
return current($this->data); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$data = $this->data->getIterator()->current()->toArray(TableMap::TYPE_COLNAME, true, [], true); |
109
|
|
|
|
110
|
|
|
foreach ($this->data->getQuery()->getWith() as $withKey => $with) { |
111
|
|
|
$data = array_merge($data, $data[$withKey]); |
112
|
|
|
unset($data[$withKey]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $data; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function key() |
119
|
|
|
{ |
120
|
|
|
if ($this->dataIsArray) { |
121
|
|
|
return key($this->data); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ($this->data->getIterator()->key() !== null) { |
125
|
|
|
return $this->data->getIterator()->key() + ($this->data->getPage() - 1) * 1000; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function next() |
132
|
|
|
{ |
133
|
|
|
if ($this->dataIsArray) { |
134
|
|
|
next($this->data); |
135
|
|
|
} else { |
136
|
|
|
$this->data->getIterator()->next(); |
137
|
|
|
if (!$this->valid() && !$this->data->isLastPage()) { |
138
|
|
|
$this->data = $this->data->getQuery()->paginate($this->data->getNextPage(), 1000); |
139
|
|
|
$this->data->getIterator()->rewind(); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function rewind() |
145
|
|
|
{ |
146
|
|
|
// Since it's first method call on traversable, we get raw data here |
147
|
|
|
// but we do not permit to go back |
148
|
|
|
|
149
|
|
|
if ($this->data === null) { |
150
|
|
|
$data = $this->getData(); |
151
|
|
|
|
152
|
|
|
if (is_array($data)) { |
153
|
|
|
$this->data = $data; |
154
|
|
|
$this->dataIsArray = true; |
155
|
|
|
|
156
|
|
|
return; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if ($data instanceof ModelCriteria) { |
160
|
|
|
$this->data = $data->setFormatter(ModelCriteria::FORMAT_ON_DEMAND)->keepQuery(false)->paginate(1, 1000); |
161
|
|
|
$this->data->getIterator()->rewind(); |
162
|
|
|
|
163
|
|
|
return; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
throw new \DomainException( |
167
|
|
|
'Data must an array or an instance of \\Propel\\Runtime\\ActiveQuery\\ModelCriteria' |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
throw new \LogicException('Export data can\'t be rewinded'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function valid() |
175
|
|
|
{ |
176
|
|
|
if ($this->dataIsArray) { |
177
|
|
|
return key($this->data) !== null; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $this->data->getIterator()->valid(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get language |
186
|
|
|
* |
187
|
|
|
* @return \Thelia\Model\Lang A language model |
188
|
|
|
*/ |
189
|
|
|
public function getLang() |
190
|
|
|
{ |
191
|
|
|
return $this->language; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Set language |
196
|
|
|
* |
197
|
|
|
* @param null|\Thelia\Model\Lang $language A language model |
198
|
|
|
* |
199
|
|
|
* @return $this Return $this, allow chaining |
200
|
|
|
*/ |
201
|
|
|
public function setLang(Lang $language = null) |
202
|
|
|
{ |
203
|
|
|
$this->language = $language; |
204
|
|
|
|
205
|
|
|
if ($this->originalOrderAndAliases === null) { |
206
|
|
|
$this->originalOrderAndAliases = $this->orderAndAliases; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
if ($this->language !== null && $this->orderAndAliases !== null) { |
210
|
|
|
$previousLocale = Translator::getInstance()->getLocale(); |
211
|
|
|
|
212
|
|
|
Translator::getInstance()->setLocale($this->language->getLocale()); |
213
|
|
|
foreach ($this->orderAndAliases as &$alias) { |
214
|
|
|
$alias = Translator::getInstance()->trans($alias); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
Translator::getInstance()->setLocale($previousLocale); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Whether images has to be exported as data |
226
|
|
|
* |
227
|
|
|
* @return boolean |
228
|
|
|
*/ |
229
|
|
|
public function hasImages() |
230
|
|
|
{ |
231
|
|
|
return static::EXPORT_IMAGE; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Get export images |
236
|
|
|
* |
237
|
|
|
* @return boolean Whether to export images or not |
238
|
|
|
*/ |
239
|
|
|
public function isExportImages() |
240
|
|
|
{ |
241
|
|
|
return $this->exportImages; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Set export images |
246
|
|
|
* |
247
|
|
|
* @param boolean $exportImages Whether to export images or not |
248
|
|
|
* |
249
|
|
|
* @return $this Return $this, allow chaining |
250
|
|
|
*/ |
251
|
|
|
public function setExportImages($exportImages) |
252
|
|
|
{ |
253
|
|
|
$this->exportImages = $exportImages; |
254
|
|
|
|
255
|
|
|
return $this; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Get images paths |
260
|
|
|
* |
261
|
|
|
* @return null|array Images paths list |
262
|
|
|
*/ |
263
|
|
|
public function getImagesPaths() |
264
|
|
|
{ |
265
|
|
|
return $this->imagesPaths; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Set images paths |
270
|
|
|
* |
271
|
|
|
* @param array $imagesPaths Images paths list |
272
|
|
|
* |
273
|
|
|
* @return $this Return $this, allow chaining |
274
|
|
|
*/ |
275
|
|
|
public function setImagesPaths(array $imagesPaths) |
276
|
|
|
{ |
277
|
|
|
$this->imagesPaths = $imagesPaths; |
278
|
|
|
|
279
|
|
|
return $this; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Whether documents has to be exported as data |
285
|
|
|
* |
286
|
|
|
* @return boolean |
287
|
|
|
*/ |
288
|
|
|
public function hasDocuments() |
289
|
|
|
{ |
290
|
|
|
return static::EXPORT_DOCUMENT; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Get export documents |
295
|
|
|
* |
296
|
|
|
* @return boolean Whether to export documents or not |
297
|
|
|
*/ |
298
|
|
|
public function isExportDocuments() |
299
|
|
|
{ |
300
|
|
|
return $this->exportDocuments; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Set export documents |
305
|
|
|
* |
306
|
|
|
* @param boolean $exportDocuments Whether to export documents or not |
307
|
|
|
* |
308
|
|
|
* @return $this Return $this, allow chaining |
309
|
|
|
*/ |
310
|
|
|
public function setExportDocuments($exportDocuments) |
311
|
|
|
{ |
312
|
|
|
$this->exportDocuments = $exportDocuments; |
313
|
|
|
|
314
|
|
|
return $this; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Get documents paths |
319
|
|
|
* |
320
|
|
|
* @return null|array Documents paths list |
321
|
|
|
*/ |
322
|
|
|
public function getDocumentsPaths() |
323
|
|
|
{ |
324
|
|
|
return $this->documentsPaths; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Set documents paths |
329
|
|
|
* |
330
|
|
|
* @param array $documentsPaths Documents paths list |
331
|
|
|
* |
332
|
|
|
* @return $this Return $this, allow chaining |
333
|
|
|
*/ |
334
|
|
|
public function setDocumentsPaths(array $documentsPaths) |
335
|
|
|
{ |
336
|
|
|
$this->documentsPaths = $documentsPaths; |
337
|
|
|
|
338
|
|
|
return $this; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Get range date |
344
|
|
|
* |
345
|
|
|
* @return null|array Array with date range |
346
|
|
|
*/ |
347
|
|
|
public function getRangeDate() |
348
|
|
|
{ |
349
|
|
|
return $this->rangeDate; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Set range date |
354
|
|
|
* |
355
|
|
|
* @param null|array $rangeDate Array with date range |
356
|
|
|
* |
357
|
|
|
* @return $this Return $this, allow chaining |
358
|
|
|
*/ |
359
|
|
|
public function setRangeDate(array $rangeDate = null) |
360
|
|
|
{ |
361
|
|
|
$this->rangeDate = $rangeDate; |
362
|
|
|
|
363
|
|
|
return $this; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Whether export bounded with date |
368
|
|
|
* |
369
|
|
|
* @return boolean |
370
|
|
|
*/ |
371
|
|
|
public function useRangeDate() |
372
|
|
|
{ |
373
|
|
|
return static::USE_RANGE_DATE; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Get file name |
379
|
|
|
* |
380
|
|
|
* @return string Export file name |
381
|
|
|
*/ |
382
|
|
|
public function getFileName() |
383
|
|
|
{ |
384
|
|
|
return static::FILE_NAME; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Apply order and aliases on data |
389
|
|
|
* |
390
|
|
|
* @param array $data Raw data |
391
|
|
|
* |
392
|
|
|
* @return array Ordered and aliased data |
393
|
|
|
*/ |
394
|
|
|
public function applyOrderAndAliases(array $data) |
395
|
|
|
{ |
396
|
|
|
if ($this->orderAndAliases === null) { |
397
|
|
|
return $data; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
$processedData = []; |
401
|
|
|
|
402
|
|
|
foreach ($this->orderAndAliases as $key => $value) { |
403
|
|
|
if (is_integer($key)) { |
404
|
|
|
$fieldName = $value; |
405
|
|
|
$fieldAlias = $value; |
406
|
|
|
} else { |
407
|
|
|
$fieldName = $key; |
408
|
|
|
$fieldAlias = $value; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
$processedData[$fieldAlias] = null; |
412
|
|
|
if (array_key_exists($fieldName, $data)) { |
413
|
|
|
$processedData[$fieldAlias] = $data[$fieldName]; |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
return $processedData; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Process data before serialization |
422
|
|
|
* |
423
|
|
|
* @param array $data Data before serialization |
424
|
|
|
* |
425
|
|
|
* @return array Processed data before serialization |
426
|
|
|
*/ |
427
|
|
|
public function beforeSerialize(array $data) |
428
|
|
|
{ |
429
|
|
|
foreach ($data as $idx => &$value) { |
430
|
|
|
if ($value instanceof \DateTime) { |
431
|
|
|
$value = $value->format('Y-m-d H:i:s'); |
432
|
|
|
} |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
return $data; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Process data after serialization |
440
|
|
|
* |
441
|
|
|
* @param string $data Data after serialization |
442
|
|
|
* |
443
|
|
|
* @return string Processed after before serialization |
444
|
|
|
*/ |
445
|
|
|
public function afterSerialize($data) |
446
|
|
|
{ |
447
|
|
|
return $data; |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* @param ContainerInterface $container |
452
|
|
|
*/ |
453
|
|
|
public function setContainer(ContainerInterface $container) |
454
|
|
|
{ |
455
|
|
|
$this->container = $container; |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* @return ContainerInterface |
460
|
|
|
*/ |
461
|
|
|
protected function getContainer() |
462
|
|
|
{ |
463
|
|
|
return $this->container; |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Get data to export |
468
|
|
|
* |
469
|
|
|
* @return array|\Propel\Runtime\ActiveQuery\ModelCriteria Data to export |
470
|
|
|
*/ |
471
|
|
|
abstract protected function getData(); |
472
|
|
|
} |
473
|
|
|
|