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

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

/**
9 10
 * Returns HTML for the project status report.
 *
11
 * @param array $variables
12 13
 *   An associative array containing:
 *   - data: An array of data about each project's status.
14 15
 *
 * @ingroup themeable
16
 */
17 18 19
function theme_update_report($variables) {
  $data = $variables['data'];

20
  $last = \Drupal::state()->get('update.last_check') ?: 0;
21 22 23 24 25
  $update_last_check = array(
    '#theme' => 'update_last_check',
    '#last' => $last,
  );
  $output = drupal_render($update_last_check);
26 27

  if (!is_array($data)) {
28
    $output .= '<p>' . $data . '</p>';
29 30 31 32 33 34
    return $output;
  }

  $header = array();
  $rows = array();

35 36 37 38 39 40 41 42 43
  // Create an array of status values keyed by module or theme name, since
  // we'll need this while generating the report if we have to cross reference
  // anything (e.g. subthemes which have base themes missing an update).
  foreach ($data as $project) {
    foreach ($project['includes'] as $key => $name) {
      $status[$key] = $project['status'];
    }
  }

44 45 46 47
  foreach ($data as $project) {
    switch ($project['status']) {
      case UPDATE_CURRENT:
        $class = 'ok';
48 49
        $uri = 'core/misc/watchdog-ok.png';
        $text = t('ok');
50
        break;
51
      case UPDATE_UNKNOWN:
52
      case UPDATE_FETCH_PENDING:
53
      case UPDATE_NOT_FETCHED:
54
        $class = 'unknown';
55 56
        $uri = 'core/misc/watchdog-warning.png';
        $text = t('warning');
57
        break;
58
      case UPDATE_NOT_SECURE:
59 60
      case UPDATE_REVOKED:
      case UPDATE_NOT_SUPPORTED:
61
        $class = 'error';
62 63
        $uri = 'core/misc/watchdog-error.png';
        $text = t('error');
64 65
        break;
      case UPDATE_NOT_CHECKED:
66 67 68
      case UPDATE_NOT_CURRENT:
      default:
        $class = 'warning';
69 70
        $uri = 'core/misc/watchdog-warning.png';
        $text = t('warning');
71 72 73
        break;
    }

74 75 76 77 78 79 80 81 82
    $icon = array(
      '#theme' => 'image',
      '#width' => 18,
      '#height' => 18,
      '#uri' => $uri,
      '#alt' => $text,
      '#title' => $text,
    );

83
    $row = '<div class="version-status">';
84 85
    $update_status_label = array('#theme' => 'update_status_label', '#status' => $project['status']);
    $status_label = drupal_render($update_status_label);
86
    $row .= !empty($status_label) ? $status_label : check_plain($project['reason']);
87
    $row .= '<span class="icon">' . drupal_render($icon) . '</span>';
88 89 90 91 92 93 94 95 96 97 98 99 100 101
    $row .= "</div>\n";

    $row .= '<div class="project">';
    if (isset($project['title'])) {
      if (isset($project['link'])) {
        $row .= l($project['title'], $project['link']);
      }
      else {
        $row .= check_plain($project['title']);
      }
    }
    else {
      $row .= check_plain($project['name']);
    }
102
    $row .= ' ' . check_plain($project['existing_version']);
103
    if ($project['install_type'] == 'dev' && !empty($project['datestamp'])) {
104
      $row .= ' <span class="version-date">(' . format_date($project['datestamp'], 'custom', 'Y-M-d') . ')</span>';
105 106 107
    }
    $row .= "</div>\n";

108 109 110
    $versions_inner = '';
    $security_class = array();
    $version_class = array();
111
    if (isset($project['recommended'])) {
112
      if ($project['status'] != UPDATE_CURRENT || $project['existing_version'] !== $project['recommended']) {
113 114 115 116 117

        // 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.
118
        if (!empty($project['security updates']) && count($project['security updates']) == 1 && $project['security updates'][0]['version'] === $project['recommended']) {
119 120
          $security_class[] = 'version-recommended';
          $security_class[] = 'version-recommended-strong';
121 122
        }
        else {
123
          $version_class[] = 'version-recommended';
124 125
          // Apply an extra class if we're displaying both a recommended
          // version and anything else for an extra visual hint.
126
          if ($project['recommended'] !== $project['latest_version']
127 128
              || !empty($project['also'])
              || ($project['install_type'] == 'dev'
129 130 131
                && isset($project['dev_version'])
                && $project['latest_version'] !== $project['dev_version']
                && $project['recommended'] !== $project['dev_version'])
132
              || (isset($project['security updates'][0])
133
                && $project['recommended'] !== $project['security updates'][0])
134
              ) {
135
            $version_class[] = 'version-recommended-strong';
136
          }
137 138 139 140 141 142 143
          $update_version = array(
            '#theme' => 'update_version',
            '#version' => $project['releases'][$project['recommended']],
            '#tag' => t('Recommended version:'),
            '#class' => $version_class,
          );
          $versions_inner .= drupal_render($update_version);
144 145 146 147
        }

        // Now, print any security updates.
        if (!empty($project['security updates'])) {
148
          $security_class[] = 'version-security';
149
          foreach ($project['security updates'] as $security_update) {
150 151 152 153 154 155 156
            $update_version = array(
              '#theme' => 'update_version',
              '#version' => $security_update,
              '#tag' => t('Security update:'),
              '#class' => $security_class,
            );
            $versions_inner .= drupal_render($update_version);
157 158 159 160
          }
        }
      }

161
      if ($project['recommended'] !== $project['latest_version']) {
162 163 164 165 166 167 168
        $update_version = array(
          '#theme' => 'update_version',
          '#version' => $project['releases'][$project['latest_version']],
          '#tag' => t('Latest version:'),
          '#class' => array('version-latest'),
        );
        $versions_inner .= drupal_render($update_version);
169 170 171
      }
      if ($project['install_type'] == 'dev'
          && $project['status'] != UPDATE_CURRENT
172
          && isset($project['dev_version'])
173
          && $project['recommended'] !== $project['dev_version']) {
174 175 176 177 178 179 180
        $update_version = array(
          '#theme' => 'update_version',
          '#version' => $project['releases'][$project['dev_version']],
          '#tag' => t('Development version:'),
          '#class' => array('version-latest'),
        );
        $versions_inner .= drupal_render($update_version);
181 182 183 184 185
      }
    }

    if (isset($project['also'])) {
      foreach ($project['also'] as $also) {
186 187 188 189 190 191 192
        $update_version = array(
          '#theme' => 'update_version',
          '#version' => $project['releases'][$also],
          '#tag' => t('Also available:'),
          '#class' => array('version-also-available'),
        );
        $versions_inner .= drupal_render($update_version);
193 194 195
      }
    }

196 197 198
    if (!empty($versions_inner)) {
      $row .= "<div class=\"versions\">\n" . $versions_inner . "</div>\n";
    }
199 200
    $row .= "<div class=\"info\">\n";
    if (!empty($project['extra'])) {
201
      $row .= '<div class="extra">' . "\n";
202
      foreach ($project['extra'] as $value) {
203
        $row .= '<div class="' . implode(' ', $value['class']) . '">';
204
        $row .= check_plain($value['label']) . ': ';
205
        $row .= drupal_placeholder($value['data']);
206 207 208 209 210 211 212
        $row .= "</div>\n";
      }
      $row .= "</div>\n";  // extra div.
    }

    $row .= '<div class="includes">';
    sort($project['includes']);
213 214 215 216 217 218 219
    if (!empty($project['disabled'])) {
      sort($project['disabled']);
      // Make sure we start with a clean slate for each project in the report.
      $includes_items = array();
      $row .= t('Includes:');
      $includes_items[] = t('Enabled: %includes', array('%includes' => implode(', ', $project['includes'])));
      $includes_items[] = t('Disabled: %disabled', array('%disabled' => implode(', ', $project['disabled'])));
220 221 222 223 224
      $item_list = array(
        '#theme' => 'item_list',
        '#items' => $includes_items,
      );
      $row .= drupal_render($item_list);
225 226 227 228
    }
    else {
      $row .= t('Includes: %includes', array('%includes' => implode(', ', $project['includes'])));
    }
229 230
    $row .= "</div>\n";

231 232 233 234 235
    if (!empty($project['base_themes'])) {
      $row .= '<div class="basethemes">';
      asort($project['base_themes']);
      $base_themes = array();
      foreach ($project['base_themes'] as $base_key => $base_theme) {
236 237 238 239
        $update_status_label = array(
          '#theme' => 'update_status_label',
          '#status' => $status[$base_key],
        );
240 241 242 243
        switch ($status[$base_key]) {
          case UPDATE_NOT_SECURE:
          case UPDATE_REVOKED:
          case UPDATE_NOT_SUPPORTED:
244
            $base_themes[] = t('%base_theme (!base_label)', array('%base_theme' => $base_theme, '!base_label' => drupal_render($update_status_label)));
245
            break;
246

247
          default:
248
            $base_themes[] = drupal_placeholder($base_theme);
249 250 251 252 253 254 255 256 257 258 259 260 261
        }
      }
      $row .= t('Depends on: !basethemes', array('!basethemes' => implode(', ', $base_themes)));
      $row .= "</div>\n";
    }

    if (!empty($project['sub_themes'])) {
      $row .= '<div class="subthemes">';
      sort($project['sub_themes']);
      $row .= t('Required by: %subthemes', array('%subthemes' => implode(', ', $project['sub_themes'])));
      $row .= "</div>\n";
    }

262 263 264 265 266
    $row .= "</div>\n"; // info div.

    if (!isset($rows[$project['project_type']])) {
      $rows[$project['project_type']] = array();
    }
267 268
    $row_key = isset($project['title']) ? drupal_strtolower($project['title']) : drupal_strtolower($project['name']);
    $rows[$project['project_type']][$row_key] = array(
269
      'class' => array($class),
270 271 272 273 274 275 276 277
      'data' => array($row),
    );
  }

  $project_types = array(
    'core' => t('Drupal core'),
    'module' => t('Modules'),
    'theme' => t('Themes'),
278 279
    'module-disabled' => t('Disabled modules'),
    'theme-disabled' => t('Disabled themes'),
280 281 282
  );
  foreach ($project_types as $type_name => $type_label) {
    if (!empty($rows[$type_name])) {
283
      ksort($rows[$type_name]);
284
      $output .= "\n<h3>" . $type_label . "</h3>\n";
285 286 287 288 289 290 291 292 293
      $table = array(
        '#theme' => 'table',
        '#header' => $header,
        '#rows' => $rows[$type_name],
        '#attributes' => array(
          'class' => array('update'),
        ),
      );
      $output .= drupal_render($table);
294 295
    }
  }
296 297 298 299

  $assets = array(
    '#attached' => array(
      'library' => array(
300
        'update/drupal.update.admin',
301 302 303 304 305
      ),
    )
  );
  drupal_render($assets);

306 307 308
  return $output;
}

