pepebe
10/13/2014 - 11:26 AM

pdoResources_json - Return a json string containing resource contents including tvs.

pdoResources_json - Return a json string containing resource contents including tvs.

<?php
/*
pdoResources_json
Return a json string containing resource contents including tvs.

source: http://forums.modx.com/thread/94631/get-results-of-article-in-modx-in-json-format#dis-post-512500
author: http://forums.modx.com/u/Bruno17

&parents=`1` Parents to return children from
returnIds=`1` ???

UNTESTED! Seemed to be worth adding to my gists. ;)

*/

/* @var array $scriptProperties */
 
if (isset($parents) && $parents === '') {
    $scriptProperties['parents'] = $modx->resource->id;
}
if (!empty($returnIds)) {
    $scriptProperties['return'] = 'ids';
}
 
// Adding extra parameters into special place so we can put them in results
/** @var modSnippet $snippet */
$additionalPlaceholders = array();
if ($snippet = $modx->getObject('modSnippet', array('name' => 'pdoResources'))) {
    $properties = unserialize($snippet->properties);
    foreach ($scriptProperties as $k => $v) {
        if (!isset($properties[$k])) {
            $additionalPlaceholders[$k] = $v;
        }
    }
}
$scriptProperties['additionalPlaceholders'] = $additionalPlaceholders;
 
$scriptProperties['return'] = 'data'; //return results as array
 
/* @var pdoFetch $pdoFetch */
$fqn = $modx->getOption('pdoFetch.class', null, 'pdotools.pdofetch', true);
if ($pdoClass = $modx->loadClass($fqn, '', false, true)) {
    $pdoFetch = new $pdoClass($modx, $scriptProperties);
}
elseif ($pdoClass = $modx->loadClass($fqn, MODX_CORE_PATH . 'components/pdotools/model/', false, true)) {
    $pdoFetch = new $pdoClass($modx, $scriptProperties);
}
else {
    $modx->log(modX::LOG_LEVEL_ERROR, 'Could not load pdoFetch from "MODX_CORE_PATH/components/pdotools/model/".');
    return false;
}
$pdoFetch->addTime('pdoTools loaded');
$output = $pdoFetch->run();
 
$output = $modx->toJson($output); // convert array to JSON
 
$log = '';
if ($modx->user->hasSessionContext('mgr') && !empty($showLog)) {
    $log .= '<pre class="pdoResourcesLog">' . print_r($pdoFetch->getTime(), 1) . '</pre>';
}
 
// Return output
if (!empty($returnIds)) {
    $modx->setPlaceholder('pdoResources.log', $log);
    if (!empty($toPlaceholder)) {
        $modx->setPlaceholder($toPlaceholder, $output);
    }
    else {
        return $output;
    }
}
elseif (!empty($toSeparatePlaceholders)) {
    $output['log'] = $log;
    $modx->setPlaceholders($output, $toSeparatePlaceholders);
}
else {
    $output .= $log;
 
    if (!empty($tplWrapper) && (!empty($wrapIfEmpty) || !empty($output))) {
        $output = $pdoFetch->getChunk($tplWrapper, array('output' => $output), $pdoFetch->config['fastMode']);
    }
 
    if (!empty($toPlaceholder)) {
        $modx->setPlaceholder($toPlaceholder, $output);
    }
    else {
        return $output;
    }
}