UpdateSettingsForm.php 4.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
<?php

/**
 * @file
 * Contains \Drupal\update\UpdateSettingsForm.
 */

namespace Drupal\update;

use Drupal\system\SystemConfigFormBase;

/**
 * Configure update settings for this site.
 */
class UpdateSettingsForm extends SystemConfigFormBase {

  /**
   * Implements \Drupal\Core\Form\FormInterface::getFormID().
   */
  public function getFormID() {
    return 'update_settings';
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::buildForm().
   */
  public function buildForm(array $form, array &$form_state) {
    $config = $this->configFactory->get('update.settings');

    $form['update_check_frequency'] = array(
      '#type' => 'radios',
      '#title' => t('Check for updates'),
      '#default_value' => $config->get('check.interval_days'),
      '#options' => array(
        '1' => t('Daily'),
        '7' => t('Weekly'),
      ),
      '#description' => t('Select how frequently you want to automatically check for new releases of your currently installed modules and themes.'),
    );

    $form['update_check_disabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('Check for updates of disabled modules and themes'),
      '#default_value' => $config->get('check.disabled_extensions'),
    );

    $notification_emails = $config->get('notification.emails');
    $form['update_notify_emails'] = array(
      '#type' => 'textarea',
      '#title' => t('E-mail addresses to notify when updates are available'),
      '#rows' => 4,
      '#default_value' => implode("\n", $notification_emails),
      '#description' => t('Whenever your site checks for available updates and finds new releases, it can notify a list of users via e-mail. Put each address on a separate line. If blank, no e-mails will be sent.'),
    );

    $form['update_notification_threshold'] = array(
      '#type' => 'radios',
      '#title' => t('E-mail notification threshold'),
      '#default_value' => $config->get('notification.threshold'),
      '#options' => array(
        'all' => t('All newer versions'),
        'security' => t('Only security updates'),
      ),
      '#description' => t('You can choose to send e-mail 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.', array('@status_report' => url('admin/reports/status')))
    );

    return parent::buildForm($form, $form_state);
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::validateForm().
   */
  public function validateForm(array &$form, array &$form_state) {
    $form_state['notify_emails'] = array();
    if (!empty($form_state['values']['update_notify_emails'])) {
      $valid = array();
      $invalid = array();
      foreach (explode("\n", trim($form_state['values']['update_notify_emails'])) as $email) {
        $email = trim($email);
        if (!empty($email)) {
          if (valid_email_address($email)) {
            $valid[] = $email;
          }
          else {
            $invalid[] = $email;
          }
        }
      }
      if (empty($invalid)) {
        $form_state['notify_emails'] = $valid;
      }
      elseif (count($invalid) == 1) {
        form_set_error('update_notify_emails', t('%email is not a valid e-mail address.', array('%email' => reset($invalid))));
      }
      else {
        form_set_error('update_notify_emails', t('%emails are not valid e-mail addresses.', array('%emails' => implode(', ', $invalid))));
      }
    }

    parent::validateForm($form, $form_state);
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::submitForm().
   */
  public function submitForm(array &$form, array &$form_state) {
    $config = $this->configFactory->get('update.settings');
     // See if the update_check_disabled setting is being changed, and if so,
109
    // invalidate all update status data.
110
    if ($form_state['values']['update_check_disabled'] != $config->get('check.disabled_extensions')) {
111
      update_storage_clear();
112 113 114 115 116 117 118 119 120 121 122 123 124
    }

    $config
      ->set('check.disabled_extensions', $form_state['values']['update_check_disabled'])
      ->set('check.interval_days', $form_state['values']['update_check_frequency'])
      ->set('notification.emails', $form_state['notify_emails'])
      ->set('notification.threshold', $form_state['values']['update_notification_threshold'])
      ->save();

    parent::submitForm($form, $form_state);
  }

}