View source
<?php
namespace Drupal\w3c_validator\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Drupal\w3c_validator\W3CProcessor;
use Symfony\Component\DependencyInjection\ContainerInterface;
class W3CLogController extends ControllerBase {
protected $renderer;
protected $formBuilder;
protected $w3cProcessor;
public static function create(ContainerInterface $container) {
return new static($container
->get('renderer'), $container
->get('form_builder'), $container
->get('w3c.processor'));
}
public function __construct(RendererInterface $renderer, FormBuilderInterface $form_builder, W3CProcessor $w3c_processor) {
$this->renderer = $renderer;
$this->formBuilder = $form_builder;
$this->w3cProcessor = $w3c_processor;
}
public function overview() {
$output = [
'#prefix' => '<div id="foobar">',
'#suffix' => '</div>',
];
$rows = [];
$output['operations'] = $this->formBuilder
->getForm('Drupal\\w3c_validator\\Form\\W3CValidatorOperationForm');
$pages = $this->w3cProcessor
->findAllPages();
$all_validated_pages = $this->w3cProcessor
->findAllValidatedPages();
foreach ($pages as $url => $page) {
$validation = $this
->buildValidationResult($all_validated_pages, $url);
$row = [];
$row[$url]['summary'] = $this
->buildValidationDisplay($page, $validation);
if (isset($validation['status']) && $validation['status'] != $this
->t('Unknown')) {
$row[$url]['details'] = $this
->buildValidationDetailDisplay($validation);
}
$rows[] = [
'data' => [
[
'data' => $this->renderer
->render($row),
'class' => 'w3c_validator-wrapper collapsed',
],
],
'class' => [
$validation['class'],
],
];
}
$output['pages'] = [
'#type' => 'table',
'#rows' => $rows,
'#attributes' => [
'id' => 'w3c-report',
],
'#empty' => $this
->t('No data to display.'),
];
$output['#attached']['library'][] = 'w3c_validator/w3c_validator.report';
return $output;
}
protected function buildValidationResult($all_validated_pages, $url) {
$validation = [];
if (isset($all_validated_pages[$url])) {
$validation = $all_validated_pages[$url];
$validation['result'] = $this
->t('@errors errors, @warnings warnings', [
'@errors' => $validation['error_count'],
'@warnings' => $validation['warning_count'],
]);
if ($validation['need_validation']) {
$validation['class'] = 'color-outdated';
$validation['status'] = $this
->t('Outdated');
}
elseif ($validation['validity']) {
$validation['class'] = $validation['warning_count'] ? 'color-warning' : 'color-status';
$validation['status'] = $this
->t('Valid');
}
else {
$validation['class'] = 'color-error';
$validation['status'] = $this
->t('Invalid');
}
}
else {
$validation['class'] = 'color-unknown';
$validation['status'] = $this
->t('Unknown');
$validation['result'] = $this
->t('Not yet validated');
}
return $validation;
}
protected function buildValidationDisplay(array $page, array $validation) {
$display = [
'#type' => 'container',
'#attributes' => [
'class' => [
'page-summary',
],
],
];
$display['icon'] = [
'#prefix' => '<span class="icon">',
'#suffix' => '</span>',
];
$display['title'] = [
'#prefix' => '<span class="title">',
'#suffix' => '</span>',
'#markup' => $page['title'],
];
$display['result'] = [
'#prefix' => '<span class="result">',
'#suffix' => '</span>',
'#markup' => $validation['result'],
];
$display['status'] = [
'#prefix' => '<span class="status">',
'#suffix' => '</span>',
'#markup' => $validation['status'],
];
return $display;
}
protected function buildValidationDetailDisplay(array $validation) {
$display = [
'#prefix' => '<div class="fieldset-wrapper analysis-results">',
'#suffix' => '</div>',
];
if ($validation['validity']) {
$output = $this
->t('This document was successfully checked !');
}
else {
$output = $this
->t('Errors found while checking this document !');
}
$display['message'] = [
'#prefix' => '<h2 class="message ' . $validation['class'] . '">',
'#suffix' => '</h2>',
'#markup' => $output,
];
$uri = Url::fromUri('base:' . $validation['uri'], [
'absolute' => TRUE,
]);
$rows[] = [
$this
->t('Uri'),
Link::fromTextAndUrl($uri
->toString(), $uri),
];
$rows[] = [
$this
->t('Validity'),
$validation['status'],
];
$url = Url::fromUri($this->w3cProcessor
->getValidatorUrl(), [
'query' => [
'doc' => $uri
->toString(),
],
'attributes' => [
'target' => '_new',
],
]);
$rows[] = [
$this
->t('Validator results'),
Link::fromTextAndUrl($url
->toString(), $url),
];
$rows[] = [
$this
->t('Doctype'),
$validation['doctype'],
];
$rows[] = [
$this
->t('Summary'),
$validation['result'],
];
$display['detail-table'] = [
'#type' => 'table',
'#rows' => $rows,
'#attributes' => [
'class' => 'report',
],
'#empty' => $this
->t('No data to display.'),
];
$display['errors-title'] = [
'#prefix' => '<h2>',
'#suffix' => '</h2>',
'#markup' => $this
->t('Errors'),
];
$validation['errors'] = is_array($validation['errors']) ? $validation['errors'] : unserialize($validation['errors']);
if (is_array($validation['errors']) && !empty($validation['errors'])) {
foreach ($validation['errors'] as $id => $error) {
$display['error'][$id] = [
'#prefix' => '<div class="message-wrapper message-error">',
'#suffix' => '</div>',
'message' => [
'#prefix' => '<div class="message">',
'#suffix' => '</div>',
'#markup' => '<span class="where">' . $this
->t('Line @line, Column @col:', [
'@line' => $error
->getFirstLine(),
'@col' => $error
->getFirstColumn(),
]) . '</span> <span class="descr">' . $this
->t('@descr', [
'@descr' => $error
->getText(),
]) . '</span>',
],
'source' => [
'#prefix' => '<div class="source">',
'#suffix' => '</div>',
'#markup' => '<pre>' . $this
->highlightExtract($error
->getExtract(), $error
->getHighlightStart(), $error
->getHighlightLength()) . '</pre>',
],
];
}
}
else {
$display['error'] = [
'#prefix' => '<div>',
'#suffix' => '</div>',
'#markup' => $this
->t('No errors found'),
];
}
$display['warnings-title'] = [
'#prefix' => '<h2>',
'#suffix' => '</h2>',
'#markup' => $this
->t('Warnings'),
];
$validation['warnings'] = is_array($validation['warnings']) ? $validation['warnings'] : unserialize($validation['warnings']);
if (is_array($validation['warnings']) && !empty($validation['warnings'])) {
foreach ($validation['warnings'] as $id => $warning) {
$display['warning'][$id] = [
'#prefix' => '<div class="message-wrapper message-warning">',
'#suffix' => '</div>',
'message' => [
'#prefix' => '<div class="message">',
'#suffix' => '</div>',
'#markup' => '<span class="where">' . $this
->t('Line @line, Column @col:', [
'@line' => $warning
->getFirstLine(),
'@col' => $warning
->getFirstColumn(),
]) . '</span> <span class="descr">' . $this
->t('@descr', [
'@descr' => $warning
->getText(),
]) . '</span>',
],
'source' => [
'#prefix' => '<div class="source">',
'#suffix' => '</div>',
'#markup' => '<pre>' . $this
->highlightExtract($warning
->getExtract(), $warning
->getHighlightStart(), $warning
->getHighlightLength()) . '</pre>',
],
];
}
}
else {
$display['warning'] = [
'#prefix' => '<div>',
'#suffix' => '</div>',
'#markup' => $this
->t('No warnings found'),
];
}
$display['infos-title'] = [
'#prefix' => '<h2>',
'#suffix' => '</h2>',
'#markup' => $this
->t('Infos'),
];
$validation['infos'] = is_array($validation['infos']) ? $validation['infos'] : unserialize($validation['infos']);
if (is_array($validation['infos']) && !empty($validation['infos'])) {
foreach ($validation['infos'] as $id => $info) {
$display['info'][$id] = [
'#prefix' => '<div class="message-wrapper message-info">',
'#suffix' => '</div>',
'message' => [
'#prefix' => '<div class="message">',
'#suffix' => '</div>',
'#markup' => '<span class="where">' . $this
->t('Line @line, Column @col:', [
'@line' => $info
->getFirstLine(),
'@col' => $info
->getFirstColumn(),
]) . '</span> <span class="descr">' . $this
->t('@descr', [
'@descr' => $info
->getText(),
]) . '</span>',
],
'source' => [
'#prefix' => '<div class="source">',
'#suffix' => '</div>',
'#markup' => '<pre>' . $this
->highlightExtract($info
->getExtract(), $info
->getHighlightStart(), $info
->getHighlightLength()) . '</pre>',
],
];
}
}
else {
$display['infos'] = [
'#prefix' => '<div>',
'#suffix' => '</div>',
'#markup' => $this
->t('No information found'),
];
}
return $display;
}
protected function highlightExtract($str, $start, $length) {
$parts = [
substr($str, 0, $start),
substr($str, $start, $length),
substr($str, $start + $length),
];
$parts = array_map('htmlentities', $parts);
$highlighted = $parts[0] . '<b>';
$highlighted .= $parts[1] . '</b>' . $parts[2];
return trim($highlighted);
}
}