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
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...