At the moment the ModX Class has a method getChildIds, but that method does not preserve the structure of the document tree, instead it just returns a plain list.
I would suggest to add a parameter to getChildIds that determines if you get a flat version or the tree (or to add a new function getChildIdsAsTree). This could be just a simple associative array that mirrors the tree structure:
ID->entry
entry = {null | array(ID->entry)}.
This would be usefull in all cases where you have to get the subtree to a document Id, for example if you want to build a menu.
The modified function might look like this (the arrows indicate differences to the current version of getChildIds()):
/**
* Gets all of the child resource ids for a given resource.
*
* @param integer $id The resource id for the starting node.
* @param integer $depth How many levels max to search for children (default 10).
--> * @param boolean $asTree If true an associative array is returned, if false
* @return array An array of all the child resource ids for the specified resource.
*/
function getChildIds($id= null, $depth= 10, $asTree = false) {
$children= array ();
if ($id !== null && intval($depth) >= 1) {
$id= intval($id);
--> if($asTree) {
--> $children[$id]= array();
--> }
if (isset ($this->resourceMap["{$id}"])) {
if ($children= $this->resourceMap["{$id}"]) {
foreach ($children as $child) {
$processDepth = $depth - 1;
if ($c= $this->getChildIds($child, $processDepth)) {
--> if($asTree) {
--> $children[$id]= $c;
--> } else {
--> $children= array_merge($children, $c);
--> }
}
}
}
}
}
return $children;
}
I am not sure if this method saves the user any work, but since the tree structure is there in the beginning on might as well preserve it for later use.