You are here

public function AcquiaCohesionDataConverter::findAndConvertCohesionXmlDataToCohesionJsonData in TMGMT Translator Smartling 8.4

Converts TMGMT Smartling's XML data items to Cohesion's JSON components.

Parameters

array $data:

Return value

array

File

modules/tmgmt_smartling_acquia_cohesion/src/AcquiaCohesionDataConverter.php, line 115

Class

AcquiaCohesionDataConverter
Class AcquiaCohesionDataConverter @package Drupal\tmgmt_smartling_acquia_cohesion

Namespace

Drupal\tmgmt_smartling_acquia_cohesion

Code

public function findAndConvertCohesionXmlDataToCohesionJsonData(array $data) {
  $newData = [];
  $cohesionData = [];

  // Grab Cohesion's fields from XML data items.
  foreach ($data as $dataKey => $dataValue) {
    if ($this
      ->isCohesionDataKey($dataKey)) {
      $tmgmtKey = $this
        ->getTmgmtKeyFromDataKey($dataKey);
      $cohesionKey = $this
        ->getCohesionKeyFromDataKey($dataKey);
      if (isset($tmgmtKey) && isset($cohesionKey)) {
        $cohesionData[$tmgmtKey][$cohesionKey] = $dataValue;
      }
      else {
        $this->logger
          ->warning('Got invalid dataKey="@dataKey" from xml file: tmgmtKey="@tmgmtKey" or/and cohesionKey="@cohesionKey" is/are invalid. Skipping applying translation for cohesion field', [
          '@dataKey' => $dataKey,
          '@tmgmtKey' => $tmgmtKey,
          '@cohesionKey' => $cohesionKey,
        ]);
      }
    }
    else {
      $newData[$dataKey] = $dataValue;
    }
  }

  // Turn Cohesion's XML data items back to Cohesion's JSON.
  foreach ($cohesionData as $tmgmtKey => $cohesionItems) {
    $jobItem = $this
      ->getJobItemFromTmgmtKey($tmgmtKey);
    if (empty($jobItem)) {
      $this->logger
        ->warning('Failed to load TMGMT Job Item by tmgmtKey="@tmgmtKey". Skipping applying translation for the cohesion fields', [
        '@tmgmtKey' => $tmgmtKey,
      ]);
      continue;
    }
    $layoutCanvas = $this
      ->getLayoutCanvasFromJobItem($jobItem, $tmgmtKey);
    if (empty($layoutCanvas)) {
      $this->logger
        ->warning('Failed to load Layout Canvas by tmgmtKey="@tmgmtKey". Skipping applying translation for the cohesion fields', [
        '@tmgmtKey' => $tmgmtKey,
      ]);
      continue;
    }
    foreach ($cohesionItems as $cohesionItemKey => $cohesionItemData) {

      // Cohesion's key: "model(0):<model_uid>(1):property(2):<property_uid>(3)".
      $explodedCohesionKey = explode(":", $cohesionItemKey);
      $modelUid = isset($explodedCohesionKey[1]) ? $explodedCohesionKey[1] : null;
      $propertyUid = isset($explodedCohesionKey[3]) ? $explodedCohesionKey[3] : null;
      $text = $cohesionItemData["#text"];
      foreach ($layoutCanvas
        ->iterateModels() as $model) {
        if ($model
          ->getUUID() === $modelUid) {
          $property = $model
            ->getProperty($propertyUid);
          if ($this
            ->isCohesionPropertyHtmlText($property)) {
            $this->logger
              ->info('Importing Acquia Cohesion rich text field: model=@modelUid property=@propertyUid', [
              '@modelUid' => $modelUid,
              '@propertyUid' => $propertyUid,
            ]);
            $property->text = $text;
          }
          else {
            $this->logger
              ->info('Importing Acquia Cohesion plain text field: model=@modelUid property=@propertyUid', [
              '@modelUid' => $modelUid,
              '@propertyUid' => $propertyUid,
            ]);
            $property = $text;
          }
          $model
            ->setProperty($propertyUid, $property);
        }
      }
    }
    $newData[$tmgmtKey]["#text"] = json_encode($layoutCanvas
      ->jsonSerialize());
  }
  return $newData;
}