Skip to content

Commit

Permalink
refactor: migrated add FAQ page to controller (#3257)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Dec 24, 2024
1 parent b474047 commit 2e13740
Show file tree
Hide file tree
Showing 58 changed files with 821 additions and 639 deletions.
11 changes: 9 additions & 2 deletions phpmyfaq/admin/assets/src/api/faqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@

export const fetchAllFaqsByCategory = async (categoryId, language, onlyInactive, onlyNew) => {
try {
const currentUrl = window.location.protocol + '//' + window.location.host + window.location.pathname;
const url = new URL(`${currentUrl}api/faqs/${categoryId}/${language}`);
let currentUrl = window.location.protocol + '//' + window.location.host;
let pathname = window.location.pathname;

if (pathname.endsWith('/faqs')) {
pathname = pathname.slice(0, -5);
}

currentUrl += pathname;
const url = new URL(`${currentUrl}/api/faqs/${categoryId}/${language}`);
if (onlyInactive) {
url.searchParams.set('only-inactive', onlyInactive);
}
Expand Down
6 changes: 3 additions & 3 deletions phpmyfaq/admin/faqs.editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@
'notifyUser' => $notifyUser,
'notifyEmail' => $notifyEmail,
'csrfToken' => Token::getInstance($container->get('session'))->getTokenString('edit-faq'),
'ad_entry_theme' => Translation::get('ad_entry_theme'),
'msgQuestion' => Translation::get('msgQuestion'),
'msgNoHashAllowed' => Translation::get('msgNoHashAllowed'),
'msgShowHelp' => Translation::get('msgShowHelp'),
'ad_entry_content' => Translation::get('ad_entry_content'),
Expand Down Expand Up @@ -375,7 +375,7 @@
'ad_entry_restricted_users' => Translation::get('ad_entry_restricted_users'),
'userPermissionOptions' => $userHelper->getAllUserOptions($userPermission[0], true),
'ad_entry_changelog' => Translation::get('ad_entry_changelog'),
'ad_entry_date' => Translation::get('ad_entry_date'),
'msgDate' => Translation::get('msgDate'),
'ad_entry_changed' => Translation::get('ad_entry_changed'),
'ad_admin_notes_hint' => Translation::get('ad_admin_notes_hint'),
'ad_admin_notes' => Translation::get('ad_admin_notes'),
Expand All @@ -397,7 +397,7 @@
'ad_entry_new_revision' => Translation::get('ad_entry_new_revision'),
'ad_gen_yes' => Translation::get('ad_gen_yes'),
'ad_gen_no' => Translation::get('ad_gen_no'),
'ad_entry_sticky' => Translation::get('ad_entry_sticky'),
'msgStickyFAQ' => Translation::get('msgStickyFAQ'),
'ad_entry_allowComments' => Translation::get('ad_entry_allowComments'),
'ad_entry_solution_id' => Translation::get('ad_entry_solution_id'),
'nextSolutionId' => $faq->getNextSolutionId(),
Expand Down
3 changes: 2 additions & 1 deletion phpmyfaq/admin/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
$secLevelEntries['content'] .= $adminHelper->addMenuEntry(
PermissionType::FAQ_ADD->value,
'editentry',
'ad_entry_add'
'ad_entry_add',
'faq/add'
);
$secLevelEntries['content'] .= $adminHelper->addMenuEntry(
'edit_faq+delete_faq',
Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/admin/import.csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
'msgImportRecordsColumnStructure' => Translation::get('msgImportRecordsColumnStructure'),
'csrfToken' => Token::getInstance($container->get('session'))->getTokenString('importfaqs'),
'is_active' => Translation::get('ad_entry_active'),
'is_sticky' => Translation::get('ad_entry_sticky'),
'is_sticky' => Translation::get('msgStickyFAQ'),
'trueFalse' => Translation::get('msgCSVImportTrueOrFalse')
];

Expand Down
921 changes: 467 additions & 454 deletions phpmyfaq/assets/templates/admin/content/faq.editor.twig

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions phpmyfaq/assets/templates/admin/content/faq.overview.twig
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@
ID
</th>
<th class="align-middle">
{{ msgQuestion }}
{{ 'msgQuestion' | translate }}
</th>
<th class="align-middle">
{{ msgDate }}
{{ 'msgDate' | translate }}
</th>
<th class="align-middle">
{{ msgSticky }}
{{ 'msgStickyFAQ' | translate }}
</th>
<th class="align-middle">
{{ 'msgActive' | translate }}
Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/assets/templates/admin/dashboard.twig
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
<div class="col mr-2">
<div class="text-xs font-weight-bold mb-1">
<a class="text-white" href="?action=comments">
{{ 'ad_start_comments' | translate }}
{{ 'msgComments' | translate }}
</a>
</div>
<div class="h5 mb-0 mr-3 font-weight-bold text-gray-800">{{ adminDashboardInfoNumComments }}</div>
Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/faq.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@
);
$templateVars = [
... $templateVars,
'numberOfComments' => sprintf('%d %s', $numComments[$faqId] ?? 0, Translation::get('ad_start_comments')),
'numberOfComments' => sprintf('%d %s', $numComments[$faqId] ?? 0, Translation::get('msgComments')),
'writeCommentMsg' => $commentMessage
];
}
Expand Down
5 changes: 5 additions & 0 deletions phpmyfaq/src/admin-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
'controller' => [ConfigurationController::class, 'index'],
'methods' => 'GET'
],
'admin.faq.add' => [
'path' => '/faq/add',
'controller' => [FaqController::class, 'add'],
'methods' => 'GET'
],
'admin.faqs' => [
'path' => '/faqs',
'controller' => [FaqController::class, 'index'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ protected function getHeader(Request $request): array
$secLevelEntries['content'] .= $adminHelper->addMenuEntry(
PermissionType::FAQ_ADD->value,
'editentry',
'ad_entry_add'
'ad_entry_add',
'faq/add'
);
$secLevelEntries['content'] .= $adminHelper->addMenuEntry(
'edit_faq+delete_faq',
'faqs-overview',
'msgHeaderFAQOverview'
'msgHeaderFAQOverview',
'faqs'
);
$secLevelEntries['content'] .= $adminHelper->addMenuEntry(
PermissionType::FAQ_EDIT->value,
Expand Down
153 changes: 148 additions & 5 deletions phpmyfaq/src/phpMyFAQ/Controller/Administration/FaqController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@
use phpMyFAQ\Category;
use phpMyFAQ\Category\Relation;
use phpMyFAQ\Core\Exception;
use phpMyFAQ\Database;
use phpMyFAQ\Enums\PermissionType;
use phpMyFAQ\Helper\LanguageHelper;
use phpMyFAQ\Session\Token;
use phpMyFAQ\Template\Extensions\FormatBytesTwigExtension;
use phpMyFAQ\Template\Extensions\IsoDateTwigExtension;
use phpMyFAQ\Template\Extensions\UserNameTwigExtension;
use phpMyFAQ\Translation;
use phpMyFAQ\User\CurrentUser;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -69,11 +74,149 @@ public function index(Request $request): Response
'categories' => $category->getCategoryTree(),
'numberOfRecords' => $categoryRelation->getNumberOfFaqsPerCategory(),
'numberOfComments' => $comments->getNumberOfCommentsByCategory(),
'msgComments' => Translation::get('ad_start_comments'),
'msgQuestion' => Translation::get('ad_entry_theme'),
'msgDate' => Translation::get('ad_entry_date'),
'msgSticky' => Translation::get('ad_entry_sticky'),
'msgActive' => Translation::get('ad_record_active'),
]
);
}

/**
* @throws Exception
* @throws LoaderError
* @throws \Exception
* @todo refactor Twig template variables
*/
#[Route('/faq/add', name: 'admin.faq.add', methods: ['GET'])]
public function add(Request $request): Response
{
$this->userHasPermission(PermissionType::FAQ_ADD);

[ $currentAdminUser, $currentAdminGroups ] = CurrentUser::getCurrentUserGroupId($this->currentUser);

$category = new Category($this->configuration, $currentAdminGroups, true);
$category->setUser($currentAdminUser);
$category->setGroups($currentAdminGroups);
$category->buildCategoryTree();

$categoryHelper = $this->container->get('phpmyfaq.helper.category-helper');
$categoryHelper->setCategory($category);

$faq = $this->container->get('phpmyfaq.faq');
$userHelper = $this->container->get('phpmyfaq.helper.user-helper');
$logging = $this->container->get('phpmyfaq.admin.admin-log');
$session = $this->container->get('session');

$logging->log($this->currentUser, 'admin-add-faq');
$categories = [];

$faqData = [
'id' => 0,
'lang' => $this->configuration->getLanguage()->getLanguage(),
'revision_id' => 0,
'author' => $this->currentUser->getUserData('display_name'),
'email' => $this->currentUser->getUserData('email'),
'comment' => $this->configuration->get('records.defaultAllowComments') ? 'checked' : null,
];

$this->addExtension(new IsoDateTwigExtension());
$this->addExtension(new UserNameTwigExtension());
$this->addExtension(new FormatBytesTwigExtension());
return $this->render(
'@admin/content/faq.editor.twig',
[
... $this->getHeader($request),
... $this->getFooter(),
'ad_record_faq' => Translation::get('ad_record_faq'),
'ad_menu_faq_meta' => Translation::get('ad_menu_faq_meta'),
'ad_record_permissions' => Translation::get('ad_record_permissions'),
'adminFaqEditorHeader' => Translation::get('ad_entry_add'),
'editExistingFaq' => false,
'isEditorEnabled' => $this->configuration->get('main.enableWysiwygEditor'),
'isMarkdownEditorEnabled' => $this->configuration->get('main.enableMarkdownEditor'),
'isBasicPermission' => $this->configuration->get('security.permLevel') === 'basic',
'defaultUrl' => $this->configuration->getDefaultUrl(),
'faqRevisionId' => 0,
'faqData' => $faqData,
'openQuestionId' => 0,
'notifyUser' => '',
'notifyEmail' => '',
'csrfToken' => Token::getInstance($session)->getTokenString('edit-faq'),
'msgQuestion' => Translation::get('msgQuestion'),
'msgNoHashAllowed' => Translation::get('msgNoHashAllowed'),
'msgShowHelp' => Translation::get('msgShowHelp'),
'ad_entry_content' => Translation::get('ad_entry_content'),
'ad_entry_category' => Translation::get('ad_entry_category'),
'categoryOptions' => $categoryHelper->renderOptions($categories),
'ad_entry_locale' => Translation::get('ad_entry_locale'),
'languageOptions' => LanguageHelper::renderSelectLanguage('de', false, [], 'lang'),
'hasPermissionForAddAttachments' => $this->currentUser->perm->hasPermission(
$this->currentUser->getUserId(),
PermissionType::ATTACHMENT_ADD->value
),
'hasPermissionForDeleteAttachments' => $this->currentUser->perm->hasPermission(
$this->currentUser->getUserId(),
PermissionType::ATTACHMENT_DELETE->value
),
'ad_menu_attachments' => Translation::get('ad_menu_attachments'),
'csrfTokenDeleteAttachment' => Token::getInstance($session)->getTokenString('delete-attachment'),
'attachments' => [],
'ad_att_add' => Translation::get('ad_att_add'),
'ad_entry_tags' => Translation::get('ad_entry_tags'),
'ad_entry_keywords' => Translation::get('ad_entry_keywords'),
'ad_entry_author' => Translation::get('ad_entry_author'),
'msgEmail' => Translation::get('msgEmail'),
'msgSeoCenter' => Translation::get('seoCenter'),
'msgSerp' => Translation::get('msgSerp'),
'msgSerpTitle' => Translation::get('msgSerpTitle'),
'ad_entry_grouppermission' => Translation::get('ad_entry_grouppermission'),
'ad_entry_all_groups' => Translation::get('ad_entry_all_groups'),
'allGroups' => true,
'restrictedGroups' => false,
'ad_entry_restricted_groups' => Translation::get('ad_entry_restricted_groups'),
'groupPermissionOptions' => ($this->configuration->get('security.permLevel') === 'medium') ?
$this->currentUser->perm->getAllGroupsOptions([-1], $this->currentUser) : '',
'ad_entry_userpermission' => Translation::get('ad_entry_userpermission'),
'allUsers' => true,
'msgAccessAllUsers' => Translation::get('msgAccessAllUsers'),
'restrictedUsers' => false,
'ad_entry_restricted_users' => Translation::get('ad_entry_restricted_users'),
'userPermissionOptions' => $userHelper->getAllUserOptions(-1, true),
'ad_entry_changelog' => Translation::get('ad_entry_changelog'),
'msgDate' => Translation::get('msgDate'),
'ad_entry_changed' => Translation::get('ad_entry_changed'),
'ad_admin_notes_hint' => Translation::get('ad_admin_notes_hint'),
'ad_admin_notes' => Translation::get('ad_admin_notes'),
'ad_entry_changelog_history' => Translation::get('ad_entry_changelog_history'),
'changelogs' => [],
'ad_entry_revision' => Translation::get('ad_entry_revision'),
'ad_gen_reset' => Translation::get('ad_gen_reset'),
'ad_entry_save' => Translation::get('ad_entry_save'),
'msgUpdateFaqDate' => Translation::get('msgUpdateFaqDate'),
'msgKeepFaqDate' => Translation::get('msgKeepFaqDate'),
'msgEditFaqDat' => Translation::get('msgEditFaqDat'),
'ad_entry_status' => Translation::get('ad_entry_status'),
'hasPermissionForApprove' => $this->currentUser->perm->hasPermission(
$this->currentUser->getUserId(),
PermissionType::FAQ_APPROVE->value
),
'isActive' => null,
'isInActive' => null,
'ad_entry_visibility' => Translation::get('ad_entry_visibility'),
'ad_entry_not_visibility' => Translation::get('ad_entry_not_visibility'),
'canBeNewRevision' => false,
'ad_entry_new_revision' => Translation::get('ad_entry_new_revision'),
'ad_gen_yes' => Translation::get('ad_gen_yes'),
'ad_gen_no' => Translation::get('ad_gen_no'),
'msgStickyFAQ' => Translation::get('msgStickyFAQ'),
'ad_entry_allowComments' => Translation::get('ad_entry_allowComments'),
'ad_entry_solution_id' => Translation::get('ad_entry_solution_id'),
'nextSolutionId' => $faq->getNextSolutionId(),
'nextFaqId' => $this->configuration->getDb()->nextId(Database::getTablePrefix() . 'faqdata', 'id'),
'ad_att_addto' => Translation::get('ad_att_addto'),
'ad_att_addto_2' => Translation::get('ad_att_addto_2'),
'ad_att_att' => Translation::get('ad_att_att'),
'maxAttachmentSize' => $this->configuration->get('records.maxAttachmentSize'),
'csrfTokenUploadAttachment' => Token::getInstance($session)->getTokenString('upload-attachment'),
'msgAttachmentsFilesize' => Translation::get('msgAttachmentsFilesize'),
'ad_att_butt' => Translation::get('ad_att_butt'),
]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function index(Request $request): Response
'msgImportRecordsColumnStructure' => Translation::get('msgImportRecordsColumnStructure'),
'csrfToken' => Token::getInstance($this->container->get('session'))->getTokenString('importfaqs'),
'is_active' => Translation::get('ad_entry_active'),
'is_sticky' => Translation::get('ad_entry_sticky'),
'is_sticky' => Translation::get('msgStickyFAQ'),
'trueFalse' => Translation::get('msgCSVImportTrueOrFalse')
]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function index(Request $request): Response
'csrfTokenDeleteQuestion' => Token::getInstance($session)->getTokenString('delete-questions'),
'currentLocale' => $this->configuration->getLanguage()->getLanguage(),
'msgAuthor' => Translation::get('ad_entry_author'),
'msgQuestion' => Translation::get('ad_entry_theme'),
'msgQuestion' => Translation::get('msgQuestion'),
'msgVisibility' => Translation::get('ad_entry_visibility'),
'questions' => $question->getAll(),
'yes' => Translation::get('ad_gen_yes'),
Expand Down
19 changes: 15 additions & 4 deletions phpmyfaq/src/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use phpMyFAQ\Administration\AdminLog;
use phpMyFAQ\Administration\Backup;
use phpMyFAQ\Administration\Category;
use phpMyFAQ\Administration\Changelog;
use phpMyFAQ\Administration\RatingData;
use phpMyFAQ\Administration\Session as AdminSession;
use phpMyFAQ\Attachment\AttachmentCollection;
Expand Down Expand Up @@ -79,6 +80,11 @@
new Reference('phpmyfaq.configuration'),
]);

