Skip to content

Instantly share code, notes, and snippets.

@jmaddington
Last active September 17, 2022 02:49
Show Gist options
  • Save jmaddington/5475400 to your computer and use it in GitHub Desktop.
Save jmaddington/5475400 to your computer and use it in GitHub Desktop.
Quick snippet to set placeholders from a JSON array. Made basically so I could create MIGXs inside MIGXS. There is little error checking are anything else at this point. Use at your own risk.
<?php
/**
* jsonToPlaceholders snippet for JM Customer Database extra
*
* Copyright 2013 by JM Addington [email protected]
* Created on 04-28-2013
*
* @package jmc
*/
/**
* 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);
$skipFirstLevel = $modx->getOption('skipFirstLevel', $scriptProperties, false);
$debug = $modx->getOption('debug', $scriptProperties, false);
$idxPrefix = $modx->getOption('idxPrefix', $scriptProperties, '');
$output = '';
$phpArray = $modx->fromJSON($json);
$phpArray = $skipFirstLevel? $phpArray[0] : $phpArray;
$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) {
$output .= $c->process($phpArray, $row);
}
} else {
if ($chunkName != "") {
foreach ($phpArray as $row) {
$row = array_merge($row, array('idx' => $idx));
$row = array_merge($row, array("$idxPrefix" => $row["idx"]));
$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