UpdateManagerUpdate.php 11 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Drupal\update\Form;

use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase;
7
use Drupal\Core\Form\FormStateInterface;
8
use Drupal\Core\State\StateInterface;
9
use Drupal\Core\Url;
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
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Configure update settings for this site.
 */
class UpdateManagerUpdate extends FormBase {

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The Drupal state storage service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * Constructs a new UpdateManagerUpdate object.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\State\StateInterface $state
37
   *   The state service.
38 39 40 41 42 43 44 45 46
   */
  public function __construct(ModuleHandlerInterface $module_handler, StateInterface $state) {
    $this->moduleHandler = $module_handler;
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
47
  public function getFormId() {
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    return 'update_manager_update_form';
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('module_handler'),
      $container->get('state')
    );
  }

  /**
   * {@inheritdoc}
   */
64
  public function buildForm(array $form, FormStateInterface $form_state) {
65 66 67 68 69 70 71
    $this->moduleHandler->loadInclude('update', 'inc', 'update.manager');

    $last_markup = array(
      '#theme' => 'update_last_check',
      '#last' => $this->state->get('update.last_check') ?: 0,
    );
    $form['last_check'] = array(
72
      '#markup' => \Drupal::service('renderer')->render($last_markup),
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
    );

    if (!_update_manager_check_backends($form, 'update')) {
      return $form;
    }

    $available = update_get_available(TRUE);
    if (empty($available)) {
      $form['message'] = array(
        '#markup' => $this->t('There was a problem getting update information. Try again later.'),
      );
      return $form;
    }

    $form['#attached']['library'][] = 'update/drupal.update.admin';

    // This will be a nested array. The first key is the kind of project, which
    // can be either 'enabled', 'disabled', 'manual' (projects which require
    // manual updates, such as core). Then, each subarray is an array of
    // projects of that type, indexed by project short name, and containing an
    // array of data for cells in that project's row in the appropriate table.
    $projects = array();

    // This stores the actual download link we're going to update from for each
    // project in the form, regardless of if it's enabled or disabled.
    $form['project_downloads'] = array('#tree' => TRUE);
    $this->moduleHandler->loadInclude('update', 'inc', 'update.compare');
    $project_data = update_calculate_project_data($available);
    foreach ($project_data as $name => $project) {
      // Filter out projects which are up to date already.
      if ($project['status'] == UPDATE_CURRENT) {
        continue;
      }
      // The project name to display can vary based on the info we have.
      if (!empty($project['title'])) {
        if (!empty($project['link'])) {
109
          $project_name = $this->l($project['title'], Url::fromUri($project['link']));
110 111
        }
        else {
112
          $project_name = $project['title'];
113 114 115
        }
      }
      elseif (!empty($project['info']['name'])) {
116
        $project_name = $project['info']['name'];
117 118
      }
      else {
119
        $project_name = $name;
120 121 122 123 124 125 126 127 128 129 130 131
      }
      if ($project['project_type'] == 'theme' || $project['project_type'] == 'theme-disabled') {
        $project_name .= ' ' . $this->t('(Theme)');
      }

      if (empty($project['recommended'])) {
        // If we don't know what to recommend they upgrade to, we should skip
        // the project entirely.
        continue;
      }

      $recommended_release = $project['releases'][$project['recommended']];
132
      $recommended_version = '{{ release_version }} (<a href="{{ release_link }}" title="{{ project_title }}">{{ release_notes }}</a>)';
133
      if ($recommended_release['version_major'] != $project['existing_major']) {
134
        $recommended_version .= '<div title="{{ major_update_warning_title }}" class="update-major-version-warning">{{ major_update_warning_text }}</div>';
135 136
      }

137 138 139 140 141 142 143 144 145 146 147 148 149
      $recommended_version = array(
        '#type' => 'inline_template',
        '#template' => $recommended_version,
        '#context' => array(
          'release_version' => $recommended_release['version'],
          'release_link' => $recommended_release['release_link'],
          'project_title' => $this->t('Release notes for @project_title', array('@project_title' => $project['title'])),
          'major_update_warning_title' => $this->t('Major upgrade warning'),
          'major_update_warning_text' => $this->t('This update is a major version update which means that it may not be backwards compatible with your currently running version. It is recommended that you read the release notes and proceed at your own risk.'),
          'release_notes' => $this->t('Release notes'),
        ),
      );

150 151 152 153
      // Create an entry for this project.
      $entry = array(
        'title' => $project_name,
        'installed_version' => $project['existing_version'],
154
        'recommended_version' => array('data' => $recommended_version),
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
      );

      switch ($project['status']) {
        case UPDATE_NOT_SECURE:
        case UPDATE_REVOKED:
          $entry['title'] .= ' ' . $this->t('(Security update)');
          $entry['#weight'] = -2;
          $type = 'security';
          break;

        case UPDATE_NOT_SUPPORTED:
          $type = 'unsupported';
          $entry['title'] .= ' ' . $this->t('(Unsupported)');
          $entry['#weight'] = -1;
          break;

        case UPDATE_UNKNOWN:
        case UPDATE_NOT_FETCHED:
        case UPDATE_NOT_CHECKED:
        case UPDATE_NOT_CURRENT:
          $type = 'recommended';
          break;

        default:
          // Jump out of the switch and onto the next project in foreach.
          continue 2;
      }

      // Use the project title for the tableselect checkboxes.
      $entry['title'] = array('data' => array(
        '#title' => $entry['title'],
        '#markup' => $entry['title'],
      ));
      $entry['#attributes'] = array('class' => array('update-' . $type));

      // Drupal core needs to be upgraded manually.
      $needs_manual = $project['project_type'] == 'core';

      if ($needs_manual) {
        // There are no checkboxes in the 'Manual updates' table so it will be
195 196 197
        // rendered by '#theme' => 'table', not '#theme' => 'tableselect'. Since
        // the data formats are incompatible, we convert now to the format
        // expected by '#theme' => 'table'.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
        unset($entry['#weight']);
        $attributes = $entry['#attributes'];
        unset($entry['#attributes']);
        $entry = array(
          'data' => $entry,
        ) + $attributes;
      }
      else {
        $form['project_downloads'][$name] = array(
          '#type' => 'value',
          '#value' => $recommended_release['download_link'],
        );
      }

      // Based on what kind of project this is, save the entry into the
      // appropriate subarray.
      switch ($project['project_type']) {
        case 'core':
          // Core needs manual updates at this time.
          $projects['manual'][$name] = $entry;
          break;

        case 'module':
        case 'theme':
          $projects['enabled'][$name] = $entry;
          break;

        case 'module-disabled':
        case 'theme-disabled':
          $projects['disabled'][$name] = $entry;
          break;
      }
    }

    if (empty($projects)) {
      $form['message'] = array(
        '#markup' => $this->t('All of your projects are up to date.'),
      );
      return $form;
    }

    $headers = array(
      'title' => array(
        'data' => $this->t('Name'),
        'class' => array('update-project-name'),
      ),
      'installed_version' => $this->t('Installed version'),
      'recommended_version' => $this->t('Recommended version'),
    );

    if (!empty($projects['enabled'])) {
      $form['projects'] = array(
        '#type' => 'tableselect',
        '#header' => $headers,
        '#options' => $projects['enabled'],
      );
      if (!empty($projects['disabled'])) {
        $form['projects']['#prefix'] = '<h2>' . $this->t('Enabled') . '</h2>';
      }
    }

    if (!empty($projects['disabled'])) {
      $form['disabled_projects'] = array(
        '#type' => 'tableselect',
        '#header' => $headers,
        '#options' => $projects['disabled'],
        '#weight' => 1,
        '#prefix' => '<h2>' . $this->t('Disabled') . '</h2>',
      );
    }

    // If either table has been printed yet, we need a submit button and to
    // validate the checkboxes.
    if (!empty($projects['enabled']) || !empty($projects['disabled'])) {
      $form['actions'] = array('#type' => 'actions');
      $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#value' => $this->t('Download these updates'),
      );
    }

