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\Bundle\FrameworkBundle\Controller\AbstractController; |
||
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 extends AbstractController |
||
28 | { |
||
29 | private $cvxmlfile; |
||
30 | private $pathToFile; |
||
31 | private $lang; |
||
32 | private $curriculumVitae; |
||
33 | private $exposedLanguages; |
||
34 | private $requestFormat; |
||
35 | private $parameters = []; |
||
36 | |||
37 | const CVXMLFILE = 'cvxmlfile'; |
||
38 | const PDF_A5SYS = 'a5sys_pdf.pdf_service'; |
||
39 | const PDF_SNAPPY = 'knp_snappy.pdf'; |
||
40 | |||
41 | /** |
||
42 | * @return Response |
||
43 | */ |
||
44 | 3 | public function indexAction($cvxmlfile = NULL, Request $request) |
|
45 | { |
||
46 | 3 | if ($cvxmlfile) { |
|
47 | 2 | $response = $this->forward( |
|
48 | 2 | 'FabienCrassat\CurriculumVitaeBundle\Controller\DefaultController::displayAction', |
|
49 | array( |
||
50 | 2 | self::CVXMLFILE => $cvxmlfile, |
|
51 | 2 | '_locale' => $this->lang, |
|
52 | 2 | 'request' => $request, |
|
53 | ) |
||
54 | ); |
||
55 | 2 | return $response; |
|
56 | } |
||
57 | |||
58 | 1 | $this->initialization($cvxmlfile); |
|
59 | 1 | return new RedirectResponse($this->container->get('router')->generate( |
|
60 | 1 | 'fabiencrassat_curriculumvitae_cvxmlfileonly', |
|
61 | 1 | [self::CVXMLFILE => $this->cvxmlfile]), |
|
62 | 1 | 301); |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * @return Response |
||
67 | */ |
||
68 | 7 | public function displayAction($cvxmlfile, $_locale, Request $request) |
|
69 | { |
||
70 | 7 | $this->initialization($cvxmlfile, $_locale); |
|
71 | 5 | $this->requestFormat = $request->getRequestFormat(); |
|
72 | 5 | $this->setViewParameters(); |
|
73 | |||
74 | 5 | switch ($this->requestFormat) { |
|
75 | 5 | case 'json': |
|
76 | 1 | return new Response(json_encode($this->parameters)); |
|
77 | 4 | case 'xml': |
|
78 | //initialisation du serializer |
||
79 | 1 | $encoders = [new XmlEncoder('CurriculumVitae'), new JsonEncoder()]; |
|
80 | 1 | $normalizers = [new GetSetMethodNormalizer()]; |
|
81 | 1 | $serializer = new Serializer($normalizers, $encoders); |
|
82 | |||
83 | 1 | $response = new Response(); |
|
84 | 1 | $response->setContent($serializer->serialize($this->parameters, 'xml')); |
|
85 | 1 | $response->headers->set('Content-Type', 'application/xml'); |
|
86 | |||
87 | 1 | return $response; |
|
88 | default: |
||
89 | 3 | return $this->render( |
|
90 | 3 | $this->container->getParameter(FabienCrassatCurriculumVitaeExtension::TEMPLATE), |
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
91 | 3 | $this->parameters); |
|
92 | } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @return Response |
||
97 | */ |
||
98 | 1 | public function exportPDFAction($cvxmlfile, $_locale) |
|
99 | { |
||
100 | 1 | if (!$this->hasExportPDF()) { |
|
101 | 1 | throw new NotFoundHttpException('No export PDF service installed.'); |
|
102 | } |
||
103 | |||
104 | $this->initialization($cvxmlfile, $_locale); |
||
105 | $this->setViewParameters(); |
||
106 | |||
107 | $html = $this->container->get('templating')->render( |
||
108 | 'FabienCrassatCurriculumVitaeBundle:CurriculumVitae:index.pdf.twig', |
||
109 | $this->parameters); |
||
110 | $filename = $this->curriculumVitae->getHumanFileName().'.pdf'; |
||
111 | |||
112 | $content = ''; |
||
113 | if ($this->container->has(self::PDF_A5SYS)) { |
||
114 | $content = $this->container->get(self::PDF_A5SYS)->sendPDF($html, $filename); |
||
115 | } elseif ($this->container->has(self::PDF_SNAPPY)) { |
||
116 | $content = $this->container->get(self::PDF_SNAPPY)->getOutputFromHtml($html); |
||
117 | } |
||
118 | |||
119 | return new Response($content, 200, |
||
120 | ['Content-Type' => 'application/pdf', |
||
121 | 'Content-Disposition' => 'attachment; filename="'.$filename.'"'] |
||
122 | ); |
||
123 | } |
||
124 | |||
125 | 8 | private function initialization($file = NULL, $lang = NULL) |
|
126 | { |
||
127 | 8 | $this->cvxmlfile = $file; |
|
128 | 8 | if (!$this->cvxmlfile) { |
|
129 | // Retreive the CV file depending the configuration |
||
130 | 1 | $this->cvxmlfile = $this->container->getParameter(FabienCrassatCurriculumVitaeExtension::DEFAULT_CV); |
|
131 | } |
||
132 | // Check the file in the filesystem |
||
133 | 8 | $this->pathToFile = |
|
134 | 8 | $this->container->getParameter(FabienCrassatCurriculumVitaeExtension::PATH_TO_CV) |
|
135 | 8 | .'/'.$this->cvxmlfile.'.xml'; |
|
136 | |||
137 | 8 | if (!is_file($this->pathToFile)) { |
|
138 | 1 | throw new NotFoundHttpException( |
|
139 | 1 | 'There is no curriculum vitae file defined for "'.$this->cvxmlfile.'" ('.$this->pathToFile.').'); |
|
140 | } |
||
141 | |||
142 | 7 | $this->lang = $lang; |
|
143 | 7 | if (!$this->lang) { |
|
144 | 2 | $this->lang = $this->container->getParameter(FabienCrassatCurriculumVitaeExtension::DEFAULT_LANG); |
|
145 | } |
||
146 | |||
147 | 7 | $this->readCVFile(); |
|
148 | 6 | } |
|
149 | |||
150 | 7 | private function readCVFile() { |
|
151 | // Read the Curriculum Vitae |
||
152 | 7 | $this->curriculumVitae = new CurriculumVitae($this->pathToFile, $this->lang); |
|
153 | |||
154 | // Check if there is at least 1 language defined |
||
155 | 7 | $this->exposedLanguages = $this->curriculumVitae->getDropDownLanguages(); |
|
156 | 7 | if (is_array($this->exposedLanguages) && !array_key_exists($this->lang, $this->exposedLanguages)) { |
|
157 | 1 | throw new NotFoundHttpException('There is no curriculum vitae defined for the language '.$this->lang); |
|
158 | } |
||
159 | 6 | } |
|
160 | |||
161 | 5 | private function setViewParameters() |
|
162 | { |
||
163 | 5 | if ($this->requestFormat != 'json' && $this->requestFormat != 'xml') { |
|
164 | 3 | $this->setToolParameters(); |
|
165 | } |
||
166 | 5 | $this->setParameters($this->curriculumVitae->getCurriculumVitaeArray()); |
|
167 | 5 | } |
|
168 | |||
169 | 4 | private function hasExportPDF() |
|
170 | { |
||
171 | 4 | return $this->container->has(self::PDF_SNAPPY) xor $this->container->has(self::PDF_A5SYS); |
|
172 | } |
||
173 | |||
174 | 3 | private function hasSecureDisplayBundle() |
|
175 | { |
||
176 | 3 | return $this->container->has('netinfluence.twig.secure_display_extension'); |
|
177 | } |
||
178 | |||
179 | 3 | private function setToolParameters() |
|
180 | { |
||
181 | 3 | $this->setParameters([ |
|
182 | 3 | self::CVXMLFILE => $this->cvxmlfile, |
|
183 | 3 | 'languageView' => $this->lang, |
|
184 | 3 | 'languages' => $this->exposedLanguages, |
|
185 | 3 | 'anchors' => $this->curriculumVitae->getAnchors(), |
|
186 | 3 | 'hasExportPDF' => $this->hasExportPDF(), |
|
187 | 3 | 'hasSecureDisplayBundle' => $this->hasSecureDisplayBundle() |
|
188 | ]); |
||
189 | 3 | } |
|
190 | |||
191 | /** |
||
192 | * @param array $parametersToAdd |
||
193 | */ |
||
194 | 5 | private function setParameters(array $parametersToAdd) |
|
195 | { |
||
196 | 5 | $this->parameters = array_merge($this->parameters, $parametersToAdd); |
|
197 | 5 | } |
|
198 | } |
||
199 |