update.report.inc 9.23 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Code required only when rendering the available updates report.
 */

8
use Drupal\Core\Template\Attribute;
9
use Drupal\Core\Url;
10

11
/**
12 13 14
 * Prepares variables for project status report templates.
 *
 * Default template: update-report.html.twig.
15
 *
16
 * @param array $variables
17 18
 *   An associative array containing:
 *   - data: An array of data about each project's status.
19
 */
20
function template_preprocess_update_report(&$variables) {
21 22
  $data = $variables['data'];

23
  $last = \Drupal::state()->get('update.last_check') ?: 0;
24

25
  $variables['last_checked'] = [
26 27
    '#theme' => 'update_last_check',
    '#last' => $last,
28
    // Attach the library to a variable that gets printed always.
29 30
    '#attached' => [
      'library' => [
31
        'update/drupal.update.admin',
32
      ],
33
    ],
34
  ];
35 36 37 38

  // For no project update data, populate no data message.
  if (empty($data)) {
    $variables['no_updates_message'] = _update_no_data();
39 40
  }

41
  $rows = [];
42

43
  foreach ($data as $project) {
44
    $project_status = [
45
      '#theme' => 'update_project_status',
46
      '#project' => $project,
47
    ];
48 49 50

    // Build project rows.
    if (!isset($rows[$project['project_type']])) {
51
      $rows[$project['project_type']] = [
52
        '#type' => 'table',
53 54
        '#attributes' => ['class' => ['update']],
      ];
55
    }
56
    $row_key = !empty($project['title']) ? mb_strtolower($project['title']) : mb_strtolower($project['name']);
57 58 59 60 61

    // Add the project status row and details.
    $rows[$project['project_type']][$row_key]['status'] = $project_status;

    // Add project status class attribute to the table row.
62 63
    switch ($project['status']) {
      case UPDATE_CURRENT:
64
        $rows[$project['project_type']][$row_key]['#attributes'] = ['class' => ['color-success']];
65
        break;
66
      case UPDATE_UNKNOWN:
67
      case UPDATE_FETCH_PENDING:
68
      case UPDATE_NOT_FETCHED:
69
      case UPDATE_NOT_SECURE:
70 71
      case UPDATE_REVOKED:
      case UPDATE_NOT_SUPPORTED:
72
        $rows[$project['project_type']][$row_key]['#attributes'] = ['class' => ['color-error']];
73 74
        break;
      case UPDATE_NOT_CHECKED:
75 76
      case UPDATE_NOT_CURRENT:
      default:
77
        $rows[$project['project_type']][$row_key]['#attributes'] = ['class' => ['color-warning']];
78 79
        break;
    }
80
  }
81

82
  $project_types = [
83 84 85
    'core' => t('Drupal core'),
    'module' => t('Modules'),
    'theme' => t('Themes'),
86 87
    'module-disabled' => t('Uninstalled modules'),
    'theme-disabled' => t('Uninstalled themes'),
88
  ];
89

90
  $variables['project_types'] = [];
91 92 93
  foreach ($project_types as $type_name => $type_label) {
    if (!empty($rows[$type_name])) {
      ksort($rows[$type_name]);
94
      $variables['project_types'][] = [
95 96
        'label' => $type_label,
        'table' => $rows[$type_name],
97
      ];
98
    }
99 100
  }
}
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
/**
 * Prepares variables for update project status templates.
 *
 * Default template: update-project-status.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - project: An array of information about the project.
 */
