We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21301
    • 93 Posts
    wouldn’t that mean all TVs are included by default ?
    Since the interrest was rather poor, I’ve not taken the time to write proper documentation. To quickly answer your question, it will include any value found in the document array. And since the function used to retrieve the TV’s adds all the TV’s and their parameters to the document array, yes all TV’s are included by default.

    By example, these values are correct:
    $doc[’id’]
    $doc[’pagetitle’]
    $doc[’tvname’]
    $doc[’tvname.type’]

    And in the chunk:
    [+id+]
    [+pagetitle+]
    [+tvname+]
    [+tvname.type+]

    The last example is a tricky one. All the fields from the TV table in the database (like the type field) are returned and accessible this way. No prefix necessary. Not sure what happens if you have a TV named ’pagetitle’ though wink Best to prefix all your tv’s with e.g. tv_ yourself. Though this can be very easily changed in the code btw if you want.

    And without a modified API if you use the version 1.3+.

    Not sure, but you do HAVE to tell the snippet what TV’s to use.
    This used to be the case, but not for version 1.3 anymore.
      • 18397
      • 3,250 Posts
      My apologies Theo, I must’ve been looking at an older version.
        • 21301
        • 93 Posts
        No worries wink
          • 1764
          • 680 Posts
          If I havn’t said it yet, let me just say excellent snippet Theo. It’s come in very handy for me on quite a few occasions.

          I do have one suggestion though. It would be best if you were to move your functions up to the top of your code and wrap each one in a
          <?php
          if(!function_exists('myFunction')) {
           function myFunction() {
            ...
           }
          }
          ?>
          


          That way it won’t error out if it ever gets called twice.
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Um...shouldn’t that be
            <?php
            if(!function_exists('myFunction')) {
             function myFunction() {
              ...
             }
            }
            ?>
            
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 1764
              • 680 Posts
              Oops.

              Thanks for catching that Susan. I’ve updated my post.
                • 33114
                • 518 Posts
                thanks very much olaf and theo! this would ba a core snippet for me!
                  http://modx.ru - российская поддержка MODx
                  http://newscup.ru - экспериментальный проект
                  http://yentsun.com - персональный сайт
                  • 21301
                  • 93 Posts
                  Thanks Adam (and Susan wink) for the comments. I’ve changed the code accordingly in version 1.3.1.

                  And great to hear the snippet is of use!

                  @Olaf, could you maybe update your first post with (a link to) the latest version?

                  <?php
                  
                  /*
                   * ChildDocumentMapper - v1.3.1
                   * This snippet takes a start document ID and queries this page for it's children.
                   * The variables of each document are parsed into a chunk (template). The output
                   * of the snippet contains the parsed chunks for each document.
                   *
                   * 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
                   *
                   * Version 1.3.1
                   * - Added function_exisits wrapper (thanks to Adam Crownoble)
                   *
                   *
                   * 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";
                  
                  // ================= FUNCTIONS =================
                  if(!function_exists('getDocumentChildrenTV')) {
                    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;
                    }
                  }
                  
                  // Parameters:
                  // data: an array of MODx documents
                  // fields: comma separated list of fields to sort on
                  // order: 'ASC' or 'DESC'
                  if(!function_exists('customSort')) {
                    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
                  if(!function_exists('correctTime')) {
                    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");
                          }
                        }
                      }
                    }
                  }
                  
                  // ================= PARAMETERS =================
                  // 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;
                  
                  // ================= MAIN =================
                  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;
                  
                  
                  ?>
                  
                    • 33114
                    • 518 Posts
                    I suppose if I have a TV with, say @INHERIT value this snippet won’t reneder it properly?:)
                    It will reneder the TV like "@INHERIT".

                    Well, thats a little pity.
                      http://modx.ru - российская поддержка MODx
                      http://newscup.ru - экспериментальный проект
                      http://yentsun.com - персональный сайт
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      Quote from: yentsun at Mar 31, 2006, 04:15 PM

                      I suppose if I have a TV with, say @INHERIT value this snippet won’t reneder it properly?:)
                      It will reneder the TV like "@INHERIT".

                      Has this been tested?
                        Studying MODX in the desert - http://sottwell.com
                        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                        Join the Slack Community - http://modx.org