View source
<?php
namespace Drupal\cleaner\Form;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CleanerSettingsForm extends ConfigFormBase {
private static $intervals = [
900 => '15 min',
1800 => '30 min',
3600 => '1 hour',
7200 => '2 hour',
14400 => '4 hours',
21600 => '6 hours',
43200 => '12 hours',
86400 => '1 day',
172800 => '2 days',
259200 => '3 days',
604800 => '1 week',
];
public function __construct(ConfigFactory $config_factory) {
parent::__construct($config_factory);
}
public static function create(ContainerInterface $container) {
return parent::create($container);
}
public function getFormId() {
return 'cleaner_settings_form';
}
protected function getEditableConfigNames() {
return [
'cleaner.settings',
];
}
protected function getCacheTablesTable() {
$list = self::getAllCacheTables();
if (!empty($list)) {
$rows = self::prepareRows($list);
$table = self::themeTable($rows);
return $this
->t('The current cache tables are:') . $table;
}
return $this
->t('There is no cache tables in the database.');
}
private static function getAllCacheTables() {
$query = \Drupal::database()
->select('INFORMATION_SCHEMA.TABLES', 'tables')
->fields('tables', [
'table_name',
'table_schema',
])
->condition('table_schema', \Drupal::database()
->getConnectionOptions()['database'])
->condition('table_name', 'cache_%', 'LIKE')
->condition('table_name', 'cachetags', '<>');
return $query
->execute()
->fetchCol();
}
private static function prepareRows(array $list) {
$table_rows = [];
$cols = 4;
$count = count($list);
$rows = ceil($count / $cols);
$list = array_pad($list, $rows * $cols, ' ');
for ($i = 0; $i < $count; $i += $cols) {
$table_rows[] = array_slice($list, $i, $cols);
}
return $table_rows;
}
private static function themeTable($rows = []) {
return \Drupal::theme()
->render('table', [
'rows' => $rows,
'attributes' => [
'class' => [
'cleaner-cache-tables',
],
],
]);
}
private static function getSessionSettings() {
$lifetime = (int) session_get_cookie_params()['lifetime'];
$connection = \Drupal::database();
$count = $connection
->select('sessions', 's')
->fields('s', [
'sid',
'timestamp',
])
->condition('timestamp', REQUEST_TIME - $lifetime, '<');
$count = count((array) $count
->execute()
->fetchCol());
return [
'lifetime' => $lifetime,
'old_sessions' => $count,
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$conf = $this
->config(CLEANER_SETTINGS);
$yes_no = [
$this
->t('No:'),
$this
->t('Yes:'),
];
$form['cleaner']['#attached']['library'][] = 'cleaner/cleaner-admin';
$form['cleaner']['cleaner_cron'] = [
'#type' => 'select',
'#title' => $this
->t('Run interval'),
'#options' => array_merge([
0 => $this
->t('Every time'),
], self::$intervals),
'#default_value' => (int) $conf
->get('cleaner_cron'),
'#description' => $this
->t('This is how often the options below will occur. <br> The actions will occur on the next Cron run after this interval expires. <br>"Every time" means on every Cron run.'),
];
$form['cleaner']['cleaner_clear_cache'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Clean up cache'),
'#default_value' => (int) $conf
->get('cleaner_clear_cache'),
'#description' => $this
->getCacheTablesTable(),
];
$form['cleaner']['cleaner_additional_tables'] = [
'#type' => 'textfield',
'#title' => $this
->t('Additional tables to clear'),
'#default_value' => (string) $conf
->get('cleaner_additional_tables'),
'#description' => $this
->t('A comma separated list of table names which also needs to be cleared.'),
];
$form['cleaner']['cleaner_empty_watchdog'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Clean up Watchdog'),
'#default_value' => (int) $conf
->get('cleaner_empty_watchdog'),
'#description' => $this
->t('There is a standard setting for controlling Watchdog contents. This is more useful for test sites.'),
];
$session_settings = self::getSessionSettings();
$form['cleaner']['cleaner_clean_sessions'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Clean up Sessions table'),
'#default_value' => (int) $conf
->get('cleaner_clean_sessions'),
'#description' => $this
->t('The sessions table can quickly become full with old, abandoned sessions. <br>This will delete all sessions older than @interval (as set by your site administrator). <br>There are currently @count such sessions.', [
'@interval' => $session_settings['lifetime'],
'@count' => $session_settings['old_sessions'],
]),
];
if (\Drupal::database()
->driver() == 'mysql') {
$form['cleaner']['cleaner_optimize_db'] = [
'#type' => 'radios',
'#options' => $yes_no + [
'2' => $this
->t('Local only:'),
],
'#title' => $this
->t('Optimize tables with "overhead" space'),
'#default_value' => (int) $conf
->get('cleaner_optimize_db'),
'#description' => $this
->t('The module will compress (optimize) all database tables with unused space.<br><strong>NOTE</strong>: During an optimization, the table will locked against any other activity; on a high vloume site, this may be undesirable. "Local only" means do not replicate the optimization (if it is being done).'),
];
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = \Drupal::configFactory()
->getEditable(CLEANER_SETTINGS);
foreach ($form_state
->getValues() as $name => $value) {
if (stripos($name, 'cleaner') !== FALSE) {
$config
->set($name, $value);
}
}
$config
->save();
parent::submitForm($form, $form_state);
}
}