public function JobItem::addTranslatedData

Adds translated data to a job item.

This function calls for JobItem::addTranslatedDataRecursive() which sets the status of each added data item to TMGMT_DATA_ITEM_STATE_TRANSLATED.

Following rules apply while adding translated data:

1) Updated are only items that are changed. In case there is local modification the translation is added as a revision with a message stating this fact.

2) Merging happens at the data items level, so updating only those that are changed. If a data item is in review/reject status and is being updated with translation originating from remote the status is updated to 'translated' no matter if it is changed or not.

3) Each time a data item is updated the previous translation becomes a revision.

If all data items are translated, the status of the job item is updated to needs review.

@todo To update the job item status to needs review we could take advantage of the JobItem::getCountPending() and JobItem::getCountTranslated(). The catch is, that this counter gets updated while saveing which not yet hapened.

Parameters

array $translation: Nested array of translated data. Can either be a single text entry, the whole data structure or parts of it.

array|string $key: (Optional) Either a flattened key (a 'key1][key2][key3' string) or a nested one, e.g. array('key1', 'key2', 'key2'). Defaults to an empty array which means that it will replace the whole translated data array.

int|null $status: (Optional) The data item status that will be set. Defaults to NULL, which means that it will be set to translated unless it was previously set to preliminary, then it will keep that state. Explicitly pass TMGMT_DATA_ITEM_STATE_TRANSLATED or TMGMT_DATA_ITEM_STATE_PRELIMINARY to set it to that value. Other statuses are not supported.

Throws

\Drupal\tmgmt\TMGMTException If is given an unsupported status.

Overrides JobItemInterface::addTranslatedData

File

src/Entity/JobItem.php, line 839

Class

JobItem
Entity class for the tmgmt_job_item entity.

Namespace

Drupal\tmgmt\Entity

Code

public function addTranslatedData(array $translation, $key = array(), $status = NULL) {
  if ($this
    ->isInactive()) {

    // The job item can not be inactive and receive translations.
    $this
      ->setState(JobItemInterface::STATE_ACTIVE);
  }
  $this
    ->addTranslatedDataRecursive($translation, $key, $status);

  // Check if the job item has all the translated data that it needs now.
  // Only attempt to change the status to needs review if it is currently
  // active.
  if ($this
    ->isActive()) {
    $data = \Drupal::service('tmgmt.data')
      ->filterTranslatable($this
      ->getData());
    $finished = TRUE;
    foreach ($data as $item) {
      if (empty($item['#status']) || $item['#status'] == TMGMT_DATA_ITEM_STATE_PENDING || $item['#status'] == TMGMT_DATA_ITEM_STATE_PRELIMINARY) {
        $finished = FALSE;
        break;
      }
    }
    if ($finished && $this
      ->getJob()
      ->hasTranslator()) {

      // There are no unfinished elements left.
      if ($this
        ->getJob()
        ->getTranslator()
        ->isAutoAccept()) {

        // If the job item is going to be auto-accepted, set to review without
        // a message.
        $this
          ->needsReview(FALSE);
      }
      else {

        // Otherwise, create a message that contains source label, target
        // language and links to the review form.
        $job_url = $this
          ->getJob()
          ->toUrl()
          ->toString();
        $variables = array(
          '@source' => $this
            ->getSourceLabel(),
          '@language' => $this
            ->getJob()
            ->getTargetLanguage()
            ->getName(),
          ':review_url' => $this
            ->toUrl('canonical', array(
            'query' => array(
              'destination' => $job_url,
            ),
          ))
            ->toString(),
        );
        !$this
          ->getSourceUrl() ? $variables[':source_url'] = (string) $job_url : ($variables[':source_url'] = $this
          ->getSourceUrl()
          ->toString());
        $this
          ->needsReview('The translation of <a href=":source_url">@source</a> to @language is finished and can now be <a href=":review_url">reviewed</a>.', $variables);
      }
    }
  }
  $this
    ->save();
}