Skip to content

Instantly share code, notes, and snippets.

@bwente
Created January 11, 2022 17:54
Show Gist options
  • Save bwente/f1486ac7371fea0d2d64901ee5eef68d to your computer and use it in GitHub Desktop.
Save bwente/f1486ac7371fea0d2d64901ee5eef68d to your computer and use it in GitHub Desktop.
JSONtoPlaceholders
<?php
/**
* jsonToPlaceholders snippet for JM Customer Database extra
*
* Copyright 2013 by JM Addington jm@jmaddington.com
* Created on 04-28-2013
*
* @package jmc
*
* modified by bwente
*/
/**
* Description
* -----------
* Sets a JSON array to placeholders
*
* Variables
* ---------
* @var $modx modX
* @var $scriptProperties array
* @var $json string JSON Array to parse
* @var $tpl string,optional Name of chunk to use a template
* @var $code string,optional Use as template instead of chunk
* @var $ph string,optional Placeholder to set instead of simply returning parsed content
* @var $skipFirstLevel boolean,optional If set to true, skips the first level of the array. Basically for MIGX support
* @var $loop boolean,optional If set to true, loops through the first array level and parses
* @var $idxPrefix boolean,optional If set prefixes the idx placeholder with the given value
* @var $debug boolean,optional If set to true dump $scriptProperties
*
* @package jmc
**/
$json = $modx->getOption('json', $scriptProperties);
$chunkName = $modx->getOption('tpl', $scriptProperties);
$code = $modx->getOption('code', $scriptProperties);
$ph = $modx->getOption('ph', $scriptProperties);
$loop = $modx->getOption('loop', $scriptProperties, false);
$startLevel = $modx->getOption('startLevel', $scriptProperties, '');
$skipFirstLevel = $modx->getOption('skipFirstLevel', $scriptProperties, false);
$limit = $modx->getOption('limit', $scriptProperties, 25);
$debug = $modx->getOption('debug', $scriptProperties, false);
$idxPrefix = $modx->getOption('idxPrefix', $scriptProperties, '');
$payload = $modx->getOption('payload', $scriptProperties, '');
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n",
'method' => "POST",
'content' => $payload
)
);
$context = stream_context_create($options);
$output = '';
if($json[0] != "{"){
if ($payload) {
$jsonPath = file_get_contents($json, false, $context);
} else {
$jsonPath = file_get_contents($json);
}
//$jsonPath = file_get_contents($json, false, $context);
$phpArray = json_decode($jsonPath, true);
}else{
$phpArray = $modx->fromJSON($json);
}
$phpArray = $startLevel? $phpArray[$startLevel] : $phpArray;
$phpArray = $skipFirstLevel? $phpArray[0] : $phpArray;
$modx->setPlaceholder('total', count($phpArray));
$phpArray = array_slice($phpArray, 0, $limit);
$idx = 1;
//If we aren't looping, just process the array like normal, setting placeholders directly.
if (!$loop) {
if ($code != "") {
//Create a temporary chunk for the code
$uniqid = uniqid();
$c = $modx->newObject('modChunk', array('name' => "{tmp}-{$uniqid}"));
$c->setCacheable(false);
$output = $c->process($phpArray, $code);
} else {
if ($chunkName != "") {
$output = $modx->getChunk($chunkName, $phpArray);
}
}
} else {
//If we are looping, well, loop!
if ($code != "") {
//Create a temporary chunk for the code
$uniqid = uniqid();
$c = $modx->newObject('modChunk', array('name' => "{tmp}-{$uniqid}"));
$c->setCacheable(false);
foreach ($phpArray as $row) {
$modx->setPlaceholder('idx', $idx);
$output .= $c->process($row, $code);
$modx->log(modX::LOG_LEVEL_ERROR, 'JSON to ' . print_r($row, true));
$idx++;
}
} else {
if ($chunkName != "") {
foreach ($phpArray as $row) {
$row = array_merge($row, array('idx' => $idx));
$row = array_merge($row, array("$idxPrefix" => $row["idx"]));
$modx->log(modX::LOG_LEVEL_ERROR, 'JSON to ' . print_r($row, true));
$output .= $modx->getChunk($chunkName, $row);
$idx++;
}
}
}
}
if ($debug){
$output .= 'ScriptProperties: ' . print_r($scriptProperties, true);
$output .= 'phpArray: ' .print_r($phpArray, true);
}
if ($chunkName == "" && $code == "") {
$output .= print_r($phpArray, true);
}
if ($ph != "") {
$modx->setPlaceholder($ph, $output);
} else {
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment