From 98804d301d3d620874d43a2b3f27e89090b6691f Mon Sep 17 00:00:00 2001 From: Jan <96944229+modelrailroader@users.noreply.github.com> Date: Fri, 7 Jun 2024 06:32:21 +0200 Subject: [PATCH] Adapted add-faq-form to #2886 * fix: improved translations in forms * refactor: moved add-faq-page to twig and improved form due to #2886 * fix: cleaned up add-faq-form * fix: corrected translations due to #2886 * fix: fix minor bugs * fix: added necessary blocks for twig * fix: removed html from translations * fix: resolve problem with missing escaping of html * fix: remove inline css --- phpmyfaq/add.php | 56 +++++++++ phpmyfaq/admin/assets/src/api/forms.js | 7 +- .../admin/assets/src/configuration/forms.js | 3 +- phpmyfaq/assets/templates/add.twig | 117 ++++++++++++++++++ .../configuration/forms.translations.twig | 10 +- .../Controller/Frontend/FaqController.php | 24 ++-- .../Enums/Forms/AddNewFaqInputIds.php | 2 - phpmyfaq/src/phpMyFAQ/Faq/MetaData.php | 2 +- phpmyfaq/translations/language_bn.php | 2 +- phpmyfaq/translations/language_bs.php | 2 +- phpmyfaq/translations/language_cy.php | 2 +- phpmyfaq/translations/language_da.php | 2 +- phpmyfaq/translations/language_de.php | 6 +- phpmyfaq/translations/language_el.php | 2 +- phpmyfaq/translations/language_en.php | 7 +- phpmyfaq/translations/language_es.php | 2 +- phpmyfaq/translations/language_eu.php | 2 +- phpmyfaq/translations/language_fa.php | 2 +- phpmyfaq/translations/language_fi.php | 2 +- phpmyfaq/translations/language_fr-ca.php | 2 +- phpmyfaq/translations/language_fr.php | 2 +- phpmyfaq/translations/language_he.php | 2 +- phpmyfaq/translations/language_hi.php | 1 - phpmyfaq/translations/language_hu.php | 2 +- phpmyfaq/translations/language_id.php | 2 +- phpmyfaq/translations/language_it.php | 2 +- phpmyfaq/translations/language_ja.php | 2 +- phpmyfaq/translations/language_ko.php | 2 +- phpmyfaq/translations/language_lt.php | 2 +- phpmyfaq/translations/language_lv.php | 2 +- phpmyfaq/translations/language_mn.php | 2 +- phpmyfaq/translations/language_ms.php | 2 +- phpmyfaq/translations/language_nb.php | 2 +- phpmyfaq/translations/language_nl.php | 2 +- phpmyfaq/translations/language_pl.php | 2 +- phpmyfaq/translations/language_pt-br.php | 2 +- phpmyfaq/translations/language_pt.php | 2 +- phpmyfaq/translations/language_ro.php | 2 +- phpmyfaq/translations/language_ru.php | 2 +- phpmyfaq/translations/language_sk.php | 2 +- phpmyfaq/translations/language_sl.php | 2 +- phpmyfaq/translations/language_sr.php | 2 +- phpmyfaq/translations/language_sv.php | 2 +- phpmyfaq/translations/language_th.php | 2 +- phpmyfaq/translations/language_tr.php | 2 +- phpmyfaq/translations/language_tw.php | 2 +- phpmyfaq/translations/language_uk.php | 2 +- phpmyfaq/translations/language_vi.php | 2 +- phpmyfaq/translations/language_zh.php | 2 +- 49 files changed, 244 insertions(+), 67 deletions(-) create mode 100644 phpmyfaq/assets/templates/add.twig diff --git a/phpmyfaq/add.php b/phpmyfaq/add.php index 0194322c4b..8257aaade8 100644 --- a/phpmyfaq/add.php +++ b/phpmyfaq/add.php @@ -17,6 +17,7 @@ use phpMyFAQ\Captcha\Captcha; use phpMyFAQ\Captcha\Helper\CaptchaHelper; +use phpMyFAQ\Category; use phpMyFAQ\Configuration; use phpMyFAQ\Enums\PermissionType; use phpMyFAQ\Filter; @@ -24,10 +25,14 @@ use phpMyFAQ\Question; use phpMyFAQ\Strings; use phpMyFAQ\System; +use phpMyFAQ\Template\TwigWrapper; use phpMyFAQ\Translation; use phpMyFAQ\User\CurrentUser; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; +use phpMyFAQ\Forms; +use phpMyFAQ\Enums\Forms\FormIds; +use phpMyFAQ\Enums\Forms\AddNewFaqInputIds; if (!defined('IS_VALID_PHPMYFAQ')) { http_response_code(400); @@ -117,3 +122,54 @@ 'msgNewContentSubmit' => Translation::get('msgNewContentSubmit'), ] ); + +$forms = new Forms($faqConfig); +$formData = $forms->getFormData(FormIds::ADD_NEW_FAQ->value); + +$category = new Category($faqConfig); +$categories = $category->getAllCategoryIds(); + +// Twig template variables +$templateVars = [ + 'baseHref' => $faqSystem->getSystemUri($faqConfig), + 'msgNewContentHeader' => Translation::get('msgNewContentHeader'), + 'msgNewContentAddon' => Translation::get('msgNewContentAddon'), + 'lang' => $Language->getLanguage(), + 'openQuestionID' => $selectedQuestion, + 'defaultContentMail' => ($user->getUserId() > 0) ? $user->getUserData('email') : '', + 'defaultContentName' => + ($user->getUserId() > 0) ? Strings::htmlentities($user->getUserData('display_name')) : '', + 'msgNewContentName' => Translation::get('msgNewContentName'), + 'msgNewContentMail' => Translation::get('msgNewContentMail'), + 'msgNewContentCategory' => Translation::get('msgNewContentCategory'), + 'renderCategoryOptions' => $categoryHelper->renderOptions($selectedCategory), + 'msgNewContentTheme' => Translation::get('msgNewContentTheme'), + 'readonly' => $readonly, + 'printQuestion' => $question, + 'msgNewContentArticle' => Translation::get('msgNewContentArticle'), + 'msgNewContentKeywords' => Translation::get('msgNewContentKeywords'), + 'msgNewContentLink' => Translation::get('msgNewContentLink'), + 'captchaFieldset' => + $captchaHelper->renderCaptcha($captcha, 'add', Translation::get('msgCaptcha'), $user->isLoggedIn()), + 'msgNewContentSubmit' => Translation::get('msgNewContentSubmit'), + 'enableWysiwygEditor' => $faqConfig->get('main.enableWysiwygEditorFrontend'), + 'currentTimestamp' => $request->server->get('REQUEST_TIME'), + 'msgSeperateKeywordsWithCommas' => Translation::get('msgSeperateKeywordsWithCommas'), + 'noCategories' => empty($categories), + 'msgFormDisabledDueToMissingCategories' => Translation::get('msgFormDisabledDueToMissingCategories') +]; + +// Collect data for displaying form +foreach ($formData as $input) { + if ((int)$input->input_active !== 0) { + $label = sprintf('id%d_label', (int)$input->input_id); + $required = sprintf('id%d_required', (int)$input->input_id); + $templateVars = [ + ...$templateVars, + $label => $input->input_label, + $required => ((int)$input->input_required !== 0) ? 'required' : '' + ]; + } +} + +//echo $template1->render($templateVars); diff --git a/phpmyfaq/admin/assets/src/api/forms.js b/phpmyfaq/admin/assets/src/api/forms.js index 3a4f19660c..8f0751f0ff 100644 --- a/phpmyfaq/admin/assets/src/api/forms.js +++ b/phpmyfaq/admin/assets/src/api/forms.js @@ -113,7 +113,7 @@ export const fetchEditTranslation = async (csrf, formId, inputId, label, lang) = } } -export const fetchDeleteTranslation = async (csrf, formId, inputId, lang) => { +export const fetchDeleteTranslation = async (csrf, formId, inputId, lang, element) => { try { const response = await fetch('api/forms/translation-delete', { method: 'POST', @@ -134,13 +134,16 @@ export const fetchDeleteTranslation = async (csrf, formId, inputId, lang) => { if (result.success) { pushNotification(result.success); document.getElementById('item_' + element.getAttribute('data-pmf-lang')).remove(); + const option = document.createElement('option'); + option.innerText = element.getAttribute('data-pmf-langname'); + document.getElementById('languageSelect').appendChild(option); } else { console.error(result.error); } } else { throw new Error('Network response was not ok: ', response.text()); } - } catch { + } catch (error) { console.error('Error deleting translation:', error); throw error; } diff --git a/phpmyfaq/admin/assets/src/configuration/forms.js b/phpmyfaq/admin/assets/src/configuration/forms.js index caf37d7c39..e97f049ebf 100644 --- a/phpmyfaq/admin/assets/src/configuration/forms.js +++ b/phpmyfaq/admin/assets/src/configuration/forms.js @@ -105,7 +105,8 @@ export const handleFormTranslations = () => { const inputId = element.getAttribute('data-pmf-inputId'); const formId = element.getAttribute('data-pmf-formId'); const lang = element.getAttribute('data-pmf-lang'); - await fetchDeleteTranslation(csrf, formId, inputId, lang); + await fetchDeleteTranslation(csrf, formId, inputId, lang, element); + }); }); // Add Translation diff --git a/phpmyfaq/assets/templates/add.twig b/phpmyfaq/assets/templates/add.twig new file mode 100644 index 0000000000..95fb97cfa5 --- /dev/null +++ b/phpmyfaq/assets/templates/add.twig @@ -0,0 +1,117 @@ +{% extends 'index.twig' %} + +{% block content %} + +
+{% if noCategories == true %} +
{{ msgFormDisabledDueToMissingCategories|raw }}
+{% else %} + {% if id1_label is defined %} +