309
/**
310
 * Returns HTML for a label to display for a project's update status.
311
 *
312
 * @param array $variables
313 314
 *   An associative array containing:
 *   - status: The integer code for a project's current update status.
315 316
 *
 * @see update_calculate_project_data()
317
 * @ingroup themeable
318
 */
319 320
function theme_update_status_label($variables) {
  switch ($variables['status']) {
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    case UPDATE_NOT_SECURE:
      return '<span class="security-error">' . t('Security update required!') . '</span>';

    case UPDATE_REVOKED:
      return '<span class="revoked">' . t('Revoked!') . '</span>';

    case UPDATE_NOT_SUPPORTED:
      return '<span class="not-supported">' . t('Not supported!') . '</span>';

    case UPDATE_NOT_CURRENT:
      return '<span class="not-current">' . t('Update available') . '</span>';

    case UPDATE_CURRENT:
      return '<span class="current">' . t('Up to date') . '</span>';

  }
}

339
/**
340 341
 * Returns HTML for the version display of a project.
 *
342
 * @param array $variables
343 344 345 346 347 348 349 350
 *   An associative array containing:
 *   - version: An array of data about the latest released version, containing:
 *     - version: The version number.
 *     - release_link: The URL for the release notes.
 *     - date: The date of the release.
 *     - download_link: The URL for the downloadable file.
 *   - tag: The title of the project.
 *   - class: A string containing extra classes for the wrapping table.
351 352 353
 *
 * @ingroup themeable
 */
354 355 356
function theme_update_version($variables) {
  $version = $variables['version'];
  $tag = $variables['tag'];
357
  $class = implode(' ', $variables['class']);
358

359
  $output = '';
360
  $output .= '<table class="version ' . $class . '">';
361
  $output .= '<tr>';
362
  $output .= '<td class="version-title">' . $tag . "</td>\n";
363 364
  $output .= '<td class="version-details">';
  $output .= l($version['version'], $version['release_link']);
365
  $output .= ' <span class="version-date">(' . format_date($version['date'], 'custom', 'Y-M-d') . ')</span>';
366 367 368 369 370 371 372 373 374 375 376
  $output .= "</td>\n";
  $output .= '<td class="version-links">';
  $links = array();
  $links['update-download'] = array(
    'title' => t('Download'),
    'href' => $version['download_link'],
  );
  $links['update-release-notes'] = array(
    'title' => t('Release notes'),
    'href' => $version['release_link'],
  );
377 378 379 380 381
  $links__update_version = array(
    '#theme' => 'links__update_version',
    '#links' => $links,
  );
  $output .= drupal_render($links__update_version);
382 383 384 385 386
  $output .= '</td>';
  $output .= '</tr>';
  $output .= "</table>\n";
  return $output;
}