Skip to content

Commit

Permalink
Create Preferences\ImportController class
Browse files Browse the repository at this point in the history
Extracts the controller from the FormsController.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Feb 5, 2020
1 parent 7bfaffa commit f6bbecc
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 79 deletions.
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Controllers\Preferences;
Expand All @@ -15,13 +16,8 @@
use PhpMyAdmin\Url;
use PhpMyAdmin\UserPreferences;
use PhpMyAdmin\UserPreferencesHeader;
use function define;
use function ltrim;

/**
* User preferences page.
*/
class FormsController extends AbstractController
class ImportController extends AbstractController
{
/** @var UserPreferences */
private $userPreferences;
Expand Down Expand Up @@ -50,40 +46,25 @@ public function __construct(

public function index(): void
{
global $cf, $url_params, $error, $tabHash, $hash;
global $cfg, $cf, $error, $tabHash, $hash;
global $server, $PMA_Config;

$cf = new ConfigFile($PMA_Config->base_settings);
$this->userPreferences->pageInit($cf);

// handle form processing
$formParam = $_GET['form'] ?? null;

switch ($formParam) {
case 'Import':
$formDisplay = new ImportForm($cf, 1);
break;
default:
Core::fatalError(__('Incorrect form specified!'));
return;
}
$formDisplay = new ImportForm($cf, 1);

if (isset($_POST['revert'])) {
// revert erroneous fields to their default values
$formDisplay->fixErrors();
// redirect
$url_params = ['form' => $formParam];
Core::sendHeaderLocation(
'./index.php?route=/preferences/forms'
. Url::getCommonRaw($url_params, '&')
);
Core::sendHeaderLocation('./index.php?route=/preferences/import');
return;
}

$error = null;
if ($formDisplay->process(false) && ! $formDisplay->hasErrors()) {
// Load 2FA settings
$twoFactor = new TwoFactor($GLOBALS['cfg']['Server']['user']);
$twoFactor = new TwoFactor($cfg['Server']['user']);
// save settings
$result = $this->userPreferences->save($cf->getConfigArray());
// save back the 2FA setting only
Expand All @@ -94,8 +75,8 @@ public function index(): void
$tabHash = $_POST['tab_hash'] ?? null;
$hash = ltrim($tabHash, '#');
$this->userPreferences->redirect(
'index.php?route=/preferences/forms',
['form' => $formParam],
'index.php?route=/preferences/import',
null,
$hash
);
return;
Expand Down Expand Up @@ -123,7 +104,7 @@ public function index(): void
true,
true,
true,
Url::getFromRoute('/preferences/forms', ['form' => $formParam]),
Url::getFromRoute('/preferences/import'),
['server' => $server]
),
]));
Expand Down
2 changes: 1 addition & 1 deletion libraries/classes/Menu.php
Expand Up @@ -567,7 +567,7 @@ private function _getServerTabs()
$tabs['settings']['active'] = in_array($route, [
'/preferences/export',
'/preferences/features',
'/preferences/forms',
'/preferences/import',
'/preferences/main-panel',
'/preferences/manage',
'/preferences/navigation',
Expand Down
40 changes: 8 additions & 32 deletions libraries/classes/UserPreferencesHeader.php
Expand Up @@ -6,7 +6,6 @@

namespace PhpMyAdmin;

use PhpMyAdmin\Config\Forms\User\UserFormList;
use PhpMyAdmin\Html\Generator;
use Throwable;
use Twig_Error_Loader;
Expand Down Expand Up @@ -111,7 +110,14 @@ protected static function displayTabs(Template $template): string
]
) . "\n";

$content .= self::displayTabsWithIcon();
$content .= Generator::getHtmlTab(
[
'link' => 'index.php?route=/preferences/import',
'text' => __('Import'),
'icon' => 'b_import',
'active' => $route === '/preferences/import',
]
) . "\n";

return '<div class=container-fluid><div class=row>' .
$template->render(
Expand All @@ -124,36 +130,6 @@ protected static function displayTabs(Template $template): string
) . '<div class="clearfloat"></div></div>';
}

