You are here

class Xml in TMGMT Translator Smartling 8

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
  2. 8.2 src/Plugin/tmgmt_file/Format/Xml.php \Drupal\tmgmt_smartling\Plugin\tmgmt_file\Format\Xml
  3. 8.3 src/Plugin/tmgmt_file/Format/Xml.php \Drupal\tmgmt_smartling\Plugin\tmgmt_file\Format\Xml

Export into HTML.

Plugin annotation


@FormatPlugin(
  id = "xml",
  label = @Translation("XML")
)

Hierarchy

Expanded class hierarchy of Xml

File

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

Namespace

Drupal\tmgmt_smartling\Plugin\tmgmt_file\Format
View source
class Xml extends Html {

  /**
   * {@inheritdoc}.
   */
  public function export(JobInterface $job, $conditions = []) {

    // Export job items data without relation to their ids.
    $items = [];
    foreach ($job
      ->getItems($conditions) as $item) {
      $data = \Drupal::service('tmgmt.data')
        ->filterTranslatable($item
        ->getData());
      foreach ($data as $key => $value) {

        // TODO: identical filename task.
        // $items[$item->id()][$this->encodeIdSafeBase64($item->getItemType() . ':' . $item->getItemId() . '][' . $key)] = $value;
        $items[$item
          ->id()][$this
          ->encodeIdSafeBase64($item
          ->id() . '][' . $key)] = $value;
      }
    }
    $elements = [
      '#theme' => 'tmgmt_smartling_xml_template',
      '#items' => $items,
    ];
    return \Drupal::service('renderer')
      ->renderPlain($elements);
  }

  /**
   * {@inheritdoc}
   */
  public function validateImport($imported_file, $job = TRUE) {
    $xml = simplexml_load_file($imported_file);
    if (!$xml) {
      return FALSE;
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function import($imported_file, $job = 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']);
      }
    }
    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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Html::decodeIdSafeBase64 protected function Returns decoded id safe base64 data.
Html::encodeIdSafeBase64 protected function Returns base64 encoded data that is safe for use in xml ids.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
Xml::export public function . Overrides Html::export
Xml::import public function Implements TMGMTFileExportInterface::import(). Overrides Html::import
Xml::validateImport public function Validates that the given file is valid and can be imported. Overrides Html::validateImport