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 Symfony\Component\DependencyInjection\ContainerAwareInterface; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
17
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
18
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
21
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
22
|
|
|
use Symfony\Component\Serializer\Serializer; |
23
|
|
|
use Symfony\Component\Serializer\Encoder\XmlEncoder; |
24
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder; |
25
|
|
|
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; |
26
|
|
|
|
27
|
|
|
class DefaultController implements ContainerAwareInterface |
28
|
|
|
{ |
29
|
|
|
use ContainerAwareTrait; |
30
|
|
|
|
31
|
|
|
private $cvxmlfile; |
32
|
|
|
private $pathToFile; |
33
|
|
|
private $lang; |
34
|
|
|
private $curriculumVitae; |
35
|
|
|
private $exposedLanguages; |
36
|
|
|
private $requestFormat; |
37
|
|
|
private $parameters = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return Response |
41
|
|
|
*/ |
42
|
|
|
public function indexAction($cvxmlfile = NULL) |
43
|
|
|
{ |
44
|
|
|
if($cvxmlfile) { |
45
|
|
|
$path = array( |
46
|
|
|
'_controller' => 'FabienCrassatCurriculumVitaeBundle:Default:display', |
47
|
|
|
'cvxmlfile' => $cvxmlfile, |
48
|
|
|
'_locale' => $this->lang, |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$request = $this->container->get('request'); |
52
|
|
|
$subRequest = $request->duplicate(array(), NULL, $path); |
53
|
|
|
|
54
|
|
|
$httpKernel = $this->container->get('http_kernel'); |
55
|
|
|
$response = $httpKernel->handle( |
56
|
|
|
$subRequest, |
57
|
|
|
HttpKernelInterface::SUB_REQUEST |
58
|
|
|
); |
59
|
|
|
return $response; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->initialization($cvxmlfile); |
63
|
|
|
return new RedirectResponse($this->container->get('router')->generate( |
64
|
|
|
'fabiencrassat_curriculumvitae_cvxmlfileonly', |
65
|
|
|
array( |
66
|
|
|
'cvxmlfile' => $this->cvxmlfile, |
67
|
|
|
)), 301); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return Response |
72
|
|
|
*/ |
73
|
|
|
public function displayAction($cvxmlfile, $_locale, Request $request) |
74
|
|
|
{ |
75
|
|
|
$this->initialization($cvxmlfile, $_locale); |
76
|
|
|
$this->requestFormat = $request->getRequestFormat(); |
77
|
|
|
$this->setViewParameters(); |
78
|
|
|
|
79
|
|
|
switch ($this->requestFormat) { |
80
|
|
|
case 'json': |
81
|
|
|
return new Response(json_encode($this->parameters)); |
82
|
|
|
case 'xml': |
83
|
|
|
//initialisation du serializer |
84
|
|
|
$encoders = array(new XmlEncoder('CurriculumVitae'), new JsonEncoder()); |
85
|
|
|
$normalizers = array(new GetSetMethodNormalizer()); |
86
|
|
|
$serializer = new Serializer($normalizers, $encoders); |
87
|
|
|
|
88
|
|
|
$response = new Response(); |
89
|
|
|
$response->setContent($serializer->serialize($this->parameters, 'xml')); |
90
|
|
|
$response->headers->set('Content-Type', 'application/xml'); |
91
|
|
|
|
92
|
|
|
return $response; |
93
|
|
|
default: |
94
|
|
|
return $this->container->get('templating')->renderResponse( |
95
|
|
|
$this->container->getParameter('fabiencrassat_curriculumvitae.template'), |
96
|
|
|
$this->parameters); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return Response |
102
|
|
|
*/ |
103
|
|
|
public function exportPDFAction($cvxmlfile, $_locale) |
104
|
|
|
{ |
105
|
|
|
if (!$this->hasExportPDF()) { |
106
|
|
|
throw new NotFoundHttpException('No export PDF service installed.'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->initialization($cvxmlfile, $_locale); |
110
|
|
|
$this->setViewParameters(); |
111
|
|
|
|
112
|
|
|
$html = $this->container->get('templating')->render( |
|
|
|
|
113
|
|
|
'FabienCrassatCurriculumVitaeBundle:CurriculumVitae:index.pdf.twig', |
114
|
|
|
$this->parameters); |
115
|
|
|
$filename = $this->curriculumVitae->getHumanFileName().'.pdf'; |
116
|
|
|
|
117
|
|
|
$hasPdfService = false; |
118
|
|
|
if (!$hasPdfService && $this->container->has('a5sys_pdf.pdf_service')) { |
119
|
|
|
$hasPdfService = true; |
120
|
|
|
$content = $this->container->get('a5sys_pdf.pdf_service')->sendPDF($html, $filename); |
|
|
|
|
121
|
|
|
}; |
122
|
|
|
if (!$hasPdfService && $this->container->has('knp_snappy.pdf')) { |
123
|
|
|
$hasPdfService = true; |
|
|
|
|
124
|
|
|
$content = $this->container->get('knp_snappy.pdf')->getOutputFromHtml($html); |
|
|
|
|
125
|
|
|
}; |
126
|
|
|
|
127
|
|
|
return new Response($content, |
|
|
|
|
128
|
|
|
200, |
129
|
|
|
array('Content-Type' => 'application/pdf', |
130
|
|
|
'Content-Disposition' => 'attachment; filename="'.$filename.'"') |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private function initialization($file = NULL, $lang = NULL) |
135
|
|
|
{ |
136
|
|
|
$this->cvxmlfile = $file; |
137
|
|
|
if (!$this->cvxmlfile) { |
138
|
|
|
// Retreive the CV file depending the configuration |
139
|
|
|
$this->cvxmlfile = $this->container->getParameter('fabiencrassat_curriculumvitae.default_cv'); |
140
|
|
|
} |
141
|
|
|
// Check the file in the filesystem |
142
|
|
|
$this->pathToFile = |
143
|
|
|
$this->container->getParameter('fabiencrassat_curriculumvitae.path_to_cv') |
144
|
|
|
.'/'.$this->cvxmlfile.'.xml'; |
145
|
|
|
|
146
|
|
|
if (!is_file($this->pathToFile)) { |
147
|
|
|
throw new NotFoundHttpException( |
148
|
|
|
'There is no curriculum vitae file defined for '.$this->cvxmlfile.' ('.$this->pathToFile.').'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$this->lang = $lang; |
152
|
|
|
if (!$this->lang) { |
153
|
|
|
$this->lang = $this->container->getParameter('fabiencrassat_curriculumvitae.default_lang'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$this->readCVFile(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
private function readCVFile() { |
160
|
|
|
// Read the Curriculum Vitae |
161
|
|
|
$this->curriculumVitae = new CurriculumVitae($this->pathToFile, $this->lang); |
162
|
|
|
|
163
|
|
|
// Check if there is at least 1 language defined |
164
|
|
|
$this->exposedLanguages = $this->curriculumVitae->getDropDownLanguages(); |
165
|
|
|
if(is_array($this->exposedLanguages)) { |
166
|
|
|
if (!array_key_exists($this->lang, $this->exposedLanguages)) { |
167
|
|
|
throw new NotFoundHttpException('There is no curriculum vitae defined for the language '.$this->lang); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
private function setViewParameters() |
173
|
|
|
{ |
174
|
|
|
if ($this->requestFormat != 'json' && $this->requestFormat != 'xml') { |
175
|
|
|
$this->setToolParameters(); |
176
|
|
|
} |
177
|
|
|
$this->setCoreParameters(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
private function hasExportPDF() |
181
|
|
|
{ |
182
|
|
|
return $this->container->has('knp_snappy.pdf') xor $this->container->has('a5sys_pdf.pdf_service'); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
private function hasSecureDisplayBundle() |
186
|
|
|
{ |
187
|
|
|
return $this->container->has('netinfluence.twig.secure_display_extension'); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
private function setToolParameters() |
191
|
|
|
{ |
192
|
|
|
$this->setParameters(array( |
193
|
|
|
'cvxmlfile' => $this->cvxmlfile, |
194
|
|
|
'languageView' => $this->lang, |
195
|
|
|
'languages' => $this->exposedLanguages, |
196
|
|
|
'anchors' => $this->curriculumVitae->getAnchors(), |
197
|
|
|
'hasExportPDF' => $this->hasExportPDF(), |
198
|
|
|
'hasSecureDisplayBundle' => $this->hasSecureDisplayBundle(), |
199
|
|
|
)); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
private function setCoreParameters() |
203
|
|
|
{ |
204
|
|
|
$this->setParameters(array( |
205
|
|
|
'identity' => $this->curriculumVitae->getIdentity(), |
206
|
|
|
'followMe' => $this->curriculumVitae->getFollowMe(), |
207
|
|
|
'lookingFor' => $this->curriculumVitae->getLookingFor(), |
208
|
|
|
'experiences' => $this->curriculumVitae->getExperiences(), |
209
|
|
|
'skills' => $this->curriculumVitae->getSkills(), |
210
|
|
|
'educations' => $this->curriculumVitae->getEducations(), |
211
|
|
|
'languageSkills' => $this->curriculumVitae->getLanguageSkills(), |
212
|
|
|
'miscellaneous' => $this->curriculumVitae->getMiscellaneous(), |
213
|
|
|
)); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param array $parametersToAdd |
218
|
|
|
*/ |
219
|
|
|
private function setParameters(array $parametersToAdd) |
220
|
|
|
{ |
221
|
|
|
$this->parameters = array_merge($this->parameters, $parametersToAdd); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.