function template_preprocess_update_project_status(&$variables) {
  // Storing by reference because we are sorting the project values.
  $project = &$variables['project'];

  // Set the project title and URL.
  $variables['title'] = (isset($project['title'])) ? $project['title'] : $project['name'];
117
  $variables['url'] = (isset($project['link'])) ? Url::fromUri($project['link'])->toString() : NULL;
118 119 120

  $variables['install_type'] = $project['install_type'];
  if ($project['install_type'] == 'dev' && !empty($project['datestamp'])) {
121
    $variables['datestamp'] = \Drupal::service('date.formatter')->format($project['datestamp'], 'custom', 'Y-M-d');
122
  }
123

124 125
  $variables['existing_version'] = $project['existing_version'];

126 127 128
  $versions_inner = [];
  $security_class = [];
  $version_class = [];
129 130 131 132 133 134 135 136 137 138 139
  if (isset($project['recommended'])) {
    if ($project['status'] != UPDATE_CURRENT || $project['existing_version'] !== $project['recommended']) {

      // First, figure out what to recommend.
      // If there's only 1 security update and it has the same version we're
      // recommending, give it the same CSS class as if it was recommended,
      // but don't print out a separate "Recommended" line for this project.
      if (!empty($project['security updates'])
        && count($project['security updates']) == 1
        && $project['security updates'][0]['version'] === $project['recommended']
      ) {
140 141
        $security_class[] = 'project-update__version--recommended';
        $security_class[] = 'project-update__version---strong';
142
      }
143
      else {
144
        $version_class[] = 'project-update__version--recommended';
145 146 147 148 149 150 151 152 153 154 155
        // Apply an extra class if we're displaying both a recommended
        // version and anything else for an extra visual hint.
        if ($project['recommended'] !== $project['latest_version']
          || !empty($project['also'])
          || ($project['install_type'] == 'dev'
            && isset($project['dev_version'])
            && $project['latest_version'] !== $project['dev_version']
            && $project['recommended'] !== $project['dev_version'])
          || (isset($project['security updates'][0])
            && $project['recommended'] !== $project['security updates'][0])
        ) {
156
          $version_class[] = 'project-update__version--recommended-strong';
157
        }
158
        $versions_inner[] = [
159
          '#theme' => 'update_version',
160 161
          '#version' => $project['releases'][$project['recommended']],
          '#title' => t('Recommended version:'),
162 163
          '#attributes' => ['class' => $version_class],
        ];
164 165
      }

166 167 168 169
      // Now, print any security updates.
      if (!empty($project['security updates'])) {
        $security_class[] = 'version-security';
        foreach ($project['security updates'] as $security_update) {
170
          $versions_inner[] = [
171 172 173
            '#theme' => 'update_version',
            '#version' => $security_update,
            '#title' => t('Security update:'),
174 175
            '#attributes' => ['class' => $security_class],
          ];
176
        }
177 178 179
      }
    }

180
    if ($project['recommended'] !== $project['latest_version']) {
181
      $versions_inner[] = [
182 183 184
        '#theme' => 'update_version',
        '#version' => $project['releases'][$project['latest_version']],
        '#title' => t('Latest version:'),
185 186
        '#attributes' => ['class' => ['version-latest']],
      ];
187
    }
188 189 190 191
    if ($project['install_type'] == 'dev'
      && $project['status'] != UPDATE_CURRENT
      && isset($project['dev_version'])
      && $project['recommended'] !== $project['dev_version']) {
192
      $versions_inner[] = [
193 194 195
        '#theme' => 'update_version',
        '#version' => $project['releases'][$project['dev_version']],
        '#title' => t('Development version:'),
196 197
        '#attributes' => ['class' => ['version-latest']],
      ];
198
    }
199
  }
200

201 202
  if (isset($project['also'])) {
    foreach ($project['also'] as $also) {
203
      $versions_inner[] = [
204 205 206
        '#theme' => 'update_version',
        '#version' => $project['releases'][$also],
        '#title' => t('Also available:'),
207 208
        '#attributes' => ['class' => ['version-also-available']],
      ];
209
    }
210
  }
211

212 213 214
  if (!empty($versions_inner)) {
    $variables['versions'] = $versions_inner;
  }
215

216 217 218 219
  if (!empty($project['disabled'])) {
    sort($project['disabled']);
    $variables['disabled'] = $project['disabled'];
  }
220

221 222 223
  sort($project['includes']);
  $variables['includes'] = $project['includes'];

224
  $variables['extras'] = [];
225 226
  if (!empty($project['extra'])) {
    foreach ($project['extra'] as $value) {
227
      $extra_item = [];
228
      $extra_item['attributes'] = new Attribute();
229
      $extra_item['label'] = $value['label'];
230 231 232
      $extra_item['data'] = [
        '#prefix' => '<em>',
        '#markup' => $value['data'],
233
        '#suffix' => '</em>',
234
      ];
235
      $variables['extras'][] = $extra_item;
236 237 238
    }
  }

239 240 241
  // Set the project status details.
  $status_label = NULL;
  switch ($project['status']) {
242
    case UPDATE_NOT_SECURE:
243 244
      $status_label = t('Security update required!');
      break;
245
    case UPDATE_REVOKED:
246 247
      $status_label = t('Revoked!');
      break;
248
    case UPDATE_NOT_SUPPORTED:
249 250
      $status_label = t('Not supported!');
      break;
251
    case UPDATE_NOT_CURRENT:
252 253
      $status_label = t('Update available');
      break;
254
    case UPDATE_CURRENT:
255 256 257 258
      $status_label = t('Up to date');
      break;
  }
  $variables['status']['label'] = $status_label;
259
  $variables['status']['attributes'] = new Attribute();
260
  $variables['status']['reason'] = (isset($project['reason'])) ? $project['reason'] : NULL;
261

262 263
  switch ($project['status']) {
    case UPDATE_CURRENT:
264
      $uri = 'core/misc/icons/73b355/check.svg';
265 266 267 268 269
      $text = t('Ok');
      break;
    case UPDATE_UNKNOWN:
    case UPDATE_FETCH_PENDING:
    case UPDATE_NOT_FETCHED:
270
      $uri = 'core/misc/icons/e29700/warning.svg';
271 272 273 274 275
      $text = t('Warning');
      break;
    case UPDATE_NOT_SECURE:
    case UPDATE_REVOKED:
    case UPDATE_NOT_SUPPORTED:
276
      $uri = 'core/misc/icons/e32700/error.svg';
277 278 279 280 281
      $text = t('Error');
      break;
    case UPDATE_NOT_CHECKED:
    case UPDATE_NOT_CURRENT:
    default:
282
      $uri = 'core/misc/icons/e29700/warning.svg';
283 284
      $text = t('Warning');
      break;
285
  }
286

287
  $variables['status']['icon'] = [
288 289 290 291 292 293
    '#theme' => 'image',
    '#width' => 18,
    '#height' => 18,
    '#uri' => $uri,
    '#alt' => $text,
    '#title' => $text,
294
  ];
295
}