Changeset View
Changeset View
Standalone View
Standalone View
src/Controller/MainController.php
- This file was added.
1 | <?php | ||||
---|---|---|---|---|---|
2 | | ||||
3 | namespace App\Controller; | ||||
4 | | ||||
5 | use App\Entity\Search; | ||||
6 | use App\Form\SearchBarType; | ||||
7 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||||
8 | use Symfony\Component\Form\FormInterface; | ||||
9 | use Symfony\Component\HttpFoundation\Request; | ||||
10 | use Symfony\Component\HttpFoundation\Response; | ||||
11 | use Symfony\Component\Routing\Annotation\Route; | ||||
12 | use App\Data\GeneratedData; | ||||
13 | use App\Data\StaticAppData; | ||||
14 | use Symfony\Component\Cache\Adapter\FilesystemAdapter; | ||||
15 | use Symfony\Contracts\Translation\TranslatorInterface; | ||||
16 | | ||||
17 | class MainController extends AbstractController | ||||
18 | { | ||||
19 | private $programs_docs; | ||||
20 | | ||||
21 | /** | ||||
22 | * Return the homepage | ||||
23 | * @Route("/{_locale}", name="index", defaults={"_locale": "en"}, requirements={"_locale": "%app.locales%"}) | ||||
24 | * @param string $_locale | ||||
25 | * @param TranslatorInterface $translator | ||||
26 | * @return Response | ||||
27 | */ | ||||
28 | public function indexAction(string $_locale, TranslatorInterface $translator): Response | ||||
29 | { | ||||
30 | return $this->render('index.html.twig', [ | ||||
31 | 'modules_programs' => GeneratedData::modules_programs, | ||||
32 | 'lang' => $_locale, | ||||
33 | 'search_form' => $this->createSearchForm()->createView(), | ||||
34 | 'all_application_names' => $this->getAllApplicationsName($_locale, $translator), | ||||
35 | 'languages' => GeneratedData::languagelist, | ||||
36 | 'branch_descriptions' => GeneratedData::branch_description, | ||||
37 | ]); | ||||
38 | } | ||||
39 | | ||||
40 | /** | ||||
41 | * Return the dropdown menu in case the user has noScript | ||||
42 | * @Route("/{_locale}/menu", name="menu", defaults={"_locale": "en"}, requirements={"_locale": "%app.locales%"}) | ||||
43 | * @param string $_locale | ||||
44 | * @param TranslatorInterface $translator | ||||
45 | * @return Response | ||||
46 | */ | ||||
47 | public function menuAction(string $_locale, TranslatorInterface $translator): Response | ||||
48 | { | ||||
49 | return $this->render('menu.html.twig', [ | ||||
50 | 'lang' => $_locale, | ||||
51 | 'search_form' => $this->createSearchForm()->createView(), | ||||
52 | 'all_application_names' => $this->getAllApplicationsName($_locale, $translator), | ||||
53 | 'languages' => GeneratedData::languagelist, | ||||
54 | ]); | ||||
55 | } | ||||
56 | | ||||
57 | | ||||
58 | /** | ||||
59 | * @Route("/{_locale}/search", name="search", defaults={"_locale": "en"}, requirements={"_locale": "%app.locales%"}) | ||||
60 | * @param string $_locale | ||||
61 | * @param Request $request | ||||
62 | * @param TranslatorInterface $translator | ||||
63 | * @return Response | ||||
64 | * @throws \Psr\Cache\InvalidArgumentException | ||||
65 | */ | ||||
66 | public function searchAction(string $_locale, Request $request, TranslatorInterface $translator): Response | ||||
67 | { | ||||
68 | // create search form | ||||
69 | $search = new Search(); | ||||
70 | $form = $this->createForm(SearchBarType::class, $search, [ | ||||
71 | 'action' => $this->generateUrl('search'), | ||||
72 | ]); | ||||
73 | | ||||
74 | $form->handleRequest($request); | ||||
75 | | ||||
76 | if ($form->isSubmitted() && $form->isValid()) { | ||||
77 | // do the search with the provided search term | ||||
78 | $searchTerm = $search->getSearchTerm(); | ||||
79 | $cache = new FilesystemAdapter(); | ||||
80 | $searchResult = []; | ||||
81 | $searchTermSimple = str_replace('/', '-', $searchTerm); | ||||
82 | $searchResultCache = $cache->getItem('search.' . $_locale . '.' . $searchTermSimple); | ||||
83 | $searchResultCache->expiresAt(new \DateTime('tomorrow')); | ||||
84 | | ||||
85 | if (!$searchResultCache->isHit()) { | ||||
86 | foreach (GeneratedData::modules_programs as $module => $moduleContent) { | ||||
87 | foreach ($moduleContent as $submodule) { | ||||
88 | $array = explode('/', $submodule); | ||||
89 | if (array_key_exists($_locale, GeneratedData::programs_docs[$submodule])) { | ||||
90 | $branch_available = GeneratedData::programs_docs[$submodule][$_locale]; | ||||
91 | | ||||
92 | if (count($array) == 1 && strpos(($displayName = $translator->trans($submodule, [], "modules")), $searchTerm) !== false) { | ||||
93 | $searchResult[$module][$submodule] = ["branch" => $branch_available]; | ||||
94 | } elseif (count($array) == 2 | ||||
95 | && strpos(($displayName = $translator->trans($array[1], [], "modules")), $searchTerm) !== false) { | ||||
96 | $searchResult[$module][$array[0]][$array[1]] = ["branch" => $branch_available]; | ||||
97 | } | ||||
98 | } | ||||
99 | } | ||||
100 | } | ||||
101 | $searchResultCache->set($searchResult); | ||||
102 | $cache->save($searchResultCache); | ||||
103 | } else { | ||||
104 | $searchResult = $searchResultCache->get(); | ||||
105 | } | ||||
106 | | ||||
107 | | ||||
108 | return $this->render('search.html.twig', [ | ||||
109 | 'lang' => $_locale, | ||||
110 | 'search_term' => $search->getSearchTerm(), | ||||
111 | 'search_result' => $searchResult, | ||||
112 | 'branch_descriptions' => GeneratedData::branch_description, | ||||
113 | 'all_application_names' => $this->getAllApplicationsName($_locale, $translator), | ||||
114 | 'search_form' => $this->createSearchForm()->createView(), | ||||
115 | 'languages' => GeneratedData::languagelist, | ||||
116 | ]); | ||||
117 | } | ||||
118 | | ||||
119 | // no search term provided | ||||
120 | // create page with only search form | ||||
121 | return $this->render('search.html.twig', [ | ||||
122 | 'lang' => $_locale, | ||||
123 | 'search_result' => null, | ||||
124 | 'all_application_names' => $this->getAllApplicationsName($_locale, $translator), | ||||
125 | 'search_form' => $this->createSearchForm()->createView(), | ||||
126 | 'languages' => GeneratedData::languagelist, | ||||
127 | 'branch_descriptions' => GeneratedData::branch_description, | ||||
128 | ]); | ||||
129 | | ||||
130 | } | ||||
131 | | ||||
132 | /** | ||||
133 | * @Route("/{_locale}/{module}", name="module", defaults={"_locale": "en"}, requirements={"_locale": "%app.locales%"}) | ||||
134 | * @param string $_locale | ||||
135 | * @param string $module | ||||
136 | * @param TranslatorInterface $translator | ||||
137 | * @return Response | ||||
138 | * @throws \Exception | ||||
139 | */ | ||||
140 | public function moduleAction(string $_locale, string $module, TranslatorInterface $translator): Response | ||||
141 | { | ||||
142 | if (!array_key_exists($module, GeneratedData::modules_programs)) { | ||||
143 | throw new \Exception("Module is unknown"); | ||||
144 | } | ||||
145 | | ||||
146 | $submodules = []; | ||||
147 | $subsubmodules = []; // contains submodules with a bigger hierarchy | ||||
148 | | ||||
149 | // first add all submodules that aren't kcm | ||||
150 | foreach (GeneratedData::modules_programs[$module] as $submodule) { | ||||
151 | if (in_array($module . '/' . $submodule, StaticAppData::ignore_array)) { | ||||
152 | continue; | ||||
153 | } | ||||
154 | $array = explode('/', $submodule); | ||||
155 | if (count($array) === 1 && $array[0]) { | ||||
156 | if (array_key_exists($_locale, GeneratedData::programs_docs[$submodule])) { | ||||
157 | $branch_available = GeneratedData::programs_docs[$submodule][$_locale]; | ||||
158 | $submodules[$submodule] = ["branch" => $branch_available]; | ||||
159 | } | ||||
160 | } | ||||
161 | } | ||||
162 | | ||||
163 | // now add the others submodules | ||||
164 | foreach (GeneratedData::modules_programs[$module] as $submodule) { | ||||
165 | if (in_array($module . '/' . $submodule, StaticAppData::ignore_array)) { | ||||
166 | continue; | ||||
167 | } | ||||
168 | $array = explode('/', $submodule); | ||||
169 | if (count($array) !== 1 && array_key_exists($_locale, GeneratedData::programs_docs[$submodule])) { | ||||
170 | $branch_available = GeneratedData::programs_docs[$submodule][$_locale]; | ||||
171 | $subsubmodules[$array[0]][$array[1]] = ["branch" => $branch_available]; | ||||
172 | } | ||||
173 | } | ||||
174 | | ||||
175 | return $this->render('module.html.twig', [ | ||||
176 | 'lang' => $_locale, | ||||
177 | 'module' => $module, | ||||
178 | 'categories' => $submodules, | ||||
179 | 'subsubmodules' => $subsubmodules, | ||||
180 | 'branch_descriptions' => GeneratedData::branch_description, | ||||
181 | 'search_form' => $this->createSearchForm()->createView(), | ||||
182 | 'all_application_names' => $this->getAllApplicationsName($_locale, $translator), | ||||
183 | 'languages' => GeneratedData::languagelist, | ||||
184 | ]); | ||||
185 | } | ||||
186 | | ||||
187 | public function languageListDropdown() | ||||
188 | { | ||||
189 | return $this->render( | ||||
190 | 'language_dropdown.html.twig', [ | ||||
191 | 'languages' => GeneratedData::languagelist, | ||||
192 | 'full_language_name' => StaticAppData::full_language_name, | ||||
193 | ] | ||||
194 | ); | ||||
195 | } | ||||
196 | | ||||
197 | /** | ||||
198 | * @return SearchBarType | ||||
199 | */ | ||||
200 | private function createSearchForm(): FormInterface | ||||
201 | { | ||||
202 | // create search form | ||||
203 | $search = new Search(); | ||||
204 | $form = $this->createForm(SearchBarType::class, $search, [ | ||||
205 | 'action' => $this->generateUrl('search'), | ||||
206 | ]); | ||||
207 | return $form; | ||||
208 | } | ||||
209 | | ||||
210 | private function getAllApplicationsName(string $_locale, TranslatorInterface $translator): string | ||||
211 | { | ||||
212 | $cache = new FilesystemAdapter(); | ||||
213 | $applicationsNameCache = $cache->getItem('applicationsName.' . $_locale); | ||||
214 | | ||||
215 | if (!$applicationsNameCache->isHit()) { | ||||
216 | $applicationsName = []; | ||||
217 | foreach (GeneratedData::modules_programs as $module => $moduleContent) { | ||||
218 | foreach ($moduleContent as $submodule) { | ||||
219 | $array = explode('/', $submodule); | ||||
220 | if (count($array) == 1) { | ||||
221 | $applicationsName[] = $translator->trans($submodule, [], "modules"); | ||||
222 | } elseif (count($array) == 2) { | ||||
223 | $applicationsName[] = $translator->trans($array[1], [], "modules"); | ||||
224 | } | ||||
225 | } | ||||
226 | } | ||||
227 | $applicationsNameString = ''; | ||||
228 | foreach ($applicationsName as $name) { | ||||
229 | $applicationsNameString .= $name . ', '; | ||||
230 | } | ||||
231 | $applicationsNameCache->set($applicationsNameString); | ||||
232 | $applicationsNameCache->expiresAt(new \DateTime('tomorrow')); | ||||
233 | $cache->save($applicationsNameCache); | ||||
234 | return $applicationsNameString; | ||||
235 | } else { | ||||
236 | return $applicationsNameCache->get(); | ||||
237 | } | ||||
238 | } | ||||
239 | | ||||
240 | | ||||
241 | /** | ||||
242 | * Check if translation exist | ||||
243 | * @param string $app | ||||
244 | * @param string $lang | ||||
245 | * @return bool | ||||
246 | */ | ||||
247 | function language_for_application_exist(string $app, string $lang): bool | ||||
248 | { | ||||
249 | return (array_key_exists($lang, $this->programs_docs[$app])); | ||||
250 | } | ||||
251 | | ||||
252 | /** | ||||
253 | * Check if text exist for the specified branch and language | ||||
254 | * @param $app | ||||
255 | * @param $lang | ||||
256 | * @param $branch | ||||
257 | * @return string the branch | ||||
258 | */ | ||||
259 | function branch_exist($app, $lang, $branch): string | ||||
260 | { | ||||
261 | // no translations for the specified application in the current language | ||||
262 | if (!$this->language_for_application_exist($app, $lang)) { | ||||
263 | return ""; | ||||
264 | } | ||||
265 | // Handle old URL - compatibility with the old schema | ||||
266 | $compatibility_branches = array( | ||||
267 | "development" => array("trunk5", "trunk4"), | ||||
268 | "stable" => array("stable5", "stable4"), | ||||
269 | ); | ||||
270 | // Search in compatibility branches first | ||||
271 | if (array_key_exists($branch, $compatibility_branches)) { | ||||
272 | foreach ($compatibility_branches[$branch] as $comp_branch) { | ||||
273 | # echo "Looking for $comp_branch into ".var_dump($this->programs_docs[$app][$lang]); | ||||
274 | if (in_array($comp_branch, $this->programs_docs[$app][$lang])) { | ||||
275 | return $comp_branch; | ||||
276 | } | ||||
277 | } | ||||
278 | } | ||||
279 | // Normal branch | ||||
280 | if (in_array($branch, $this->programs_docs[$app][$lang])) { | ||||
281 | return $branch; | ||||
282 | } | ||||
283 | return ""; | ||||
284 | } | ||||
285 | } | ||||
286 | |