    if (!empty($projects['manual'])) {
      $prefix = '<h2>' . $this->t('Manual updates required') . '</h2>';
      $prefix .= '<p>' . $this->t('Updates of Drupal core are not supported at this time.') . '</p>';
      $form['manual_updates'] = array(
        '#type' => 'table',
        '#header' => $headers,
        '#rows' => $projects['manual'],
        '#prefix' => $prefix,
        '#weight' => 120,
      );
    }

    return $form;
  }

  /**
   * {@inheritdoc}
   */
297
  public function validateForm(array &$form, FormStateInterface $form_state) {
298 299
    if (!$form_state->isValueEmpty('projects')) {
      $enabled = array_filter($form_state->getValue('projects'));
300
    }
301 302
    if (!$form_state->isValueEmpty('disabled_projects')) {
      $disabled = array_filter($form_state->getValue('disabled_projects'));
303 304
    }
    if (empty($enabled) && empty($disabled)) {
305
      $form_state->setErrorByName('projects', $this->t('You must select at least one project to update.'));
306 307 308 309 310 311
    }
  }

  /**
   * {@inheritdoc}
   */
312
  public function submitForm(array &$form, FormStateInterface $form_state) {
313 314 315
    $this->moduleHandler->loadInclude('update', 'inc', 'update.manager');
    $projects = array();
    foreach (array('projects', 'disabled_projects') as $type) {
316 317
      if (!$form_state->isValueEmpty($type)) {
        $projects = array_merge($projects, array_keys(array_filter($form_state->getValue($type))));
318 319 320 321 322 323 324 325
      }
    }
    $operations = array();
    foreach ($projects as $project) {
      $operations[] = array(
        'update_manager_batch_project_get',
        array(
          $project,
326
          $form_state->getValue(array('project_downloads', $project)),
327 328 329 330 331 332 333 334 335 336 337 338 339 340
        ),
      );
    }
    $batch = array(
      'title' => $this->t('Downloading updates'),
      'init_message' => $this->t('Preparing to download selected updates'),
      'operations' => $operations,
      'finished' => 'update_manager_download_batch_finished',
      'file' => drupal_get_path('module', 'update') . '/update.manager.inc',
    );
    batch_set($batch);
  }

}