Skip to content

Instantly share code, notes, and snippets.

@AlexanderKomkov
Last active April 10, 2020 00:50
Show Gist options
  • Save AlexanderKomkov/d1ac0823229c79d0056c to your computer and use it in GitHub Desktop.
Save AlexanderKomkov/d1ac0823229c79d0056c to your computer and use it in GitHub Desktop.
Modx Revolution, сниппет вывода JSON через $pdoTools->getChunk(). Условие можно сделать только 1. Оператор ==.
<?php
$time = microtime(true);
$json = $modx->getOption('json', $scriptProperties, '');
$json = !empty($json) ? $modx->fromJSON($json) : array();
$log = $modx->getOption('showLog', $scriptProperties, '');
$tpl = $modx->getOption('tpl', $scriptProperties, '');
$where = $modx->getOption('where', $scriptProperties, '');
$where = !empty($where) ? $modx->fromJSON($where) : array();
$textlog = '';
$fqn = $modx->getOption('pdoFetch.class', null, 'pdotools.pdofetch', true);
if ($pdoClass = $modx->loadClass($fqn, '', false, true)) {
$pdoTools = new $pdoClass($modx, $scriptProperties);
}
elseif ($pdoClass = $modx->loadClass($fqn, MODX_CORE_PATH . 'components/pdotools/model/', false, true)) {
$pdoTools = new $pdoClass($modx, $scriptProperties);
}
else {
$modx->log(modX::LOG_LEVEL_ERROR, 'Could not load pdoFetch from "MODX_CORE_PATH/components/pdotools/model/".');
return '';
}
$output = '';
if (count($json) > 0) {
$items = array();
$countWhere = count($where);
foreach($json as $k => $v) {
if ($countWhere === 1) {
foreach ($where as $key => $value) {
if ($v[$key] == $value) {
$items[] = $json[$k];
}
}
} else {
$items[] = $json[$k];
}
}
if (!empty($tpl)) {
foreach($items as $k => $v) {
$output .= $pdoTools->getChunk($tpl, $items[$k]);
}
}
else {
echo "<pre>".print_r($items, true)."</pre>";
}
}
$textlog .= "<pre>";
$textlog .= "Total time: " . number_format(round(microtime(true) - $time, 7), 7) . "<br/>";
$textlog .= "</pre>";
if ($log == 1) echo $textlog;
return $output;
?>
[[!getListFromJson?
&json=`[[*mysjon]]`
&tpl=`mytpl`
&where=`{"publiched":"1"}`
]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment