Skip to content

Commit

Permalink
refactor: migrated admin import page to controller (#3257)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Nov 23, 2024
1 parent 3cf01e8 commit 2dc202c
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 79 deletions.
2 changes: 1 addition & 1 deletion nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ server {
rewrite admin/api/(.*) /admin/api/index.php last;

# Administration pages
rewrite admin/(attachments|backup|backup/export|backup/restore|configuration|elasticsearch|export|instance/edit|instance/update|instances|session-keep-alive|stopwords|system|update) /admin/front.php last;
rewrite admin/(attachments|backup|backup/export|backup/restore|configuration|elasticsearch|export|import|instance/edit|instance/update|instances|session-keep-alive|stopwords|system|update) /admin/front.php last;

# REST API v3.0 and v3.1
rewrite ^api/v3\.[01]/(.*) /api/index.php last;
Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ Header set Access-Control-Allow-Headers "Content-Type, Authorization"
# Administration API
RewriteRule ^admin/api/(.*) admin/api/index.php [L,QSA]
# Administration pages
RewriteRule ^admin/(attachments|backup|backup/export|backup/restore|configuration|elasticsearch|export|instance/edit|instance/update|instances|session-keep-alive|stopwords|system|update) admin/front.php [L,QSA]
RewriteRule ^admin/(attachments|backup|backup/export|backup/restore|configuration|elasticsearch|export|import|instance/edit|instance/update|instances|session-keep-alive|stopwords|system|update) admin/front.php [L,QSA]
# Private APIs
RewriteRule ^api/(autocomplete|bookmark/delete|bookmark/create|user/data/update|user/password/update|user/request-removal|user/remove-twofactor|contact|voting|register|captcha|share|comment/create|faq/create|question/create|webauthn/prepare|webauthn/register|webauthn/prepare-login|webauthn/login) api/index.php [L,QSA]
# Setup APIs
Expand Down
3 changes: 2 additions & 1 deletion phpmyfaq/admin/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@
$secLevelEntries['imports_exports'] = $adminHelper->addMenuEntry(
PermissionType::FAQ_ADD->value,
'importcsv',
'msgImportRecords'
'msgImportRecords',
'import'
);
$secLevelEntries['imports_exports'] .= $adminHelper->addMenuEntry('export', 'export', 'ad_menu_export', 'export');

Expand Down
4 changes: 0 additions & 4 deletions phpmyfaq/admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,6 @@
case 'reports':
require 'report.main.php';
break;
// functions for FAQ import and export
case 'importcsv':
require 'import.csv.php';
break;
// Config administration
case 'forms':
require 'forms.php';
Expand Down
56 changes: 0 additions & 56 deletions phpmyfaq/assets/templates/admin/import-export/import.csv.twig

This file was deleted.

61 changes: 61 additions & 0 deletions phpmyfaq/assets/templates/admin/import-export/import.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{% extends '@admin/index.twig' %}

{% block content %}
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">
<i aria-hidden="true" class="bi bi-upload"></i>
{{ adminHeaderImport }}
</h1>
</div>

<div class="card shadow mb-4" id="divImportColumns">
<h5 class="card-header py-3">
{{ adminHeaderCSVImportColumns }}
</h5>
<div class="card-body">
<div class="row">
<div class="col-md-12">
<p>{{ msgImportRecordsColumnStructure }}</p>
<ul>
<li>{{ categoryId }} *</li>
<li>{{ question }} *</li>
<li>{{ answer }} *</li>
<li>{{ keywords }} {{ seperateWithCommas }}</li>
<li>{{ languageCode }} *</li>
<li>{{ author }} *</li>
<li>{{ email }} *</li>
<li>{{ is_active }} {{ trueFalse }} *</li>
<li>{{ is_sticky }} {{ trueFalse }} *</li>
</ul>
</div>
</div>
</div>
</div>


<div class="card shadow mb-4">
<form id="uploadCSVFileForm" method="post" action="./api/faqs/import"
enctype="multipart/form-data" data-pmf-csrf="{{ csrfToken }}">
<h5 class="card-header py-3">
{{ adminHeaderCSVImport }}
</h5>
<div class="card-body">
<p>{{ adminBodyCSVImport }}</p>
<div class="row">
<label class="col-lg-4 col-form-label">{{ adminImportLabel }}:</label>
<div class="col-lg-8">
<input type="file" id="fileInputCSVUpload" name="userfile">
</div>
</div>
<div class="form-row row">
<div class="d-flex justify-content-center">
<button class="btn btn-primary btn-lg m-2" type="submit" id="submitButton">
<i aria-hidden="true" class="bi bi-upload"></i> {{ adminCSVImport }}
</button>
</div>
</div>
</div>
</form>
</div>

{% endblock %}
6 changes: 6 additions & 0 deletions phpmyfaq/src/admin-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use phpMyFAQ\Controller\Administration\ConfigurationController;
use phpMyFAQ\Controller\Administration\ElasticsearchController;
use phpMyFAQ\Controller\Administration\ExportController;
use phpMyFAQ\Controller\Administration\ImportController;
use phpMyFAQ\Controller\Administration\InstanceController;
use phpMyFAQ\Controller\Administration\SessionKeepAliveController;
use phpMyFAQ\Controller\Administration\StopWordsController;
Expand Down Expand Up @@ -66,6 +67,11 @@
'controller' => [ExportController::class, 'index'],
'methods' => 'GET'
],
'admin.import' => [
'path' => '/import',
'controller' => [ImportController::class, 'index'],
'methods' => 'GET'
],
'admin.instance.edit' => [
'path' => '/instance/edit/{id}',
'controller' => [InstanceController::class, 'edit'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,25 @@ protected function getHeader(Request $request): array
$secLevelEntries['imports_exports'] = $adminHelper->addMenuEntry(
PermissionType::FAQ_ADD->value,
'importcsv',
'msgImportRecords'
'msgImportRecords',
'import'
);
$secLevelEntries['imports_exports'] .= $adminHelper->addMenuEntry(
'export',
PermissionType::EXPORT->value,
'export',
'ad_menu_export',
'export'
);

$secLevelEntries['backup'] = $adminHelper->addMenuEntry('editconfig', 'backup', 'ad_menu_backup', 'backup');
$secLevelEntries['backup'] = $adminHelper->addMenuEntry(
PermissionType::CONFIGURATION_EDIT->value,
'backup',
'ad_menu_backup',
'backup'
);

$secLevelEntries['config'] = $adminHelper->addMenuEntry(
'editconfig',
PermissionType::CONFIGURATION_EDIT->value,
'config',
'ad_menu_editconfig',
'configuration'
Expand All @@ -160,26 +166,31 @@ protected function getHeader(Request $request): array
'instances'
);
$secLevelEntries['config'] .= $adminHelper->addMenuEntry(
'editconfig',
PermissionType::CONFIGURATION_EDIT->value,
'stopwordsconfig',
'ad_menu_stopwordsconfig',
'stopwords'
);
$secLevelEntries['config'] .= $adminHelper->addMenuEntry(
'editconfig',
PermissionType::CONFIGURATION_EDIT->value,
'upgrade',
'msgAdminHeaderUpdate',
'update'
);
if ($this->configuration->get('search.enableElasticsearch')) {
$secLevelEntries['config'] .= $adminHelper->addMenuEntry(
'editconfig',
PermissionType::CONFIGURATION_EDIT->value,
'elasticsearch',
'msgAdminHeaderElasticsearch',
'elasticsearch'
);
}
$secLevelEntries['config'] .= $adminHelper->addMenuEntry('editconfig', 'system', 'ad_system_info', 'system');
$secLevelEntries['config'] .= $adminHelper->addMenuEntry(
PermissionType::CONFIGURATION_EDIT->value,
'system',
'ad_system_info',
'system'
);

switch ($action) {
case 'user':
Expand Down Expand Up @@ -228,13 +239,6 @@ protected function getHeader(Request $request): array
case 'reportview':
$statisticsPage = true;
break;
case 'importcsv':
$exportsPage = true;
break;
case 'backup':
$backupPage = true;
break;
case 'config':
case 'forms':
$configurationPage = true;
break;
Expand All @@ -247,6 +251,7 @@ protected function getHeader(Request $request): array
$contentPage = true;
break;
case 'admin.export':
case 'admin.import':
$exportsPage = true;
break;
case 'admin.backup':
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<?php

/**
* The Administration Export Controller
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
* @copyright 2024 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2024-11-23
*/

declare(strict_types=1);

namespace phpMyFAQ\Controller\Administration;
Expand All @@ -22,7 +37,7 @@ class ExportController extends AbstractAdministrationController
* @throws Exception
* @throws LoaderError
*/
#[Route('/export')]
#[Route('/export', name: 'admin.export', methods: ['GET'])]
public function index(Request $request): Response
{
$this->userHasPermission(PermissionType::EXPORT);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* The Administration Import Controller
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
* @copyright 2024 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2024-11-23
*/

declare(strict_types=1);

namespace phpMyFAQ\Controller\Administration;

use phpMyFAQ\Core\Exception;
use phpMyFAQ\Enums\PermissionType;
use phpMyFAQ\Session\Token;
use phpMyFAQ\Translation;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Twig\Error\LoaderError;

class ImportController extends AbstractAdministrationController
{
/**
* @throws Exception
* @throws LoaderError
* @throws \Exception
*/
#[Route('/import', name: 'admin.import', methods: ['GET'])]
public function index(Request $request): Response
{
$this->userHasPermission(PermissionType::FAQ_ADD);

return $this->render(
'@admin/import-export/import.twig',
[
... $this->getHeader($request),
... $this->getFooter(),
'adminHeaderImport' => Translation::get('msgImportRecords'),
'adminHeaderCSVImport' => Translation::get('msgImportCSVFile'),
'adminBodyCSVImport' => Translation::get('msgImportCSVFileBody'),
'adminImportLabel' => Translation::get('ad_csv_file'),
'adminCSVImport' => Translation::get('msgImport'),
'adminHeaderCSVImportColumns' => Translation::get('msgColumnStructure'),
'categoryId' => Translation::get('ad_categ_categ'),
'question' => Translation::get('ad_entry_topic'),
'answer' => Translation::get('ad_entry_content'),
'keywords' => Translation::get('ad_entry_keywords'),
'author' => Translation::get('ad_entry_author'),
'email' => Translation::get('msgEmail'),
'languageCode' => Translation::get('msgLanguageCode'),
'seperateWithCommas' => Translation::get('msgSeperateWithCommas'),
'tags' => Translation::get('ad_entry_tags'),
'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'),
'trueFalse' => Translation::get('msgCSVImportTrueOrFalse')
]
);
}
}

0 comments on commit 2dc202c

Please sign in to comment.