{{ id1_label }}

+ {% endif %} + {% if id2_label is defined %} +

{{ id2_label|raw }}

+ {% endif %} + +
+
+
+ Loading... +
+
+
+
+ +
+ + + + {% if id3_label is defined %} +
+ +
+ +
+
+ {% endif %} + + {% if id4_label is defined %} +
+ +
+ +
+
+ {% endif %} + + {% if id5_label is defined %} +
+ +
+ +
+
+ {% endif %} + + {% if id6_label is defined %} +
+ +
+ +
+
+ {% endif %} + + {% if id7_label is defined %} +
+ +
+ +
+
+ {% endif %} + + {% if id8_label is defined %} +
+ +
+ + {{ msgSeperateKeywordsWithCommas }} +
+
+ {% endif %} + + {{ captchaFieldset|raw }} + +
+
+ +
+
+
+
+ +{% if enableWysiwygEditor == true %} + +{% endif %} +{% endif %} + +{% endblock %} diff --git a/phpmyfaq/assets/templates/admin/configuration/forms.translations.twig b/phpmyfaq/assets/templates/admin/configuration/forms.translations.twig index 030d62bc70..285fc020d9 100644 --- a/phpmyfaq/assets/templates/admin/configuration/forms.translations.twig +++ b/phpmyfaq/assets/templates/admin/configuration/forms.translations.twig @@ -29,14 +29,14 @@ value="{{ translation.input_label }}" disabled /> {% if translation.input_lang != 'default' %} - - -