You are here

protected function TMGMTJobItem::addTranslatedDataRecursive in Translation Management Tool 7

Recursively writes translated data to the data array of a job item.

While doing this the #status of each data item is set to TMGMT_DATA_ITEM_STATE_TRANSLATED.

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.

1 call to TMGMTJobItem::addTranslatedDataRecursive()
TMGMTJobItem::addTranslatedData in entity/tmgmt.entity.job_item.inc
Adds translated data to a job item.

File

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

Class

TMGMTJobItem
Entity class for the tmgmt_job entity.

Code

protected function addTranslatedDataRecursive($translation, $key = array()) {
  if (isset($translation['#text'])) {
    $data = $this
      ->getData(tmgmt_ensure_keys_array($key));
    if (empty($data['#status']) || $data['#status'] != TMGMT_DATA_ITEM_STATE_ACCEPTED) {

      // In case the origin is not set consider it to be remote.
      if (!isset($translation['#origin'])) {
        $translation['#origin'] = 'remote';
      }

      // If we already have a translation text and it hasn't changed, don't
      // update anything unless the origin is remote.
      if (!empty($data['#translation']['#text']) && $data['#translation']['#text'] == $translation['#text'] && $translation['#origin'] != 'remote') {
        return;
      }

      // In case the timestamp is not set consider it to be now.
      if (!isset($translation['#timestamp'])) {
        $translation['#timestamp'] = REQUEST_TIME;
      }

      // If we have a translation text and is different from new one create
      // revision.
      if (!empty($data['#translation']['#text']) && $data['#translation']['#text'] != $translation['#text']) {

        // Copy into $translation existing revisions.
        if (!empty($data['#translation']['#text_revisions'])) {
          $translation['#text_revisions'] = $data['#translation']['#text_revisions'];
        }

        // If current translation was created locally and the incoming one is
        // remote, do not override the local, just create a new revision.
        if (isset($data['#translation']['#origin']) && $data['#translation']['#origin'] == 'local' && $translation['#origin'] == 'remote') {
          $translation['#text_revisions'][] = array(
            '#text' => $translation['#text'],
            '#origin' => $translation['#origin'],
            '#timestamp' => $translation['#timestamp'],
          );
          $this
            ->addMessage('Translation for customized @key received. Revert your changes if you wish to use it.', array(
            '@key' => tmgmt_ensure_keys_string($key),
          ));

          // Unset text and origin so that the current translation does not
          // get overridden.
          unset($translation['#text'], $translation['#origin'], $translation['#timestamp']);
        }
        elseif ($translation['#origin'] == 'remote' && !empty($data['#status']) && $data['#status'] == TMGMT_DATA_ITEM_STATE_REVIEWED) {
          $translation['#text_revisions'][] = array(
            '#text' => $translation['#text'],
            '#origin' => $translation['#origin'],
            '#timestamp' => $translation['#timestamp'],
          );
          $this
            ->addMessage('Translation for already reviewed @key received and stored as a new revision. Revert to it if you wish to use it.', array(
            '@key' => tmgmt_ensure_keys_string($key),
          ));

          // Unset text and origin so that the current translation does not
          // get overridden.
          unset($translation['#text'], $translation['#origin'], $translation['#timestamp']);
        }
        else {
          $translation['#text_revisions'][] = array(
            '#text' => $data['#translation']['#text'],
            '#origin' => isset($data['#translation']['#origin']) ? $data['#translation']['#origin'] : 'remote',
            '#timestamp' => isset($data['#translation']['#timestamp']) ? $data['#translation']['#timestamp'] : $this->changed,
          );

          // Add a message if the translation update is from remote.
          if ($translation['#origin'] == 'remote') {
            $diff = drupal_strlen($translation['#text']) - drupal_strlen($data['#translation']['#text']);
            $this
              ->addMessage('Updated translation for key @key, size difference: @diff characters.', array(
              '@key' => tmgmt_ensure_keys_string($key),
              '@diff' => $diff,
            ));
          }
        }
      }
      $values = array(
        '#translation' => $translation,
        '#status' => TMGMT_DATA_ITEM_STATE_TRANSLATED,
      );
      $this
        ->updateData($key, $values);
    }
    return;
  }
  foreach (element_children($translation) as $item) {
    $this
      ->addTranslatedDataRecursive($translation[$item], array_merge($key, array(
      $item,
    )));
  }
}