[[ChildDocumentMapper?docChunk=NewsMapChunk]]
describe site_content;
<h3>[+pagetitle+]</h3> created by [+createdby+] on [+createdon+]<br /> [+introtext+]
/*
* ChildDocumentMapper. This snippet takes a start document ID and queries
* this page for it's children. For every document a chunk is inserted.
*
* Version 1.0
*
* Parameters:
*
* docChunk: The chunk that is inserted for every document. (required)
* emptyChunk: The chunk displayed when no child documents found.
* publishedOnly: Use only published documents. (default = true)
*/
// Check parameters and set defaults
if (!isset($docChunk))
return "No docChunk parameter set!";
if (!isset($emptyChunk))
$emptyChunk = "No documents found";
if (!isset($publishedOnly))
$publishedOnly = true;
$root = $modx->documentIdentifier;
$childPages = $modx->getDocumentChildren($root, $publishedOnly, 0);
$nrChildPages = count($childPages);
if ($nrChildPages != 0)
{
for ($index = 0; $index < $nrChildPages; $index++)
{
// Set placeholders that can be used in the Chunk
foreach ($childPages[$index] as $docVar => $docVarValue)
$modx->setPlaceholder($docVar, $docVarValue);
// Expand the chunk code, and replace Placeholders
$output .= $modx->mergePlaceholderContent(
$modx->mergeChunkContent("{{".$docChunk."}}"));
}
} else
$output = $emptyChunk;
return $output;

<?php
/*
* ChildDocumentMapper. This snippet takes a start document ID and queries
* this page for it's children. For every document a chunk is inserted.
*
* Version 1.0
* - Originial version by Olaf
*
* Version 1.1
* - Added TV support (requires newer API that supports TVs)
* - Added sorting parameter
* - Added max parameter (limits the listing)
*
* Version 1.2
* - Added exclude feature by TV; exclude when TV-name=excluded, value=yes
*
* Parameters:
*
* id: The root id that contains the children
* doChunk: The chunk that is inserted for every document. (required)
* emptyChunk: The chunk displayed when no child documents found.
* Defaults to: template_nodocuments
* moreChunk: The chunk displayed when the max number of docs is reached.
* Defaults to: template_moredocuments
* moreId: The page id that contains more documents.
* This variable can be used in the moreChunk as [+moreId+]
* sort: Max 2 fields to sort on. Defaults to: pagetitle
* sortorder: Defaults to 'ASC'
* max: Maximum number of items before 'moreChunk' is shown
* Defaults to 0, no maximum
* locate: Sets the language for the date format. Defaults to uk_UK
* no_tv: Turns off the configured TV's. Defaults to false
* excluded: Turn on exclusion feature (excluded=1)
*
*/
// Configuration:
// Comma separated list with TVs to include
$tvs = "localfile, itemdate";
// Date format for date TV's
$dateformat = "%B %e, %Y";
if ($locale=="nl_NL") $dateformat = "%e %B %Y";
// Check parameters and set defaults
if (!isset($doChunk))
return "No template parameter set!";
if (!isset($emptyChunk))
$emptyChunk = "template_nodocuments";
if (!isset($moreChunk))
$moreChunk = "template_moredocuments";
if (!isset($sort))
$sort = "pagetitle";
if (!isset($sortorder))
$sortorder = 'ASC';
if (!isset($locale))
$locale = 'uk_UK';
if (!isset($id))
$id = $modx->documentIdentifier;
if (!isset($max))
$max = 0;
$excluded_feature = isset($excluded) ? 1 : 0;
setlocale(LC_TIME, $locale);
// Covert $sort string to array
foreach(explode(',',$sort) as $s) $sortkey[]=trim($s);
// Create TV-array for passing to API-call if not disabled by parameter
if ($no_tv) $tvars=array();
else foreach(explode(',',$tvs) as $tv) $tvars[trim($tv)]="*";
// Get the documents
$childPages = $modx->getDocumentChildrenByTVar($id, $tvars, 1, $fields="*");
// Convert formatted times to unix time-stamps (adapt function if needed)
correctTime($childPages);
// Do the sorting
customSort($childPages, $sortkey[0], $sortkey[1], $sortorder);
// Check for maximum to display
$nrChildPages = count($childPages);
$maxindex = $nrChildPages>$max ? $max : $nrChildPages;
if (!$maxindex) $maxindex=$nrChildPages;
// Count pages that are excluded from the list
$excluded_pages=0;
// Set the placeholders and produce the chunks
if ($nrChildPages != 0) {
for ($index = 0; $index < $maxindex; $index++) {
// Check if page has an excluded TV set to yes
if ($excluded_feature) {
$excluded = $modx->getTemplateVar("excluded","",$childPages[$index]["id"]);
$excluded = $excluded['value'];
}
if ($excluded=="yes") {
// increase excluded pages and increase maxindex
$excluded_pages++;
if ($nrChildPages-1>$index && $nrChildPages>$maxindex) $maxindex++;
} else {
foreach ($childPages[$index] as $docVar => $docVarValue) {
// Format date if needed
if ($childPages[$index]["$docVar.type"]=='date') $docVarValue=trim(strftime($dateformat,$docVarValue));
// Set placeholders that can be used in the Chunk
$modx->setPlaceholder($docVar, $docVarValue);
}
// Expand the chunk code, and replace Placeholders
$output .= $modx->mergePlaceholderContent(
$modx->mergeChunkContent("{{".$doChunk."}}"));
}
}
if ($nrChildPages>$max && $max) {
$output .= "{{".$moreChunk."}}";
$modx->setPlaceholder("moreId", $moreId);
$modx->setPlaceholder("moreCount", $nrChildPages-$max+$excluded_pages);
}
} else
$output = "{{".$emptyChunk."}}";
return $output;
function customSort(&$data, $field1, $field2, $order) {
$code = "\$retval = strnatcmp(\$a['$field1'], \$b['$field1']); if(!\$retval) return strnatcmp(\$a['$field2'], \$b['$field2']); return \$retval;";
$params = ($order=='ASC') ? '$a,$b' : '$b,$a';
usort($data, create_function($params, $code));
}
// This function corrects locally formatted date-TVs
// like 'dd-mm-yyyy' to the universal unix time stamp
function correctTime(&$data) {
for ($index=0; $index<count($data); $index++) {
foreach ($data[$index] as $name => $value) {
if ($data[$index]["$name.type"]=='date') {
list($date,$time) = split('[ ]', $data[$index][$name]);
list($mday,$mon,$year) = split('[-]', $date);
$data[$index][$name] = strtotime("$mon/$mday/$year");
}
}
}
}
?>