Backup of db before drupal security update
[yaffs-website] / web / core / modules / update / src / UpdateSettingsForm.php
1 <?php
2
3 namespace Drupal\update;
4
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Symfony\Component\DependencyInjection\ContainerInterface;
7 use Drupal\Core\Form\ConfigFormBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Egulias\EmailValidator\EmailValidator;
10
11 /**
12  * Configure update settings for this site.
13  */
14 class UpdateSettingsForm extends ConfigFormBase implements ContainerInjectionInterface {
15
16   /**
17    * The email validator.
18    *
19    * @var \Egulias\EmailValidator\EmailValidator
20    */
21   protected $emailValidator;
22
23   /**
24    * Constructs a new UpdateSettingsForm.
25    *
26    * @param \Egulias\EmailValidator\EmailValidator $email_validator
27    *   The email validator.
28    */
29   public function __construct(EmailValidator $email_validator) {
30     $this->emailValidator = $email_validator;
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public static function create(ContainerInterface $container) {
37     return new static(
38       $container->get('email.validator')
39     );
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function getFormId() {
46     return 'update_settings';
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   protected function getEditableConfigNames() {
53     return ['update.settings'];
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function buildForm(array $form, FormStateInterface $form_state) {
60     $config = $this->config('update.settings');
61
62     $form['update_check_frequency'] = [
63       '#type' => 'radios',
64       '#title' => t('Check for updates'),
65       '#default_value' => $config->get('check.interval_days'),
66       '#options' => [
67         '1' => t('Daily'),
68         '7' => t('Weekly'),
69       ],
70       '#description' => t('Select how frequently you want to automatically check for new releases of your currently installed modules and themes.'),
71     ];
72
73     $form['update_check_disabled'] = [
74       '#type' => 'checkbox',
75       '#title' => t('Check for updates of uninstalled modules and themes'),
76       '#default_value' => $config->get('check.disabled_extensions'),
77     ];
78
79     $notification_emails = $config->get('notification.emails');
80     $form['update_notify_emails'] = [
81       '#type' => 'textarea',
82       '#title' => t('Email addresses to notify when updates are available'),
83       '#rows' => 4,
84       '#default_value' => implode("\n", $notification_emails),
85       '#description' => t('Whenever your site checks for available updates and finds new releases, it can notify a list of users via email. Put each address on a separate line. If blank, no emails will be sent.'),
86     ];
87
88     $form['update_notification_threshold'] = [
89       '#type' => 'radios',
90       '#title' => t('Email notification threshold'),
91       '#default_value' => $config->get('notification.threshold'),
92       '#options' => [
93         'all' => t('All newer versions'),
94         'security' => t('Only security updates'),
95       ],
96       '#description' => t('You can choose to send email only if a security update is available, or to be notified about all newer versions. If there are updates available of Drupal core or any of your installed modules and themes, your site will always print a message on the <a href=":status_report">status report</a> page, and will also display an error message on administration pages if there is a security update.', [':status_report' => $this->url('system.status')])
97     ];
98
99     return parent::buildForm($form, $form_state);
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   public function validateForm(array &$form, FormStateInterface $form_state) {
106     $form_state->set('notify_emails', []);
107     if (!$form_state->isValueEmpty('update_notify_emails')) {
108       $valid = [];
109       $invalid = [];
110       foreach (explode("\n", trim($form_state->getValue('update_notify_emails'))) as $email) {
111         $email = trim($email);
112         if (!empty($email)) {
113           if ($this->emailValidator->isValid($email)) {
114             $valid[] = $email;
115           }
116           else {
117             $invalid[] = $email;
118           }
119         }
120       }
121       if (empty($invalid)) {
122         $form_state->set('notify_emails', $valid);
123       }
124       elseif (count($invalid) == 1) {
125         $form_state->setErrorByName('update_notify_emails', $this->t('%email is not a valid email address.', ['%email' => reset($invalid)]));
126       }
127       else {
128         $form_state->setErrorByName('update_notify_emails', $this->t('%emails are not valid email addresses.', ['%emails' => implode(', ', $invalid)]));
129       }
130     }
131
132     parent::validateForm($form, $form_state);
133   }
134
135   /**
136    * {@inheritdoc}
137    */
138   public function submitForm(array &$form, FormStateInterface $form_state) {
139     $config = $this->config('update.settings');
140     // See if the update_check_disabled setting is being changed, and if so,
141     // invalidate all update status data.
142     if ($form_state->getValue('update_check_disabled') != $config->get('check.disabled_extensions')) {
143       update_storage_clear();
144     }
145
146     $config
147       ->set('check.disabled_extensions', $form_state->getValue('update_check_disabled'))
148       ->set('check.interval_days', $form_state->getValue('update_check_frequency'))
149       ->set('notification.emails', $form_state->get('notify_emails'))
150       ->set('notification.threshold', $form_state->getValue('update_notification_threshold'))
151       ->save();
152
153     parent::submitForm($form, $form_state);
154   }
155
156 }