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
  $notification_level = Drupal::config('update.settings')->get('notification.threshold');
36

37 38 39 40 41 42 43 44 45
  // 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'];
    }
  }

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

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

85
    $row = '<div class="version-status">';
86 87
    $update_status_label = array('#theme' => 'update_status_label', '#status' => $project['status']);
    $status_label = drupal_render($update_status_label);
88
    $row .= !empty($status_label) ? $status_label : check_plain($project['reason']);
89
    $row .= '<span class="icon">' . drupal_render($icon) . '</span>';
90 91 92 93 94 95 96 97 98 99 100 101 102 103
    $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']);
    }
104
    $row .= ' ' . check_plain($project['existing_version']);
105
    if ($project['install_type'] == 'dev' && !empty($project['datestamp'])) {
106
      $row .= ' <span class="version-date">(' . format_date($project['datestamp'], 'custom', 'Y-M-d') . ')</span>';
107 108 109
    }
    $row .= "</div>\n";

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

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

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

163
      if ($project['recommended'] !== $project['latest_version']) {
164 165 166 167 168 169 170
        $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);
171 172 173
      }
      if ($project['install_type'] == 'dev'
          && $project['status'] != UPDATE_CURRENT
174
          && isset($project['dev_version'])
175
          && $project['recommended'] !== $project['dev_version']) {
176 177 178 179 180 181 182
        $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);
183 184 185 186 187
      }
    }

    if (isset($project['also'])) {
      foreach ($project['also'] as $also) {
188 189 190 191 192 193 194
        $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);
195 196 197
      }
    }

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

    $row .= '<div class="includes">';
    sort($project['includes']);
215 216 217 218 219 220 221
    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'])));
222 223 224 225 226
      $item_list = array(
        '#theme' => 'item_list',
        '#items' => $includes_items,
      );
      $row .= drupal_render($item_list);
227 228 229 230
    }
    else {
      $row .= t('Includes: %includes', array('%includes' => implode(', ', $project['includes'])));
    }
231 232
    $row .= "</div>\n";

233 234 235 236 237
    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) {
238 239 240 241
        $update_status_label = array(
          '#theme' => 'update_status_label',
          '#status' => $status[$base_key],
        );
242 243 244 245
        switch ($status[$base_key]) {
          case UPDATE_NOT_SECURE:
          case UPDATE_REVOKED:
          case UPDATE_NOT_SUPPORTED:
246
            $base_themes[] = t('%base_theme (!base_label)', array('%base_theme' => $base_theme, '!base_label' => drupal_render($update_status_label)));
247
            break;
248

249
          default:
250
            $base_themes[] = drupal_placeholder($base_theme);
251 252 253 254 255 256 257 258 259 260 261 262 263
        }
      }
      $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";
    }

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

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

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

302
/**
303
 * Returns HTML for a label to display for a project's update status.
304
 *
305
 * @param array $variables
306 307
 *   An associative array containing:
 *   - status: The integer code for a project's current update status.
308 309
 *
 * @see update_calculate_project_data()
310
 * @ingroup themeable
311
 */
312 313
function theme_update_status_label($variables) {
  switch ($variables['status']) {
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    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>';

  }
}

332
/**
333 334
 * Returns HTML for the version display of a project.
 *
335
 * @param array $variables
336 337 338 339 340 341 342 343
 *   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.
344 345 346
 *
 * @ingroup themeable
 */
347 348 349
function theme_update_version($variables) {
  $version = $variables['version'];
  $tag = $variables['tag'];
350
  $class = implode(' ', $variables['class']);
351

352
  $output = '';
353
  $output .= '<table class="version ' . $class . '">';
354
  $output .= '<tr>';
355
  $output .= '<td class="version-title">' . $tag . "</td>\n";
356 357
  $output .= '<td class="version-details">';
  $output .= l($version['version'], $version['release_link']);
358
  $output .= ' <span class="version-date">(' . format_date($version['date'], 'custom', 'Y-M-d') . ')</span>';
359 360 361 362 363 364 365 366 367 368 369
  $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'],
  );
370 371 372 373 374
  $links__update_version = array(
    '#theme' => 'links__update_version',
    '#links' => $links,
  );
  $output .= drupal_render($links__update_version);
375 376 377 378 379
  $output .= '</td>';
  $output .= '</tr>';
  $output .= "</table>\n";
  return $output;
}