Skip to content

Commit

Permalink
Refactor Table\RelationController controller
Browse files Browse the repository at this point in the history
Moves the table relation 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 25, 2019
1 parent 8441ebd commit af1c859
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 264 deletions.
217 changes: 108 additions & 109 deletions libraries/classes/Controllers/Table/RelationController.php
@@ -1,8 +1,6 @@
<?php
/**
* Holds the PhpMyAdmin\Controllers\Table\RelationController
*
* @package PhpMyAdmin\Controllers
* @package PhpMyAdmin\Controllers\Table
*/
declare(strict_types=1);

Expand All @@ -17,87 +15,39 @@
use PhpMyAdmin\Table;
use PhpMyAdmin\Template;
use PhpMyAdmin\Util;
use function mb_strtoupper;

/**
* Handles table relation logic
* Display table relations for viewing and editing.
*
* Includes phpMyAdmin relations and InnoDB relations.
*
* @package PhpMyAdmin\Controllers
* @package PhpMyAdmin\Controllers\Table
*/
class RelationController extends AbstractController
final class RelationController extends AbstractController
{
/**
* @var array
*/
protected $options_array;

/**
* @var array
*/
protected $cfgRelation;

/**
* @var array
*/
protected $existrel;

/**
* @var string
*/
protected $tbl_storage_engine;

/**
* @var array
*/
protected $existrel_foreign;

/**
* @var Table
*/
protected $upd_query;

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

/**
* Constructor
*
* @param Response $response Response object
* @param DatabaseInterface $dbi DatabaseInterface object
* @param Template $template Template object
* @param string $db Database name
* @param string $table Table name
* @param array|null $options_array Options
* @param array|null $cfgRelation Config relation
* @param string $tbl_storage_engine Table storage engine
* @param array|null $existrel Relations
* @param array|null $existrel_foreign External relations
* @param Table $upd_query Update query
* @param Relation $relation Relation instance
* @param Response $response Response object
* @param DatabaseInterface $dbi DatabaseInterface object
* @param Template $template Template object
* @param string $db Database name
* @param string $table Table name
* @param Relation $relation Relation instance
*/
public function __construct(
$response,
$dbi,
Template $template,
$db,
$table,
$options_array,
$cfgRelation,
$tbl_storage_engine,
$existrel,
$existrel_foreign,
$upd_query,
Relation $relation
) {
parent::__construct($response, $dbi, $template, $db, $table);

$this->options_array = $options_array;
$this->cfgRelation = $cfgRelation;
$this->tbl_storage_engine = $tbl_storage_engine;
$this->existrel = $existrel;
$this->existrel_foreign = $existrel_foreign;
$this->upd_query = $upd_query;
$this->relation = $relation;
}

Expand All @@ -106,20 +56,52 @@ public function __construct(
*
* @return void
*/
public function indexAction()
public function index(): void
{
global $route;

$options = [
'CASCADE' => 'CASCADE',
'SET_NULL' => 'SET NULL',
'NO_ACTION' => 'NO ACTION',
'RESTRICT' => 'RESTRICT',
];

$table = $this->dbi->getTable($this->db, $this->table);
$storageEngine = mb_strtoupper((string) $table->getStatusInfo('Engine'));

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

$relations = [];
if ($cfgRelation['relwork']) {
$relations = $this->relation->getForeigners(
$this->db,
$this->table,
'',
'internal'
);
}

$relationsForeign = [];
if (Util::isForeignKeySupported($storageEngine)) {
$relationsForeign = $this->relation->getForeigners(
$this->db,
$this->table,
'',
'foreign'
);
}

// Send table of column names to populate corresponding dropdowns depending
// on the current selection
if (isset($_POST['getDropdownValues'])
&& $_POST['getDropdownValues'] === 'true'
) {
// if both db and table are selected
if (isset($_POST['foreignTable'])) {
$this->getDropdownValueForTableAction();
$this->getDropdownValueForTable();
} else { // if only the db is selected
$this->getDropdownValueForDbAction();
$this->getDropdownValueForDatabase($storageEngine);
}
return;
}
Expand All @@ -135,31 +117,31 @@ public function indexAction()
$this->dbi->selectDb($this->db);

// updates for Internal relations
if (isset($_POST['destination_db']) && $this->cfgRelation['relwork']) {
$this->updateForInternalRelationAction();
if (isset($_POST['destination_db']) && $cfgRelation['relwork']) {
$this->updateForInternalRelation($table, $cfgRelation, $relations);
}

// updates for foreign keys
$this->updateForForeignKeysAction();
$this->updateForForeignKeys($table, $options, $relationsForeign);

// Updates for display field
if ($this->cfgRelation['displaywork'] && isset($_POST['display_field'])) {
$this->updateForDisplayField();
if ($cfgRelation['displaywork'] && isset($_POST['display_field'])) {
$this->updateForDisplayField($table, $cfgRelation);
}

// If we did an update, refresh our data
if (isset($_POST['destination_db']) && $this->cfgRelation['relwork']) {
$this->existrel = $this->relation->getForeigners(
if (isset($_POST['destination_db']) && $cfgRelation['relwork']) {
$relations = $this->relation->getForeigners(
$this->db,
$this->table,
'',
'internal'
);
}
if (isset($_POST['destination_foreign_db'])
&& Util::isForeignKeySupported($this->tbl_storage_engine)
&& Util::isForeignKeySupported($storageEngine)
) {
$this->existrel_foreign = $this->relation->getForeigners(
$relationsForeign = $this->relation->getForeigners(
$this->db,
$this->table,
'',
Expand All @@ -179,7 +161,7 @@ public function indexAction()
$column_hash_array = [];
$column_array[''] = '';
foreach ($columns as $column) {
if (strtoupper($this->tbl_storage_engine) == 'INNODB'
if (strtoupper($storageEngine) == 'INNODB'
|| ! empty($column['Key'])
) {
$column_array[$column['Field']] = $column['Field'];
Expand All @@ -192,18 +174,19 @@ public function indexAction()

// common form
$engine = $this->dbi->getTable($this->db, $this->table)->getStorageEngine();
$foreignKeySupported = Util::isForeignKeySupported($this->tbl_storage_engine);
$foreignKeySupported = Util::isForeignKeySupported($storageEngine);
$this->response->addHTML(
$this->template->render('table/relation/common_form', [
'is_foreign_key_supported' => Util::isForeignKeySupported($engine),
'db' => $this->db,
'table' => $this->table,
'cfg_relation' => $this->cfgRelation,
'tbl_storage_engine' => $this->tbl_storage_engine,
'existrel' => $this->existrel ?? [],
'existrel_foreign' => is_array($this->existrel_foreign) && array_key_exists('foreign_keys_data', $this->existrel_foreign)
? $this->existrel_foreign['foreign_keys_data'] : [],
'options_array' => $this->options_array,
'cfg_relation' => $cfgRelation,
'tbl_storage_engine' => $storageEngine,
'existrel' => $relations,
'existrel_foreign' => array_key_exists('foreign_keys_data', $relationsForeign)
? $relationsForeign['foreign_keys_data']
: [],
'options_array' => $options,
'column_array' => $column_array,
'column_hash_array' => $column_hash_array,
'save_row' => array_values($columns),
Expand All @@ -221,15 +204,17 @@ public function indexAction()
/**
* Update for display field
*
* @param Table $table table
* @param array $cfgRelation relation parameters
*
* @return void
*/
public function updateForDisplayField()
private function updateForDisplayField(Table $table, array $cfgRelation): void
{
if ($this->upd_query->updateDisplayField(
if ($table->updateDisplayField(
$_POST['display_field'],
$this->cfgRelation
)
) {
$cfgRelation
)) {
$this->response->addHTML(
Generator::getMessage(
__('Display column was successfully updated.'),
Expand All @@ -243,9 +228,13 @@ public function updateForDisplayField()
/**
* Update for FK
*
* @param Table $table Table
* @param array $options Options
* @param array $relationsForeign External relations
*
* @return void
*/
public function updateForForeignKeysAction()
private function updateForForeignKeys(Table $table, array $options, array $relationsForeign): void
{
$multi_edit_columns_name = $_POST['foreign_key_fields_name'] ?? null;
$preview_sql_data = '';
Expand All @@ -255,17 +244,22 @@ public function updateForForeignKeysAction()
// foreign db is not the same)
if (isset($_POST['destination_foreign_db'], $_POST['destination_foreign_table'])
&& isset($_POST['destination_foreign_column'])) {
[$html, $preview_sql_data, $display_query, $seen_error]
= $this->upd_query->updateForeignKeys(
$_POST['destination_foreign_db'],
$multi_edit_columns_name,
$_POST['destination_foreign_table'],
$_POST['destination_foreign_column'],
$this->options_array,
$this->table,
is_array($this->existrel_foreign) && array_key_exists('foreign_keys_data', $this->existrel_foreign)
? $this->existrel_foreign['foreign_keys_data'] : []
);
[
$html,
$preview_sql_data,
$display_query,
$seen_error,
] = $table->updateForeignKeys(
$_POST['destination_foreign_db'],
$multi_edit_columns_name,
$_POST['destination_foreign_table'],
$_POST['destination_foreign_column'],
$options,
$this->table,
array_key_exists('foreign_keys_data', $relationsForeign)
? $relationsForeign['foreign_keys_data']
: []
);
$this->response->addHTML($html);
}

Expand All @@ -289,21 +283,24 @@ public function updateForForeignKeysAction()
/**
* Update for internal relation
*
* @param Table $table Table
* @param array $cfgRelation Relation parameters
* @param array $relations Relations
*
* @return void
*/
public function updateForInternalRelationAction()
private function updateForInternalRelation(Table $table, array $cfgRelation, array $relations): void
{
$multi_edit_columns_name = $_POST['fields_name'] ?? null;

if ($this->upd_query->updateInternalRelations(
if ($table->updateInternalRelations(
$multi_edit_columns_name,
$_POST['destination_db'],
$_POST['destination_table'],
$_POST['destination_column'],
$this->cfgRelation,
$this->existrel ?? null
)
) {
$cfgRelation,
$relations
)) {
$this->response->addHTML(
Generator::getMessage(
__('Internal relationships were successfully updated.'),
Expand All @@ -319,7 +316,7 @@ public function updateForInternalRelationAction()
*
* @return void
*/
public function getDropdownValueForTableAction()
public function getDropdownValueForTable(): void
{
$foreignTable = $_POST['foreignTable'];
$table_obj = $this->dbi->getTable($_POST['foreignDb'], $foreignTable);
Expand Down Expand Up @@ -351,9 +348,11 @@ public function getDropdownValueForTableAction()
/**
* Send database selection values for dropdown
*
* @param string $storageEngine Storage engine.
*
* @return void
*/
public function getDropdownValueForDbAction()
public function getDropdownValueForDatabase(string $storageEngine): void
{
$tables = [];
$foreign = isset($_POST['foreign']) && $_POST['foreign'] === 'true';
Expand All @@ -369,7 +368,7 @@ public function getDropdownValueForDbAction()

while ($row = $this->dbi->fetchArray($tables_rs)) {
if (isset($row['Engine'])
&& mb_strtoupper($row['Engine']) == $this->tbl_storage_engine
&& mb_strtoupper($row['Engine']) == $storageEngine
) {
$tables[] = htmlspecialchars($row['Name']);
}
Expand Down

0 comments on commit af1c859

Please sign in to comment.