2015-08-17 17:00:26 -07:00
< ? php
/**
* @ file
* Code required only when rendering the available updates report .
*/
use Drupal\Core\Template\Attribute ;
use Drupal\Core\Url ;
/**
* Prepares variables for project status report templates .
*
* Default template : update - report . html . twig .
*
* @ param array $variables
* An associative array containing :
* - data : An array of data about each project ' s status .
*/
function template_preprocess_update_report ( & $variables ) {
$data = $variables [ 'data' ];
$last = \Drupal :: state () -> get ( 'update.last_check' ) ? : 0 ;
2017-04-13 15:53:35 +01:00
$variables [ 'last_checked' ] = [
2015-08-17 17:00:26 -07:00
'#theme' => 'update_last_check' ,
'#last' => $last ,
// Attach the library to a variable that gets printed always.
2017-04-13 15:53:35 +01:00
'#attached' => [
'library' => [
2015-08-17 17:00:26 -07:00
'update/drupal.update.admin' ,
2017-04-13 15:53:35 +01:00
],
2018-11-23 12:29:20 +00:00
],
2017-04-13 15:53:35 +01:00
];
2015-08-17 17:00:26 -07:00
// For no project update data, populate no data message.
if ( empty ( $data )) {
$variables [ 'no_updates_message' ] = _update_no_data ();
}
2017-04-13 15:53:35 +01:00
$rows = [];
2015-08-17 17:00:26 -07:00
foreach ( $data as $project ) {
2017-04-13 15:53:35 +01:00
$project_status = [
2016-05-04 14:35:41 -07:00
'#theme' => 'update_project_status' ,
2015-08-17 17:00:26 -07:00
'#project' => $project ,
2017-04-13 15:53:35 +01:00
];
2015-08-17 17:00:26 -07:00
// Build project rows.
if ( ! isset ( $rows [ $project [ 'project_type' ]])) {
2017-04-13 15:53:35 +01:00
$rows [ $project [ 'project_type' ]] = [
2015-08-17 17:00:26 -07:00
'#type' => 'table' ,
2017-04-13 15:53:35 +01:00
'#attributes' => [ 'class' => [ 'update' ]],
];
2015-08-17 17:00:26 -07:00
}
2018-11-23 12:29:20 +00:00
$row_key = ! empty ( $project [ 'title' ]) ? mb_strtolower ( $project [ 'title' ]) : mb_strtolower ( $project [ 'name' ]);
2015-08-17 17:00:26 -07:00
// 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.
switch ( $project [ 'status' ]) {
case UPDATE_CURRENT :
2017-04-13 15:53:35 +01:00
$rows [ $project [ 'project_type' ]][ $row_key ][ '#attributes' ] = [ 'class' => [ 'color-success' ]];
2015-09-04 13:20:09 -07:00
break ;
2015-08-17 17:00:26 -07:00
case UPDATE_UNKNOWN :
case UPDATE_FETCH_PENDING :
case UPDATE_NOT_FETCHED :
case UPDATE_NOT_SECURE :
case UPDATE_REVOKED :
case UPDATE_NOT_SUPPORTED :
2017-04-13 15:53:35 +01:00
$rows [ $project [ 'project_type' ]][ $row_key ][ '#attributes' ] = [ 'class' => [ 'color-error' ]];
2015-08-17 17:00:26 -07:00
break ;
case UPDATE_NOT_CHECKED :
case UPDATE_NOT_CURRENT :
default :
2017-04-13 15:53:35 +01:00
$rows [ $project [ 'project_type' ]][ $row_key ][ '#attributes' ] = [ 'class' => [ 'color-warning' ]];
2015-08-17 17:00:26 -07:00
break ;
}
}
2017-04-13 15:53:35 +01:00
$project_types = [
2015-08-17 17:00:26 -07:00
'core' => t ( 'Drupal core' ),
'module' => t ( 'Modules' ),
'theme' => t ( 'Themes' ),
2015-10-08 11:40:12 -07:00
'module-disabled' => t ( 'Uninstalled modules' ),
'theme-disabled' => t ( 'Uninstalled themes' ),
2017-04-13 15:53:35 +01:00
];
2015-08-17 17:00:26 -07:00
2017-04-13 15:53:35 +01:00
$variables [ 'project_types' ] = [];
2015-08-17 17:00:26 -07:00
foreach ( $project_types as $type_name => $type_label ) {
if ( ! empty ( $rows [ $type_name ])) {
ksort ( $rows [ $type_name ]);
2017-04-13 15:53:35 +01:00
$variables [ 'project_types' ][] = [
2015-08-17 17:00:26 -07:00
'label' => $type_label ,
'table' => $rows [ $type_name ],
2017-04-13 15:53:35 +01:00
];
2015-08-17 17:00:26 -07:00
}
}
}
/**
* 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' ];
$variables [ 'url' ] = ( isset ( $project [ 'link' ])) ? Url :: fromUri ( $project [ 'link' ]) -> toString () : NULL ;
$variables [ 'install_type' ] = $project [ 'install_type' ];
if ( $project [ 'install_type' ] == 'dev' && ! empty ( $project [ 'datestamp' ])) {
$variables [ 'datestamp' ] = format_date ( $project [ 'datestamp' ], 'custom' , 'Y-M-d' );
}
$variables [ 'existing_version' ] = $project [ 'existing_version' ];
2017-04-13 15:53:35 +01:00
$versions_inner = [];
$security_class = [];
$version_class = [];
2015-08-17 17:00:26 -07:00
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' ]
) {
$security_class [] = 'project-update__version--recommended' ;
$security_class [] = 'project-update__version---strong' ;
}
else {
$version_class [] = 'project-update__version--recommended' ;
// 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 ])
) {
$version_class [] = 'project-update__version--recommended-strong' ;
}
2017-04-13 15:53:35 +01:00
$versions_inner [] = [
2015-08-17 17:00:26 -07:00
'#theme' => 'update_version' ,
'#version' => $project [ 'releases' ][ $project [ 'recommended' ]],
'#title' => t ( 'Recommended version:' ),
2017-04-13 15:53:35 +01:00
'#attributes' => [ 'class' => $version_class ],
];
2015-08-17 17:00:26 -07:00
}
// Now, print any security updates.
if ( ! empty ( $project [ 'security updates' ])) {
$security_class [] = 'version-security' ;
foreach ( $project [ 'security updates' ] as $security_update ) {
2017-04-13 15:53:35 +01:00
$versions_inner [] = [
2015-08-17 17:00:26 -07:00
'#theme' => 'update_version' ,
'#version' => $security_update ,
'#title' => t ( 'Security update:' ),
2017-04-13 15:53:35 +01:00
'#attributes' => [ 'class' => $security_class ],
];
2015-08-17 17:00:26 -07:00
}
}
}
if ( $project [ 'recommended' ] !== $project [ 'latest_version' ]) {
2017-04-13 15:53:35 +01:00
$versions_inner [] = [
2015-08-17 17:00:26 -07:00
'#theme' => 'update_version' ,
'#version' => $project [ 'releases' ][ $project [ 'latest_version' ]],
'#title' => t ( 'Latest version:' ),
2017-04-13 15:53:35 +01:00
'#attributes' => [ 'class' => [ 'version-latest' ]],
];
2015-08-17 17:00:26 -07:00
}
if ( $project [ 'install_type' ] == 'dev'
&& $project [ 'status' ] != UPDATE_CURRENT
&& isset ( $project [ 'dev_version' ])
&& $project [ 'recommended' ] !== $project [ 'dev_version' ]) {
2017-04-13 15:53:35 +01:00
$versions_inner [] = [
2015-08-17 17:00:26 -07:00
'#theme' => 'update_version' ,
'#version' => $project [ 'releases' ][ $project [ 'dev_version' ]],
'#title' => t ( 'Development version:' ),
2017-04-13 15:53:35 +01:00
'#attributes' => [ 'class' => [ 'version-latest' ]],
];
2015-08-17 17:00:26 -07:00
}
}
if ( isset ( $project [ 'also' ])) {
foreach ( $project [ 'also' ] as $also ) {
2017-04-13 15:53:35 +01:00
$versions_inner [] = [
2015-08-17 17:00:26 -07:00
'#theme' => 'update_version' ,
'#version' => $project [ 'releases' ][ $also ],
'#title' => t ( 'Also available:' ),
2017-04-13 15:53:35 +01:00
'#attributes' => [ 'class' => [ 'version-also-available' ]],
];
2015-08-17 17:00:26 -07:00
}
}
if ( ! empty ( $versions_inner )) {
$variables [ 'versions' ] = $versions_inner ;
}
if ( ! empty ( $project [ 'disabled' ])) {
sort ( $project [ 'disabled' ]);
$variables [ 'disabled' ] = $project [ 'disabled' ];
}
sort ( $project [ 'includes' ]);
$variables [ 'includes' ] = $project [ 'includes' ];
2017-04-13 15:53:35 +01:00
$variables [ 'extras' ] = [];
2015-08-17 17:00:26 -07:00
if ( ! empty ( $project [ 'extra' ])) {
foreach ( $project [ 'extra' ] as $value ) {
2017-04-13 15:53:35 +01:00
$extra_item = [];
2015-08-17 17:00:26 -07:00
$extra_item [ 'attributes' ] = new Attribute ();
$extra_item [ 'label' ] = $value [ 'label' ];
2015-09-04 13:20:09 -07:00
$extra_item [ 'data' ] = [
'#prefix' => '<em>' ,
'#markup' => $value [ 'data' ],
2018-11-23 12:29:20 +00:00
'#suffix' => '</em>' ,
2015-09-04 13:20:09 -07:00
];
2015-08-17 17:00:26 -07:00
$variables [ 'extras' ][] = $extra_item ;
}
}
// Set the project status details.
$status_label = NULL ;
switch ( $project [ 'status' ]) {
case UPDATE_NOT_SECURE :
$status_label = t ( 'Security update required!' );
break ;
case UPDATE_REVOKED :
$status_label = t ( 'Revoked!' );
break ;
case UPDATE_NOT_SUPPORTED :
$status_label = t ( 'Not supported!' );
break ;
case UPDATE_NOT_CURRENT :
$status_label = t ( 'Update available' );
break ;
case UPDATE_CURRENT :
$status_label = t ( 'Up to date' );
break ;
}
$variables [ 'status' ][ 'label' ] = $status_label ;
$variables [ 'status' ][ 'attributes' ] = new Attribute ();
$variables [ 'status' ][ 'reason' ] = ( isset ( $project [ 'reason' ])) ? $project [ 'reason' ] : NULL ;
switch ( $project [ 'status' ]) {
case UPDATE_CURRENT :
$uri = 'core/misc/icons/73b355/check.svg' ;
$text = t ( 'Ok' );
break ;
case UPDATE_UNKNOWN :
case UPDATE_FETCH_PENDING :
case UPDATE_NOT_FETCHED :
$uri = 'core/misc/icons/e29700/warning.svg' ;
$text = t ( 'Warning' );
break ;
case UPDATE_NOT_SECURE :
case UPDATE_REVOKED :
case UPDATE_NOT_SUPPORTED :
2015-09-04 13:20:09 -07:00
$uri = 'core/misc/icons/e32700/error.svg' ;
2015-08-17 17:00:26 -07:00
$text = t ( 'Error' );
break ;
case UPDATE_NOT_CHECKED :
case UPDATE_NOT_CURRENT :
default :
$uri = 'core/misc/icons/e29700/warning.svg' ;
$text = t ( 'Warning' );
break ;
}
2017-04-13 15:53:35 +01:00
$variables [ 'status' ][ 'icon' ] = [
2015-08-17 17:00:26 -07:00
'#theme' => 'image' ,
'#width' => 18 ,
'#height' => 18 ,
'#uri' => $uri ,
'#alt' => $text ,
'#title' => $text ,
2017-04-13 15:53:35 +01:00
];
2015-08-17 17:00:26 -07:00
}