diff --git a/phpmyfaq/admin/assets/src/api/faqs.js b/phpmyfaq/admin/assets/src/api/faqs.js index b0e72d86ca..3ba22b99da 100644 --- a/phpmyfaq/admin/assets/src/api/faqs.js +++ b/phpmyfaq/admin/assets/src/api/faqs.js @@ -94,3 +94,41 @@ export const deleteFaq = async (faqId, faqLanguage, token) => { throw error; } }; + +export const create = async (formData) => { + try { + const response = await fetch('./api/faq/create', { + method: 'POST', + headers: { + Accept: 'application/json, text/plain, */*', + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + data: formData, + }), + }); + + return await response.json(); + } catch (error) { + console.error(error); + } +}; + +export const update = async (formData) => { + try { + const response = await fetch('./api/faq/update', { + method: 'PUT', + headers: { + Accept: 'application/json, text/plain, */*', + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + data: formData, + }), + }); + + return await response.json(); + } catch (error) { + console.error(error); + } +}; diff --git a/phpmyfaq/admin/assets/src/content/editor.js b/phpmyfaq/admin/assets/src/content/editor.js index 4255475092..4cb669dfad 100644 --- a/phpmyfaq/admin/assets/src/content/editor.js +++ b/phpmyfaq/admin/assets/src/content/editor.js @@ -233,6 +233,11 @@ export const renderEditor = () => { image_dimensions: true, images_upload_url: '/admin/api/content/images', automatic_uploads: true, + setup: (editor) => { + editor.on('change', () => { + tinymce.triggerSave(); + }); + }, }); } }; diff --git a/phpmyfaq/admin/assets/src/content/faqs.editor.js b/phpmyfaq/admin/assets/src/content/faqs.editor.js new file mode 100644 index 0000000000..f8762b6251 --- /dev/null +++ b/phpmyfaq/admin/assets/src/content/faqs.editor.js @@ -0,0 +1,37 @@ +import { create, update } from '../api'; +import { serialize } from '../../../../assets/src/utils'; +import { pushErrorNotification, pushNotification } from '../utils'; + +export const handleSaveFaqData = () => { + const submitButton = document.getElementById('faqEditorSubmit'); + + if (submitButton) { + submitButton.addEventListener('click', async (event) => { + event.preventDefault(); + const form = document.getElementById('faqEditor'); + const formData = new FormData(form); + + const serializedData = serialize(formData); + + let response; + if (serializedData.faqId === '0') { + response = await create(serializedData); + } else { + response = await update(serializedData); + } + + if (response.success) { + const data = JSON.parse(response.data); + const faqId = document.getElementById('faqId'); + const revisionId = document.getElementById('revisionId'); + + faqId.value = data.id; + revisionId.value = data.revisionId; + + pushNotification(response.success); + } else { + pushErrorNotification(response.error); + } + }); + } +}; diff --git a/phpmyfaq/admin/assets/src/content/index.js b/phpmyfaq/admin/assets/src/content/index.js index a03a2c7190..05301f7e72 100644 --- a/phpmyfaq/admin/assets/src/content/index.js +++ b/phpmyfaq/admin/assets/src/content/index.js @@ -6,6 +6,7 @@ export * from './csvimport'; export * from './editor'; export * from './faqs'; export * from './faqs.autocomplete'; +export * from './faqs.editor'; export * from './glossary'; export * from './markdown'; export * from './media.browser'; diff --git a/phpmyfaq/admin/assets/src/index.js b/phpmyfaq/admin/assets/src/index.js index 48ad9c555a..6a2a1312c7 100644 --- a/phpmyfaq/admin/assets/src/index.js +++ b/phpmyfaq/admin/assets/src/index.js @@ -43,7 +43,11 @@ import { handleDeleteGlossary, handleAddGlossary, onOpenUpdateGlossaryModal, - handleUpdateGlossary, handleAddNews, handleNews, handleEditNews, + handleUpdateGlossary, + handleAddNews, + handleNews, + handleEditNews, + handleSaveFaqData, } from './content'; import { handleUserList, handleUsers } from './user'; import { handleGroups } from './group'; @@ -82,6 +86,7 @@ document.addEventListener('DOMContentLoaded', async () => { handleMarkdownForm(); handleAttachmentUploads(); handleFileFilter(); + handleSaveFaqData(); await handleFaqOverview(); // Content -> Comments diff --git a/phpmyfaq/admin/header.php b/phpmyfaq/admin/header.php index 8423dd38df..bc908a8863 100644 --- a/phpmyfaq/admin/header.php +++ b/phpmyfaq/admin/header.php @@ -187,7 +187,6 @@ case 'showcategory': case 'faqs-overview': case 'editentry': - case 'insertentry': case 'saveentry': case 'glossary': case 'saveglossary': diff --git a/phpmyfaq/admin/index.php b/phpmyfaq/admin/index.php index 02cce99963..7e1913597d 100755 --- a/phpmyfaq/admin/index.php +++ b/phpmyfaq/admin/index.php @@ -265,9 +265,6 @@ case 'editpreview': require 'record.edit.php'; break; - case 'insertentry': - require 'record.add.php'; - break; case 'saveentry': require 'record.save.php'; break; diff --git a/phpmyfaq/admin/record.add.php b/phpmyfaq/admin/record.add.php deleted file mode 100644 index 8efa708293..0000000000 --- a/phpmyfaq/admin/record.add.php +++ /dev/null @@ -1,283 +0,0 @@ - - * @copyright 2003-2024 phpMyFAQ Team - * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 - * @link https://www.phpmyfaq.de - * @since 2003-02-23 - */ - -use phpMyFAQ\Administration\AdminLog; -use phpMyFAQ\Administration\Changelog; -use phpMyFAQ\Category; -use phpMyFAQ\Category\Permission; -use phpMyFAQ\Category\Relation; -use phpMyFAQ\Component\Alert; -use phpMyFAQ\Configuration; -use phpMyFAQ\Core\Exception; -use phpMyFAQ\Entity\FaqEntity; -use phpMyFAQ\Enums\PermissionType; -use phpMyFAQ\Faq\Permission; -use phpMyFAQ\Filter; -use phpMyFAQ\Helper\CategoryHelper; -use phpMyFAQ\Instance\Elasticsearch; -use phpMyFAQ\Link; -use phpMyFAQ\Notification; -use phpMyFAQ\Question; -use phpMyFAQ\Strings; -use phpMyFAQ\Tags; -use phpMyFAQ\Translation; -use phpMyFAQ\User\CurrentUser; -use phpMyFAQ\Visits; -use Symfony\Component\Mailer\Exception\TransportExceptionInterface; - -if (!defined('IS_VALID_PHPMYFAQ')) { - http_response_code(400); - exit(); -} - -$faqConfig = Configuration::getConfigurationInstance(); -$user = CurrentUser::getCurrentUser($faqConfig); - -if ($user->perm->hasPermission($user->getUserId(), PermissionType::FAQ_ADD->value)) { - $faqPermission = new Permission($faqConfig); - - // FAQ data - $dateStart = Filter::filterInput(INPUT_POST, 'dateStart', FILTER_SANITIZE_SPECIAL_CHARS); - $dateEnd = Filter::filterInput(INPUT_POST, 'dateEnd', FILTER_SANITIZE_SPECIAL_CHARS); - $question = Filter::filterInput(INPUT_POST, 'question', FILTER_SANITIZE_SPECIAL_CHARS); - $categories = Filter::filterInputArray( - INPUT_POST, - [ - 'rubrik' => [ - 'filter' => FILTER_VALIDATE_INT, - 'flags' => FILTER_REQUIRE_ARRAY, - ], - ] - ); - $recordLang = Filter::filterInput(INPUT_POST, 'lang', FILTER_SANITIZE_SPECIAL_CHARS); - $tags = Filter::filterInput(INPUT_POST, 'tags', FILTER_SANITIZE_SPECIAL_CHARS); - $active = Filter::filterInput(INPUT_POST, 'active', FILTER_SANITIZE_SPECIAL_CHARS); - $sticky = Filter::filterInput(INPUT_POST, 'sticky', FILTER_SANITIZE_SPECIAL_CHARS); - $content = Filter::filterInput(INPUT_POST, 'answer', FILTER_SANITIZE_SPECIAL_CHARS); - $keywords = Filter::filterInput(INPUT_POST, 'keywords', FILTER_SANITIZE_SPECIAL_CHARS); - $author = Filter::filterInput(INPUT_POST, 'author', FILTER_SANITIZE_SPECIAL_CHARS); - $email = Filter::filterInput(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL, ''); - $comment = Filter::filterInput(INPUT_POST, 'comment', FILTER_SANITIZE_SPECIAL_CHARS); - $recordId = Filter::filterInput(INPUT_POST, 'id', FILTER_VALIDATE_INT); - $solutionId = Filter::filterInput(INPUT_POST, 'solution_id', FILTER_VALIDATE_INT); - $revisionId = Filter::filterInput(INPUT_POST, 'revision_id', FILTER_VALIDATE_INT); - $changed = Filter::filterInput(INPUT_POST, 'changed', FILTER_SANITIZE_SPECIAL_CHARS); - $date = Filter::filterInput(INPUT_POST, 'date', FILTER_SANITIZE_SPECIAL_CHARS); - $notes = Filter::filterInput(INPUT_POST, 'notes', FILTER_SANITIZE_SPECIAL_CHARS); - - // Permissions - $permissions = $faqPermission->createPermissionArray(); - - if (!isset($categories['rubrik'])) { - $categories['rubrik'] = []; - } - - if (!is_null($question) && !is_null($categories['rubrik'])) { - // new entry - $logging = new AdminLog($faqConfig); - $logging->log($user, 'admin-save-new-faq'); - ?> -
%s
', $e->getMessage()); - } - } - - // Let the admin and the category owners to be informed by email of this new entry - try { - $categoryHelper = new CategoryHelper(); - $categoryHelper - ->setCategory($category) - ->setConfiguration($faqConfig); - $moderators = $categoryHelper->getModerators($categories['rubrik']); - $notification->sendNewFaqAdded($moderators, $recordId, $recordLang); - } catch (Exception | TransportExceptionInterface $e) { - printf('%s
', $e->getMessage()); - } - - // If Elasticsearch is enabled, index new FAQ document - if ($faqConfig->get('search.enableElasticsearch')) { - $esInstance = new Elasticsearch($faqConfig); - $esInstance->index( - [ - 'id' => $recordId, - 'lang' => $recordLang, - 'solution_id' => $solutionId, - 'question' => $faqData->getQuestion(), - 'answer' => $faqData->getAnswer(), - 'keywords' => $keywords, - 'category_id' => $categories['rubrik'][0] - ] - ); - } - - echo Alert::success('ad_entry_savedsuc'); - ?> -