View source
<?php
namespace Drupal\tmgmt_smartling\Plugin\tmgmt_file\Format;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\tmgmt\JobInterface;
use Drupal\tmgmt_file\Plugin\tmgmt_file\Format\Html;
use Drupal\tmgmt\Data;
class Xml extends Html {
private $moduleHandler;
public function __construct() {
$this->moduleHandler = \Drupal::moduleHandler();
}
public function export(JobInterface $job, $conditions = []) {
$items = [];
foreach ($job
->getItems($conditions) as $item) {
$data = \Drupal::service('tmgmt.data')
->filterTranslatable($item
->getData());
foreach ($data as $key => $value) {
$items[$item
->id()][$this
->encodeIdSafeBase64($item
->id() . '][' . $key)] = $value + [
'sl-variant' => $item
->getItemType() . '-' . $item
->getItemId() . '-' . $key,
];
}
}
$this->moduleHandler
->alter('tmgmt_smartling_xml_file_export_data', $items);
$variables = [
'items' => $items,
];
$theme_registry = theme_get_registry();
$info = $theme_registry['tmgmt_smartling_xml_template'];
$template_file = $info['template'] . '.html.twig';
if (isset($info['path'])) {
$template_file = $info['path'] . '/' . $template_file;
}
return $this
->escapePluralStringDelimiter(\Drupal::service('twig')
->loadTemplate($template_file)
->render($variables));
}
public function validateImport($imported_file, $job = TRUE) {
$xml = simplexml_load_file($imported_file);
if (!$xml) {
return FALSE;
}
return TRUE;
}
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 = [];
$lock_fields_manager = \Drupal::getContainer()
->get('tmgmt_smartling.lock_fields_manager');
foreach ($xml
->xpath("//div[@class='atom']|//span[@class='atom']") as $atom) {
$key = $this
->decodeIdSafeBase64((string) $atom['id']);
$data[$key]['#text'] = (string) $atom;
$sl_variant_data = $this
->parseSmartlingSlVariantKey((string) $atom['sl-variant']);
$lang_code = $job
->getTargetLangcode();
if ($sl_variant_data) {
$locked_fields = $lock_fields_manager
->getLockedFieldsByContentEntityData($sl_variant_data['entity_type'], $sl_variant_data['entity_id'], $lang_code);
if (in_array($sl_variant_data['field_name'], $locked_fields)) {
$data[$key]['#text'] = $lock_fields_manager
->getLockedFieldValue($sl_variant_data['entity_type'], $sl_variant_data['entity_id'], $lang_code, $sl_variant_data['field_name'], $sl_variant_data['field_index'], $sl_variant_data['field_value_name']);
}
}
if ($atom
->getName() == 'span') {
$data[$key]['#text'] = html_entity_decode($data[$key]['#text']);
}
$data[$key]['#text'] = $this
->unEscapePluralStringDelimiter($data[$key]['#text']);
}
$this->moduleHandler
->alter('tmgmt_smartling_xml_file_import_data', $data);
return \Drupal::service('tmgmt.data')
->unflatten($data);
}
protected function parseSmartlingSlVariantKey($sl_variant) {
$sl_variant = explode('-', $sl_variant);
if (!isset($sl_variant[0]) || !isset($sl_variant[1]) || !isset($sl_variant[2])) {
return [];
}
$field_array = explode(Data::TMGMT_ARRAY_DELIMITER, $sl_variant[2]);
if (!isset($field_array[0]) || !isset($field_array[1]) || !isset($field_array[2])) {
return [];
}
return [
'entity_type' => $sl_variant[0],
'entity_id' => $sl_variant[1],
'field_name' => $field_array[0],
'field_index' => $field_array[1],
'field_value_name' => $field_array[2],
];
}
protected function escapePluralStringDelimiter($string) {
return preg_replace("/\3/", "!PLURAL_STRING_DELIMITER", $string);
}
protected function unEscapePluralStringDelimiter($string) {
return preg_replace("/!PLURAL_STRING_DELIMITER/", "\3", $string);
}
public function decodeIdSafeBase64($data) {
return parent::decodeIdSafeBase64($data);
}
public function encodeIdSafeBase64($data) {
return parent::encodeIdSafeBase64($data);
}
}