From ff86a0b6d6ce08ea6e17758b511f3c366d5ba728 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Tue, 19 May 2020 11:21:42 -0300 Subject: [PATCH] Extract drop confirmation action from index action MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extracts the /table/structure/drop-confirm route from the /table/structure route. Signed-off-by: MaurĂ­cio Meneghini Fauth --- js/table/structure.js | 11 +++- .../Controllers/Table/StructureController.php | 59 ++++++++++++++----- libraries/routes.php | 5 +- templates/table/structure/drop_confirm.twig | 22 +++++++ .../Table/StructureControllerTest.php | 6 +- 5 files changed, 81 insertions(+), 22 deletions(-) create mode 100644 templates/table/structure/drop_confirm.twig diff --git a/js/table/structure.js b/js/table/structure.js index 1d4961e57e12..ca1b1737d376 100644 --- a/js/table/structure.js +++ b/js/table/structure.js @@ -412,12 +412,19 @@ AJAX.registerOnload('table/structure.js', function () { $('body').on('click', '#fieldsForm.ajax button[name="submit_mult"], #fieldsForm.ajax input[name="submit_mult"]', function (e) { e.preventDefault(); var $button = $(this); + var action = $button.val(); var $form = $button.parents('form'); var argsep = CommonParams.get('arg_separator'); - var submitData = $form.serialize() + argsep + 'ajax_request=true' + argsep + 'ajax_page_request=true' + argsep + 'submit_mult=' + $button.val(); + var submitData = $form.serialize() + argsep + 'ajax_request=true' + argsep + 'ajax_page_request=true' + argsep + 'submit_mult=' + action; Functions.ajaxShowMessage(); AJAX.source = $form; - $.post($form.attr('action'), submitData, AJAX.responseHandler); + var url = $form.attr('action'); + + if (action === 'drop') { + url = 'index.php?route=/table/structure/drop-confirm'; + } + + $.post(url, submitData, AJAX.responseHandler); }); /** diff --git a/libraries/classes/Controllers/Table/StructureController.php b/libraries/classes/Controllers/Table/StructureController.php index dc1deb7aa833..60c5a6a0d9f1 100644 --- a/libraries/classes/Controllers/Table/StructureController.php +++ b/libraries/classes/Controllers/Table/StructureController.php @@ -315,18 +315,6 @@ public function index(): void $full_query = preg_replace('@,$@', ');
', $full_query); } break; - case 'drop_fld': - if ($full_query == '') { - $full_query .= 'ALTER TABLE ' - . Util::backquote(htmlspecialchars($table)); - } - $full_query .= '
  DROP ' - . Util::backquote(htmlspecialchars($selectedValue)) - . ','; - if ($i == $selectedCount - 1) { - $full_query = preg_replace('@,$@', ';
', $full_query); - } - break; } $i++; } @@ -551,6 +539,49 @@ public function index(): void ); } + public function dropConfirm(): void + { + global $db, $table; + + $selected = $_POST['selected_fld'] ?? null; + + if (empty($selected)) { + $this->response->setRequestStatus(false); + $this->response->addJSON('message', __('No column selected.')); + + return; + } + + $this->dbi->selectDb($this->db); + + PageSettings::showGroup('TableStructure'); + + $checkUserPrivileges = new CheckUserPrivileges($this->dbi); + $checkUserPrivileges->getPrivileges(); + + $this->response->getHeader()->getScripts()->addFiles([ + 'table/structure.js', + 'indexes.js', + ]); + + Common::table(); + + $urlParams = [ + 'db' => $db, + 'table' => $table, + 'query_type' => 'drop_fld', + ]; + foreach ($selected as $selectedValue) { + $urlParams['selected'][] = $selectedValue; + } + + $this->render('table/structure/drop_confirm', [ + 'url_params' => $urlParams, + 'table' => $table, + 'fields' => $selected, + ]); + } + /** * Moves columns in the table's structure based on $_REQUEST * @@ -966,7 +997,6 @@ protected function getMultipleFieldCommandType() { $types = [ 'change', - 'drop', 'primary', 'index', 'unique', @@ -1748,9 +1778,6 @@ protected function getDataForSubmitMult($submit_mult, $selected, $action) $mult_btn = null; $centralColsError = null; switch ($submit_mult) { - case 'drop': - $what = 'drop_fld'; - break; case 'primary': // Gets table primary key $primary = $this->getKeyForTablePrimary(); diff --git a/libraries/routes.php b/libraries/routes.php index f76e8628c150..2144770a55a2 100644 --- a/libraries/routes.php +++ b/libraries/routes.php @@ -251,7 +251,10 @@ $routes->addRoute(['GET', 'POST'], '/replace', [ReplaceController::class, 'index']); $routes->addRoute(['GET', 'POST'], '/search', [TableSearchController::class, 'index']); $routes->addRoute(['GET', 'POST'], '/sql', [TableSqlController::class, 'index']); - $routes->addRoute(['GET', 'POST'], '/structure', [TableStructureController::class, 'index']); + $routes->addGroup('/structure', function (RouteCollector $routes) { + $routes->addRoute(['GET', 'POST'], '', [TableStructureController::class, 'index']); + $routes->post('/drop-confirm', [TableStructureController::class, 'dropConfirm']); + }); $routes->addRoute(['GET', 'POST'], '/tracking', [TableTrackingController::class, 'index']); $routes->addRoute(['GET', 'POST'], '/triggers', [TableTriggersController::class, 'index']); $routes->addRoute(['GET', 'POST'], '/zoom-search', [ZoomSearchController::class, 'index']); diff --git a/templates/table/structure/drop_confirm.twig b/templates/table/structure/drop_confirm.twig new file mode 100644 index 000000000000..5e9a206e702f --- /dev/null +++ b/templates/table/structure/drop_confirm.twig @@ -0,0 +1,22 @@ +
+ {{ get_hidden_inputs(url_params) }} + +
+ + {% trans 'Do you really want to execute the following query?' %} + + + + ALTER TABLE {{ backquote(table) }}
+ {% for field in fields %} +   DROP {{ backquote(field) }} + {%- if loop.last %};{% else %},
{% endif %} + {% endfor %} +
+
+ +
+ + +
+
diff --git a/test/classes/Controllers/Table/StructureControllerTest.php b/test/classes/Controllers/Table/StructureControllerTest.php index 5c698380341c..4f30eeafad5f 100644 --- a/test/classes/Controllers/Table/StructureControllerTest.php +++ b/test/classes/Controllers/Table/StructureControllerTest.php @@ -215,12 +215,12 @@ public function testGetMultipleFieldCommandType() $method->invoke($ctrl) ); - $_POST['submit_mult_drop_x'] = true; + $_POST['submit_mult_unique_x'] = true; $this->assertEquals( - 'drop', + 'unique', $method->invoke($ctrl) ); - unset($_POST['submit_mult_drop_x']); + unset($_POST['submit_mult_unique_x']); $_POST['submit_mult'] = 'create'; $this->assertEquals(