Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / update / update.report.inc
1 <?php
2
3 /**
4  * @file
5  * Code required only when rendering the available updates report.
6  */
7
8 use Drupal\Core\Template\Attribute;
9 use Drupal\Core\Url;
10
11 /**
12  * Prepares variables for project status report templates.
13  *
14  * Default template: update-report.html.twig.
15  *
16  * @param array $variables
17  *   An associative array containing:
18  *   - data: An array of data about each project's status.
19  */
20 function template_preprocess_update_report(&$variables) {
21   $data = $variables['data'];
22
23   $last = \Drupal::state()->get('update.last_check') ?: 0;
24
25   $variables['last_checked'] = [
26     '#theme' => 'update_last_check',
27     '#last' => $last,
28     // Attach the library to a variable that gets printed always.
29     '#attached' => [
30       'library' => [
31         'update/drupal.update.admin',
32       ],
33     ],
34   ];
35
36   // For no project update data, populate no data message.
37   if (empty($data)) {
38     $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     // Build project rows.
50     if (!isset($rows[$project['project_type']])) {
51       $rows[$project['project_type']] = [
52         '#type' => 'table',
53         '#attributes' => ['class' => ['update']],
54       ];
55     }
56     $row_key = !empty($project['title']) ? mb_strtolower($project['title']) : mb_strtolower($project['name']);
57
58     // Add the project status row and details.
59     $rows[$project['project_type']][$row_key]['status'] = $project_status;
60
61     // Add project status class attribute to the table row.
62     switch ($project['status']) {
63       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       case UPDATE_REVOKED:
71       case UPDATE_NOT_SUPPORTED:
72         $rows[$project['project_type']][$row_key]['#attributes'] = ['class' => ['color-error']];
73         break;
74       case UPDATE_NOT_CHECKED:
75       case UPDATE_NOT_CURRENT:
76       default:
77         $rows[$project['project_type']][$row_key]['#attributes'] = ['class' => ['color-warning']];
78         break;
79     }
80   }
81
82   $project_types = [
83     'core' => t('Drupal core'),
84     'module' => t('Modules'),
85     'theme' => t('Themes'),
86     'module-disabled' => t('Uninstalled modules'),
87     'theme-disabled' => t('Uninstalled themes'),
88   ];
89
90   $variables['project_types'] = [];
91   foreach ($project_types as $type_name => $type_label) {
92     if (!empty($rows[$type_name])) {
93       ksort($rows[$type_name]);
94       $variables['project_types'][] = [
95         'label' => $type_label,
96         'table' => $rows[$type_name],
97       ];
98     }
99   }
100 }
101
102 /**
103  * Prepares variables for update project status templates.
104  *
105  * Default template: update-project-status.html.twig.
106  *
107  * @param array $variables
108  *   An associative array containing:
109  *   - project: An array of information about the project.
110  */
111 function template_preprocess_update_project_status(&$variables) {
112   // Storing by reference because we are sorting the project values.
113   $project = &$variables['project'];
114
115   // Set the project title and URL.
116   $variables['title'] = (isset($project['title'])) ? $project['title'] : $project['name'];
117   $variables['url'] = (isset($project['link'])) ? Url::fromUri($project['link'])->toString() : NULL;
118
119   $variables['install_type'] = $project['install_type'];
120   if ($project['install_type'] == 'dev' && !empty($project['datestamp'])) {
121     $variables['datestamp'] = format_date($project['datestamp'], 'custom', 'Y-M-d');
122   }
123
124   $variables['existing_version'] = $project['existing_version'];
125
126   $versions_inner = [];
127   $security_class = [];
128   $version_class = [];
129   if (isset($project['recommended'])) {
130     if ($project['status'] != UPDATE_CURRENT || $project['existing_version'] !== $project['recommended']) {
131
132       // First, figure out what to recommend.
133       // If there's only 1 security update and it has the same version we're
134       // recommending, give it the same CSS class as if it was recommended,
135       // but don't print out a separate "Recommended" line for this project.
136       if (!empty($project['security updates'])
137         && count($project['security updates']) == 1
138         && $project['security updates'][0]['version'] === $project['recommended']
139       ) {
140         $security_class[] = 'project-update__version--recommended';
141         $security_class[] = 'project-update__version---strong';
142       }
143       else {
144         $version_class[] = 'project-update__version--recommended';
145         // Apply an extra class if we're displaying both a recommended
146         // version and anything else for an extra visual hint.
147         if ($project['recommended'] !== $project['latest_version']
148           || !empty($project['also'])
149           || ($project['install_type'] == 'dev'
150             && isset($project['dev_version'])
151             && $project['latest_version'] !== $project['dev_version']
152             && $project['recommended'] !== $project['dev_version'])
153           || (isset($project['security updates'][0])
154             && $project['recommended'] !== $project['security updates'][0])
155         ) {
156           $version_class[] = 'project-update__version--recommended-strong';
157         }
158         $versions_inner[] = [
159           '#theme' => 'update_version',
160           '#version' => $project['releases'][$project['recommended']],
161           '#title' => t('Recommended version:'),
162           '#attributes' => ['class' => $version_class],
163         ];
164       }
165
166       // Now, print any security updates.
167       if (!empty($project['security updates'])) {
168         $security_class[] = 'version-security';
169         foreach ($project['security updates'] as $security_update) {
170           $versions_inner[] = [
171             '#theme' => 'update_version',
172             '#version' => $security_update,
173             '#title' => t('Security update:'),
174             '#attributes' => ['class' => $security_class],
175           ];
176         }
177       }
178     }
179
180     if ($project['recommended'] !== $project['latest_version']) {
181       $versions_inner[] = [
182         '#theme' => 'update_version',
183         '#version' => $project['releases'][$project['latest_version']],
184         '#title' => t('Latest version:'),
185         '#attributes' => ['class' => ['version-latest']],
186       ];
187     }
188     if ($project['install_type'] == 'dev'
189       && $project['status'] != UPDATE_CURRENT
190       && isset($project['dev_version'])
191       && $project['recommended'] !== $project['dev_version']) {
192       $versions_inner[] = [
193         '#theme' => 'update_version',
194         '#version' => $project['releases'][$project['dev_version']],
195         '#title' => t('Development version:'),
196         '#attributes' => ['class' => ['version-latest']],
197       ];
198     }
199   }
200
201   if (isset($project['also'])) {
202     foreach ($project['also'] as $also) {
203       $versions_inner[] = [
204         '#theme' => 'update_version',
205         '#version' => $project['releases'][$also],
206         '#title' => t('Also available:'),
207         '#attributes' => ['class' => ['version-also-available']],
208       ];
209     }
210   }
211
212   if (!empty($versions_inner)) {
213     $variables['versions'] = $versions_inner;
214   }
215
216   if (!empty($project['disabled'])) {
217     sort($project['disabled']);
218     $variables['disabled'] = $project['disabled'];
219   }
220
221   sort($project['includes']);
222   $variables['includes'] = $project['includes'];
223
224   $variables['extras'] = [];
225   if (!empty($project['extra'])) {
226     foreach ($project['extra'] as $value) {
227       $extra_item = [];
228       $extra_item['attributes'] = new Attribute();
229       $extra_item['label'] = $value['label'];
230       $extra_item['data'] = [
231         '#prefix' => '<em>',
232         '#markup' => $value['data'],
233         '#suffix' => '</em>',
234       ];
235       $variables['extras'][] = $extra_item;
236     }
237   }
238
239   // Set the project status details.
240   $status_label = NULL;
241   switch ($project['status']) {
242     case UPDATE_NOT_SECURE:
243       $status_label = t('Security update required!');
244       break;
245     case UPDATE_REVOKED:
246       $status_label = t('Revoked!');
247       break;
248     case UPDATE_NOT_SUPPORTED:
249       $status_label = t('Not supported!');
250       break;
251     case UPDATE_NOT_CURRENT:
252       $status_label = t('Update available');
253       break;
254     case UPDATE_CURRENT:
255       $status_label = t('Up to date');
256       break;
257   }
258   $variables['status']['label'] = $status_label;
259   $variables['status']['attributes'] = new Attribute();
260   $variables['status']['reason'] = (isset($project['reason'])) ? $project['reason'] : NULL;
261
262   switch ($project['status']) {
263     case UPDATE_CURRENT:
264       $uri = 'core/misc/icons/73b355/check.svg';
265       $text = t('Ok');
266       break;
267     case UPDATE_UNKNOWN:
268     case UPDATE_FETCH_PENDING:
269     case UPDATE_NOT_FETCHED:
270       $uri = 'core/misc/icons/e29700/warning.svg';
271       $text = t('Warning');
272       break;
273     case UPDATE_NOT_SECURE:
274     case UPDATE_REVOKED:
275     case UPDATE_NOT_SUPPORTED:
276       $uri = 'core/misc/icons/e32700/error.svg';
277       $text = t('Error');
278       break;
279     case UPDATE_NOT_CHECKED:
280     case UPDATE_NOT_CURRENT:
281     default:
282       $uri = 'core/misc/icons/e29700/warning.svg';
283       $text = t('Warning');
284       break;
285   }
286
287   $variables['status']['icon'] = [
288     '#theme' => 'image',
289     '#width' => 18,
290     '#height' => 18,
291     '#uri' => $uri,
292     '#alt' => $text,
293     '#title' => $text,
294   ];
295 }