LingotekNotificationController.php in Lingotek Translation 8
File
src/Controller/LingotekNotificationController.php
View source
<?php
namespace Drupal\lingotek\Controller;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\lingotek\Entity\LingotekProfile;
use Drupal\lingotek\LanguageLocaleMapperInterface;
use Drupal\lingotek\Lingotek;
use Drupal\lingotek\LingotekLocale;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class LingotekNotificationController extends LingotekControllerBase {
public function endpoint(Request $request) {
$content_translation_service = \Drupal::service('lingotek.content_translation');
$config_translation_service = \Drupal::service('lingotek.config_translation');
$translation_service = $content_translation_service;
$request_method = $request
->getMethod();
$http_status_code = Response::HTTP_NOT_IMPLEMENTED;
$type = $request
->get('type');
$result = array();
$messages = array();
$security_token = $request
->get('security_token');
if ($security_token == 1) {
$http_status_code = Response::HTTP_NOT_IMPLEMENTED;
}
parse_str($request
->getQueryString(), $params);
switch ($type) {
case 'project':
break;
case 'document':
break;
case 'document_uploaded':
$entity = $this
->getEntity($request
->get('document_id'));
$profile = $this
->getProfile($entity);
if ($entity) {
if ($entity instanceof ConfigEntityInterface) {
$translation_service = $config_translation_service;
}
$http_status_code = Response::HTTP_OK;
$translation_service
->setSourceStatus($entity, Lingotek::STATUS_CURRENT);
$result['request_translations'] = $profile
->hasAutomaticUpload() ? $translation_service
->requestTranslations($entity) : [];
}
else {
$http_status_code = Response::HTTP_NOT_FOUND;
}
break;
case 'phase':
case 'target':
$entity = $this
->getEntity($request
->get('document_id'));
$profile = $this
->getProfile($entity);
if ($entity) {
if ($entity instanceof ConfigEntityInterface) {
$translation_service = $config_translation_service;
}
$http_status_code = Response::HTTP_OK;
$locale = $request
->get('locale');
$langcode = $this->languageLocaleMapper
->getConfigurableLanguageForLocale($locale)
->id();
$result['set_target_status'] = $translation_service
->setTargetStatus($entity, $langcode, Lingotek::STATUS_READY);
$result['download'] = $profile
->hasAutomaticDownloadForTarget($langcode) ? $translation_service
->downloadDocument($entity, $locale) : FALSE;
}
else {
$http_status_code = Response::HTTP_NOT_FOUND;
$messages[] = "Document not found.";
}
break;
default:
$http_status_code = Response::HTTP_NOT_IMPLEMENTED;
$messages[] = "Not implemented.";
break;
}
$response = array(
'service' => 'notify',
'method' => $request_method,
'params' => $params,
'result' => $result,
'messages' => $messages,
);
return JsonResponse::create($response, $http_status_code);
}
protected function getProfile($entity) {
$configuration_service = \Drupal::service('lingotek.configuration');
$profile = NULL;
if ($entity instanceof ContentEntityInterface) {
$profile = $configuration_service
->getEntityProfile($entity, FALSE);
}
elseif ($entity instanceof ConfigEntityInterface) {
$profile = $configuration_service
->getConfigEntityProfile($entity, FALSE);
}
return $profile;
}
protected function getEntity($document_id) {
$translation_service = \Drupal::service('lingotek.content_translation');
$entity = $translation_service
->loadByDocumentId($document_id);
if ($entity === NULL) {
$translation_service = \Drupal::service('lingotek.config_translation');
$entity = $translation_service
->loadByDocumentId($document_id);
}
return $entity;
}
}