Skip to content

Commit

Permalink
ISSUE-1: Save settings. Some UI improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
robiningelbrecht committed Jan 10, 2019
1 parent 3b05c80 commit 15a090a
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 30 deletions.
14 changes: 1 addition & 13 deletions config/install/gdpr_dumper.settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,4 @@ gdpr_replacements:
mail:
formatter: 'email'
pass:
formatter: 'clear'
# TODO: move this to enum class.
drivers:
mysql:
dump_command: 'mysqldump'
oracle:
dump_command: 'mysqldump'
pqsql:
dump_command: 'pg_dump'
sqlite:
dump_command: 'dump'
sqlsrv:
dump_command: 'mysqldump'
formatter: 'clear'
8 changes: 8 additions & 0 deletions gdpr_dumper.libraries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
settings-form:
version: 1.x
js:
js/gdpr-dumper.js: {}
dependencies:
- core/jquery
- core/drupal.form

5 changes: 5 additions & 0 deletions gdpr_dumper.links.menu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
gdpr_dumper.settings:
title: 'GDPR dumper'
description: 'Configure GDPR dumper drush command'
parent: system.admin_config_development
route_name: gdpr_dumper.settings
3 changes: 3 additions & 0 deletions gdpr_dumper.permissions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
administer gdpr dumper:
title: 'Administer gdpr dumper'
description: 'Allow to configure the gdpr dumper drush command'
13 changes: 13 additions & 0 deletions js/gdpr-dumper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(function($, Drupal) {

Drupal.behaviors.gdprDumperSummary = {
attach: function (context, settings) {
// Display the action in the vertical tab summary.
$(context).find('details[data-table-summary]').drupalSetSummary(function(context) {
return Drupal.checkPlain($(context).data('table-summary'));
});

}
}

})(jQuery, Drupal);
25 changes: 20 additions & 5 deletions src/Form/GdprDumperSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ protected function getEditableConfigNames() {
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$replacements = $this->settings->get('gdpr_replacements');
$empty_tables = $this->settings->get('empty_tables');

$database_tables = $this->databaseManager->getTableColumns();
$db_schema = $this->connection->schema();
$schema_handles_db_comments = \is_callable([$db_schema, 'getComment']);
Expand Down Expand Up @@ -102,26 +104,32 @@ public function buildForm(array $form, FormStateInterface $form_state) {

$form['replacements'] = [
'#tree' => TRUE,
'#attached' => [
'library' => ['gdpr_dumper/settings-form']
],
];

foreach ($replacements as $table => $columns) {
if (isset($database_tables[$table])) {
$table_summary = $schema_handles_db_comments ? $db_schema->getComment($table) : '-';
$form['replacements'][$table] = [
'#type' => 'details',
'#title' => $table,
'#group' => 'advanced',
'#attributes' => [
'data-table-summary' => $table_summary,

This comment has been minimized.

Copy link
@anotherjames

anotherjames Jan 11, 2019

Collaborator

We should probably ensure $table_summary is safe markup.

This comment has been minimized.

Copy link
@robiningelbrecht

robiningelbrecht Jan 11, 2019

Author Owner

the JS file already checks this: return Drupal.checkPlain($(context).data('table-summary')); or is this not sufficient?

This comment has been minimized.

Copy link
@anotherjames

anotherjames Jan 11, 2019

Collaborator

Oh, true :-)

],
];

$form['replacements'][$table]['columns'] = [
'#type' => 'table',
'#caption' => $table_summary,
'#header' => [
['data' => $this->t('Field')],
['data' => $this->t('Type')],
['data' => $this->t('Description')],
['data' => $this->t('Apply anonymization')],
],
// @todo: attach this in JS.
//'#title' => $schema_handles_db_comments ? $db_schema->getComment($table) : NULL,
];

foreach ($database_tables[$table] as $column_name => $column_properties) {
Expand Down Expand Up @@ -150,6 +158,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#type' => 'checkbox',
'#title' => $this->t('Empty this table'),
'#button_type' => 'secondary',
'#default_value' => isset($empty_tables[$table]) ? $empty_tables[$table] : FALSE,
];
}
}
Expand All @@ -171,6 +180,9 @@ public function submitAddTable(array &$form, FormStateInterface $form_state) {
$replacements[$table] = [];
}

// Order tables alphabetically before saving.
ksort($replacements);

// Update config.
$this->settings->set('gdpr_replacements', $replacements)->save();
$this->messenger()
Expand All @@ -184,6 +196,7 @@ public function submitAddTable(array &$form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) {
$settings = [
'gdpr_replacements' => [],
'empty_tables' => [],
];

$replacements = $form_state->getValue('replacements');
Expand All @@ -193,11 +206,13 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
if (!empty($column['anonymization'])) {
$settings['gdpr_replacements'][$table_name][$column_name]['formatter'] = $column['anonymization'];
}
}
};
$settings['empty_tables'][$table_name] = $properties['empty'];
}

// @todo: order tables alphabetically before saving.
// @todo: save settings if driver config is moved out of config file.
// Save settings.
$this->settings->set('gdpr_replacements', $settings['gdpr_replacements'])
->set('empty_tables', $settings['empty_tables'])->save();

parent::submitForm($form, $form_state);
}
Expand Down
46 changes: 36 additions & 10 deletions src/GdprDumperEnums.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,43 @@ public static function fakerFormatters() {
return [
'name' => 'Generate a name',
'phoneNumber' => t('Generate a phone number'),
'username' => t('Generate a random user name'),
'password' => t('Generate a random password'),
'email' => t('Generate a random email address'),
'date' => t('Generate a date'),
'longText' => t('Generate a sentence'),
'number' => t('Generate a number'),
'randomText' => t('Generate a sentence'),
'text' => t('Generate a paragraph'),
'uri' => t('Generate a URI'),
'clear' => t('Generate an empty string'),
'username' => t('Generate a random user name'),
'password' => t('Generate a random password'),
'email' => t('Generate a random email address'),
'date' => t('Generate a date'),
'longText' => t('Generate a sentence'),
'number' => t('Generate a number'),
'randomText' => t('Generate a sentence'),
'text' => t('Generate a paragraph'),
'uri' => t('Generate a URI'),
'clear' => t('Generate an empty string'),
];
}

/**
* @param $driver
* @return array
*/
public static function driverOptions($driver) {
$map = [
'mysql' => [
'dump_command' => 'mysqldump',
],
'oracle' => [
'dump_command' => 'mysqldump',
],
'pqsql' => [
'dump_command' => 'pg_dump',
],
'sqlite' => [
'dump_command' => 'dump',
],
'sqlsrv' => [
'dump_command' => 'mysqldump',
],
];

return isset($map[$driver]) ? $map[$driver] : [];
}

}
4 changes: 2 additions & 2 deletions src/Sql/GdprSqlBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Drupal\Core\Database\Database;
use Drupal\gdpr_dumper\Event\GdprDumperEvents;
use Drupal\gdpr_dumper\Event\GdprReplacementsEvent;
use Drupal\gdpr_dumper\GdprDumperEnums;
use Drush\Drush;
use Drush\Sql\SqlBase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -71,10 +72,9 @@ public static function getInstance($dbSpec, $options, EventDispatcherInterface $
}

$instance = new $className($dbSpec, $options);
$driver_options = isset($config->get('drivers')[$driver]) ? $config->get('drivers')[$driver] : [];
// Inject config
$instance->setConfig(Drush::config());
$instance->setDriverOptions($driver_options);
$instance->setDriverOptions(GdprDumperEnums::driverOptions($driver));
return $instance;
}

Expand Down

0 comments on commit 15a090a

Please sign in to comment.