1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Lexik\Controller; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\DBALException; |
6
|
|
|
use Lexik\Bundle\TranslationBundle\Entity\TransUnit; |
7
|
|
|
use Lexik\Bundle\TranslationBundle\Manager\TranslationInterface; |
8
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
9
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
10
|
|
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
11
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
12
|
|
|
use Symfony\Component\Yaml\Dumper; |
13
|
|
|
|
14
|
|
|
trait NonActionTrait |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Execute a batch download |
18
|
|
|
* |
19
|
|
|
* @param ProxyQueryInterface $query |
20
|
|
|
* |
21
|
|
|
* @return RedirectResponse |
22
|
|
|
*/ |
23
|
|
|
public function batchActionDownload(ProxyQueryInterface $queryProxy) |
24
|
|
|
{ |
25
|
|
|
$flashType = 'success'; |
26
|
|
|
|
27
|
|
|
$dumper = new Dumper(4); |
28
|
|
|
|
29
|
|
|
$response = new StreamedResponse(function() use ($queryProxy, &$flashType, $dumper) { |
30
|
|
|
try { |
31
|
|
|
/** |
32
|
|
|
* @var TransUnit $transUnit |
33
|
|
|
*/ |
34
|
|
|
foreach ($queryProxy->getQuery()->iterate() as $pos => $object) { |
35
|
|
|
foreach ($object as $transUnit) { |
36
|
|
|
$chunkPrefix = $transUnit->getDomain().'__'.$transUnit->getKey().'__'.$transUnit->getId().'__'; |
37
|
|
|
$chunk = []; |
38
|
|
|
/** @var TranslationInterface $translation */ |
39
|
|
|
foreach ($transUnit->getTranslations() as $translation) { |
40
|
|
|
$chunk[$chunkPrefix.$translation->getLocale()] = $translation->getContent(); |
41
|
|
|
} |
42
|
|
|
echo $dumper->dump($chunk, 2); |
43
|
|
|
flush(); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} catch (\PDOException $e) { |
47
|
|
|
$flashType = 'error'; |
48
|
|
|
flush(); |
49
|
|
|
} catch (DBALException $e) { |
50
|
|
|
$flashType = 'error'; |
51
|
|
|
flush(); |
52
|
|
|
} |
53
|
|
|
}); |
54
|
|
|
|
55
|
|
|
$this->addFlash('sonata_flash_'.$flashType, 'translations.flash_batch_download_'.$flashType); |
|
|
|
|
56
|
|
|
|
57
|
|
|
$response->headers->set('Content-Type', 'text/x-yaml'); |
58
|
|
|
$response->headers->set('Cache-Control', ''); |
59
|
|
|
$response->headers->set('Transfer-Encoding', 'chunked'); |
60
|
|
|
$response->headers->set('Last-Modified', gmdate('D, d M Y H:i:s')); |
61
|
|
|
$contentDisposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'translations.yml'); |
62
|
|
|
$response->headers->set('Content-Disposition', $contentDisposition); |
63
|
|
|
|
64
|
|
|
return $response; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function getManagedLocales() |
68
|
|
|
{ |
69
|
|
|
return $this->container->getParameter('lexik_translation.managed_locales'); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function __toString() |
73
|
|
|
{ |
74
|
|
|
return 'sludio_helper.lexik.crud.controller'; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|