protected function JobItem::addTranslatedDataRecursive

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

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, TMGMT_DATA_ITEM_STATE_PRELIMINARY or TMGMT_DATA_ITEM_STATE_REVIEWED to set it to that value. Other statuses are not supported.

Throws

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

1 call to JobItem::addTranslatedDataRecursive()
JobItem::addTranslatedData in src/Entity/JobItem.php
Adds translated data to a job item.

File

src/Entity/JobItem.php, line 682

Class

JobItem
Entity class for the tmgmt_job_item entity.

Namespace

Drupal\tmgmt\Entity

Code

protected function addTranslatedDataRecursive(array $translation, $key = array(), $status = NULL) {
  if ($status != NULL && !in_array($status, [
    TMGMT_DATA_ITEM_STATE_PRELIMINARY,
    TMGMT_DATA_ITEM_STATE_TRANSLATED,
    TMGMT_DATA_ITEM_STATE_REVIEWED,
  ])) {
    new TMGMTException('Unsupported status given.');
  }
  if (isset($translation['#text']) || isset($translation['#file'])) {
    $translation_key = isset($translation['#text']) ? '#text' : '#file';
    $data_service = \Drupal::service('tmgmt.data');
    $data = $this
      ->getData($data_service
      ->ensureArrayKey($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'][$translation_key]) && $data['#translation'][$translation_key] == $translation[$translation_key] && $translation['#origin'] != 'remote') {
        return;
      }

      // In case the timestamp is not set consider it to be now.
      if (!isset($translation['#timestamp'])) {
        $translation['#timestamp'] = \Drupal::time()
          ->getRequestTime();
      }

      // If we have a translation text and is different from new one create
      // revision.
      // @todo support file revisions?
      if (isset($translation['#text']) && !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' => $data_service
              ->ensureStringKey($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' => $data_service
              ->ensureStringKey($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
              ->getChangedTime(),
          );

          // Add a message if the translation update is from remote.
          if ($translation['#origin'] == 'remote') {
            $diff = mb_strlen($translation['#text']) - mb_strlen($data['#translation']['#text']);
            $this
              ->addMessage('Updated translation for key @key, size difference: @diff characters.', array(
              '@key' => $data_service
                ->ensureStringKey($key),
              '@diff' => $diff,
            ));
          }
        }
      }
      if ($status == NULL) {
        if (isset($data['#status']) && $data['#status'] == TMGMT_DATA_ITEM_STATE_PRELIMINARY) {
          $status = TMGMT_DATA_ITEM_STATE_PRELIMINARY;
        }
        else {
          $status = TMGMT_DATA_ITEM_STATE_TRANSLATED;
        }
      }
      $values = array(
        '#translation' => $translation,
        '#status' => $status,
      );
      $this
        ->updateData($key, $values);
    }
    return;
  }
  foreach (Element::children($translation) as $item) {
    $this
      ->addTranslatedDataRecursive($translation[$item], array_merge($key, array(
      $item,
    )), $status);
  }
}