We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24495
    • 407 Posts
    Like the subject says: how can I find all document using a specific template without checking whole document tree?
      • 18397
      • 3,250 Posts
      For what purpose?
        • 24495
        • 407 Posts
        I want to check whether all docs with this template are only a special type of documents.
        In this case I must know whether all docs with a template named "empty Temp" (created by a client) are ONLY used as special portions of other pages (margin column pieces) and not for other purposes. With a positive answer I must put a new snippet to this template, otherwise I must change the template for any docs before I can put this new snippet.

        Clear enough what I mean? undecided
          • 14412
          • 74 Posts
          rasc, I’m not sure I fully understand what you need, but this is also something that intrigues me.

          I guess the key in your post is

          Quote from: rasc at May 01, 2007, 02:40 PM

          find all document using a specific template without checking whole document tree?

          While working on a site I got to a point where I was wondering if there was a easier, faster way to select documents given the templates that are applied to them. Real example:

          I set up a site using folders for each section (template = "section"); these folders contain subfolders, each containing another two types of documents (templates "news" and "children"):

          [tt]section 1 (tpl: "section")
          |
          |-news
          | |---article 1 (tpl: "news")
          | |---article 2 (tpl: "news")
          |
          |-children
          |---page 1 (tpl: "children")
          |---page 2 (tpl: "children")
          section 2 (tpl: "section")
          |
          |-news
          | ...
          |-children
          | ...[/tt]



          This is great from a back-end admin point of view because all of a given section’s documents are neatly arranged inside their folders but...

          ...from a front-end perpective, it complicates things: for instance, on a given page, i want to feed ditto with all "news" documents that belong to all the sections. As the news articles aren’t directly beneath the section’s root folder, I can’t just call ditto with &parents = "section IDs".

          My first approach was to create a number TV (NewsFolder) on the "section" template that stores, for each section, its news folder page ID and then feed the &parents parameter in Ditto with an ID list generated through a small snippet - getTV:

          [tt][!Ditto?parents=`[[getTV?template=`section` &TVname=`NewsFolder`]]` &depth=`1` &display=`all` &limit=`10`!][/tt]

          The getTV snippet contains this:
          <?php
          //Get a list of TV values, given a container and document's template
          //Container Document
              $container = isset($container) ? $container : 0;//defaults to doc tree root - search all documents
              //Template to search for
              $template = isset($template) ? $template : '';
              //TV to search for
              $TVname = isset($TVname) ? $TVname : '';
              //Fetch Template's ID
              $table_site_templates= $modx->getFullTableName('site_templates');
              $tplId=$modx->db->getValue($modx->db->select("id", $table_site_templates, "templatename='$template'"));
              //Fetch Children documents and get TV value on them
              $res=$modx->getChildIds($container);
              $result = '';
              foreach ($res as $key=>$value){
                  $doc=$modx->getDocument($key);
                  if($doc['template']==$tplId){
                      if ($result != '') $result .= ',';
                      $TVvalue=$modx->getTemplateVar($TVname, '', $doc['id'], 1);
                      $result .= $TVvalue['value'];
                  }
              }
              return $result;
          ?>
          


          I guess this is very inefficient since I’m selecting all the documents below tree root and then checking the applied template and TV value on each document, one by one. All this just to get a list of parent folder, i.e., before Ditto does its wonderful stuff...I guess that if you have 1000’s of documents (I’m approaching my 1st thousand docs pretty fast...), this alone can introduce a significant delay on the page load, no?

          I wish there was a better way to do this. In this specific example, it would be great to just tell ditto to "select all documents that match the "news" template". Ditto 2 now has a &documents parameter - it would be great if we could use something like &documentsByTpl=`template1, template2` to select the docs to list.

          I eventually came up with a simpler solution to this example, eliminating the need for the TV (i just set all news folder to a specific template and then modified the snippet to search for docs using that template), but I’m still stuck searching the whole DB for docs matching the template.

          I tried looking at the Ditto code to see if I could come up with a better solution, but given my limited php skills, it all seems chinese to me embarrassed

          It would be great if someone with greater experience with the MODx API could shed some light on how this could be done more efficiently...

          Regards, JP

          PS - In the example i mentioned, I’m aware that it would be easy to just hardcode all news folders ID’s in the Ditto call, BUT I’m thinking of a site that will have many sections created by the inexperienced users so I’d prefer to stick to templates and don’t mess with page ID’s...
            Got Music? Try XSPFjukebox - Skinnable MODx Media Player
            • 18397
            • 3,250 Posts
            Assuming you are using Ditto 2:

            [!Ditto?parents=`0`&depth=`0`&filter=`template,TEMPLATE ID`!]
              • 24495
              • 407 Posts
              @ganeshXL
              What I mean is: "find in manager" because I must change/edit some pages/templates as administrator. This question maybe point to a more complex request: some search features for administrators like: which documents uses template X / image X / snippet X ...
                • 24495
                • 407 Posts
                With DocManager I can SET attributes/templates, but can’t find and check documents with defined attributes. Would be a nice feature for DocManager if this could be working like "give me all id’s of documents using template X/documents that are cachable/documents published before date x ...
                  • 14412
                  • 74 Posts
                  Quote from: Mark at May 02, 2007, 01:38 AM

                  Assuming you are using Ditto 2:

                  [!Ditto?parents=`0`&depth=`0`&filter=`template,TEMPLATE ID`!]

                  Doh! I feel really dumb. Thanks for this tip, Mark. Never really cared about the filter parameter in Ditto...now I know I should have RTFM laugh

                  Still, the example i gave was just a situation where i’d like to have more flexibility when searching for documents. I second rasc:

                  Quote from: rasc at May 02, 2007, 02:03 AM

                  This question maybe point to a more complex request: some search features for administrators like: which documents uses template X / image X / snippet X ...
                    Got Music? Try XSPFjukebox - Skinnable MODx Media Player
                    • 11975
                    • 2,542 Posts
                    Hi,


                    This question maybe point to a more complex request: some search features for administrators like: which documents uses template X / image X / snippet X ...

                    I’ve already a module for the first one (which docs use templateX), will post it ASAP

                    I will try to add the ability to find documents per snippet

                    :-)
                      Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
                      • 24495
                      • 407 Posts
                      Wow, that’s the heartbeat of a community grin