l10n_server-2.x-dev/l10n_statistics/src/Controller/L10nStatisticsCalendarController.php

l10n_statistics/src/Controller/L10nStatisticsCalendarController.php
<?php

declare(strict_types=1);

namespace Drupal\l10n_statistics\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Database;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;

/**
 * Returns responses for l10n_statistics routes.
 */
class L10nStatisticsCalendarController extends ControllerBase {

  /**
   * Builds the response.
   */
  public function build(?ConfigurableLanguage $language = NULL) {
    $build['subtitle'] = [
      '#type' => 'html_tag',
      '#tag' => 'h2',
    ];
    if ($language) {
      $build['subtitle']['#value'] = $this->t('@language string submissions', [
        '@language' => $language->getName(),
      ]);
    }
    else {
      $build['subtitle']['#value'] = $this->t('All languages string submissions');
    }

    $items = [];
    $items[] = [
      '#markup' => Link::fromTextAndUrl(
        $this->t('All languages'),
        Url::fromRoute('l10n_statistics.submissions')
      )->toString(),
    ];
    $languages = \Drupal::languageManager()->getLanguages();
    foreach ($languages as $configurable_language) {
      $items[] = [
        '#markup' => Link::fromTextAndUrl(
          $configurable_language->getName(),
          Url::fromRoute('l10n_statistics.submissions.language', [
            'language' => $configurable_language->getId(),
          ])
        )->toString(),
      ];
    }
    $build['languages'] = [
      '#theme' => 'item_list',
      '#type' => 'ul',
      '#title' => 'Language list',
      '#items' => $items,
      '#attributes' => ['class' => 'language-list'],
      '#wrapper_attributes' => ['class' => 'container'],
    ];

    $years = range($this->getTheBeginningOfTime(), date("Y"));
    $years = [2022];
    rsort($years);
    foreach ($years as $year) {
      $build['chart_' . $year] = [
        '#type' => 'html_tag',
        '#tag' => 'div',
        '#attributes' => [
          'id' => ['l10n-statistics-chart-' . $year],
          'class' => ['l10n-statistics-chart', 'l10n-statistics-chart-' . $year],
        ],
      ];
      $build['#attached']['drupalSettings']['l10nStatistics']['calendar']['charts']['year' . $year] = $this->getSubmissions($year, $language);
    }
    $build['#attached']['library'][] = 'l10n_statistics/google-charts';
    $build['#attached']['library'][] = 'l10n_statistics/calendar';
    $build['#attached']['drupalSettings']['l10nStatistics']['calendar']['years'] = $years;

    return $build;
  }

  /**
   * Get submissions.
   *
   * @param int $year
   *   A year integer.
   * @param \Drupal\language\Entity\ConfigurableLanguage|null $language
   *   A configurable language or null.
   *
   * @return array
   *   An array of records.
   *
   * @todo Check if commented code is still needed.
   *
   * @throws \Exception
   */
  private function getSubmissions(int $year, ?ConfigurableLanguage $language = NULL) {
    $from = new \DateTime($year . '-01-01 00:00:00');
    $until = new \DateTime($year . '-12-31 23:59:59');
    //
    //    // @todo Fix database access to default key.
    //    $connection = Database::getConnection('default', 'migrate');
    //    $query = $connection
    //    ->select('l10n_server_translation_history', 'th');
    //    if ($language instanceof ConfigurableLanguage) {
    //    $query
    //    ->innerJoin('l10n_server_translation', 't', 'th.tid = t.tid');
    //    $query
    //    ->condition('language', $language->getId());
    //    }
    //    $query
    //    ->condition('time_action', $from->getTimestamp(), '>=')
    //    ->condition('time_action', $until->getTimestamp(), '<=');
    //    $query->addExpression("FROM_UNIXTIME(time_action, '%d')", 'day');
    //    $query->addExpression("FROM_UNIXTIME(time_action, '%m')", 'month');
    //    $query->addExpression("FROM_UNIXTIME(time_action, '%Y')", 'year');
    //    $query->addExpression('SUM(IF(type_action=1), 1, 0)', 'actions');
    // $query
    // ->groupBy('day');
    // $query
    // ->orderBy('day', 'ASC');
    // $result = $query->execute();
    // $records = [];
    // foreach ($result as $record) {
    // $records[$record->day] = [
    // $record->day,
    // $record->month,
    // $record->year,
    // int) $record->actions,
    // ];
    // }
    $records = [
      [
        'year' => $year,
        'month' => rand(1, 12),
        'day' => rand(1, 28),
        'actions' => rand(0, 99999),
      ],
      [
        'year' => $year,
        'month' => rand(1, 12),
        'day' => rand(1, 28),
        'actions' => rand(0, 99999),
      ],
      [
        'year' => $year,
        'month' => rand(1, 12),
        'day' => rand(1, 28),
        'actions' => rand(0, 99999),
      ],
    ];

    return array_values($records);
  }

  /**
   * Get the beginning of time.
   *
   * @return int
   *   An integer representing a year.
   */
  private function getTheBeginningOfTime() {
    // @todo Fix database access to default key.
    $connection = Database::getConnection('default', 'migrate');
    $query = $connection
      ->select('l10n_server_translation_history', 'th');
    $query
      ->addExpression("FROM_UNIXTIME(time_action, '%Y')", 'year');
    $query
      ->orderBy('time_action,', 'ASC');
    $query
      ->range(0, 1);
    return (int) $query->execute()->fetchField();
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc