ac8981cf49eb035aab1ea666100b7a9afae3a877
[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  * @internal
15  */
16 class UpdateSettingsForm extends ConfigFormBase implements ContainerInjectionInterface {
17
18   /**
19    * The email validator.
20    *
21    * @var \Egulias\EmailValidator\EmailValidator
22    */
23   protected $emailValidator;
24
25   /**
26    * Constructs a new UpdateSettingsForm.
27    *
28    * @param \Egulias\EmailValidator\EmailValidator $email_validator
29    *   The email validator.
30    */
31   public function __construct(EmailValidator $email_validator) {
32     $this->emailValidator = $email_validator;
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public static function create(ContainerInterface $container) {
39     return new static(
40       $container->get('email.validator')
41     );
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function getFormId() {
48     return 'update_settings';
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   protected function getEditableConfigNames() {
55     return ['update.settings'];
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function buildForm(array $form, FormStateInterface $form_state) {
62     $config = $this->config('update.settings');
63
64     $form['update_check_frequency'] = [
65       '#type' => 'radios',
66       '#title' => t('Check for updates'),
67       '#default_value' => $config->get('check.interval_days'),
68       '#options' => [
69         '1' => t('Daily'),
70         '7' => t('Weekly'),
71       ],
72       '#description' => t('Select how frequently you want to automatically check for new releases of your currently installed modules and themes.'),
73     ];
74
75     $form['update_check_disabled'] = [
76       '#type' => 'checkbox',
77       '#title' => t('Check for updates of uninstalled modules and themes'),
78       '#default_value' => $config->get('check.disabled_extensions'),
79     ];
80
81     $notification_emails = $config->get('notification.emails');
82     $form['update_notify_emails'] = [
83       '#type' => 'textarea',
84       '#title' => t('Email addresses to notify when updates are available'),
85       '#rows' => 4,
86       '#default_value' => implode("\n", $notification_emails),
87       '#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.'),
88     ];
89
90     $form['update_notification_threshold'] = [
91       '#type' => 'radios',
92       '#title' => t('Email notification threshold'),
93       '#default_value' => $config->get('notification.threshold'),
94       '#options' => [
95         'all' => t('All newer versions'),
96         'security' => t('Only security updates'),
97       ],
98       '#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')])
99     ];
100
101     return parent::buildForm($form, $form_state);
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function validateForm(array &$form, FormStateInterface $form_state) {
108     $form_state->set('notify_emails', []);
109     if (!$form_state->isValueEmpty('update_notify_emails')) {
110       $valid = [];
111       $invalid = [];
112       foreach (explode("\n", trim($form_state->getValue('update_notify_emails'))) as $email) {
113         $email = trim($email);
114         if (!empty($email)) {
115           if ($this->emailValidator->isValid($email)) {
116             $valid[] = $email;
117           }
118           else {
119             $invalid[] = $email;
120           }
121         }
122       }
123       if (empty($invalid)) {
124         $form_state->set('notify_emails', $valid);
125       }
126       elseif (count($invalid) == 1) {
127         $form_state->setErrorByName('update_notify_emails', $this->t('%email is not a valid email address.', ['%email' => reset($invalid)]));
128       }
129       else {
130         $form_state->setErrorByName('update_notify_emails', $this->t('%emails are not valid email addresses.', ['%emails' => implode(', ', $invalid)]));
131       }
132     }
133
134     parent::validateForm($form, $form_state);
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public function submitForm(array &$form, FormStateInterface $form_state) {
141     $config = $this->config('update.settings');
142     // See if the update_check_disabled setting is being changed, and if so,
143     // invalidate all update status data.
144     if ($form_state->getValue('update_check_disabled') != $config->get('check.disabled_extensions')) {
145       update_storage_clear();
146     }
147
148     $config
149       ->set('check.disabled_extensions', $form_state->getValue('update_check_disabled'))
150       ->set('check.interval_days', $form_state->getValue('update_check_frequency'))
151       ->set('notification.emails', $form_state->get('notify_emails'))
152       ->set('notification.threshold', $form_state->getValue('update_notification_threshold'))
153       ->save();
154
155     parent::submitForm($form, $form_state);
156   }
157
158 }