Skip to content

Commit 28b7f43

Browse files
refactor: refactored full category-overview, converted to twig and fixed minor bugs
1 parent 2fb54f7 commit 28b7f43

File tree

7 files changed

+287
-213
lines changed

7 files changed

+287
-213
lines changed

composer.lock

Lines changed: 20 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpmyfaq/admin/category.main.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
->setGroupId(Filter::filterInput(INPUT_POST, 'group_id', FILTER_VALIDATE_INT))
7171
->setActive(Filter::filterInput(INPUT_POST, 'active', FILTER_VALIDATE_INT))
7272
->setImage($categoryImage->getFileName($categoryId, $categoryLang))
73+
->setParentId($parentId)
7374
->setShowHome(Filter::filterInput(INPUT_POST, 'show_home', FILTER_VALIDATE_INT));
7475

7576
$permissions = [];
@@ -166,6 +167,7 @@
166167
$categoryId = Filter::filterInput(INPUT_POST, 'id', FILTER_VALIDATE_INT);
167168
$categoryLang = Filter::filterInput(INPUT_POST, 'catlang', FILTER_SANITIZE_SPECIAL_CHARS);
168169
$existingImage = Filter::filterInput(INPUT_POST, 'existing_image', FILTER_SANITIZE_SPECIAL_CHARS);
170+
$existingImage = is_null($existingImage) ? '' : $existingImage;
169171
$image = count($uploadedFile) ? $categoryImage->getFileName(
170172
$categoryId,
171173
$categoryLang

phpmyfaq/admin/category.showstructure.php

Lines changed: 66 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* @package phpMyFAQ
1111
* @author Thorsten Rinne <[email protected]>
1212
* @author Rudi Ferrari <[email protected]>
13+
* @author Jan Harms <[email protected]>
1314
* @copyright 2006-2024 phpMyFAQ Team
1415
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
1516
* @link https://www.phpmyfaq.de
@@ -23,8 +24,10 @@
2324
use phpMyFAQ\Filter;
2425
use phpMyFAQ\Language\LanguageCodes;
2526
use phpMyFAQ\Strings;
27+
use phpMyFAQ\Template\TwigWrapper;
2628
use phpMyFAQ\Translation;
2729
use Symfony\Component\HttpFoundation\Request;
30+
use Twig\Extension\DebugExtension;
2831

2932
if (!defined('IS_VALID_PHPMYFAQ')) {
3033
http_response_code(400);
@@ -33,26 +36,9 @@
3336

3437
$request = Request::createFromGlobals();
3538

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
5539
if ($user->perm->hasPermission($user->getUserId(), PermissionType::CATEGORY_EDIT->value)) {
40+
$templateVars = [];
41+
5642
$category = new Category($faqConfig, [], false);
5743
$category->setUser($currentAdminUser);
5844
$category->setGroups($currentAdminGroups);
@@ -74,105 +60,78 @@
7460

7561
// translate.category only returns non-existent languages to translate too
7662
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+
];
7868
} 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+
];
8075
}
8176
}
8277

8378
$category->getMissingCategories();
8479
$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+
10790
foreach ($category->getCategoryTree() as $cat) {
108-
print "<tr>\n";
109-
110-
$indent = str_repeat('&nbsp;&nbsp;&nbsp;', $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&amp;cat=%s&amp;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-
'&nbsp;%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
13592
$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+
}
136102

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&amp;cat=%s&amp;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);
168108
}
169-
print "</tr>\n";
170109
}
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);
176135
} else {
177136
require __DIR__ . '/no-permission.php';
178137
}

0 commit comments

Comments
 (0)