You are here

public function LingotekDashboardController::endpoint in Lingotek Translation 3.0.x

Same name and namespace in other branches
  1. 8 src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController::endpoint()
  2. 8.2 src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController::endpoint()
  3. 4.0.x src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController::endpoint()
  4. 3.1.x src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController::endpoint()
  5. 3.2.x src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController::endpoint()
  6. 3.3.x src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController::endpoint()
  7. 3.4.x src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController::endpoint()
  8. 3.5.x src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController::endpoint()
  9. 3.6.x src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController::endpoint()
  10. 3.7.x src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController::endpoint()
  11. 3.8.x src/Controller/LingotekDashboardController.php \Drupal\lingotek\Controller\LingotekDashboardController::endpoint()
1 string reference to 'LingotekDashboardController::endpoint'
lingotek.routing.yml in ./lingotek.routing.yml
lingotek.routing.yml

File

src/Controller/LingotekDashboardController.php, line 133

Class

LingotekDashboardController
Returns responses for lingotek module setup routes.

Namespace

Drupal\lingotek\Controller

Code

public function endpoint(Request $request) {
  if ($redirect = $this
    ->checkSetup()) {
    return $redirect;
  }
  $request_method = $request
    ->getMethod();
  $http_status_code = Response::HTTP_NOT_IMPLEMENTED;
  $response = [
    '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 = $languageStorage
          ->getQuery()
          ->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 = $languageStorage
            ->getQuery()
            ->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([
            '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:

      // isset($request->get('code')) ? $_REQUEST['code'] : NULL;
      $locale_code = $request
        ->get('code');
      $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);
}