You are here

public function Xml::import in TMGMT Translator Smartling 8.3

Same name and namespace in other branches
  1. 8.4 src/Plugin/tmgmt_file/Format/Xml.php \Drupal\tmgmt_smartling\Plugin\tmgmt_file\Format\Xml::import()
  2. 8 src/Plugin/tmgmt_file/Format/Xml.php \Drupal\tmgmt_smartling\Plugin\tmgmt_file\Format\Xml::import()
  3. 8.2 src/Plugin/tmgmt_file/Format/Xml.php \Drupal\tmgmt_smartling\Plugin\tmgmt_file\Format\Xml::import()

Implements TMGMTFileExportInterface::import().

Overrides Html::import

File

src/Plugin/tmgmt_file/Format/Xml.php, line 73

Class

Xml
Export into XML.

Namespace

Drupal\tmgmt_smartling\Plugin\tmgmt_file\Format

Code

public function import($imported_file, $job = TRUE) {
  libxml_use_internal_errors(true);
  $dom = new \DOMDocument();
  $dom
    ->loadHTMLFile($imported_file);
  $xml = simplexml_import_dom($dom);
  $data = [];

  // Get job items data from xml.
  foreach ($xml
    ->xpath("//div[@class='atom']|//span[@class='atom']") as $atom) {

    // Assets are our strings (eq fields in nodes).
    $key = $this
      ->decodeIdSafeBase64((string) $atom['id']);
    $data[$key]['#text'] = (string) $atom;

    // If we have some markup in plain text fields we need to decode it.
    if ($atom
      ->getName() == 'span') {
      $data[$key]['#text'] = html_entity_decode($data[$key]['#text']);
    }
    $data[$key]['#text'] = $this
      ->unEscapePluralStringDelimiter($data[$key]['#text']);
  }
  return \Drupal::service('tmgmt.data')
    ->unflatten($data);

  // TODO: identical filename task.
  // Map job items from xml to job items from a given job.
  // $result = [];
  // $data = \Drupal::service('tmgmt.data')->unflatten($data);
  //
  // foreach ($data as $data_key => $data_item) {
  //   $conditions = explode(':', $data_key);
  //   $job_item = $job->getItems([
  //     'item_type' => $conditions[0],
  //     'item_id' => $conditions[1],
  //   ]);
  //   $job_item = reset($job_item);
  //
  //   if (!empty($job_item)) {
  //     $result[$job_item->id()] = $data_item;
  //   }
  // }
  //
  // return $result;
}