$services->set('phpmyfaq.admin.changelog', Changelog::class)
->args([
new Reference('phpmyfaq.configuration'),
]);

$services->set('phpmyfaq.admin.rating-data', RatingData::class)
->args([
new Reference('phpmyfaq.configuration'),
Expand Down Expand Up @@ -161,11 +167,9 @@
new Reference('phpmyfaq.configuration')
]);

$services->set('phpmyfaq.helper.statistics', StatisticsHelper::class)
$services->set('phpmyfaq.faq.permission', Faq\Permission::class)
->args([
new Reference('phpmyfaq.admin.session'),
new Reference('phpmyfaq.visits'),
new Reference('phpmyfaq.date')
new Reference('phpmyfaq.configuration')
]);

$services->set('phpmyfaq.faq.statistics', Statistics::class)
Expand All @@ -185,6 +189,13 @@

$services->set('phpmyfaq.helper.category-helper', CategoryHelper::class);

$services->set('phpmyfaq.helper.statistics', StatisticsHelper::class)
->args([
new Reference('phpmyfaq.admin.session'),
new Reference('phpmyfaq.visits'),
new Reference('phpmyfaq.date')
]);

$services->set('phpmyfaq.helper.user-helper', UserHelper::class)
->args([
new Reference('phpmyfaq.user.current_user')
Expand Down
8 changes: 4 additions & 4 deletions phpmyfaq/translations/language_ar.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@
$PMF_LANG['ad_entry_action'] = 'عمليات';
$PMF_LANG['ad_entry_edit_1'] = 'تحرير السؤال';
$PMF_LANG['ad_entry_edit_2'] = '';
$PMF_LANG['ad_entry_theme'] = 'عنوان السؤال:';
$PMF_LANG['msgQuestion'] = 'عنوان السؤال:';
$PMF_LANG['ad_entry_content'] = 'السؤال:';
$PMF_LANG['ad_entry_keywords'] = 'كلمات البحث:';
$PMF_LANG['ad_entry_author'] = 'الكاتب:';
$PMF_LANG['ad_entry_category'] = 'التصنيف:';
$PMF_LANG['ad_entry_active'] = 'نشط؟';
$PMF_LANG['ad_entry_date'] = 'التاريخ';
$PMF_LANG['msgDate'] = 'التاريخ';
$PMF_LANG['ad_entry_status'] = 'حالة السؤال';
$PMF_LANG['ad_entry_changed'] = 'معدّل؟';
$PMF_LANG['ad_entry_changelog'] = 'سجل التعديلات';
Expand Down Expand Up @@ -370,7 +370,7 @@
$PMF_LANG['ad_log_bede'] = 'تم حذف السؤال التالية: ';
$PMF_LANG['msgVisits'] = 'زيارات';
$PMF_LANG['ad_start_articles'] = 'مواضيع';
$PMF_LANG['ad_start_comments'] = 'تعليقات';
$PMF_LANG['msgComments'] = 'تعليقات';
$PMF_LANG['ad_categ_paste'] = 'لصق';
$PMF_LANG['ad_categ_cut'] = 'قص';
$PMF_LANG['msgCopy'] = 'نسخ';
Expand Down Expand Up @@ -751,7 +751,7 @@
$PMF_LANG['ad_searchstats_search_term_lang'] = 'لغة';
$PMF_LANG['ad_searchstats_search_term_percentage'] = 'نسبة';
$PMF_LANG['ad_record_sticky'] = 'مثبت';
$PMF_LANG['ad_entry_sticky'] = 'مثبت';
$PMF_LANG['msgStickyFAQ'] = 'مثبت';
$PMF_LANG['stickyRecordsHeader'] = 'أسئلة مثبتة';
$PMF_LANG['ad_menu_stopwordsconfig'] = 'الكلمات المستثناة';
$PMF_LANG['ad_config_stopword_input'] = 'Add new stop word';
Expand Down
Loading

0 comments on commit 2e13740

Please sign in to comment.