230 lines
7.0 KiB
PHP
230 lines
7.0 KiB
PHP
<?php
|
|
|
|
/**
|
|
* File from http://PrestaShow.pl
|
|
*
|
|
* DISCLAIMER
|
|
* Do not edit or add to this file if you wish to upgrade this module to newer
|
|
* versions in the future.
|
|
*
|
|
* @authors PrestaShow.pl <kontakt@prestashow.pl>
|
|
* @copyright 2015 PrestaShow.pl
|
|
* @license http://PrestaShow.pl/license
|
|
*/
|
|
class PShow_Xml
|
|
{
|
|
|
|
public static function getFileRowsToMatchData($file_path, $limit = 0)
|
|
{
|
|
$contents = '';
|
|
$handle = fopen($file_path, "r");
|
|
while (!feof($handle)) {
|
|
$contents .= fgets($handle);
|
|
|
|
if ($limit > 0 && Tools::strlen($contents) >= $limit) {
|
|
break;
|
|
}
|
|
}
|
|
fclose($handle);
|
|
|
|
return $contents;
|
|
}
|
|
|
|
public static function formatXmlFile($file_path)
|
|
{
|
|
$domxml = new DOMDocument('1.0');
|
|
$domxml->preserveWhiteSpace = false;
|
|
$domxml->formatOutput = true;
|
|
$xml = self::getXmlFile($file_path);
|
|
|
|
if ($xml === false) {
|
|
return;
|
|
}
|
|
|
|
$domxml->loadXML($xml->asXML());
|
|
$domxml->save($file_path);
|
|
}
|
|
|
|
public static function getXmlFile($file_path)
|
|
{
|
|
return simplexml_load_file($file_path);
|
|
}
|
|
|
|
public static function removeCDATA($contents)
|
|
{
|
|
$contents = self::str_replace_with_split('<![CDATA[', "", $contents);
|
|
|
|
$contents = self::str_replace_with_split(']]>', "", $contents);
|
|
|
|
return $contents;
|
|
}
|
|
|
|
public static function htmlspecialchars($contents)
|
|
{
|
|
$contents = self::removeCDATA($contents);
|
|
|
|
$contents = htmlspecialchars($contents);
|
|
|
|
$contents = self::str_replace_with_split("> <", "><", $contents);
|
|
|
|
$contents = self::str_replace_with_split("> <", "><", $contents);
|
|
|
|
$contents = self::str_replace_with_split("> <", "><", $contents);
|
|
|
|
$contents = self::str_replace_with_split("><", "><br><", $contents);
|
|
|
|
return $contents;
|
|
}
|
|
|
|
/**
|
|
* getRows()
|
|
*
|
|
* @param string path to the file
|
|
* @param char csv delimiter
|
|
* @param int count of rows to get
|
|
* @return array rows ffile
|
|
*/
|
|
public static function getRows($file_path, $file_config, $start_from = 0, $rows_count = 3)
|
|
{
|
|
if (!file_exists($file_path)) {
|
|
return false;
|
|
}
|
|
|
|
$parser = PShow_Xml_Parser::getInstance($file_path);
|
|
$elements = $parser->getElementsByPath($file_config[1]['objectTag']);
|
|
$results = array();
|
|
|
|
if (($start_from + $rows_count) > count($elements))
|
|
$rows_count = count($elements) - $start_from;
|
|
|
|
for ($i = $start_from - 1; $i < $rows_count; ++$i) {
|
|
$results[] = $elements[$i];
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param int $nextRow
|
|
* @return array
|
|
*/
|
|
public static function compareFileRowWithMatchedFields($nextRow)
|
|
{
|
|
$config = PShow_Config::getFileConfig(PShow_Import::getInstance()->filename);
|
|
$object = PShow_Xml_Parser::getInstance()->getElementByPath($config['matched']['objectTag'], $nextRow - 1);
|
|
$matches = array();
|
|
|
|
/**
|
|
* @param string $val
|
|
* @param int $cutStart
|
|
* @param int $cutEnd
|
|
* @param string $append
|
|
* @param string $prepend
|
|
*/
|
|
$fieldMods = function (&$val, $cutStart, $cutEnd, $append, $prepend, $replacement) use ($config) {
|
|
$val = substr($val, (int) $cutStart);
|
|
$val = substr($val, 0, (strlen($val) - (int) $cutEnd));
|
|
$val = $prepend . $val . $append;
|
|
|
|
foreach ($replacement as $r) {
|
|
if ($r['regex']) {
|
|
$val = preg_replace($r['find'], $r['replace'], $val);
|
|
} else {
|
|
$val = str_replace($r['find'], $r['replace'], $val);
|
|
}
|
|
}
|
|
|
|
if (array_key_exists('replacement', $config['additional'])) {
|
|
foreach ($config['additional']['replacement'] as $r) {
|
|
if ($r['regex']) {
|
|
$val = preg_replace($r['find'], $r['replace'], $val);
|
|
} else {
|
|
$val = str_replace($r['find'], $r['replace'], $val);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
foreach ($config['matched'] as $matchingType => $matchingData) {
|
|
foreach ($matchingData as $field) {
|
|
|
|
// if not matched with any prestashop field
|
|
if ($field['value'] === 'none') {
|
|
continue;
|
|
}
|
|
|
|
switch ($matchingType) {
|
|
case 'matches':
|
|
$val = PShow_Xml_Parser::getInstance()->getValueByPathAndObject($field['key'], $object);
|
|
|
|
foreach ($val as &$v) {
|
|
$value = (string) $v;
|
|
|
|
if (ctype_space($value) || empty($value)) {
|
|
$value = (string) $v->asXML();
|
|
|
|
$tags = explode('/', $field['key']);
|
|
$lastTag = end($tags);
|
|
|
|
$value = str_replace(
|
|
array('<' . $lastTag . '>', '<' . $lastTag . ' />', '</' . $lastTag . '>'), null, $value
|
|
);
|
|
}
|
|
|
|
$v = $value;
|
|
}
|
|
break;
|
|
case 'custommatches':
|
|
$val = array_map(function($x) {
|
|
return (string) $x;
|
|
}, $object->xpath('//' . $field['key']));
|
|
break;
|
|
case 'customvalues':
|
|
$val = $field['key'];
|
|
break;
|
|
}
|
|
|
|
if (!is_array($val)) {
|
|
$val = array($val);
|
|
}
|
|
|
|
if (array_key_exists('options', $field)) {
|
|
foreach ($val as &$v) {
|
|
$fieldMods($v, $field['options']['cutStart'], $field['options']['cutEnd'], $field['options']['append'], $field['options']['prepend'], $field['options']['replacement']);
|
|
}
|
|
}
|
|
|
|
list($fieldtype, $fieldname) = explode('.', $field['value']);
|
|
|
|
if (isset($matches[$fieldtype][$fieldname])) {
|
|
$matches[$fieldtype][$fieldname] = array_merge($matches[$fieldtype][$fieldname], $val);
|
|
} else {
|
|
$matches[$fieldtype][$fieldname] = $val;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $matches;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param type $find
|
|
* @param type $target
|
|
* @param type $string
|
|
* @return type
|
|
*/
|
|
public static function str_replace_with_split($find, $target, $string)
|
|
{
|
|
$split = explode("\n", $string);
|
|
$result = "";
|
|
|
|
foreach ($split as $part) {
|
|
$result .= str_replace($find, $target, $part);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
}
|