1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FabienCrassat\CurriculumVitaeBundle Symfony bundle. |
5
|
|
|
* |
6
|
|
|
* (c) Fabien Crassat <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FabienCrassat\CurriculumVitaeBundle\Controller; |
13
|
|
|
|
14
|
|
|
use FabienCrassat\CurriculumVitaeBundle\Entity\CurriculumVitae; |
15
|
|
|
use FabienCrassat\CurriculumVitaeBundle\DependencyInjection\FabienCrassatCurriculumVitaeExtension; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
18
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
19
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\HttpFoundation\Response; |
22
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
23
|
|
|
use Symfony\Component\Serializer\Serializer; |
24
|
|
|
use Symfony\Component\Serializer\Encoder\XmlEncoder; |
25
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder; |
26
|
|
|
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; |
27
|
|
|
|
28
|
|
|
class DefaultController implements ContainerAwareInterface |
29
|
|
|
{ |
30
|
|
|
use ContainerAwareTrait; |
31
|
|
|
|
32
|
|
|
private $cvxmlfile; |
33
|
|
|
private $pathToFile; |
34
|
|
|
private $lang; |
35
|
|
|
private $curriculumVitae; |
36
|
|
|
private $exposedLanguages; |
37
|
|
|
private $requestFormat; |
38
|
|
|
private $parameters = []; |
39
|
|
|
|
40
|
|
|
const CVXMLFILE = 'cvxmlfile'; |
41
|
|
|
const PDF_A5SYS = 'a5sys_pdf.pdf_service'; |
42
|
|
|
const PDF_SNAPPY = 'knp_snappy.pdf'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return Response |
46
|
|
|
*/ |
47
|
4 |
|
public function indexAction($cvxmlfile = NULL) |
48
|
|
|
{ |
49
|
3 |
|
if ($cvxmlfile) { |
50
|
|
|
$path = [ |
51
|
2 |
|
'_controller' => 'FabienCrassatCurriculumVitaeBundle:Default:display', |
52
|
2 |
|
self::CVXMLFILE => $cvxmlfile, |
53
|
2 |
|
'_locale' => $this->lang, |
54
|
2 |
|
]; |
55
|
|
|
|
56
|
2 |
|
$request = $this->container->get('request'); |
57
|
2 |
|
$subRequest = $request->duplicate([], NULL, $path); |
58
|
|
|
|
59
|
2 |
|
$httpKernel = $this->container->get('http_kernel'); |
60
|
|
|
|
61
|
2 |
|
return $httpKernel->handle( |
62
|
4 |
|
$subRequest, |
63
|
|
|
HttpKernelInterface::SUB_REQUEST |
64
|
2 |
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
$this->initialization($cvxmlfile); |
68
|
1 |
|
return new RedirectResponse($this->container->get('router')->generate( |
69
|
4 |
|
'fabiencrassat_curriculumvitae_cvxmlfileonly', |
70
|
1 |
|
[self::CVXMLFILE => $this->cvxmlfile]), |
71
|
1 |
|
301); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return Response |
76
|
|
|
*/ |
77
|
7 |
|
public function displayAction($cvxmlfile, $_locale, Request $request) |
78
|
|
|
{ |
79
|
7 |
|
$this->initialization($cvxmlfile, $_locale); |
80
|
5 |
|
$this->requestFormat = $request->getRequestFormat(); |
81
|
5 |
|
$this->setViewParameters(); |
82
|
|
|
|
83
|
5 |
|
switch ($this->requestFormat) { |
84
|
5 |
|
case 'json': |
85
|
1 |
|
return new Response(json_encode($this->parameters)); |
86
|
4 |
|
case 'xml': |
87
|
|
|
//initialisation du serializer |
88
|
1 |
|
$encoders = [new XmlEncoder('CurriculumVitae'), new JsonEncoder()]; |
89
|
1 |
|
$normalizers = [new GetSetMethodNormalizer()]; |
90
|
1 |
|
$serializer = new Serializer($normalizers, $encoders); |
91
|
|
|
|
92
|
1 |
|
$response = new Response(); |
93
|
1 |
|
$response->setContent($serializer->serialize($this->parameters, 'xml')); |
94
|
1 |
|
$response->headers->set('Content-Type', 'application/xml'); |
95
|
|
|
|
96
|
1 |
|
return $response; |
97
|
3 |
|
default: |
98
|
3 |
|
return $this->container->get('templating')->renderResponse( |
99
|
3 |
|
$this->container->getParameter(FabienCrassatCurriculumVitaeExtension::TEMPLATE), |
100
|
3 |
|
$this->parameters); |
101
|
3 |
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return Response |
106
|
|
|
*/ |
107
|
1 |
|
public function exportPDFAction($cvxmlfile, $_locale) |
108
|
|
|
{ |
109
|
1 |
|
if (!$this->hasExportPDF()) { |
110
|
1 |
|
throw new NotFoundHttpException('No export PDF service installed.'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->initialization($cvxmlfile, $_locale); |
114
|
|
|
$this->setViewParameters(); |
115
|
|
|
|
116
|
|
|
$html = $this->container->get('templating')->render( |
117
|
|
|
'FabienCrassatCurriculumVitaeBundle:CurriculumVitae:index.pdf.twig', |
118
|
|
|
$this->parameters); |
119
|
|
|
$filename = $this->curriculumVitae->getHumanFileName().'.pdf'; |
120
|
|
|
|
121
|
|
|
$content = ''; |
122
|
|
|
if ($this->container->has(self::PDF_A5SYS)) { |
123
|
|
|
$content = $this->container->get(self::PDF_A5SYS)->sendPDF($html, $filename); |
124
|
|
|
} elseif ($this->container->has(self::PDF_SNAPPY)) { |
125
|
|
|
$content = $this->container->get(self::PDF_SNAPPY)->getOutputFromHtml($html); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return new Response($content, 200, |
129
|
|
|
['Content-Type' => 'application/pdf', |
130
|
|
|
'Content-Disposition' => 'attachment; filename="'.$filename.'"'] |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
8 |
|
private function initialization($file = NULL, $lang = NULL) |
135
|
|
|
{ |
136
|
8 |
|
$this->cvxmlfile = $file; |
137
|
8 |
|
if (!$this->cvxmlfile) { |
138
|
|
|
// Retreive the CV file depending the configuration |
139
|
1 |
|
$this->cvxmlfile = $this->container->getParameter(FabienCrassatCurriculumVitaeExtension::DEFAULT_CV); |
140
|
1 |
|
} |
141
|
|
|
// Check the file in the filesystem |
142
|
8 |
|
$this->pathToFile = |
143
|
8 |
|
$this->container->getParameter(FabienCrassatCurriculumVitaeExtension::PATH_TO_CV) |
144
|
8 |
|
.'/'.$this->cvxmlfile.'.xml'; |
145
|
|
|
|
146
|
8 |
|
if (!is_file($this->pathToFile)) { |
147
|
1 |
|
throw new NotFoundHttpException( |
148
|
1 |
|
'There is no curriculum vitae file defined for '.$this->cvxmlfile.' ('.$this->pathToFile.').'); |
149
|
|
|
} |
150
|
|
|
|
151
|
7 |
|
$this->lang = $lang; |
152
|
7 |
|
if (!$this->lang) { |
153
|
2 |
|
$this->lang = $this->container->getParameter(FabienCrassatCurriculumVitaeExtension::DEFAULT_LANG); |
154
|
2 |
|
} |
155
|
|
|
|
156
|
7 |
|
$this->readCVFile(); |
157
|
6 |
|
} |
158
|
|
|
|
159
|
7 |
|
private function readCVFile() { |
160
|
|
|
// Read the Curriculum Vitae |
161
|
7 |
|
$this->curriculumVitae = new CurriculumVitae($this->pathToFile, $this->lang); |
162
|
|
|
|
163
|
|
|
// Check if there is at least 1 language defined |
164
|
7 |
|
$this->exposedLanguages = $this->curriculumVitae->getDropDownLanguages(); |
165
|
7 |
|
if (is_array($this->exposedLanguages) && !array_key_exists($this->lang, $this->exposedLanguages)) { |
166
|
1 |
|
throw new NotFoundHttpException('There is no curriculum vitae defined for the language '.$this->lang); |
167
|
|
|
} |
168
|
6 |
|
} |
169
|
|
|
|
170
|
5 |
|
private function setViewParameters() |
171
|
|
|
{ |
172
|
5 |
|
if ($this->requestFormat != 'json' && $this->requestFormat != 'xml') { |
173
|
3 |
|
$this->setToolParameters(); |
174
|
3 |
|
} |
175
|
5 |
|
$this->setParameters($this->curriculumVitae->getCurriculumVitaeArray()); |
176
|
5 |
|
} |
177
|
|
|
|
178
|
4 |
|
private function hasExportPDF() |
179
|
|
|
{ |
180
|
4 |
|
return $this->container->has(self::PDF_SNAPPY) xor $this->container->has(self::PDF_A5SYS); |
181
|
|
|
} |
182
|
|
|
|
183
|
3 |
|
private function hasSecureDisplayBundle() |
184
|
|
|
{ |
185
|
3 |
|
return $this->container->has('netinfluence.twig.secure_display_extension'); |
186
|
|
|
} |
187
|
|
|
|
188
|
3 |
|
private function setToolParameters() |
189
|
|
|
{ |
190
|
3 |
|
$this->setParameters([ |
191
|
3 |
|
self::CVXMLFILE => $this->cvxmlfile, |
192
|
3 |
|
'languageView' => $this->lang, |
193
|
3 |
|
'languages' => $this->exposedLanguages, |
194
|
3 |
|
'anchors' => $this->curriculumVitae->getAnchors(), |
195
|
3 |
|
'hasExportPDF' => $this->hasExportPDF(), |
196
|
3 |
|
'hasSecureDisplayBundle' => $this->hasSecureDisplayBundle() |
197
|
3 |
|
]); |
198
|
3 |
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param array $parametersToAdd |
202
|
|
|
*/ |
203
|
5 |
|
private function setParameters(array $parametersToAdd) |
204
|
|
|
{ |
205
|
5 |
|
$this->parameters = array_merge($this->parameters, $parametersToAdd); |
206
|
5 |
|
} |
207
|
|
|
} |
208
|
|
|
|