View source
<?php
namespace Drupal\cleaner\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CleanerSettingsForm extends ConfigFormBase {
protected $database;
protected $themeManager;
protected $dateTime;
protected 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(ConfigFactoryInterface $config_factory, Connection $database, ThemeManagerInterface $theme_manager, TimeInterface $time) {
parent::__construct($config_factory);
$this->database = $database;
$this->themeManager = $theme_manager;
$this->dateTime = $time;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('database'), $container
->get('theme.manager'), $container
->get('datetime.time'));
}
public function getFormId() {
return 'cleaner_settings_form';
}
protected function getEditableConfigNames() {
return [
'cleaner.settings',
];
}
protected function getCacheTablesTable() {
$list = $this
->getAllCacheTables();
if (!empty($list)) {
$rows = static::prepareRows($list);
$table = $this
->themeTable($rows);
return $this
->t('The current cache tables are: @table', [
'@table' => $table,
]);
}
return $this
->t('There is no cache tables in the database.');
}
protected function getAllCacheTables() {
$db_name = $this->database
->getConnectionOptions()['database'];
$query = $this->database
->select('INFORMATION_SCHEMA.TABLES', 'tables');
$query
->fields('tables', [
'table_name',
'table_schema',
]);
$query
->condition('table_schema', $db_name);
$query
->condition('table_name', '%cache_%', 'LIKE');
$query
->condition('table_name', '%cachetags', '<>');
return $query
->execute()
->fetchCol();
}
protected 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;
}
protected function themeTable($rows = []) {
return $this->themeManager
->render('table', [
'rows' => $rows,
'attributes' => [
'class' => [
'cleaner-cache-tables',
],
],
]);
}
protected function getSessionSettings() {
$lifetime = (int) session_get_cookie_params()['lifetime'];
$timestamp = (int) ($this->dateTime
->getRequestTime() - $lifetime);
$count = $this->database
->select('sessions', 's')
->fields('s', [
'sid',
'timestamp',
])
->condition('timestamp', $timestamp, '<');
$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'),
], static::$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 = $this
->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 ($this->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 = $this->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);
}
}