Skip to content

Commit

Permalink
Create Export\TemplateModel class
Browse files Browse the repository at this point in the history
Extracts the database calls from the controller to the model.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Jul 19, 2020
1 parent c8a57e8 commit ea17130
Show file tree
Hide file tree
Showing 9 changed files with 333 additions and 160 deletions.
157 changes: 67 additions & 90 deletions libraries/classes/Controllers/ExportTemplateController.php
Expand Up @@ -5,16 +5,18 @@
namespace PhpMyAdmin\Controllers;

use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Display\Export;
use PhpMyAdmin\Export\Template as ExportTemplate;
use PhpMyAdmin\Export\TemplateModel;
use PhpMyAdmin\Relation;
use PhpMyAdmin\Response;
use PhpMyAdmin\Template;
use PhpMyAdmin\Util;
use function is_array;
use function is_string;

final class ExportTemplateController extends AbstractController
{
/** @var Export */
private $export;
/** @var TemplateModel */
private $model;

/** @var Relation */
private $relation;
Expand All @@ -23,164 +25,139 @@ final class ExportTemplateController extends AbstractController
* @param Response $response
* @param DatabaseInterface $dbi
*/
public function __construct($response, $dbi, Template $template, Export $export, Relation $relation)
{
public function __construct(
$response,
$dbi,
Template $template,
TemplateModel $model,
Relation $relation
) {
parent::__construct($response, $dbi, $template);
$this->export = $export;
$this->model = $model;
$this->relation = $relation;
}

public function create(): void
{
global $cfg;

$cfgRelation = $this->relation->getRelationsParam();

if (! $cfgRelation['exporttemplateswork']) {
return;
}

$templateTable = Util::backquote($cfgRelation['db']) . '.'
. Util::backquote($cfgRelation['export_templates']);
$user = $this->dbi->escapeString($GLOBALS['cfg']['Server']['user']);

$query = 'INSERT INTO ' . $templateTable . '('
. ' `username`, `export_type`,'
. ' `template_name`, `template_data`'
. ') VALUES ('
. "'" . $user . "', "
. "'" . $this->dbi->escapeString($_POST['exportType'])
. "', '" . $this->dbi->escapeString($_POST['templateName'])
. "', '" . $this->dbi->escapeString($_POST['templateData'])
. "');";

$result = $this->relation->queryAsControlUser($query, false);
$template = ExportTemplate::fromArray([
'username' => $cfg['Server']['user'],
'exportType' => $_POST['exportType'],
'name' => $_POST['templateName'],
'data' => $_POST['templateData'],
]);
$result = $this->model->create($cfgRelation['db'], $cfgRelation['export_templates'], $template);

if (! $result) {
$error = $this->dbi->getError(DatabaseInterface::CONNECT_CONTROL);
if (is_string($result)) {
$this->response->setRequestStatus(false);
$this->response->addJSON('message', $error);
$this->response->addJSON('message', $result);

return;
}

$this->response->setRequestStatus(true);
$templates = $this->model->getAll(
$cfgRelation['db'],
$cfgRelation['export_templates'],
$template->getUsername(),
$template->getExportType()
);

$this->response->setRequestStatus(true);
$this->response->addJSON(
'data',
$this->export->getOptionsForTemplates($_POST['exportType'])
$this->template->render('display/export/template_options', [
'templates' => is_array($templates) ? $templates : [],
'selected_template' => $_POST['template_id'] ?? null,
])
);

$this->dbi->freeResult($result);
}

public function delete(): void
{
global $cfg;

$cfgRelation = $this->relation->getRelationsParam();

if (! $cfgRelation['exporttemplateswork']) {
return;
}

$id = '';
if (isset($_POST['templateId'])) {
$id = $this->dbi->escapeString($_POST['templateId']);
}

$templateTable = Util::backquote($cfgRelation['db']) . '.'
. Util::backquote($cfgRelation['export_templates']);
$user = $this->dbi->escapeString($GLOBALS['cfg']['Server']['user']);

$query = 'DELETE FROM ' . $templateTable
. ' WHERE `id` = ' . $id . " AND `username` = '" . $user . "'";

$result = $this->relation->queryAsControlUser($query, false);
$result = $this->model->delete(
$cfgRelation['db'],
$cfgRelation['export_templates'],
$cfg['Server']['user'],
(int) $_POST['templateId']
);

if (! $result) {
$error = $this->dbi->getError(DatabaseInterface::CONNECT_CONTROL);
if (is_string($result)) {
$this->response->setRequestStatus(false);
$this->response->addJSON('message', $error);
$this->response->addJSON('message', $result);

return;
}

$this->response->setRequestStatus(true);

$this->dbi->freeResult($result);
}

public function load(): void
{
global $cfg;

$cfgRelation = $this->relation->getRelationsParam();

if (! $cfgRelation['exporttemplateswork']) {
return;
}

$id = '';
if (isset($_POST['templateId'])) {
$id = $this->dbi->escapeString($_POST['templateId']);
}

$templateTable = Util::backquote($cfgRelation['db']) . '.'
. Util::backquote($cfgRelation['export_templates']);
$user = $this->dbi->escapeString($GLOBALS['cfg']['Server']['user']);

$query = 'SELECT `template_data` FROM ' . $templateTable
. ' WHERE `id` = ' . $id . " AND `username` = '" . $user . "'";

$result = $this->relation->queryAsControlUser($query, false);
$template = $this->model->load(
$cfgRelation['db'],
$cfgRelation['export_templates'],
$cfg['Server']['user'],
(int) $_POST['templateId']
);

if (! $result) {
$error = $this->dbi->getError(DatabaseInterface::CONNECT_CONTROL);
if (! $template instanceof ExportTemplate) {
$this->response->setRequestStatus(false);
$this->response->addJSON('message', $error);
$this->response->addJSON('message', $template);

return;
}

$this->response->setRequestStatus(true);

$data = null;
while ($row = $this->dbi->fetchAssoc($result, DatabaseInterface::CONNECT_CONTROL)) {
$data = $row['template_data'];
}
$this->response->addJSON('data', $data);

$this->dbi->freeResult($result);
$this->response->addJSON('data', $template->getData());
}

public function update(): void
{
global $cfg;

$cfgRelation = $this->relation->getRelationsParam();

if (! $cfgRelation['exporttemplateswork']) {
return;
}

$id = '';
if (isset($_POST['templateId'])) {
$id = $this->dbi->escapeString($_POST['templateId']);
}

$templateTable = Util::backquote($cfgRelation['db']) . '.'
. Util::backquote($cfgRelation['export_templates']);
$user = $this->dbi->escapeString($GLOBALS['cfg']['Server']['user']);
$template = ExportTemplate::fromArray([
'id' => (int) $_POST['templateId'],
'username' => $cfg['Server']['user'],
'data' => $_POST['templateData'],
]);
$result = $this->model->update($cfgRelation['db'], $cfgRelation['export_templates'], $template);

$query = 'UPDATE ' . $templateTable . ' SET `template_data` = '
. "'" . $this->dbi->escapeString($_POST['templateData']) . "'"
. ' WHERE `id` = ' . $id . " AND `username` = '" . $user . "'";

$result = $this->relation->queryAsControlUser($query, false);

if (! $result) {
$error = $this->dbi->getError(DatabaseInterface::CONNECT_CONTROL);
if (is_string($result)) {
$this->response->setRequestStatus(false);
$this->response->addJSON('message', $error);
$this->response->addJSON('message', $result);

return;
}

$this->response->setRequestStatus(true);

$this->dbi->freeResult($result);
}
}
57 changes: 17 additions & 40 deletions libraries/classes/Display/Export.php
Expand Up @@ -8,8 +8,8 @@
namespace PhpMyAdmin\Display;

use PhpMyAdmin\Core;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Encoding;
use PhpMyAdmin\Export\TemplateModel;
use PhpMyAdmin\Html\MySQLDocumentation;
use PhpMyAdmin\Message;
use PhpMyAdmin\Plugins;
Expand All @@ -27,6 +27,7 @@
use function explode;
use function function_exists;
use function in_array;
use function is_array;
use function mb_strpos;
use function strlen;
use function urldecode;
Expand All @@ -42,10 +43,14 @@ class Export
/** @var Template */
public $template;

/** @var TemplateModel */
private $templateModel;

public function __construct()
{
$this->relation = new Relation($GLOBALS['dbi']);
$this->template = new Template();
$this->templateModel = new TemplateModel($GLOBALS['dbi']);
}

/**
Expand Down Expand Up @@ -148,44 +153,6 @@ public function getHtmlForHiddenInputs(
]);
}

/**
* Returns HTML for the options in template dropdown
*
* @param string $exportType export type - server, database, or table
*
* @return string HTML for the options in teplate dropdown
*/
public function getOptionsForTemplates($exportType)
{
// Get the relation settings
$cfgRelation = $this->relation->getRelationsParam();

$query = 'SELECT `id`, `template_name` FROM '
. Util::backquote($cfgRelation['db']) . '.'
. Util::backquote($cfgRelation['export_templates'])
. ' WHERE `username` = '
. "'" . $GLOBALS['dbi']->escapeString($GLOBALS['cfg']['Server']['user'])
. "' AND `export_type` = '" . $GLOBALS['dbi']->escapeString($exportType) . "'"
. ' ORDER BY `template_name`;';

$result = $this->relation->queryAsControlUser($query);

$templates = [];
if ($result !== false) {
while ($row = $GLOBALS['dbi']->fetchAssoc($result, DatabaseInterface::CONNECT_CONTROL)) {
$templates[] = [
'name' => $row['template_name'],
'id' => $row['id'],
];
}
}

return $this->template->render('display/export/template_options', [
'templates' => $templates,
'selected_template' => ! empty($_POST['template_id']) ? $_POST['template_id'] : null,
]);
}

/**
* Prints Html For Export Options Method
*
Expand Down Expand Up @@ -705,8 +672,18 @@ public function getDisplay(
]);

if ($cfgRelation['exporttemplateswork']) {
$templates = $this->templateModel->getAll(
$cfgRelation['db'],
$cfgRelation['export_templates'],
$GLOBALS['cfg']['Server']['user'],
$exportType
);

$html .= $this->template->render('display/export/template_loading', [
'options' => $this->getOptionsForTemplates($exportType),
'options' => $this->template->render('display/export/template_options', [
'templates' => is_array($templates) ? $templates : [],
'selected_template' => $_POST['template_id'] ?? null,
]),
]);
}

Expand Down

0 comments on commit ea17130

Please sign in to comment.