Collection Tree: getting child collections

I'm trying to make a function that will display preview thumbnails for collections. The problem is, the data set I'm working with has a collection tree, and some collections don't have items--only their child collections do. So I'm trying to get my function to look for items in the collection, and if it can't find any, look for items in child collections. I have most of the function, but I need a way to look for child collections. So far I have:

<br />
function pinstripe_get_first_collection_images() {<br />
	if (total_items_in_collection() == 0) {<br />
		echo "Whoa! Nothing here!";<br />
		/* find child items */<br />
		$childID=/*call function to get id of child collection here*/<br />
	set_current_collection(get_collection_by_id($childID));<br />
		while(loop_items_in_collection(4)) echo item_square_thumbnail();<br />
        	} else {<br />
	while(loop_items_in_collection(4)) echo item_square_thumbnail();<br />
	return $html;<br />
	}<br />
}<br />

Any ideas about what function(s) I can use to get child collections?

There's not something built into CollectionTree, but something like this should do the trick

function pinstripe_get_child_collections($collectionId) {
    if(plugin_is_active('CollectionTree')) {
        $treeChildren = get_db()->getTable('CollectionTree')->getChildCollections($collectionId);
        $childCollections = array();
        foreach($treeChildren as $treeChild) {
            $childCollections[] = get_collection_by_id($treeChild['id']);
        }
        return $childCollections;
    }
    return array();
}

There's a couple things to notice here.

First, it's returning an array, not just an id, since I imagine you'd want to check through all the child collections?

Also, I haven't tested whether/how this works with multiple levels of child collections.

The $treeChildren variable close to the top is an array that contains _not_ Collection objects, but an array of the data about them, something like:

array('id'=>1, 'name'=>'Collection Name' . . . . . . );

If that's all you need, you could skip the step of looping through $treeChildren to look up the actual Collection objects. With the way you have it set up, that'd do the trick, just using the id key in the array as needed. If you leave in looking up each collection object, you'd just be setting the actual current collection, rather than by id. Little difference at that level, but if you want to use this function to do anything else with child collections, it might make sense to keep using the collection objects so you can use the usual collection functions on the results.

Either way, I imagine that you'd then just do a foreach over the returned array to do whatever work you need. Again, though, I haven't tested this with multiply-layered collection trees

The other thing to notice is that I put in a check for whether the plugin is actually there, and just return an empty array if not. That's a nod toward making the theme more generalized for others to use. It'd be a nice piece of info to say "This theme works well with the following plugins....".

Hope that helps,
Patrick

Awesome, thanks! I got it to work, although with quite a few unexpected behaviors, with this:

function pinstripe_get_first_collection_images() {
	if (total_items_in_collection() == 0) {
		/* find child items  */
		$collectionId = collection('ID');
		$childArray=pinstripe_get_child_collections($collectionId);<br />
		$thumbnailCount=0;
		$childCount=0;
		while ($thumbnailCount <= 3):
			$childID=$childArray[$childCount]['id'];
			set_current_collection(get_collection_by_id($childID));
			while (loop_items_in_collection() AND $thumbnailCount <= 3){<br />
				echo item_square_thumbnail();
				$thumbnailCount++;
			}
			$childCount++;
		endwhile;
	} else {<br />
	while(loop_items_in_collection(4)) echo item_square_thumbnail();
	return $html;
	}
}
function pinstripe_get_child_collections($collectionId) {
    if(plugin_is_active('CollectionTree')) {
        $treeChildren = get_db()->getTable('CollectionTree')->getChildCollections($collectionId);
        $childCollections = array();
        foreach($treeChildren as $treeChild) {
            $childCollections[] = get_collection_by_id($treeChild['id']);
        }
        return $childCollections;
    }
    return array();
}

This is probably not the best way of doing this, and so I'll probably try to rewrite it. If you can think of a way to make it better, let me know.