Couple small corrections to mapping across default values. Some getResources defaults are odd - why 5?
There's also a missing bit on the getResources parameter descriptions - only iterating immediate children is depth zero. The zero is missing from an otherwise handy sentence.
<?php
global $modx;
$modx->log(modX::LOG_LEVEL_DEBUG, 'sortDir ' . $sortDir);
$modx->log(modX::LOG_LEVEL_DEBUG, 'sortBy ' . $sortBy);
$modx->log(modX::LOG_LEVEL_DEBUG, 'startID ' . $startID);
$modx->log(modX::LOG_LEVEL_DEBUG, 'filter ' . $filter);
$modx->log(modX::LOG_LEVEL_DEBUG, 'tagData ' . $tagData);
$modx->log(modX::LOG_LEVEL_DEBUG, 'tags ' . $tags);
$modx->log(modX::LOG_LEVEL_DEBUG, 'display ' . $display);
$modx->log(modX::LOG_LEVEL_DEBUG, 'parents ' . $parents);
$modx->log(modX::LOG_LEVEL_DEBUG, 'total ' . $total);
$modx->log(modX::LOG_LEVEL_DEBUG, 'depth ' . $depth);
$modx->log(modX::LOG_LEVEL_DEBUG, 'tpl ' . $tpl);
$modx->log(modX::LOG_LEVEL_DEBUG, 'hideFolders ' . $hideFolders);
$modx->log(modX::LOG_LEVEL_DEBUG, 'tplLast ' . $tplLast);
$modx->log(modX::LOG_LEVEL_DEBUG, 'paginate ' . $paginate);
$getResourcesParams = array();
if (isset($startID))
{
$parents = $startID;
}
if (isset($parents))
{
$getResourcesParams['parents'] = $parents;
}
else
{
// Ditto defaults to current document's children
$getResourcesParams['parents'] = $modx->resource->get('id');
}
if (isset($sortBy) || isset($sortDir)) {
$sortDir = isset($sortDir) ? strtoupper($sortDir) : 'DESC';
$sortBy = isset($sortBy) ? $sortBy : "createdon";
$getResourcesParams['sortdir'] = $sortDir;
$getResourcesParams['sortby'] = $sortBy;
}
if (isset($filter))
{
// only works for exactly two filter conditions OR'd, using filter comparisons 1 (unequal) or 2 (equals), eg, filter=`id,328,2|template,8,1` OR filter=`createdby,2,1|template,5,1`
// extract the strings as so: `createdby,2,1|` => $filter_field,$filter_id,$filter_cmp
$filter_comma1 = strpos($filter, ",", 0);
$filter_comma2 = strpos($filter, ",", $filter_comma1 + 1);
$filter_divider = strpos($filter, "|", $filter_comma2 + 1);
$filter_field = substr($filter, 0, $filter_comma1);
$filter_id = substr($filter, $filter_comma1 + 1, $filter_comma2 - $filter_comma1 - 1);
$filter_cmp = substr($filter, $filter_comma2 + 1, $filter_divider - $filter_comma2 - 1);
// Ditto's filter excludes, whereas getResources's where filter includes, so we reverse the boolean operators
// ie, Ditto &filter=`id,328,2|template,8,1` means exclude where id=328 OR template!=8, equivalent to INCLUDE where id!=328 AND template=8, equivalent to &where=`{"id:!=":328, "AND:template:=":8}`
// This builds the first half, eg, translates the string `id,328,2|` to `{"id:!=":328, "AND:`
$filter_cmp_prm = ($filter_cmp == '2') ? '!=' : '=';
$where = '{"' . $filter_field . ':' . $filter_cmp_prm . '":' . $filter_id;
if ($filter_divider)
{
$filter_comma1 = strpos($filter, ",", $filter_divider);
$filter_comma2 = strpos($filter, ",", $filter_comma1 + 1);
$filter_field = substr($filter, $filter_divider + 1, $filter_comma1 - $filter_divider - 1);
$filter_id = substr($filter, $filter_comma1 + 1, $filter_comma2 - $filter_comma1 - 1);
$filter_cmp = substr($filter, $filter_comma2 + 1);
$filter_cmp_prm = ($filter_cmp == '2') ? '!=' : '=';
$where = $where . ', "AND:' . $filter_field . ':' . $filter_cmp_prm . '":' . $filter_id . '}';
}
else
{
$where = $where . '}';
}
$getResourcesParams['where'] = $where;
}
if (isset($tagData))
{
$tvFilters = $tagData . '==%' . $tags . '%';
$getResourcesParams['tvPrefix'] = '';
$getResourcesParams['tvFilters'] = $tvFilters;
}
if (isset($total))
{
$getResourcesParams['limit'] = $total;
}
else
{
$getResourcesParams['limit'] = 0;
}
if (isset($depth))
{
if ($depth == 0)
{
// Ditto depth==0 means unlimited depth
$getResourcesParams['depth'] = 200;
}
else
{
$getResourcesParams['depth'] = $depth;
}
}
else
{
$getResourcesParams['depth'] = 0;
}
if (isset($tpl))
{
$getResourcesParams['tpl'] = $tpl;
}
if (isset($tplLast))
{
$getResourcesParams['tplLast'] = $tplLast;
}
if (isset($hideFolders))
{
$getResourcesParams['hideContainers'] = $hideFolders;
}
foreach ($getResourcesParams as $key => $value)
{
$modx->log(modX::LOG_LEVEL_DEBUG, 'getResourceParams [' . $key .'] = '. $value);
}
$getResourcesParams['includeContent'] = '1';
$getResourcesParams['includeTVs'] = '1';
$getResourcesParams['prepareTVs'] = '1';
$getResourcesParams['processTVs'] = '1';
$getResourcesParams['tvPrefix'] = '';
if (isset($paginate))
{
$getResourcesParams['elementClass'] = 'modSnippet';
$getResourcesParams['element'] = 'getResources';
$getResourcesParams['pageVarKey'] = 'page';
$getResourcesParams['pageLimit'] = '10';
if (isset($display))
{
$getResourcesParams['limit'] = $display;
}
return $modx->runSnippet('getPage', $getResourcesParams);
}
else
{
$modx->log(modX::LOG_LEVEL_DEBUG, $modx->runSnippet('getResources',$getResourcesParams));
return $modx->runSnippet('getResources',$getResourcesParams);
}