|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrs\SonataImportBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Pagerfanta\Adapter\DoctrineORMAdapter; |
|
6
|
|
|
use Pagerfanta\Pagerfanta; |
|
7
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin; |
|
8
|
|
|
use Doctrs\SonataImportBundle\Entity\UploadFile; |
|
9
|
|
|
use Doctrs\SonataImportBundle\Form\Type\UploadFileType; |
|
10
|
|
|
use Sonata\AdminBundle\Controller\CRUDController; |
|
11
|
|
|
use Symfony\Component\Form\FormError; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
use Symfony\Component\Process\Process; |
|
15
|
|
|
|
|
16
|
|
|
class DefaultController extends CRUDController { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param Request $request |
|
20
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
|
21
|
|
|
*/ |
|
22
|
|
|
public function indexAction(Request $request) { |
|
23
|
|
|
$fileEntity = new UploadFile(); |
|
24
|
|
|
$form = $this->createForm(UploadFileType::class, $fileEntity, [ |
|
25
|
|
|
'method' => 'POST' |
|
26
|
|
|
]); |
|
27
|
|
|
$form->handleRequest($request); |
|
28
|
|
|
|
|
29
|
|
|
if ($form->isValid()) { |
|
30
|
|
|
if (!$fileEntity->getFile()->getError()) { |
|
31
|
|
|
$fileEntity->move($this->getParameter('doctrs_sonata_import.upload_dir')); |
|
32
|
|
|
|
|
33
|
|
|
$this->getDoctrine()->getManager()->persist($fileEntity); |
|
34
|
|
|
$this->getDoctrine()->getManager()->flush($fileEntity); |
|
35
|
|
|
|
|
36
|
|
|
$this->runCommand($fileEntity); |
|
37
|
|
|
return $this->redirect($this->admin->generateUrl('upload', [ |
|
38
|
|
|
'id' => $fileEntity->getId() |
|
39
|
|
|
])); |
|
40
|
|
|
} else { |
|
41
|
|
|
$form->get('file')->addError(new FormError($fileEntity->getFile()->getErrorMessage())); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
$builder = $this->get('sonata.admin.pool') |
|
47
|
|
|
->getInstance($this->admin->getCode()) |
|
48
|
|
|
->getExportFields() |
|
49
|
|
|
; |
|
50
|
|
|
return $this->render('@DoctrsSonataImport/Default/index.html.twig', [ |
|
51
|
|
|
'form' => $form->createView(), |
|
52
|
|
|
'baseTemplate' => $this->getBaseTemplate(), |
|
53
|
|
|
'builder' => $builder, |
|
54
|
|
|
'action' => 'import', |
|
55
|
|
|
'letters' => $this->getLetterArray() |
|
56
|
|
|
]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param Request $request |
|
61
|
|
|
* @param UploadFile $uploadFile |
|
62
|
|
|
* @return JsonResponse|\Symfony\Component\HttpFoundation\Response |
|
63
|
|
|
*/ |
|
64
|
|
|
public function uploadAction(Request $request, UploadFile $uploadFile) { |
|
65
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
66
|
|
|
|
|
67
|
|
|
$countImport = $em->getRepository('DoctrsSonataImportBundle:ImportLog')->count([ |
|
68
|
|
|
'uploadFile' => $uploadFile->getId() |
|
69
|
|
|
]); |
|
70
|
|
|
|
|
71
|
|
|
$data = $em->getRepository('DoctrsSonataImportBundle:ImportLog')->pagerfanta($request); |
|
72
|
|
|
$paginator = new Pagerfanta(new DoctrineORMAdapter($data)); |
|
73
|
|
|
$paginator->setCurrentPage($request->get('page', 1)); |
|
74
|
|
|
|
|
75
|
|
|
return $this->render('@DoctrsSonataImport/Default/upload.html.twig', [ |
|
76
|
|
|
'uploadFile' => $uploadFile, |
|
77
|
|
|
'paginator' => $paginator, |
|
78
|
|
|
'action' => 'upload', |
|
79
|
|
|
'admin' => $this->admin, |
|
80
|
|
|
'countImport' => $countImport, |
|
81
|
|
|
'baseTemplate' => $this->getBaseTemplate(), |
|
82
|
|
|
]); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param UploadFile $uploadFile |
|
88
|
|
|
* @return JsonResponse |
|
89
|
|
|
*/ |
|
90
|
|
|
public function importStatusAction(UploadFile $uploadFile) { |
|
91
|
|
|
$countImport = $this->getDoctrine()->getManager()->getRepository('DoctrsSonataImportBundle:ImportLog')->count([ |
|
92
|
|
|
'uploadFile' => $uploadFile->getId() |
|
93
|
|
|
]); |
|
94
|
|
|
|
|
95
|
|
|
return new JsonResponse([ |
|
96
|
|
|
'status' => $uploadFile->getStatus(), |
|
97
|
|
|
'error' => $uploadFile->getMessage(), |
|
98
|
|
|
'count' => $countImport |
|
99
|
|
|
]); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* get array from A to ZZ |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
|
|
private function getLetterArray() { |
|
107
|
|
|
$array = range('A', 'Z'); |
|
108
|
|
|
$letters = $array; |
|
109
|
|
|
foreach ($array as $first) { |
|
110
|
|
|
foreach ($array as $second) { |
|
111
|
|
|
$letters[] = $first . $second; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
return $letters; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param UploadFile $fileEntity |
|
119
|
|
|
*/ |
|
120
|
|
|
private function runCommand(UploadFile $fileEntity) { |
|
121
|
|
|
$command = sprintf( |
|
122
|
|
|
'/usr/bin/php %s/console doctrs:sonata:import %d "%s" "%s" %d > /dev/null 2>&1 &', |
|
123
|
|
|
$this->get('kernel')->getRootDir(), |
|
124
|
|
|
$fileEntity->getId(), |
|
125
|
|
|
$this->admin->getCode(), |
|
126
|
|
|
$fileEntity->getEncode() ? $fileEntity->getEncode() : 'utf8', |
|
127
|
|
|
$fileEntity->getLoaderClass() |
|
128
|
|
|
); |
|
129
|
|
|
$process = new Process($command); |
|
130
|
|
|
$process->run(); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|