protected static function displayTabsWithIcon(): string
{
$form_param = $_GET['form'] ?? null;
$tabs_icons = [
'Import' => 'b_import',
];
$route = $_GET['route'] ?? $_POST['route'] ?? '';
$content = null;
foreach (UserFormList::getAll() as $formset) {
if ($formset === 'Features'
|| $formset === 'Sql'
|| $formset === 'Navi'
|| $formset === 'Main'
|| $formset === 'Export'
) {
continue;
}

$formset_class = UserFormList::get($formset);
$tab = [
'link' => 'index.php?route=/preferences/forms',
'text' => $formset_class::getName(),
'icon' => $tabs_icons[$formset],
'active' => $route === '/preferences/forms' && $formset === $form_param,
];
$content .= Generator::getHtmlTab($tab, ['form' => $formset]) . "\n";
}
return $content;
}

protected static function displayConfigurationSavedMessage(): ?string
{
// show "configuration saved" message and reload navigation panel if needed
Expand Down
12 changes: 6 additions & 6 deletions libraries/routes.php
Expand Up @@ -40,7 +40,7 @@
use PhpMyAdmin\Controllers\PhpInfoController;
use PhpMyAdmin\Controllers\Preferences\ExportController as PreferencesExportController;
use PhpMyAdmin\Controllers\Preferences\FeaturesController;
use PhpMyAdmin\Controllers\Preferences\FormsController;
use PhpMyAdmin\Controllers\Preferences\ImportController as PreferencesImportController;
use PhpMyAdmin\Controllers\Preferences\MainPanelController;
use PhpMyAdmin\Controllers\Preferences\ManageController;
use PhpMyAdmin\Controllers\Preferences\NavigationController as PreferencesNavigationController;
Expand Down Expand Up @@ -363,11 +363,6 @@
$controller->index();
});
$routes->addGroup('/preferences', function (RouteCollector $routes) use ($containerBuilder) {
$routes->addRoute(['GET', 'POST'], '/forms', function () use ($containerBuilder) {
/** @var FormsController $controller */
$controller = $containerBuilder->get(FormsController::class);
$controller->index();
});
$routes->addRoute(['GET', 'POST'], '/export', function () use ($containerBuilder) {
/** @var PreferencesExportController $controller */
$controller = $containerBuilder->get(PreferencesExportController::class);
Expand All @@ -378,6 +373,11 @@
$controller = $containerBuilder->get(FeaturesController::class);
$controller->index();
});
$routes->addRoute(['GET', 'POST'], '/import', function () use ($containerBuilder) {
/** @var PreferencesImportController $controller */
$controller = $containerBuilder->get(PreferencesImportController::class);
$controller->index();
});
$routes->addRoute(['GET', 'POST'], '/main-panel', function () use ($containerBuilder) {
/** @var MainPanelController $controller */
$controller = $containerBuilder->get(MainPanelController::class);
Expand Down
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Expand Up @@ -3062,16 +3062,6 @@ parameters:
count: 1
path: libraries/classes/Url.php

-
message: "#^Cannot call static method getName\\(\\) on string\\|null\\.$#"
count: 1
path: libraries/classes/UserPreferencesHeader.php

-
message: "#^Method PhpMyAdmin\\\\UserPreferencesHeader\\:\\:displayTabsWithIcon\\(\\) should return string but returns string\\|null\\.$#"
count: 1
path: libraries/classes/UserPreferencesHeader.php

-
message: "#^Strict comparison using \\=\\=\\= between float\\|int and null will always evaluate to false\\.$#"
count: 1
Expand Down
4 changes: 2 additions & 2 deletions services_controllers.yml
Expand Up @@ -299,8 +299,8 @@ services:
userPreferences: '@user_preferences'
relation: '@relation'

PhpMyAdmin\Controllers\Preferences\FormsController:
class: 'PhpMyAdmin\Controllers\Preferences\FormsController'
PhpMyAdmin\Controllers\Preferences\ImportController:
class: 'PhpMyAdmin\Controllers\Preferences\ImportController'
arguments:
response: '@response'
dbi: '@dbi'
Expand Down

0 comments on commit f6bbecc

Please sign in to comment.