|
10 | 10 | * @package phpMyFAQ |
11 | 11 | * @author Thorsten Rinne <[email protected]> |
12 | 12 | * @author Rudi Ferrari <[email protected]> |
| 13 | + * @author Jan Harms <[email protected]> |
13 | 14 | * @copyright 2006-2024 phpMyFAQ Team |
14 | 15 | * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
15 | 16 | * @link https://www.phpmyfaq.de |
|
23 | 24 | use phpMyFAQ\Filter; |
24 | 25 | use phpMyFAQ\Language\LanguageCodes; |
25 | 26 | use phpMyFAQ\Strings; |
| 27 | +use phpMyFAQ\Template\TwigWrapper; |
26 | 28 | use phpMyFAQ\Translation; |
27 | 29 | use Symfony\Component\HttpFoundation\Request; |
| 30 | +use Twig\Extension\DebugExtension; |
28 | 31 |
|
29 | 32 | if (!defined('IS_VALID_PHPMYFAQ')) { |
30 | 33 | http_response_code(400); |
|
33 | 36 |
|
34 | 37 | $request = Request::createFromGlobals(); |
35 | 38 |
|
36 | | -?> |
37 | | - |
38 | | - <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom"> |
39 | | - <h1 class="h2"> |
40 | | - <i aria-hidden="true" class="bi bi-folder"></i> <?= Translation::get('ad_menu_categ_structure') ?> |
41 | | - </h1> |
42 | | - <div class="btn-toolbar mb-2 mb-md-0"> |
43 | | - <div class="btn-group mr-2"> |
44 | | - <a class="btn btn-outline-success" href="?action=addcategory"> |
45 | | - <i aria-hidden="true" class="bi bi-folder-plus"></i> <?= Translation::get('ad_kateg_add') ?> |
46 | | - </a> |
47 | | - <a class="btn btn-outline-info" href="?action=category-overview"> |
48 | | - <i aria-hidden="true" class="bi bi-list"></i> <?= Translation::get('ad_menu_categ_edit') ?> |
49 | | - </a> |
50 | | - </div> |
51 | | - </div> |
52 | | - </div> |
53 | | - |
54 | | -<?php |
55 | 39 | if ($user->perm->hasPermission($user->getUserId(), PermissionType::CATEGORY_EDIT->value)) { |
| 40 | + $templateVars = []; |
| 41 | + |
56 | 42 | $category = new Category($faqConfig, [], false); |
57 | 43 | $category->setUser($currentAdminUser); |
58 | 44 | $category->setGroups($currentAdminGroups); |
|
74 | 60 |
|
75 | 61 | // translate.category only returns non-existent languages to translate too |
76 | 62 | if ($category->create($categoryEntity)) { |
77 | | - echo Alert::success('ad_categ_translated'); |
| 63 | + $templateVars = [ |
| 64 | + ...$templateVars, |
| 65 | + 'message' => Translation::get('ad_categ_translated'), |
| 66 | + 'message_typ' => 'success' |
| 67 | + ]; |
78 | 68 | } else { |
79 | | - echo Alert::danger('ad_adus_dberr', $faqConfig->getDb()->error()); |
| 69 | + $templateVars = [ |
| 70 | + ...$templateVars, |
| 71 | + 'message_heading' => Translation::get('ad_adus_dberr'), |
| 72 | + 'error' => $faqConfig->getDb()->error(), |
| 73 | + 'message_typ' => 'danger' |
| 74 | + ]; |
80 | 75 | } |
81 | 76 | } |
82 | 77 |
|
83 | 78 | $category->getMissingCategories(); |
84 | 79 | $category->buildCategoryTree(); |
85 | | - ?> |
86 | | - <table class="table table-light table-striped align-middle"> |
87 | | - <thead class="thead-dark"> |
88 | | - <tr> |
89 | | - <th><?= $currentLanguage ?></th> |
90 | | - <?php |
91 | | - // get languages in use for all categories |
92 | | - $allLanguages = $faqConfig->getLanguage()->isLanguageAvailable(0, $table = 'faqcategories'); |
93 | | - foreach ($allLanguages as $lang) { |
94 | | - $all_lang[$lang] = LanguageCodes::get($lang); |
95 | | - } |
96 | | - asort($all_lang); |
97 | | - foreach ($all_lang as $language) { |
98 | | - if ($language != $currentLanguage) { |
99 | | - printf('<th class="text-center">' . $language . "</th>\n", $language); |
100 | | - } |
101 | | - } |
102 | | - ?> |
103 | | - </tr> |
104 | | - </thead> |
105 | | - <tbody> |
106 | | - <?php |
| 80 | + |
| 81 | + // get languages in use for all categories |
| 82 | + $allLanguages = $faqConfig->getLanguage()->isLanguageAvailable(0, $table = 'faqcategories'); |
| 83 | + foreach ($allLanguages as $lang) { |
| 84 | + $all_lang[$lang] = LanguageCodes::get($lang); |
| 85 | + } |
| 86 | + asort($all_lang); |
| 87 | + |
| 88 | + $translations = []; |
| 89 | + |
107 | 90 | foreach ($category->getCategoryTree() as $cat) { |
108 | | - print "<tr>\n"; |
109 | | - |
110 | | - $indent = str_repeat(' ', $cat['indent']); |
111 | | - // category translated in this language? |
112 | | - ($cat['lang'] == $faqLangCode) ? $categoryName = $cat['name'] : $categoryName = $cat['name'] . ' (' . LanguageCodes::get($cat['lang']) . ')'; |
113 | | - |
114 | | - // show category name in actual language |
115 | | - print '<td>'; |
116 | | - if ($cat['lang'] != $faqLangCode) { |
117 | | - // translate category |
118 | | - printf( |
119 | | - '<a href="%s?action=translatecategory&cat=%s&trlang=%s" title="%s"><span title="%s" class="bi bi-globe"></span></a></a>', |
120 | | - $request->getBasePath(), |
121 | | - $cat['id'], |
122 | | - $faqLangCode, |
123 | | - Translation::get('ad_categ_translate'), |
124 | | - Translation::get('ad_categ_translate') |
125 | | - ); |
126 | | - } |
127 | | - printf( |
128 | | - ' %s<strong>%s</strong>', |
129 | | - $indent, |
130 | | - $categoryName |
131 | | - ); |
132 | | - print "</td>\n"; |
133 | | - |
134 | | - // get languages in use for categories |
| 91 | + // get languages of category which are already translated |
135 | 92 | $id_languages = $category->getCategoryLanguagesTranslated($cat['id']); |
| 93 | + $spokenLanguage = []; |
| 94 | + |
| 95 | + // collect all languages of a category |
| 96 | + $translation_array = []; |
| 97 | + foreach ($id_languages as $lang => $title) { |
| 98 | + $translation_array[] = $lang; |
| 99 | + } |
| 100 | + $translations[$cat['id']] = $translation_array; |
| 101 | + } |
136 | 102 |
|
137 | | - foreach ($all_lang as $lang => $language) { |
138 | | - if ($language == $currentLanguage) { |
139 | | - continue; |
140 | | - } |
141 | | - |
142 | | - if (array_key_exists($language, $id_languages)) { |
143 | | - $spokenLanguage = Strings::preg_replace('/\(.*\)/', '', $id_languages[$language]); |
144 | | - printf( |
145 | | - '<td class="text-center" title="%s: %s">', |
146 | | - Translation::get('ad_categ_titel'), |
147 | | - $spokenLanguage |
148 | | - ); |
149 | | - printf( |
150 | | - '<span title="%s: %s" class="badge bg-success"><i aria-hidden="true" class="bi bi-check"></i></span></td>', |
151 | | - Translation::get('ad_categ_titel'), |
152 | | - $spokenLanguage |
153 | | - ); |
154 | | - } else { |
155 | | - printf( |
156 | | - '<td class="text-center"><a href="%s?action=translatecategory&cat=%s&trlang=%s" title="%s">', |
157 | | - $request->getBasePath(), |
158 | | - $cat['id'], |
159 | | - $lang, |
160 | | - Translation::get('ad_categ_translate') |
161 | | - ); |
162 | | - printf( |
163 | | - '<span title="%s" class="badge bg-primary"><i aria-hidden="true" class="bi bi-globe bi-white"></i></span></a>', |
164 | | - Translation::get('ad_categ_translate') |
165 | | - ); |
166 | | - } |
167 | | - print "</td>\n"; |
| 103 | + // convert language names to codes | currentLanguage is always first |
| 104 | + $all_lang_codes = [LanguageCodes::getKey($currentLanguage)]; |
| 105 | + foreach ($all_lang as $language) { |
| 106 | + if ($language !== $currentLanguage) { |
| 107 | + $all_lang_codes[] = LanguageCodes::getKey($language); |
168 | 108 | } |
169 | | - print "</tr>\n"; |
170 | 109 | } |
171 | | - ?> |
172 | | - </tbody> |
173 | | - </table> |
174 | | - <?php |
175 | | - printf('<p>%s</p>', Translation::get('ad_categ_remark_overview')); |
| 110 | + |
| 111 | + $templateVars = [ |
| 112 | + ...$templateVars, |
| 113 | + 'currentLanguage' => $currentLanguage, |
| 114 | + 'allLangs' => $all_lang, |
| 115 | + 'allLangCodes' => $all_lang_codes, |
| 116 | + 'categoryTree' => $category->getCategoryTree(), |
| 117 | + 'basePath' => $request->getBasePath(), |
| 118 | + 'faqlangcode' => $faqLangCode, |
| 119 | + 'ad_categ_remark_overview' => Translation::get('ad_categ_remark_overview'), |
| 120 | + 'ad_categ_title' => Translation::get('ad_categ_titel'), |
| 121 | + 'ad_categ_translate' => Translation::get('ad_categ_translate'), |
| 122 | + 'ad_menu_categ_structure' => Translation::get('ad_menu_categ_structure'), |
| 123 | + 'ad_kateg_add' => Translation::get('ad_kateg_add'), |
| 124 | + 'ad_menu_categ_edit' => Translation::get('ad_menu_categ_edit'), |
| 125 | + 'msgCategory' => Translation::get('msgCategory'), |
| 126 | + 'translations' => $translations, |
| 127 | + 'ad_categ_translated' => Translation::get('ad_categ_translated') |
| 128 | + ]; |
| 129 | + |
| 130 | + $twig = new TwigWrapper(PMF_ROOT_DIR . '/assets/templates'); |
| 131 | + $twig->addExtension(new DebugExtension()); |
| 132 | + $template = $twig->loadTemplate('./admin/content/category.showstructure.twig'); |
| 133 | + |
| 134 | + echo $template->render($templateVars); |
176 | 135 | } else { |
177 | 136 | require __DIR__ . '/no-permission.php'; |
178 | 137 | } |
0 commit comments