Skip to content

Instantly share code, notes, and snippets.

@bwente
Last active January 11, 2022 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwente/8781c50399d858f06440496dbfee5dba to your computer and use it in GitHub Desktop.
Save bwente/8781c50399d858f06440496dbfee5dba to your computer and use it in GitHub Desktop.
Snippet to read and parse XML input
<?php
# Snippet to read and parse XML input
$sourcePath = $scriptProperties['source'];
if (is_numeric($sourcePath))
{
$sourcePath = $modx->makeUrl($sourcePath, '', '', 'full');
}
//$output = array();
$outputSeparator = isset($outputSeparator) ? $outputSeparator : "\n";
$getGroup = $modx->getOption('getGroup', $scriptProperties, '');
$cacheKey = basename(str_replace(".xml","",$sourcePath));
$lifetime = $modx->getOption('lifetime', $scriptProperties, 10800);
$source = $modx->cacheManager->get($cacheKey);
$nodeName = $modx->getOption('nodeName', $scriptProperties, 'ROW');
$itemNode = $modx->getOption('itemNode', $scriptProperties, '');
$group = $modx->getOption('group', $scriptProperties, '');
$xpath = $modx->getOption('xpath', $scriptProperties, '//' . $nodeName);
$groupName = $modx->getOption('groupName', $scriptProperties, 'GROUP');
$idxOffset = (int) $modx->getOption('idxOffset', $scriptProperties, 0);
$hideProduct = $scriptProperties['hideProduct'];
$modx->setPlaceholder('hideProduct', $hideProduct);
if ($getGroup)
{
$xpath = $xpath . '[@' . $groupName . '="' . $getGroup . '"]';
}
$modx->setPlaceholder($groupName, $group);
$modx->setPlaceholder('modDate', strtotime("now"));
$modx->setPlaceholder('UPDATE',$xml->RATE_UPLOAD_DATE);
if ($source)
{
$modx->log(modX::LOG_LEVEL_DEBUG, 'XMLooper using cached ' . $cacheKey . '.');
}
else
{
$source = file_get_contents($sourcePath);
// $ch = curl_init();
// curl_setopt($ch, CURLOPT_URL, $sourcePath);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
// curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// $source = curl_exec($ch);
// curl_close($ch);
$modx->cacheManager->set($cacheKey, $source, $lifetime);
$modx->log(modX::LOG_LEVEL_DEBUG, 'XMLooper creating cache ' . $cacheKey . '.');
}
if (empty($source))
{
$modx->log(modX::LOG_LEVEL_DEBUG, 'Empty source address passed, aborting.');
return '';
}
else
{
if ($xml = simplexml_load_string($source))
{
$output = '';
$output = array();
$i = 0;
$n = 0;
$idx = 0;
$nodeList = explode(',', $itemNode);
$nodes = $xml->xpath($xpath);
//print "<pre>";
//print_r($nodes);
//print "</pre>";
foreach ($nodes as $item)
{
$modx->setPlaceholder('idxgroup', ($idx+1));
$attributes = [];
if ($item->attributes())
{
$attributes = (array)$item->attributes();
$attributes = current($item->attributes());
}
$elements = [];
foreach ($item->children() as $element)
{
$elements[$element->getName() ] = is_array($element) ? simplexml_to_array($element) : (string)$element;
}
$placeholders = array_merge($attributes, $elements);
if (isset($scriptProperties['itemNode']))
{
if (isset($nodeList[$n]) && $nodeList[$n] == $i)
{
//$output .= $modx->getChunk($scriptProperties['tpl'], $placeholders);
$output[] = $modx->getChunk($scriptProperties['tpl'], $placeholders);
++$n;
++$idx;
}
}
else
{
//$output .= $modx->getChunk($scriptProperties['tpl'], $placeholders);
$output[] = $modx->getChunk($scriptProperties['tpl'], $placeholders);
++$idx;
}
++$i;
$modx->setPlaceholder('idx', ($idx + $idxOffset));
}
}
else
{
$modx->log(modX::LOG_LEVEL_ERROR, 'XMLooper could not open ' . $source . '.');
// exit('Could not open ' . $source . '.');
}
return implode($outputSeparator, $output);
//return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment