We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21301
    • 93 Posts
    I noticed that pages that don’t have a value filled in for the TV you’re querying (eg the default value) won’t become part of the set you request. Needs some investigation.
      • 21301
      • 93 Posts
      Another limitation seems to be the number of TV’s. So there’s two drawbacks to this approach that are not easily solved:

      - Documents without the TV’s are not included in the query-result
      - More than 5 TV’s result in complex SQL-join structures that sort of crash MySQL

      I think rewriting the function using ’getDocumentChildren’ and ’getDocumentChildrenTVarOutput’ would be better.

      Anybody any ideas on this?
        • 21301
        • 93 Posts
        No ideas? I guess there’s no interest. Interesting.

        Here’s the solution anyway.
        <?php
        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)); 
        }
        ?>
        
        
          • 1764
          • 680 Posts
          Very interesting. I’ll see if I can test this a bit maybe some people on the testing team can do the same.

          Thanks for getting this done.
            • 33114
            • 518 Posts
            Well, I was looking for some better solution of my case and stubled this post. Here.. I have a site with a catalogue of tyres where the manufacturer is the parent for models and model as the parent for model variants. I have about 10 manufacturers and all of them has about a dozen models and each model has tens of varians.
            Now I need to write a snippet which could return needed model variants (!) by some 3 or 4 values (which are stored in TVs).

            As I’m no good php coder I wrote a snippet that ..

            • gets all documents from DB with template field equal to ...say 9 (which is my model variant template)
            • [li]starts a cycle where it looks through all of rows...findin matching documents:
            for ($i = 0; $i < $limit; $i++)
            {
            
            $row = $modx->db->getRow($result);
            $id = $row["id"];
            settype($id,"integer");
            $pagetitle = $row["pagetitle"];
            $pid = $row["parent"];
            $parentinfo = $modx -> getDocument($pid,"pagetitle, parent");
            $ppagetitle = $parentinfo["pagetitle"];
            $modinfo = $modx->getTemplateVarOutput("thumb",$pid);
            $thumb = $modinfo["thumb"];
            $ppid = $parentinfo["parent"];
            $ppinfo = $modx -> getDocument($ppid,"pagetitle");
            $pppagetitle = $ppinfo["pagetitle"];
            
            
            
            $modinfo = $modx->getTemplateVarOutput('type_size_1',$id);
            $ts1 = $modinfo['type_size_1'];
            
            
            $modinfo = $modx->getTemplateVarOutput('type_size_2',$id);
            $ts2 = $modinfo['type_size_2'];
            
            
            $modinfo = $modx->getTemplateVarOutput('r',$id);
            $ro = $modinfo['r'];
            
            
            $modinfo = $modx->getTemplateVarOutput('season',$pid);
            $seas = $modinfo['season'];
            
            $modinfo = $modx->getTemplateVarOutput('price',$id);
            $pr = $modinfo['price'];
            
            
            
            if (($ts1 == $type_size_1 || $type_size_1 == $all) && ($ts2 == $type_size_2 || $type_size_2 == $all) && ($ro == $r || $r == $all) && ($seas == $season || $season == $all) && ($ppid == $man || $man == $all) && ($pr <= $price || $price == $all))
            
            {
            
            $list .= "<div class='thumb-select'><a href='/[~$id~]'><img src='$thumb' alt='$pppagetitle $ppagetitle $pagetitle'>$pppagetitle $ppagetitle</a><br />$ts1/$ts2 R$ro$seas<br /><span class='price'>$pr р.</span></div>";
            $res++;
            
            }
            
            }
            
            $list .= "</div>";
            
            if ($res == 0) {$output .= "<p class='result'>К сожалению, по запросу <b>типоразмер <i>$type_size_1</i>/<i>$type_size_2</i> R<i>$r</i>, сезон: <i>$season</i>, цена не более: <i>$price</i></b> ничего не найдено.</p>";}
            else {
            $output .= "<p class='result'>По запросу  <b>типоразмер <i>$type_size_1</i>/<i>$type_size_2</i> R<i>$r</i>, сезон: <i>$season</i>, цена не более: <i>$price руб.</i></b> найдено $res позиций:</p>".$list;
            $output .= "<p><a href='/[~266~]?p=$next'>-></p>";
            
            }
            
              [/li]

            Well smiley that works ...but it can take dramatically long time if 4-5 values are given.
            Could someone giv advice on that?
              http://modx.ru - российская поддержка MODx
              http://newscup.ru - экспериментальный проект
              http://yentsun.com - персональный сайт