|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\os2web_logging\Form; |
| 4 | + |
| 5 | +use Drupal\Component\Serialization\Json; |
| 6 | +use Drupal\Core\File\FileSystemInterface; |
| 7 | +use Drupal\Core\Form\ConfigFormBase; |
| 8 | +use Drupal\Core\Form\FormStateInterface; |
| 9 | +use Drupal\Core\Link; |
| 10 | +use Drupal\Core\Url; |
| 11 | +use Drupal\node\Entity\NodeType; |
| 12 | +use Drupal\os2web_logging\Entity\AccessLog; |
| 13 | + |
| 14 | +/** |
| 15 | + * Watchdog Logging settings form. |
| 16 | + */ |
| 17 | +class WatchdogSettingsForm extends ConfigFormBase { |
| 18 | + |
| 19 | + /** |
| 20 | + * Name of the config. |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + public static $configName = 'os2web_logging_watchdog.settings'; |
| 25 | + |
| 26 | + /** |
| 27 | + * {@inheritdoc} |
| 28 | + */ |
| 29 | + protected function getEditableConfigNames() { |
| 30 | + return [WatchdogSettingsForm::$configName]; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * {@inheritdoc} |
| 35 | + */ |
| 36 | + public function getFormId() { |
| 37 | + return 'os2web_logging_watchdog_settings_form'; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * {@inheritdoc} |
| 42 | + */ |
| 43 | + public function buildForm(array $form, FormStateInterface $form_state) { |
| 44 | + $config = $this->config(WatchdogSettingsForm::$configName); |
| 45 | + |
| 46 | + $form['watchdog_db_details'] = [ |
| 47 | + '#type' => 'details', |
| 48 | + '#title' => $this |
| 49 | + ->t('Watchdog DB logs'), |
| 50 | + '#open' => TRUE, |
| 51 | + ]; |
| 52 | + |
| 53 | + $form['watchdog_db_details']['dblog_enabled'] = [ |
| 54 | + '#type' => 'checkbox', |
| 55 | + '#title' => $this->t('DB Log enabled'), |
| 56 | + '#description' => $this->t('If logs are to be stored in the database as dblog entries.'), |
| 57 | + '#default_value' => $config->get('dblog_enabled') ?? TRUE, |
| 58 | + ]; |
| 59 | + |
| 60 | + // Set up the link. |
| 61 | + $url = Url::fromUri('internal:/admin/reports/dblog'); |
| 62 | + $link = Link::fromTextAndUrl('Recent log messages', $url); |
| 63 | + |
| 64 | + $form['watchdog_db_details'][] = [ |
| 65 | + '#markup' => $this->t('DB Logs messages can be seen on the %reports page', ['%reports' => $link->toString()]), |
| 66 | + ]; |
| 67 | + |
| 68 | + $form['watchdog_files_details'] = [ |
| 69 | + '#type' => 'details', |
| 70 | + '#title' => $this |
| 71 | + ->t('Watchdog File logs'), |
| 72 | + ]; |
| 73 | + |
| 74 | + $form['watchdog_files_details']['files_store_period'] = [ |
| 75 | + '#type' => 'number', |
| 76 | + '#title' => $this->t('Store log files for this period'), |
| 77 | + '#field_suffix' => $this->t('days'), |
| 78 | + '#size' => 5, |
| 79 | + '#min' => 180, |
| 80 | + '#description' => $this->t('Log file will be stored for the selected number of days, after that they will be automatically deleted'), |
| 81 | + '#default_value' => $config->get('files_store_period') ?? 180, |
| 82 | + ]; |
| 83 | + |
| 84 | + $form['watchdog_files_details']['files_log_path'] = [ |
| 85 | + '#type' => 'textfield', |
| 86 | + '#title' => $this->t('Store log files directory'), |
| 87 | + '#description' => $this->t('Log file will be stored for the selected number of days, after that they will be automatically deleted'), |
| 88 | + '#default_value' => $config->get('files_log_path') ?? '../logs', |
| 89 | + ]; |
| 90 | + |
| 91 | + $options = []; |
| 92 | + if ($config->get('files_log_path')) { |
| 93 | + /** @var FileSystemInterface $fileSystem */ |
| 94 | + $fileSystem = \Drupal::service('file_system'); |
| 95 | + $storedLogFiles = $fileSystem->scanDirectory($config->get('files_log_path'), '/os2web_logging_watchdog-\d{4}-\d{2}-\d{2}\.(log|gz)/'); |
| 96 | + |
| 97 | + foreach ($storedLogFiles as $file) { |
| 98 | + $url = Url::fromRoute('os2web_logging.logfile.download', ['filename' => $file->filename]); |
| 99 | + $link = Link::fromTextAndUrl($file->filename, $url); |
| 100 | + $options[$file->uri] = $link; |
| 101 | + } |
| 102 | + arsort($options); |
| 103 | + } |
| 104 | + |
| 105 | + $watchdog_logs_build = [ |
| 106 | + '#theme' => 'item_list', |
| 107 | + '#title' => t('Watchdog logs'), |
| 108 | + '#items' => $options, |
| 109 | + ]; |
| 110 | + $watchlog_logs_rendered = \Drupal::service('renderer')->renderPlain($watchdog_logs_build); |
| 111 | + |
| 112 | + $form['watchdog_files_details'][] = [ |
| 113 | + '#markup' => $watchlog_logs_rendered |
| 114 | + ]; |
| 115 | + |
| 116 | + return parent::buildForm($form, $form_state); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * {@inheritdoc} |
| 121 | + */ |
| 122 | + public function validateForm(array &$form, FormStateInterface $form_state) { |
| 123 | + parent::validateForm($form, $form_state); |
| 124 | + |
| 125 | + $files_log_path = $form_state->getValue('files_log_path'); |
| 126 | + |
| 127 | + $exists = \Drupal::service('file_system')->prepareDirectory($files_log_path, FileSystemInterface::MODIFY_PERMISSIONS); |
| 128 | + if (!$exists) { |
| 129 | + $form_state->setErrorByName('files_log_path', t('Directory does not exist or is not writable %dir', ['%dir' => $files_log_path])); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * {@inheritdoc} |
| 135 | + */ |
| 136 | + public function submitForm(array &$form, FormStateInterface $form_state) { |
| 137 | + // Saving values. |
| 138 | + $config = $this->config(WatchdogSettingsForm::$configName); |
| 139 | + $old_files_store_period = $config->get('files_store_period'); |
| 140 | + $old_files_store_path = $config->get('files_log_path'); |
| 141 | + $old_dblog_enabled = $config->get('dblog_enabled'); |
| 142 | + |
| 143 | + $values = $form_state->getValues(); |
| 144 | + foreach ($values as $key => $value) { |
| 145 | + $config->set($key, $value); |
| 146 | + } |
| 147 | + $config->save(); |
| 148 | + |
| 149 | + // Rebuilding cache only if 'files_store_period' or 'files_log_path' |
| 150 | + // changed. New setting requires cache to be rebuilt. |
| 151 | + if ($old_files_store_period != $config->get('files_store_period') || |
| 152 | + $old_files_store_path != $config->get('files_log_path') || |
| 153 | + $old_dblog_enabled != $config->get('dblog_enabled')) { |
| 154 | + |
| 155 | + // Rebuild module and theme data. |
| 156 | + $module_data = \Drupal::service('extension.list.module')->getList(); |
| 157 | + |
| 158 | + $files = []; |
| 159 | + foreach ($module_data as $name => $extension) { |
| 160 | + if ($extension->status) { |
| 161 | + $files[$name] = $extension; |
| 162 | + } |
| 163 | + } |
| 164 | + \Drupal::service('kernel') |
| 165 | + ->updateModules(\Drupal::moduleHandler() |
| 166 | + ->getModuleList(), $files); |
| 167 | + } |
| 168 | + |
| 169 | + parent::submitForm($form, $form_state); |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | +} |
0 commit comments