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