Skip to content

Commit

Permalink
Extract drop confirmation action from index action
Browse files Browse the repository at this point in the history
Extracts the /table/structure/drop-confirm route from the
/table/structure route.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed May 19, 2020
1 parent 73290bc commit ff86a0b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 22 deletions.
11 changes: 9 additions & 2 deletions js/table/structure.js
Expand Up @@ -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);
});

/**
Expand Down
59 changes: 43 additions & 16 deletions libraries/classes/Controllers/Table/StructureController.php
Expand Up @@ -315,18 +315,6 @@ public function index(): void
$full_query = preg_replace('@,$@', ');<br>', $full_query);
}
break;
case 'drop_fld':
if ($full_query == '') {
$full_query .= 'ALTER TABLE '
. Util::backquote(htmlspecialchars($table));
}
$full_query .= '<br>&nbsp;&nbsp;DROP '
. Util::backquote(htmlspecialchars($selectedValue))
. ',';
if ($i == $selectedCount - 1) {
$full_query = preg_replace('@,$@', ';<br>', $full_query);
}
break;
}
$i++;
}
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -966,7 +997,6 @@ protected function getMultipleFieldCommandType()
{
$types = [
'change',
'drop',
'primary',
'index',
'unique',
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion libraries/routes.php
Expand Up @@ -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']);
Expand Down
22 changes: 22 additions & 0 deletions templates/table/structure/drop_confirm.twig
@@ -0,0 +1,22 @@
<form action="{{ url('/table/structure') }}" method="post">
{{ get_hidden_inputs(url_params) }}

<fieldset class="confirmation">
<legend>
{% trans 'Do you really want to execute the following query?' %}
</legend>

<code>
ALTER TABLE {{ backquote(table) }}<br>
{% for field in fields %}
&nbsp;&nbsp;DROP {{ backquote(field) }}
{%- if loop.last %};{% else %},<br>{% endif %}
{% endfor %}
</code>
</fieldset>

<fieldset class="tblFooters">
<input id="buttonYes" class="btn btn-secondary" type="submit" name="mult_btn" value="{% trans 'Yes' %}">
<input id="buttonNo" class="btn btn-secondary" type="submit" name="mult_btn" value="{% trans 'No' %}">
</fieldset>
</form>
6 changes: 3 additions & 3 deletions test/classes/Controllers/Table/StructureControllerTest.php
Expand Up @@ -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(
Expand Down

0 comments on commit ff86a0b

Please sign in to comment.