Good, that was my plan too.
Write a snippet called getdoctv which will function similiar to getdoc except with tv’s


Well, I hope to have getdoctv done very soon.
. [tt]getDocumentChildrenTVars[/tt] already does what you want but the output isn’t as you like it. A simple wrapper helps. There’s actually more information on the TV’s that gets lost using this function. But that’s easily added. All that gets converted now are the TV-name and TV-value.
<?php
// Version 1.2
//
// Like getDocumentChildren but with TV's
//
function getDocumentChildrenTV($modx, $parentid=0, $published=1, $tvfields="*", $docsort="menuindex", $docsortdir="ASC", $limit="")
{
$variablesArray = $modx->getDocumentChildrenTVars($parentid, "*", $published=1, $docsort, $docsortdir, $tvfields, $tvsort="rank", $tvsortdir="ASC");
// Rewrite array to $variablesArray_r
$variablesArray_r = array();
for($a=0; $a<count($variablesArray); $a++) {
for($b=0; $b<count($variablesArray[$a]); $b++) {
$variablesArray_r[$a][$variablesArray[$a][$b]['name']] = $variablesArray[$a][$b]['value'];
}
}
return ($variablesArray_r);
}
?>
<?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
* - Added sorting parameter
* - Added max parameter (limits the listing)
*
* Version 1.2
* - Added exclude feature by TV; exclude when TV-name=excluded, value=yes
*
* Version 1.3
* - Included TV Support (no more need for custom API)
* - Can sort on any number of fields
* - Parses all TV's and their parameters
* Use [+tvname+] and [+tvname.parameter+] in your chunk
*
* 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: Fields to sort on. Defaults to: pagetitle
* sortorder: ASC or DESC, 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
* excluded: Turn on exclusion feature (excluded=1)
*
*/
// Configuration:
// 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);
// Get the documents
$childPages = getDocumentChildrenTV($modx, $id);
// Convert formatted times to unix time-stamps (adapt function if needed)
// And do the sorting again
correctTime($childPages);
customSort($childPages, $sort, $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 = $childPages[$index]["excluded"];
}
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-$maxindex+$excluded_pages);
}
} else
$output = "{{".$emptyChunk."}}";
return $output;
// ================= FUNCTIONS =================
function getDocumentChildrenTV($modx, $parentid=0, $published=1, $tvfields="*", $docsort="pagetitle", $docsortdir="ASC", $limit="")
{
$variablesArray = $modx->getDocumentChildrenTVars($parentid, $tvfields, $published, $docsort, $docsortdir, $tvfields);
// Rewrite array to $variablesArray_r
$variablesArray_r = array();
for($a=0; $a<count($variablesArray); $a++) {
for($b=0; $b<count($variablesArray[$a]); $b++) {
$variablesArray_r[$a][$variablesArray[$a][$b]['name']] = $variablesArray[$a][$b]['value'];
foreach ($variablesArray[$a][$b] as $name => $value) {
if ($name!="name" && $name!="value")
$variablesArray_r[$a][$variablesArray[$a][$b]['name'].".$name"] =$value;
}
}
}
customSort($variablesArray_r, $docsort, $docsortdir);
return $variablesArray_r;
}
// Version 1.1
// Parameters:
// data: an array of MODx documents
// fields: comma separated list of fields to sort on
// order: 'ASC' or 'DESC'
function customSort(&$data, $fields, $order) {
// Covert $fields string to array
foreach(explode(',',$fields) as $s) $sortfields[]=trim($s);
$code="";
for($c=0;$c<count($sortfields); $c++)
$code .= "\$retval = strnatcmp(\$a['$sortfields[$c]'], \$b['$sortfields[$c]']); if(\$retval) return \$retval; ";
$code .= "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");
}
}
}
}
?>
function getDocumentChildrenTV($modx, $parentid=0, $published=1, $tvfields="*", $docsort="pagetitle", $docsortdir="ASC", $limit="")