You are here

public function TMGMTJobItem::addTranslatedData in Translation Management Tool 7

Adds translated data to a job item.

This function calls for TMGMTJobItem::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 TMGMTJobItem::getCountPending() and TMGMTJobItem::getCountTranslated(). The catch is, that this counter gets updated while saveing which not yet hapened.

Parameters

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

$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.

File

entity/tmgmt.entity.job_item.inc, line 735

Class

TMGMTJobItem
Entity class for the tmgmt_job entity.

Code

public function addTranslatedData($translation, $key = array()) {
  $this
    ->addTranslatedDataRecursive($translation, $key);

  // 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 = tmgmt_flatten_data($this
      ->getData());
    $data = array_filter($data, '_tmgmt_filter_data');
    $finished = TRUE;
    foreach ($data as $item) {
      if (empty($item['#status']) || $item['#status'] == TMGMT_DATA_ITEM_STATE_PENDING) {
        $finished = FALSE;
        break;
      }
    }
    if ($finished) {

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

        // 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.
        $uri = $this
          ->uri();
        $job_uri = $this
          ->getJob()
          ->uri();
        $variables = array(
          '!source' => l($this
            ->getSourceLabel(), $uri['path']),
          '@language' => entity_metadata_wrapper('tmgmt_job', $this
            ->getJob())->target_language
            ->label(),
          '!review_url' => url($uri['path'], array(
            'query' => array(
              'destination' => $job_uri['path'],
            ),
          )),
        );
        $this
          ->needsReview('The translation of !source to @language is finished and can now be <a href="!review_url">reviewed</a>.', $variables);
      }
    }
  }
  $this
    ->save();
}