Skip to content

Commit

Permalink
Create Preferences\FormsController controller
Browse files Browse the repository at this point in the history
Moves the preferences forms entry point logic to the controller and
removes the entry point file.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Dec 23, 2019
1 parent 286512f commit cf11c8f
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 110 deletions.
138 changes: 138 additions & 0 deletions libraries/classes/Controllers/Preferences/FormsController.php
@@ -0,0 +1,138 @@
<?php
/**
* @package PhpMyAdmin\Controllers\Preferences
*/
declare(strict_types=1);

namespace PhpMyAdmin\Controllers\Preferences;

use PhpMyAdmin\Controllers\AbstractController;

use PhpMyAdmin\Config\ConfigFile;
use PhpMyAdmin\Config\Forms\BaseForm;
use PhpMyAdmin\Config\Forms\User\UserFormList;
use PhpMyAdmin\Core;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Relation;
use PhpMyAdmin\Response;
use PhpMyAdmin\Template;
use PhpMyAdmin\Url;
use PhpMyAdmin\UserPreferences;
use PhpMyAdmin\UserPreferencesHeader;

/**
* User preferences page.
*
* @package PhpMyAdmin\Controllers\Preferences
*/
class FormsController extends AbstractController
{
/** @var UserPreferences */
private $userPreferences;

/** @var Relation */
private $relation;

/**
* @param Response $response A Response instance.
* @param DatabaseInterface $dbi A DatabaseInterface instance.
* @param Template $template A Template instance.
* @param UserPreferences $userPreferences A UserPreferences instance.
* @param Relation $relation A Relation instance.
*/
public function __construct(
$response,
$dbi,
Template $template,
UserPreferences $userPreferences,
Relation $relation
) {
parent::__construct($response, $dbi, $template);
$this->userPreferences = $userPreferences;
$this->relation = $relation;
}

/**
* @return void
*/
public function index(): void
{
global $cf, $form_param, $form_class, $form_display, $url_params, $error, $tabHash, $hash;
global $server, $PMA_Config;

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

// handle form processing
$form_param = $_GET['form'] ?? null;
$form_class = UserFormList::get($form_param);
if ($form_class === null) {
Core::fatalError(__('Incorrect form specified!'));
}

/** @var BaseForm $form_display */
$form_display = new $form_class($cf, 1);

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

$error = null;
if ($form_display->process(false) && ! $form_display->hasErrors()) {
// save settings
$result = $this->userPreferences->save($cf->getConfigArray());
if ($result === true) {
// reload config
$PMA_Config->loadUserPreferences();
$tabHash = $_POST['tab_hash'] ?? null;
$hash = ltrim($tabHash, '#');
$this->userPreferences->redirect(
'index.php?route=/preferences/forms',
['form' => $form_param],
$hash
);
return;
} else {
$error = $result;
}
}

// display forms
$header = $this->response->getHeader();
$scripts = $header->getScripts();
$scripts->addFile('config.js');

echo UserPreferencesHeader::getContent($this->template, $this->relation);

if ($form_display->hasErrors()) {
$formErrors = $form_display->displayErrors();
}

echo $this->template->render('preferences/forms/main', [
'error' => $error ? $error->getDisplay() : '',
'has_errors' => $form_display->hasErrors(),
'errors' => $formErrors ?? null,
'form' => $form_display->getDisplay(
true,
true,
true,
Url::getFromRoute('/preferences/forms', ['form' => $form_param]),
['server' => $server]
),
]);

if ($this->response->isAjax()) {
$this->response->addJSON('disableNaviSettings', true);
} else {
define('PMA_DISABLE_NAVI_SETTINGS', true);
}
}
}
107 changes: 0 additions & 107 deletions libraries/entry_points/preferences/forms.php

This file was deleted.

9 changes: 6 additions & 3 deletions libraries/routes.php
Expand Up @@ -34,6 +34,7 @@
use PhpMyAdmin\Controllers\NavigationController;
use PhpMyAdmin\Controllers\NormalizationController;
use PhpMyAdmin\Controllers\PhpInfoController;
use PhpMyAdmin\Controllers\Preferences\FormsController;
use PhpMyAdmin\Controllers\SchemaExportController;
use PhpMyAdmin\Controllers\Server\BinlogController;
use PhpMyAdmin\Controllers\Server\CollationsController;
Expand Down Expand Up @@ -315,9 +316,11 @@
$controller = $containerBuilder->get(PhpInfoController::class);
$controller->index();
});
$routes->addGroup('/preferences', function (RouteCollector $routes) {
$routes->addRoute(['GET', 'POST'], '/forms', function () {
require_once ROOT_PATH . 'libraries/entry_points/preferences/forms.php';
$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'], '/manage', function () {
require_once ROOT_PATH . 'libraries/entry_points/preferences/manage.php';
Expand Down
3 changes: 3 additions & 0 deletions services.yml
Expand Up @@ -147,6 +147,9 @@ services:
class: 'PhpMyAdmin\UserPassword'
arguments: ['@server_privileges']

user_preferences:
class: 'PhpMyAdmin\UserPreferences'

#Aliases

PhpMyAdmin\Response: '@response'
Expand Down
9 changes: 9 additions & 0 deletions services_controllers.yml
Expand Up @@ -245,6 +245,15 @@ services:
dbi: '@dbi'
template: '@template'

PhpMyAdmin\Controllers\Preferences\FormsController:
class: 'PhpMyAdmin\Controllers\Preferences\FormsController'
arguments:
response: '@response'
dbi: '@dbi'
template: '@template'
userPreferences: '@user_preferences'
relation: '@relation'

PhpMyAdmin\Controllers\SchemaExportController:
class: 'PhpMyAdmin\Controllers\SchemaExportController'
arguments:
Expand Down

0 comments on commit cf11c8f

Please sign in to comment.