diff --git a/translators/tmgmt_local/src/Controller/LocalTaskController.php b/translators/tmgmt_local/src/Controller/LocalTaskController.php index 6054154..94b05d9 100644 --- a/translators/tmgmt_local/src/Controller/LocalTaskController.php +++ b/translators/tmgmt_local/src/Controller/LocalTaskController.php @@ -11,8 +11,11 @@ use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Controller\ControllerBase; use Drupal\tmgmt_local\LocalTaskInterface; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\StreamedResponse; +use Symfony\Component\HttpFoundation\ResponseHeaderBag; use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\ReplaceCommand; +use Drupal\tmgmt_local\Entity\LocalTask; /** * Returns responses for Local task routes. @@ -49,4 +52,35 @@ class LocalTaskController extends ControllerBase { return $this->redirect(''); } + /** + * Create a link for exporting XLF file. + */ + public function exportFile($tmgmt_local_task) { + // Load the job from the task. + $task = LocalTask::load($tmgmt_local_task); + $job = $task->getJob(); + + // Build file content. + $export = \Drupal::service('plugin.manager.tmgmt_file.format')->createInstance('xlf'); + $data = $export->export($job); + + // Build the stream response. + $response = new StreamedResponse(); + $response->setCallback(function () use ($data) { + echo $data; + }); + + // Prepare for file serving. + $file_name = "JobID" . $job->id() . '_' . $job->getSourceLangcode() . '_' . $job->getTargetLangcode() . '.xlf'; + $d = $response->headers->makeDisposition( + ResponseHeaderBag::DISPOSITION_ATTACHMENT, + $file_name + ); + + $response->headers->set('Content-Disposition', $d); + $response->headers->set('data', array($data)); + + return $response; + } + } diff --git a/translators/tmgmt_local/src/Entity/LocalTask.php b/translators/tmgmt_local/src/Entity/LocalTask.php index d846622..b5dfd45 100644 --- a/translators/tmgmt_local/src/Entity/LocalTask.php +++ b/translators/tmgmt_local/src/Entity/LocalTask.php @@ -416,4 +416,20 @@ class LocalTask extends ContentEntityBase implements LocalTaskInterface { ); } + /** + * {@inheritdoc} + * + */ + public function getTaskItemFromJobItemID($job_item_id) { + $task_items = $this->getItems(); + $found_task_item = NULL; + foreach($task_items as $task_item) { + if ($task_item->getJobItem()->id() == $job_item_id) { + $found_task_item=$task_item; + break; + } + } + + return $found_task_item; + } } diff --git a/translators/tmgmt_local/src/Form/LocalTaskForm.php b/translators/tmgmt_local/src/Form/LocalTaskForm.php index 631c5e1..9f3e588 100644 --- a/translators/tmgmt_local/src/Form/LocalTaskForm.php +++ b/translators/tmgmt_local/src/Form/LocalTaskForm.php @@ -13,6 +13,7 @@ use Drupal\tmgmt_local\Entity\LocalTask; use Drupal\tmgmt_local\LocalTaskInterface; use Drupal\user\Entity\User; use Drupal\views\Views; +use Drupal\Core\Link; /** * Form controller for the localTask edit forms. @@ -119,6 +120,36 @@ class LocalTaskForm extends ContentEntityForm { '#suffix' => '', ); + // Manage file tasks. + $form['file'] = array( + '#type' => 'fieldset', + '#title' => t('Manage Files'), + ); + + // Export link. + $file_link = Link::createFromRoute(t('here'), 'entity.tmgmt_local_task.file_export', array( + 'tmgmt_local_task' => $local_task->id(), + )); + + $form['file']['file_link'] = array( + '#type' => 'item', + '#markup' => t('Download the XLIFF File ') . $file_link->toString(), + ); + + // File import. + $form['file']['import'] = array( + '#type' => 'file', + '#title' => t('File import'), + '#size' => 50, + '#description' => t('Supported formats: xlf'), + ); + + $form['file']['import_submit'] = array( + '#type' => 'submit', + '#value' => t('Import'), + '#submit' => ['::importFormSubmit'], + ); + if ($view = Views::getView('tmgmt_local_task_items')) { $block = $view->preview('block_1', [$local_task->id()]); $form['items'] = array( @@ -182,4 +213,61 @@ class LocalTaskForm extends ContentEntityForm { $form_state->setRedirectUrl($this->entity->toUrl('delete-form')); } + /** + * Form submit callback for the import button. + */ + public function importFormSubmit(array $form, FormStateInterface $form_state) { + /* @var $task_item \Drupal\tmgmt_local\entity\LocalTaskItem */ + $task = $form_state->getFormObject()->getEntity(); + $job = $task->getJob(); + if ($file = file_save_upload('import', array('file_validate_extensions' => array('xlf')), FALSE, 0)) { + $extension = 'xlf'; + $plugin = \Drupal::service('plugin.manager.tmgmt_file.format')->createInstance($extension); + if ($plugin) { + // Validate the file on job. + $validated_job = $plugin->validateImport($file->getFileUri(), $job); + if (!$validated_job) { + $job->addMessage('Failed to validate file, import aborted.', array(), 'error'); + } + elseif ($validated_job->id() != $job->id()) { + $job->addMessage('The imported file job id @file_id does not match the job id @job_id.', array( + '@file_id' => $validated_job->id(), + '@job_id' => $job->id(), + ), 'error'); + } + else { + try { + // Validation successful, start import. + $job->addTranslatedData($plugin->import($file->getFileUri())); + $job->addMessage('Successfully imported file.'); + } catch (Exception $e) { + $job->addMessage('File import failed with the following message: @message', array('@message' => $e->getMessage()), 'error'); + } + + // Transfer data from job to task. @TODO: Investigate common interface for job and task for importing + $job_data = $job->getData(); + + foreach($job_data as $tjiid => $job_item) { + foreach($job_item as $key => $value) { + if (isset($value[0]['value']['#translation']['#text']) && + $value[0]['value']['#translation']['#text'] != "") { + + if ($task_item = $task->getTaskItemFromJobItemID($tjiid)) { + + $update_key = array($key, 0, 'value', '#translation'); + $update_values = array('#text' => $value[0]['value']['#translation']['#text']); + $task_item->updateData($update_key, $update_values); + $task_item->save(); + } + } + } + } + } + } + } + $form_state->setRedirect('entity.tmgmt_local_task.canonical', array( + 'tmgmt_local_task' => $task->id(), + )); + } + } diff --git a/translators/tmgmt_local/src/LocalTaskInterface.php b/translators/tmgmt_local/src/LocalTaskInterface.php index 0484ce5..efe4b9d 100644 --- a/translators/tmgmt_local/src/LocalTaskInterface.php +++ b/translators/tmgmt_local/src/LocalTaskInterface.php @@ -240,4 +240,11 @@ interface LocalTaskInterface extends ContentEntityInterface, EntityChangedInterf */ public static function getStatuses(); + /** + * Find the task item corresponding to a job item. + * + * @param $job_item_id + * Job ID to find the corresponding task. + */ + public function getTaskItemFromJobItemID($job_item_id); } diff --git a/translators/tmgmt_local/tmgmt_local.routing.yml b/translators/tmgmt_local/tmgmt_local.routing.yml index 0dbf131..b4c2120 100644 --- a/translators/tmgmt_local/tmgmt_local.routing.yml +++ b/translators/tmgmt_local/tmgmt_local.routing.yml @@ -47,3 +47,10 @@ entity.tmgmt_local_task_item.canonical: _entity_form: tmgmt_local_task_item.edit requirements: _entity_access: tmgmt_local_task_item.update + +entity.tmgmt_local_task.file_export: + path: '/translate/{tmgmt_local_task}/xliff' + defaults: + _controller: '\Drupal\tmgmt_local\Controller\LocalTaskController::exportFile' + requirements: + _permission: 'provide translation services'