1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sigmapix\Sonata\ImportBundle\Service; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Port\Csv\CsvReader; |
7
|
|
|
use Port\Doctrine\DoctrineWriter; |
8
|
|
|
use Port\Excel\ExcelReader; |
9
|
|
|
use Port\Steps\StepAggregator; |
10
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
12
|
|
|
use Symfony\Component\Form\Form; |
13
|
|
|
use Symfony\Component\Form\SubmitButton; |
14
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
15
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
16
|
|
|
|
17
|
|
|
final class ImportService |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var EntityManagerInterface |
21
|
|
|
*/ |
22
|
|
|
private $em; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var SessionInterface |
26
|
|
|
*/ |
27
|
|
|
private $session; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ContainerInterface |
31
|
|
|
*/ |
32
|
|
|
private $container; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $doctrineWriterClass; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* ImportService constructor. |
41
|
|
|
* |
42
|
|
|
* @param EntityManagerInterface $em |
43
|
|
|
* @param $session |
44
|
|
|
* @param ContainerInterface $container |
45
|
|
|
* @param string $doctrineWriterClass |
46
|
|
|
*/ |
47
|
|
|
public function __construct(EntityManagerInterface $em, $session, ContainerInterface $container, string $doctrineWriterClass) |
48
|
|
|
{ |
49
|
|
|
$this->em = $em; |
50
|
|
|
$this->session = $session; |
51
|
|
|
$this->container = $container; |
52
|
|
|
$this->doctrineWriterClass = $doctrineWriterClass; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param UploadedFile $file |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function getHeaders(UploadedFile $file) |
61
|
|
|
{ |
62
|
|
|
$reader = $this->getReader($file); |
63
|
|
|
$columnHeaders = array_filter($reader->getColumnHeaders(), function ($h) {return null !== $h; }); |
64
|
|
|
$columnHeaders = array_map( |
65
|
|
|
function ($h) { |
66
|
|
|
if (!empty($h) && is_string($h)) { |
67
|
|
|
$h = utf8_encode($h); |
68
|
|
|
} |
69
|
|
|
return trim($h); |
70
|
|
|
}, $columnHeaders); |
71
|
|
|
$headers = array_flip($columnHeaders); |
72
|
|
|
array_walk($headers, function (&$v, $k) use ($headers) { $v = $k; }); |
73
|
|
|
return $headers; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param UploadedFile $file |
78
|
|
|
* @param Form $form |
79
|
|
|
* @param AdminInterface $admin |
80
|
|
|
* |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
public function import(UploadedFile $file, Form $form, AdminInterface $admin) |
84
|
|
|
{ |
85
|
|
|
$mapping = []; |
86
|
|
|
foreach ($form as $f) { |
87
|
|
|
if ($f instanceof SubmitButton) { |
88
|
|
|
continue; |
89
|
|
|
} |
90
|
|
|
$mapping[$f->getName()] = $f->getNormData(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$reader = $this->getReader($file); |
94
|
|
|
|
95
|
|
|
// Convert |
96
|
|
|
$columnHeaders = array_map( |
97
|
|
|
function ($h) { |
98
|
|
|
if (!empty($h) && is_string($h)) { |
99
|
|
|
$h = utf8_encode($h); |
100
|
|
|
} |
101
|
|
|
return trim($h); |
102
|
|
|
}, $reader->getColumnHeaders()); |
103
|
|
|
// Replace columnsHeader names with entity field name in our $mapping |
104
|
|
|
$columnHeaders = array_map(function ($h) use ($mapping) { |
105
|
|
|
$k = array_search($h, (array) $mapping, true); |
106
|
|
|
return false === $k ? $h : $k; |
107
|
|
|
}, $columnHeaders); |
108
|
|
|
$reader->setColumnHeaders($columnHeaders); |
109
|
|
|
|
110
|
|
|
/** @var DoctrineWriter $writer */ |
111
|
|
|
$writer = new $this->doctrineWriterClass($this->em, get_class($form->getData())); |
112
|
|
|
if (method_exists($writer, 'setContainer')) { |
113
|
|
|
$writer->setContainer($this->container); |
114
|
|
|
} |
115
|
|
|
$writer->disableTruncate(); |
116
|
|
|
|
117
|
|
|
$workflow = new StepAggregator($reader); |
118
|
|
|
$workflow->addWriter($writer); |
119
|
|
|
$admin->configureImportSteps($workflow); |
|
|
|
|
120
|
|
|
|
121
|
|
|
$result = $workflow->process(); |
122
|
|
|
|
123
|
|
|
return $result; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get session. |
128
|
|
|
* |
129
|
|
|
* @return |
130
|
|
|
*/ |
131
|
|
|
public function getSession() |
132
|
|
|
{ |
133
|
|
|
return $this->session; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set session. |
138
|
|
|
* |
139
|
|
|
* @return $this |
140
|
|
|
*/ |
141
|
|
|
public function setSession($session) |
142
|
|
|
{ |
143
|
|
|
$this->session = $session; |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param UploadedFile $file |
150
|
|
|
* |
151
|
|
|
* @return CsvReader|ExcelReader |
152
|
|
|
*/ |
153
|
|
|
private function getReader(UploadedFile $file) |
154
|
|
|
{ |
155
|
|
|
$pathFile = $file->getRealPath(); |
156
|
|
|
$fileExtension = $file->guessExtension(); |
157
|
|
|
$excelExtensions = ['xls', 'xlsx', 'zip']; |
158
|
|
|
|
159
|
|
|
if (in_array($fileExtension, $excelExtensions)) { |
160
|
|
|
$reader = new ExcelReader(new \SplFileObject($pathFile), 0, 0, true); |
161
|
|
|
} else { |
162
|
|
|
$reader = new CsvReader(new \SplFileObject($pathFile), ';'); |
163
|
|
|
$reader->setHeaderRowNumber(0, CsvReader::DUPLICATE_HEADERS_INCREMENT); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $reader; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.