You are here

class LingotekDashboardController in Lingotek Translation 8

Same name and namespace in other branches
  1. 8.2 src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController
  2. 4.0.x src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController
  3. 3.0.x src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController
  4. 3.1.x src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController
  5. 3.2.x src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController
  6. 3.3.x src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController
  7. 3.4.x src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController
  8. 3.5.x src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController
  9. 3.6.x src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController
  10. 3.7.x src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController
  11. 3.8.x src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController

Returns responses for lingotek module setup routes.

Hierarchy

Expanded class hierarchy of LingotekDashboardController

1 file declares its use of LingotekDashboardController
LingotekDashboardControllerTest.php in tests/src/Unit/Controller/LingotekDashboardControllerTest.php

File

src/Controller/LingotekDashboardController.php, line 25

Namespace

Drupal\lingotek\Controller
View source
class LingotekDashboardController extends LingotekControllerBase {

  /**
   * The entity query factory.
   *
   * @var \Drupal\Core\Entity\Query\QueryFactory
   */
  protected $queryFactory;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * @var \Drupal\lingotek\LingotekConfigurationServiceInterface
   */
  protected $lingotek_configuration;

  /**
   * Constructs a LingotekControllerBase object.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The Request instance.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
   *   The entity query factory.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Drupal\lingotek\LingotekInterface $lingotek
   *   The lingotek service.
   * @param \Drupal\lingotek\LanguageLocaleMapperInterface $language_locale_mapper
   *  The language-locale mapper.
   * @param \Drupal\lingotek\LingotekConfigurationServiceInterface $lingotek_configuration
   *   The Lingotek configuration service.
   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
   *   The form builder.
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   */
  public function __construct(Request $request, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, QueryFactory $query_factory, LanguageManagerInterface $language_manager, LingotekInterface $lingotek, LanguageLocaleMapperInterface $language_locale_mapper, LingotekConfigurationServiceInterface $lingotek_configuration, FormBuilderInterface $form_builder, LoggerInterface $logger) {
    parent::__construct($request, $config_factory, $lingotek, $language_locale_mapper, $form_builder, $logger);
    $this->queryFactory = $query_factory;
    $this->entityTypeManager = $entity_type_manager;
    $this->languageManager = $language_manager;
    $this->lingotek_configuration = $lingotek_configuration;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('request_stack')
      ->getCurrentRequest(), $container
      ->get('config.factory'), $container
      ->get('entity_type.manager'), $container
      ->get('entity.query'), $container
      ->get('language_manager'), $container
      ->get('lingotek'), $container
      ->get('lingotek.language_locale_mapper'), $container
      ->get('lingotek.configuration'), $container
      ->get('form_builder'), $container
      ->get('logger.channel.lingotek'));
  }

  /**
   * Presents a dashboard overview page of translation status through Lingotek.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The page request.
   *
   * @return array
   *   The dashboard form, or a redirect to the connect page.
   */
  public function dashboardPage(Request $request) {
    if ($redirect = $this
      ->checkSetup()) {
      return $redirect;
    }
    $cms_data = $this
      ->getDashboardInfo();
    $build = [];
    $this
      ->moduleHandler()
      ->loadInclude('locale', 'install');
    $requirements = locale_requirements('runtime');
    $build['#attached']['library'][] = 'lingotek/lingotek.dashboard';
    $build['#attached']['drupalSettings']['lingotek']['cms_data'] = $cms_data;
    $build['#title'] = $this
      ->t('Dashboard');
    $build['ltk-dashboard'] = [
      '#type' => 'container',
      '#attributes' => [
        'ltk-dashboard' => '',
        'ng-app' => 'LingotekApp',
        'style' => 'margin-top: -15px;',
      ],
    ];
    if (isset($requirements['locale_translation']) && isset($requirements['locale_translation']['description'])) {
      drupal_set_message($requirements['locale_translation']['description'], 'warning');
    }
    return $build;
  }
  public function endpoint(Request $request) {
    if ($redirect = $this
      ->checkSetup()) {
      return $redirect;
    }
    $request_method = $request
      ->getMethod();
    $http_status_code = Response::HTTP_NOT_IMPLEMENTED;
    $response = array(
      'method' => $request_method,
    );
    switch ($request_method) {
      case 'POST':
        $languageStorage = $this->entityTypeManager
          ->getStorage('configurable_language');
        $lingotek_locale = $request
          ->get('code');
        $native = $request
          ->get('native');
        $language = $request
          ->get('language');
        $direction = $request
          ->get('direction');
        if (isset($language, $lingotek_locale, $direction)) {

          // First, we try if there is a disabled language with that locale.
          $existingLanguage = $this->queryFactory
            ->get('configurable_language')
            ->condition('third_party_settings.lingotek.disabled', TRUE)
            ->condition('third_party_settings.lingotek.locale', $lingotek_locale)
            ->execute();
          if (!$existingLanguage) {

            // If we didn't find it, maybe the language was the default
            // locale, and it didn't have a locale stored.
            $existingLanguage = $this->queryFactory
              ->get('configurable_language')
              ->condition('third_party_settings.lingotek.disabled', TRUE)
              ->condition('id', LingotekLocale::convertLingotek2Drupal($lingotek_locale, FALSE))
              ->execute();
          }
          if ($existingLanguage) {
            $language = $languageStorage
              ->load(reset($existingLanguage));
          }
          else {
            $rtl = $direction == 'RTL' ? LanguageInterface::DIRECTION_RTL : LanguageInterface::DIRECTION_LTR;
            $langcode = LingotekLocale::generateLingotek2Drupal($lingotek_locale);
            $language = $languageStorage
              ->create(array(
              'id' => $langcode,
              'label' => $language,
              'native' => $native,
              'direction' => $rtl,
            ));
          }
          $language
            ->setThirdPartySetting('lingotek', 'disabled', FALSE);
          $language
            ->setThirdPartySetting('lingotek', 'locale', $lingotek_locale);
          $language
            ->save();
          $response += $this
            ->getLanguageReport($language);
          $http_status_code = 200;
        }

        //TO-DO: (1) add language to CMS if not enabled X, (2) add language to TMS project
        break;
      case 'DELETE':
        $content = $request
          ->getContent();
        $parsed_content = [];
        parse_str($content, $parsed_content);
        $locale = $parsed_content['code'];
        $language = $this->languageLocaleMapper
          ->getConfigurableLanguageForLocale($locale);
        $response['language'] = $language
          ->id();
        $this->lingotek_configuration
          ->disableLanguage($language);
        $this
          ->languageManager()
          ->reset();
        $response['message'] = "Language disabled: {$locale}";
        $http_status_code = Response::HTTP_OK;
        break;
      case 'GET':
      default:
        $locale_code = $request
          ->get('code');

        //isset($request->get('code')) ? $_REQUEST['code'] : NULL;
        $details = $this
          ->getLanguageDetails($locale_code);
        if (empty($details)) {
          $response['error'] = "language code not found.";
          return JsonResponse::create($response, Response::HTTP_NOT_FOUND);
        }
        $http_status_code = Response::HTTP_OK;
        $response = array_merge($response, $details);
        break;
    }
    return JsonResponse::create($response, $http_status_code);
  }
  private function getLanguageDetails($lingotek_locale_requested = NULL) {
    $response = array();
    $available_languages = $this->languageManager
      ->getLanguages();
    $source_total = 0;
    $target_total = 0;
    $source_totals = array();
    $target_totals = array();

    // If we get a parameter, only return that language. Otherwise return all languages.
    foreach ($available_languages as $language) {

      // We check if we have a saved lingotek locale.
      // If not, we default to the id conversion.
      // Language manager returns Language objects, not ConfigurableLanguage,
      // because the language manager is initiated before the config system, and
      // loads the configuration bypassing it.
      $lingotek_locale = $this->languageLocaleMapper
        ->getLocaleForLangcode($language
        ->getId());
      if (!is_null($lingotek_locale_requested) && $lingotek_locale_requested != $lingotek_locale) {
        continue;
      }
      $language_report = $this
        ->getLanguageReport($language);
      if ($lingotek_locale_requested == $lingotek_locale) {
        $response = $language_report;
      }
      else {
        $response[$lingotek_locale] = $language_report;
      }
      $source_total += $language_report['source']['total'];
      $target_total += $language_report['target']['total'];
      $source_totals = self::calcLanguageTotals($source_totals, $language_report['source']['types']);
      $target_totals = self::calcLanguageTotals($target_totals, $language_report['target']['types']);
    }
    if (is_null($lingotek_locale_requested)) {
      $response = array(
        'languages' => $response,
        'source' => array(
          'types' => $source_totals,
          'total' => $source_total,
        ),
        'target' => array(
          'types' => $target_totals,
          'total' => $target_total,
        ),
        'count' => count($available_languages),
      );
    }
    return $response;
  }
  protected function getDashboardInfo() {
    global $base_url, $base_root;
    return array(
      "community_id" => $this->lingotek
        ->get('default.community'),
      "external_id" => $this->lingotek
        ->get('account.login_id'),
      "vault_id" => $this->lingotek
        ->get('default.vault'),
      "workflow_id" => $this->lingotek
        ->get('default.workflow'),
      "project_id" => $this->lingotek
        ->get('default.project'),
      "first_name" => 'Drupal User',
      "last_name" => '',
      "email" => $this->lingotek
        ->get('account.login_id'),
      // cms
      "cms_site_id" => $base_url,
      "cms_site_key" => $base_url,
      "cms_site_name" => 'Drupal Site',
      "cms_type" => 'Drupal',
      "cms_version" => 'VERSION HERE',
      "cms_tag" => 'CMS TAG HERE',
      "locale" => "en_US",
      // FIX: should be currently selected locale
      "module_version" => '1.x',
      "endpoint_url" => $base_root . $this
        ->url('lingotek.dashboard_endpoint'),
    );
  }
  protected function getLanguageReport(LanguageInterface $language, $active = 1, $enabled = 1) {
    $langcode = $language
      ->getId();
    $locale = $this->languageLocaleMapper
      ->getLocaleForLangcode($langcode);
    $configLanguage = $this->entityTypeManager
      ->getStorage('configurable_language')
      ->load($langcode);
    $types = $this
      ->getEnabledTypes();
    $stat = array(
      'locale' => $locale,
      'xcode' => $langcode,
      'active' => $this->lingotek_configuration
        ->isLanguageEnabled($configLanguage) ? 1 : 0,
      'enabled' => 1,
      'source' => array(
        'types' => $this
          ->getSourceTypeCounts($langcode),
        'total' => 0,
      ),
      'target' => array(
        'types' => $this
          ->getTargetTypeCounts($langcode),
        'total' => 0,
      ),
    );
    foreach ($types as $type) {
      $stat['source']['total'] += isset($stat['source']['types'][$type]) ? $stat['source']['types'][$type] : 0;
      $stat['target']['total'] += isset($stat['target']['types'][$type]) ? $stat['target']['types'][$type] : 0;
    }
    return $stat;
  }

  /**
   * Gets the entity type ids of entities to be translated with Lingotek.
   *
   * @return array The entity type names of content entities enabled.
   */
  protected function getEnabledTypes() {
    $types = $this->lingotek_configuration
      ->getEnabledEntityTypes();
    return empty($types) ? $types : array_keys($types);
  }
  protected function getSourceTypeCounts($langcode, $types = NULL) {
    $types = is_null($types) ? $this
      ->getEnabledTypes() : $types;
    $result = array();
    foreach ($types as $type) {
      $result[$type] = $this
        ->getSourceTypeCount($langcode, $type);
    }
    return $result;
  }
  protected function getSourceTypeCount($langcode, $type) {
    $count = $this->queryFactory
      ->get($type)
      ->condition('langcode', $langcode)
      ->condition('default_langcode', 1)
      ->count()
      ->execute();
    return (int) $count;
  }
  protected function getTargetTypeCounts($langcode, $types = NULL) {
    $types = is_null($types) ? $this
      ->getEnabledTypes() : $types;
    $result = array();
    foreach ($types as $type) {
      $result[$type] = $this
        ->getTargetTypeCount($langcode, $type);
    }
    return $result;
  }
  protected function getTargetTypeCount($langcode, $type) {
    $count = $this->queryFactory
      ->get($type)
      ->condition('langcode', $langcode)
      ->condition('default_langcode', 0)
      ->count()
      ->execute();
    return (int) $count;
  }

  /**
   * Sums the values of the arrays be there keys (PHP 4, PHP 5)
   * array array_sum_values ( array array1 [, array array2 [, array ...]] )
   */
  private static function calcLanguageTotals() {
    $return = array();
    $intArgs = func_num_args();
    $arrArgs = func_get_args();
    if ($intArgs < 1) {
      trigger_error('Warning: Wrong parameter count for calcLanguageTotals()', E_USER_WARNING);
    }
    foreach ($arrArgs as $arrItem) {
      if (!is_array($arrItem)) {
        trigger_error('Warning: Wrong parameter values for calcLanguageTotals()', E_USER_WARNING);
      }
      foreach ($arrItem as $k => $v) {
        if (!key_exists($k, $return)) {
          $return[$k] = 0;
        }
        $return[$k] += $v;
      }
    }
    return $return;
    $sumArray = array();
    foreach ($myArray as $k => $subArray) {
      foreach ($subArray as $id => $value) {
        $sumArray[$id] += $value;
      }
    }
    return $sumArray;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LingotekControllerBase::$configFactory protected property The config factory. Overrides ControllerBase::$configFactory
LingotekControllerBase::$formBuilder protected property The form builder. Overrides ControllerBase::$formBuilder
LingotekControllerBase::$languageLocaleMapper protected property The language-locale mapper.
LingotekControllerBase::$logger protected property A logger instance.
LingotekControllerBase::$request protected property A Symfony request instance
LingotekControllerBase::connected public function Checks if site is connected to Lingotek.
LingotekControllerBase::getLingotekForm protected function Return a Lingotek form (convenience function)
LingotekDashboardController::$languageManager protected property The language manager. Overrides ControllerBase::$languageManager
LingotekDashboardController::$lingotek_configuration protected property
LingotekDashboardController::$queryFactory protected property The entity query factory.
LingotekDashboardController::calcLanguageTotals private static function Sums the values of the arrays be there keys (PHP 4, PHP 5) array array_sum_values ( array array1 [, array array2 [, array ...]] )
LingotekDashboardController::create public static function Instantiates a new instance of this class. Overrides LingotekControllerBase::create
LingotekDashboardController::dashboardPage public function Presents a dashboard overview page of translation status through Lingotek.
LingotekDashboardController::endpoint public function
LingotekDashboardController::getDashboardInfo protected function
LingotekDashboardController::getEnabledTypes protected function Gets the entity type ids of entities to be translated with Lingotek.
LingotekDashboardController::getLanguageDetails private function
LingotekDashboardController::getLanguageReport protected function
LingotekDashboardController::getSourceTypeCount protected function
LingotekDashboardController::getSourceTypeCounts protected function
LingotekDashboardController::getTargetTypeCount protected function
LingotekDashboardController::getTargetTypeCounts protected function
LingotekDashboardController::__construct public function Constructs a LingotekControllerBase object. Overrides LingotekControllerBase::__construct
LingotekSetupTrait::$lingotek protected property A lingotek connector object
LingotekSetupTrait::checkSetup protected function Verify the Lingotek Translation module has been properly initialized.
LingotekSetupTrait::setupCompleted public function Checks if Lingotek module is completely set up.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.