You are here

public function OptimizedbListTablesForm::buildForm in OptimizeDB 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/OptimizedbListTablesForm.php, line 64

Class

OptimizedbListTablesForm
Operations with tables.

Namespace

Drupal\optimizedb\Form

Code

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 = [];

  // Messages status execute operation.
  optimizedb_operation_messages($form);
  foreach ($tables as $table) {

    // Parameter "size_byte" is only needed to sort, now this unit to remove.
    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;
}