Unverified Commit 74766820 authored by Stefan Auditor's avatar Stefan Auditor Committed by Stefan Auditor
Browse files

Issue #3282696 by sanduhrs: Port of l10n_community: Add project statistics block

parent bc053b07
Loading
Loading
Loading
Loading
+129 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\l10n_community\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Link;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\l10n_community\L10nStatistics;
use Drupal\l10n_server\Entity\L10nServerRelease;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a project statistics block.
 *
 * @Block(
 *   id = "l10n_community_project_statistics",
 *   admin_label = @Translation("Project statistics"),
 *   category = @Translation("L10n")
 * )
 */
class ProjectStatisticsBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The l10n_community.statistics service.
   *
   * @var \Drupal\l10n_community\L10nStatistics
   */
  protected $statistics;

  /**
   * Constructs a new ProjectStatisticsBlock instance.
   *
   * @param array $configuration
   *   The plugin configuration, i.e. an array with configuration values keyed
   *   by configuration option name. The special key 'context' may be used to
   *   initialize the defined contexts by setting it to an array of context
   *   values keyed by context names.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\l10n_community\L10nStatistics $statistics
   *   The l10n_community.statistics service.
   */
  public function __construct(
      array $configuration,
      $plugin_id,
      $plugin_definition,
      L10nStatistics $statistics
  ) {
    parent::__construct(
      $configuration,
      $plugin_id,
      $plugin_definition
    );
    $this->statistics = $statistics;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(
      ContainerInterface $container,
      array $configuration,
      $plugin_id,
      $plugin_definition
  ) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('l10n_community.statistics')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    /** @var \Drupal\l10n_server\Entity\L10nServerProjectInterface $project */
    if (!$project = \Drupal::routeMatch()->getParameter('l10n_server_project')) {
      return [];
    }

    $num_source = $this->statistics->getProjectStringCount($project);
    $release_ids = \Drupal::entityQuery('l10n_server_release')
      ->condition('pid', $project->id())
      ->execute();
    $releases = L10nServerRelease::loadMultiple($release_ids);

    $num_parsed = 0;
    foreach ($releases as $release) {
      if ($release->getLastParsed() > 0) {
        $num_parsed++;
      }
    }
    $num_releases = count($releases);

    $num_warnings = $this->statistics->getProjectWarningsCount($project);

    // Build list of links for summary.
    $items = [];
    if ($project->getHomepage()) {
      $items[] = $this->t('Project home: <a href="@project_home">@project_home</a>', [
        '@project_name' => $project->label(),
        '@project_home' => $project->getHomepage(),
      ]);
    }
    if ($num_releases === 0) {
      // If we don't know of any releases, we do not list source string or
      // warning information, since we should have all zeros there too. This
      // summarizes our stand in short, that we do not yet have data here.
      $items[] = $this->t('No releases known yet');
    }
    else {
      $items[] = Link::fromTextAndUrl($this->formatPlural($num_parsed, '1 release parsed', '@count releases parsed') . ' (' . $this->formatPlural($num_releases, '1 known', '@count known') . ')', Url::fromUri('internal:/project/1/releases'))->toString();
      $items[] = ($num_source == 0 ? $this->t('No source strings found') : $this->formatPlural($num_source, '1 source string in total', '@count source strings in total'));
      $items[] = ($num_warnings == 0 ? $this->t('No source code warnings') : $this->formatPlural($num_warnings, '1 source code warning', '@count source code warnings'));
    }

    $build['content'] = [
      '#theme' => 'item_list',
      '#items' => $items,
    ];
    return $build;
  }

}