We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18397
    • 3,250 Posts
    Then plan is simple actually. I can’t be specific because I’m working with unreleased code but I can say this.

    I will either do one of the following (or both)

    Write a snippet called getdoctv which will function similiar to getdoc except with tv’s

    Use a search function to find all instances of [+TVnameoftemplatevar+] (note the TV prefix) and replace with its corresponding value retrieved from an API call.
      • 21301
      • 93 Posts
      Quote from: Mark at Dec 05, 2005, 09:49 AM

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

      I assume the snippet has to wait until the code is released in the next release? Will that be a matter of weeks or will it be more like months?
        • 18397
        • 3,250 Posts
        Well, I hope to have getdoctv done very soon.
          • 6726
          • 7,075 Posts
          Can’t wait to see it Mark laugh

          This one will be a download hit, no doubt !
          A nice christmas gift grin


          Quote from: Mark at Dec 05, 2005, 10:00 AM

          Well, I hope to have getdoctv done very soon.
            .: COO - Commerce Guys - Community Driven Innovation :.


            MODx est l'outil id
            • 21301
            • 93 Posts
            It’s actually quite simple. Some API documentation would have helped smiley. [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.

            This will sort on any of the default-TV’s. The TV’s for each document are then sorted by rank. You can use the customsort routine I posted earlier to sort the documents on TV however.

            Call it like this:

            [tt]getDocumentChildrenTV($modx, 0, 1, "*", "pagetitle", $dir="ASC");
            [/tt]

            <?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);
            }
            ?>
            
              • 21301
              • 93 Posts
              Version 1.3

              No need for custom API anymore. All functionality within the snippet.

              I’m looking forward to your implementation too Mark!


              <?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");
                    }
                  }
                }
              }
              ?>
              
                • 32241
                • 1,495 Posts
                Thanks for your share Theo. I really appreciate it... Keep it up!


                Sincerely,
                Wendy Novianto
                  Wendy Novianto
                  [font=Verdana]PT DJAMOER Technology Media
                  [font=Verdana]Xituz Media
                  • 6726
                  • 7,075 Posts
                  This is really cool Theo, I have been expecting this...

                  I don’t know how I missed this update (or rather : yeah, we DO need a snippet repository, hard to keep up with the forums)...

                  One question : do we have to prefix tv placeholders with "tv", as for the NewsListing snippet ? e.g to include a TV "myvariable" I’d add a [+tvmyvariable+] in the template chunk ?
                    .: COO - Commerce Guys - Community Driven Innovation :.


                    MODx est l&#39;outil id
                    • 18397
                    • 3,250 Posts
                    Not sure, but you do HAVE to tell the snippet what TV’s to use.
                      • 6726
                      • 7,075 Posts
                      I don’t think you have to anymore...

                      function getDocumentChildrenTV($modx, $parentid=0, $published=1, $tvfields="*", $docsort="pagetitle", $docsortdir="ASC", $limit="")


                      wouldn’t that mean all TVs are included by default ?

                        .: COO - Commerce Guys - Community Driven Innovation :.


                        MODx est l&#39;outil id