From 765059def425808380e75a33b43d33a89a84593f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Fri, 22 May 2020 17:15:15 -0300 Subject: [PATCH] Extract central columns actions from index method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extracts the /table/structure/central-columns-add and /table/structure/central-columns-remove routes from the /table/structure route. Signed-off-by: MaurĂ­cio Meneghini Fauth --- js/table/structure.js | 6 +- .../Controllers/Table/StructureController.php | 201 ++++++++++++++++-- libraries/routes.php | 2 + 3 files changed, 194 insertions(+), 15 deletions(-) diff --git a/js/table/structure.js b/js/table/structure.js index 065154f09862..4bc2d1ed55c7 100644 --- a/js/table/structure.js +++ b/js/table/structure.js @@ -420,7 +420,11 @@ AJAX.registerOnload('table/structure.js', function () { AJAX.source = $form; var url = $form.attr('action'); - if (action === 'drop') { + if (action === 'add_to_central_columns') { + url = 'index.php?route=/table/structure/central-columns-add'; + } else if (action === 'remove_from_central_columns') { + url = 'index.php?route=/table/structure/central-columns-remove'; + } else if (action === 'drop') { url = 'index.php?route=/table/structure/drop-confirm'; } else if (action === 'ftext') { url = 'index.php?route=/table/structure/fulltext'; diff --git a/libraries/classes/Controllers/Table/StructureController.php b/libraries/classes/Controllers/Table/StructureController.php index 96423543e2bd..b6b2ea36a2b3 100644 --- a/libraries/classes/Controllers/Table/StructureController.php +++ b/libraries/classes/Controllers/Table/StructureController.php @@ -390,6 +390,193 @@ public function index(): void ); } + public function addToCentralColumns(): void + { + global $sql_query, $reread_info, $showtable; + global $tbl_is_view, $tbl_storage_engine, $tbl_collation, $table_info_num_rows; + global $message, $url_params; + + $this->dbi->selectDb($this->db); + $reread_info = $this->table_obj->getStatusInfo(null, true); + $showtable = $this->table_obj->getStatusInfo( + null, + (isset($reread_info) && $reread_info) + ); + + $tbl_is_view = false; + $tbl_storage_engine = $this->table_obj->getStorageEngine(); + $tbl_collation = $this->table_obj->getCollation(); + $table_info_num_rows = $this->table_obj->getNumRows(); + + PageSettings::showGroup('TableStructure'); + + $checkUserPrivileges = new CheckUserPrivileges($this->dbi); + $checkUserPrivileges->getPrivileges(); + + $this->response->getHeader()->getScripts()->addFiles([ + 'table/structure.js', + 'indexes.js', + ]); + + $selected = $_POST['selected_fld'] ?? []; + + if (empty($selected)) { + $this->response->setRequestStatus(false); + $this->response->addJSON('message', __('No column selected.')); + + return; + } + + $centralColumns = new CentralColumns($this->dbi); + $centralColsError = $centralColumns->syncUniqueColumns( + $selected, + false + ); + + if ($centralColsError instanceof Message) { + $message = $centralColsError; + } + + if (empty($message)) { + $message = Message::success(); + } + $this->response->addHTML( + Generator::getMessage($message, $sql_query) + ); + + $cfgRelation = $this->relation->getRelationsParam(); + + $url_params = []; + + Common::table(); + + $url_params['goto'] = Url::getFromRoute('/table/structure'); + $url_params['back'] = Url::getFromRoute('/table/structure'); + + $this->_url_query = Url::getCommonRaw($url_params); + + $primary = Index::getPrimary($this->table, $this->db); + $columns_with_index = $this->dbi + ->getTable($this->db, $this->table) + ->getColumnsWithIndex( + Index::UNIQUE | Index::INDEX | Index::SPATIAL + | Index::FULLTEXT + ); + $columns_with_unique_index = $this->dbi + ->getTable($this->db, $this->table) + ->getColumnsWithIndex(Index::UNIQUE); + + $fields = (array) $this->dbi->getColumns( + $this->db, + $this->table, + null, + true + ); + + $this->response->addHTML($this->displayStructure( + $cfgRelation, + $columns_with_unique_index, + $url_params, + $primary, + $fields, + $columns_with_index + )); + } + + public function removeFromCentralColumns(): void + { + global $sql_query, $reread_info, $showtable; + global $tbl_is_view, $tbl_storage_engine, $tbl_collation, $table_info_num_rows; + global $db, $message, $url_params; + + $this->dbi->selectDb($this->db); + $reread_info = $this->table_obj->getStatusInfo(null, true); + $showtable = $this->table_obj->getStatusInfo( + null, + (isset($reread_info) && $reread_info) + ); + + $tbl_is_view = false; + $tbl_storage_engine = $this->table_obj->getStorageEngine(); + $tbl_collation = $this->table_obj->getCollation(); + $table_info_num_rows = $this->table_obj->getNumRows(); + + PageSettings::showGroup('TableStructure'); + + $checkUserPrivileges = new CheckUserPrivileges($this->dbi); + $checkUserPrivileges->getPrivileges(); + + $this->response->getHeader()->getScripts()->addFiles([ + 'table/structure.js', + 'indexes.js', + ]); + + $selected = $_POST['selected_fld'] ?? []; + + if (empty($selected)) { + $this->response->setRequestStatus(false); + $this->response->addJSON('message', __('No column selected.')); + + return; + } + + $centralColumns = new CentralColumns($this->dbi); + $centralColsError = $centralColumns->deleteColumnsFromList( + $db, + $selected, + false + ); + + if ($centralColsError instanceof Message) { + $message = $centralColsError; + } + + if (empty($message)) { + $message = Message::success(); + } + $this->response->addHTML( + Generator::getMessage($message, $sql_query) + ); + + $cfgRelation = $this->relation->getRelationsParam(); + + $url_params = []; + + Common::table(); + + $url_params['goto'] = Url::getFromRoute('/table/structure'); + $url_params['back'] = Url::getFromRoute('/table/structure'); + + $this->_url_query = Url::getCommonRaw($url_params); + + $primary = Index::getPrimary($this->table, $this->db); + $columns_with_index = $this->dbi + ->getTable($this->db, $this->table) + ->getColumnsWithIndex( + Index::UNIQUE | Index::INDEX | Index::SPATIAL + | Index::FULLTEXT + ); + $columns_with_unique_index = $this->dbi + ->getTable($this->db, $this->table) + ->getColumnsWithIndex(Index::UNIQUE); + + $fields = (array) $this->dbi->getColumns( + $this->db, + $this->table, + null, + true + ); + + $this->response->addHTML($this->displayStructure( + $cfgRelation, + $columns_with_unique_index, + $url_params, + $primary, + $fields, + $columns_with_index + )); + } + public function fulltext(): void { global $sql_query, $reread_info, $showtable; @@ -2248,26 +2435,12 @@ protected function getKeyForTablePrimary() */ protected function getDataForSubmitMult($submit_mult, $selected, $action) { - $centralColumns = new CentralColumns($this->dbi); $what = null; $query_type = null; $is_unset_submit_mult = false; $mult_btn = null; $centralColsError = null; switch ($submit_mult) { - case 'add_to_central_columns': - $centralColsError = $centralColumns->syncUniqueColumns( - $selected, - false - ); - break; - case 'remove_from_central_columns': - $centralColsError = $centralColumns->deleteColumnsFromList( - $_POST['db'], - $selected, - false - ); - break; case 'change': $this->displayHtmlForColumnChange($selected, $action); // execution stops here but PhpMyAdmin\Response correctly finishes diff --git a/libraries/routes.php b/libraries/routes.php index f818d72b82d1..1d4c37496e6a 100644 --- a/libraries/routes.php +++ b/libraries/routes.php @@ -253,6 +253,8 @@ $routes->addRoute(['GET', 'POST'], '/sql', [TableSqlController::class, 'index']); $routes->addGroup('/structure', function (RouteCollector $routes) { $routes->addRoute(['GET', 'POST'], '', [TableStructureController::class, 'index']); + $routes->post('/central-columns-add', [TableStructureController::class, 'addToCentralColumns']); + $routes->post('/central-columns-remove', [TableStructureController::class, 'removeFromCentralColumns']); $routes->post('/drop', [TableStructureController::class, 'drop']); $routes->post('/drop-confirm', [TableStructureController::class, 'dropConfirm']); $routes->post('/fulltext', [TableStructureController::class, 'fulltext']);