View source
<?php
namespace Drupal\optimizedb\Form;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Utility\TableSort;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class OptimizedbListTablesForm extends FormBase {
protected $requestStack;
protected $database;
public function __construct(RequestStack $requestStack, Connection $database) {
$this->requestStack = $requestStack;
$this->database = $database;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('request_stack'), $container
->get('database'));
}
public function getFormId() {
return 'optimizedb_list_tables_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$headers = [
'name' => [
'data' => $this
->t('Table name'),
],
'size' => [
'data' => $this
->t('Table size'),
'field' => 'size',
'sort' => 'desc',
],
];
$tables = _optimizedb_tables_list();
$sort = TableSort::getSort($headers, $this->requestStack
->getCurrentRequest());
usort($tables, function ($a, $b) use ($sort) {
if ($sort == 'asc') {
return $a['size_byte'] > $b['size_byte'];
}
return $a['size_byte'] < $b['size_byte'];
});
$rows = [];
optimizedb_operation_messages($form);
foreach ($tables as $table) {
unset($table['size_byte']);
$rows[$table['name']] = $table;
}
if ($this->database
->driver() == 'mysql') {
$form['operations'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Operations with tables:'),
];
$form['operations']['check_tables'] = [
'#type' => 'submit',
'#value' => $this
->t('Check tables'),
];
$form['operations']['repair_tables'] = [
'#type' => 'submit',
'#value' => $this
->t('Repair tables'),
];
$form['operations']['optimize_tables'] = [
'#type' => 'submit',
'#value' => $this
->t('Optimize tables'),
];
}
$form['tables'] = [
'#type' => 'tableselect',
'#header' => $headers,
'#options' => $rows,
'#empty' => $this
->t('No content available.'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$tables = $form_state
->getValue('tables');
$operation = '';
$op = (string) $form_state
->getValue('op');
switch ($op) {
case $this
->t('Check tables'):
$operation = 'CHECK TABLE';
break;
case $this
->t('Repair tables'):
$operation = 'REPAIR TABLE';
break;
case $this
->t('Optimize tables'):
$operation = 'OPTIMIZE TABLE';
break;
}
_optimizedb_list_tables_operation_execute($tables, $operation);
}
}