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 | * @Route("/{_locale}/search", name="search", defaults={"_locale": "en"}, requirements={"_locale": "%app.locales%"}) | ||||
42 | * @param string $_locale | ||||
43 | * @param Request $request | ||||
44 | * @param TranslatorInterface $translator | ||||
45 | * @return Response | ||||
46 | * @throws \Psr\Cache\InvalidArgumentException | ||||
47 | */ | ||||
48 | public function searchAction(string $_locale, Request $request, TranslatorInterface $translator): Response | ||||
49 | { | ||||
50 | // create search form | ||||
51 | $search = new Search(); | ||||
52 | $form = $this->createForm(SearchBarType::class, $search, [ | ||||
53 | 'action' => $this->generateUrl('search'), | ||||
54 | ]); | ||||
55 | | ||||
56 | $form->handleRequest($request); | ||||
57 | | ||||
58 | if ($form->isSubmitted() && $form->isValid()) { | ||||
59 | // do the search with the provided search term | ||||
60 | $searchTerm = $search->getSearchTerm(); | ||||
61 | $cache = new FilesystemAdapter(); | ||||
62 | $searchResult = []; | ||||
63 | $searchTermSimple = str_replace('/', '-', $searchTerm); | ||||
64 | $searchResultCache = $cache->getItem('search.' . $_locale . '.' . $searchTermSimple); | ||||
65 | | ||||
66 | if (!$searchResultCache->isHit()) { | ||||
67 | foreach (GeneratedData::modules_programs as $module => $moduleContent) { | ||||
68 | foreach ($moduleContent as $submodule) { | ||||
69 | $array = explode('/', $submodule); | ||||
70 | if (array_key_exists($_locale, GeneratedData::programs_docs[$submodule])) { | ||||
71 | $branch_available = GeneratedData::programs_docs[$submodule][$_locale]; | ||||
72 | | ||||
73 | if (count($array) == 1 && strpos(($displayName = $translator->trans($submodule, [], "modules")), $searchTerm) !== false) { | ||||
74 | $searchResult[$module][$submodule] = ["branch" => $branch_available]; | ||||
75 | } elseif (count($array) == 2 | ||||
76 | && strpos(($displayName = $translator->trans($array[1], [], "modules")), $searchTerm) !== false) { | ||||
77 | $searchResult[$module][$array[0]][$array[1]] = ["branch" => $branch_available]; | ||||
78 | } | ||||
79 | } | ||||
80 | } | ||||
81 | } | ||||
82 | $searchResultCache->set($searchResult); | ||||
83 | $cache->save($searchResultCache); | ||||
84 | } else { | ||||
85 | $searchResult = $searchResultCache->get(); | ||||
86 | } | ||||
87 | | ||||
88 | | ||||
89 | return $this->render('search.html.twig', [ | ||||
90 | 'lang' => $_locale, | ||||
91 | 'search_term' => $search->getSearchTerm(), | ||||
92 | 'search_result' => $searchResult, | ||||
93 | 'branch_descriptions' => GeneratedData::branch_description, | ||||
94 | 'all_application_names' => $this->getAllApplicationsName($_locale, $translator), | ||||
95 | 'search_form' => $this->createSearchForm()->createView(), | ||||
96 | 'languages' => GeneratedData::languagelist, | ||||
97 | ]); | ||||
98 | } | ||||
99 | | ||||
100 | // no search term provided | ||||
101 | // create page with only search form | ||||
102 | return $this->render('search.html.twig', [ | ||||
103 | 'lang' => $_locale, | ||||
104 | 'search_result' => null, | ||||
105 | 'all_application_names' => $this->getAllApplicationsName($_locale, $translator), | ||||
106 | 'search_form' => $this->createSearchForm()->createView(), | ||||
107 | 'languages' => GeneratedData::languagelist, | ||||
108 | 'branch_descriptions' => GeneratedData::branch_description, | ||||
109 | ]); | ||||
110 | | ||||
111 | } | ||||
112 | | ||||
113 | /** | ||||
114 | * @Route("/{_locale}/{module}", name="module", defaults={"_locale": "en"}, requirements={"_locale": "%app.locales%"}) | ||||
115 | * @param string $_locale | ||||
116 | * @param string $module | ||||
117 | * @param TranslatorInterface $translator | ||||
118 | * @return Response | ||||
119 | * @throws \Exception | ||||
120 | */ | ||||
121 | public function moduleAction(string $_locale, string $module, TranslatorInterface $translator): Response | ||||
122 | { | ||||
123 | if (!array_key_exists($module, GeneratedData::modules_programs)) { | ||||
124 | throw new \Exception("Module is unknown"); | ||||
125 | } | ||||
126 | | ||||
127 | $submodules = []; | ||||
128 | $subsubmodules = []; // contains submodules with a bigger hierarchy | ||||
129 | | ||||
130 | // first add all submodules that aren't kcm | ||||
131 | foreach (GeneratedData::modules_programs[$module] as $submodule) { | ||||
132 | if (in_array($module . '/' . $submodule, StaticAppData::ignore_array)) { | ||||
133 | continue; | ||||
134 | } | ||||
135 | $array = explode('/', $submodule); | ||||
136 | if (count($array) === 1 && $array[0]) { | ||||
137 | if (array_key_exists($_locale, GeneratedData::programs_docs[$submodule])) { | ||||
138 | $branch_available = GeneratedData::programs_docs[$submodule][$_locale]; | ||||
139 | $submodules[$submodule] = ["branch" => $branch_available]; | ||||
140 | } | ||||
141 | } | ||||
142 | } | ||||
143 | | ||||
144 | // now add the others submodules | ||||
145 | foreach (GeneratedData::modules_programs[$module] as $submodule) { | ||||
146 | if (in_array($module . '/' . $submodule, StaticAppData::ignore_array)) { | ||||
147 | continue; | ||||
148 | } | ||||
149 | $array = explode('/', $submodule); | ||||
150 | if (count($array) !== 1 && array_key_exists($_locale, GeneratedData::programs_docs[$submodule])) { | ||||
151 | $branch_available = GeneratedData::programs_docs[$submodule][$_locale]; | ||||
152 | $subsubmodules[$array[0]][$array[1]] = ["branch" => $branch_available]; | ||||
153 | } | ||||
154 | } | ||||
155 | | ||||
156 | return $this->render('module.html.twig', [ | ||||
157 | 'lang' => $_locale, | ||||
158 | 'module' => $module, | ||||
159 | 'categories' => $submodules, | ||||
160 | 'subsubmodules' => $subsubmodules, | ||||
161 | 'branch_descriptions' => GeneratedData::branch_description, | ||||
162 | 'search_form' => $this->createSearchForm()->createView(), | ||||
163 | 'all_application_names' => $this->getAllApplicationsName($_locale, $translator), | ||||
164 | 'languages' => GeneratedData::languagelist, | ||||
165 | ]); | ||||
166 | } | ||||
167 | | ||||
168 | public function languageListDropdown() | ||||
169 | { | ||||
170 | return $this->render( | ||||
171 | 'language_dropdown.html.twig', [ | ||||
172 | 'languages' => GeneratedData::languagelist, | ||||
173 | 'full_language_name' => StaticAppData::full_language_name, | ||||
174 | ] | ||||
175 | ); | ||||
176 | } | ||||
177 | | ||||
178 | /** | ||||
179 | * @return SearchBarType | ||||
180 | */ | ||||
181 | private function createSearchForm(): FormInterface | ||||
182 | { | ||||
183 | // create search form | ||||
184 | $search = new Search(); | ||||
185 | $form = $this->createForm(SearchBarType::class, $search, [ | ||||
186 | 'action' => $this->generateUrl('search'), | ||||
187 | ]); | ||||
188 | return $form; | ||||
189 | } | ||||
190 | | ||||
191 | private function getAllApplicationsName(string $_locale, TranslatorInterface $translator): string | ||||
192 | { | ||||
193 | $cache = new FilesystemAdapter(); | ||||
194 | $applicationsNameCache = $cache->getItem('applicationsName.' . $_locale); | ||||
195 | | ||||
196 | if (!$applicationsNameCache->isHit()) { | ||||
197 | $applicationsName = []; | ||||
198 | foreach (GeneratedData::modules_programs as $module => $moduleContent) { | ||||
199 | foreach ($moduleContent as $submodule) { | ||||
200 | $array = explode('/', $submodule); | ||||
201 | if (count($array) == 1) { | ||||
202 | $applicationsName[] = $translator->trans($submodule, [], "modules"); | ||||
203 | } elseif (count($array) == 2) { | ||||
204 | $applicationsName[] = $translator->trans($array[1], [], "modules"); | ||||
205 | } | ||||
206 | } | ||||
207 | } | ||||
208 | $applicationsNameString = ''; | ||||
209 | foreach ($applicationsName as $name) { | ||||
210 | $applicationsNameString .= $name . ', '; | ||||
211 | } | ||||
212 | $applicationsNameCache->set($applicationsNameString); | ||||
213 | $cache->save($applicationsNameCache); | ||||
214 | return $applicationsNameString; | ||||
215 | } else { | ||||
216 | return $applicationsNameCache->get(); | ||||
217 | } | ||||
218 | } | ||||
219 | | ||||
220 | | ||||
221 | /** | ||||
222 | * Check if translation exist | ||||
223 | * @param string $app | ||||
224 | * @param string $lang | ||||
225 | * @return bool | ||||
226 | */ | ||||
227 | function language_for_application_exist(string $app, string $lang): bool | ||||
228 | { | ||||
229 | return (array_key_exists($lang, $this->programs_docs[$app])); | ||||
230 | } | ||||
231 | | ||||
232 | /** | ||||
233 | * Check if text exist for the specified branch and language | ||||
234 | * @param $app | ||||
235 | * @param $lang | ||||
236 | * @param $branch | ||||
237 | * @return string the branch | ||||
238 | */ | ||||
239 | function branch_exist($app, $lang, $branch): string | ||||
240 | { | ||||
241 | // no translations for the specified application in the current language | ||||
242 | if (!$this->language_for_application_exist($app, $lang)) { | ||||
243 | return ""; | ||||
244 | } | ||||
245 | // Handle old URL - compatibility with the old schema | ||||
246 | $compatibility_branches = array( | ||||
247 | "development" => array("trunk5", "trunk4"), | ||||
248 | "stable" => array("stable5", "stable4"), | ||||
249 | ); | ||||
250 | // Search in compatibility branches first | ||||
251 | if (array_key_exists($branch, $compatibility_branches)) { | ||||
252 | foreach ($compatibility_branches[$branch] as $comp_branch) { | ||||
253 | # echo "Looking for $comp_branch into ".var_dump($this->programs_docs[$app][$lang]); | ||||
254 | if (in_array($comp_branch, $this->programs_docs[$app][$lang])) { | ||||
255 | return $comp_branch; | ||||
256 | } | ||||
257 | } | ||||
258 | } | ||||
259 | // Normal branch | ||||
260 | if (in_array($branch, $this->programs_docs[$app][$lang])) { | ||||
261 | return $branch; | ||||
262 | } | ||||
263 | return ""; | ||||
264 | } | ||||
265 | } | ||||
266 | |