Page 1 of 1

Get recursive list of children [SOLVED]

Posted: Sun Feb 21, 2010 9:35 pm
by isaacd
Hello.

I would like to be able to get a recursive array of the children of a page. This would be much like $cgsimple->get_children except that it would need to include children of children (ie grand children, great grand children etc).

So in the following example (which is fictitious):
1 Home
2 About
   2.1 Who we are
   2.2 Mission
      2.2.1 Vision

the children of About are:
-Who we are
-Mission
-Vision.

[EDIT] Using the UDT that Peciura created (see below) works slightly different. It takes a hierarchical position to find the children are (in example: using the structure above, get the children of page #2, not about). It returns page aliases. It will also include the page itself, in example: if you said to get the children of page 2, you would get these aliases in the array (assuming the navigation structure above): about, who-we-are, mission, vision.

Thanks in advance.
    Isaac.

Re: Get recursive list of children

Posted: Fri Feb 26, 2010 4:17 am
by isaacd
As another approach, is it possible to get all the pages that have "1" as the first number in the hierarchical position? The logic would be something like this:

$position >= 1 && $position < 2

In other words, get all the pages that have a hierarchical position that is greater than or equal to one, but also less than 2.
This would accomplish essentially the same thing as my first post (get children recursive).

I have heard of something called the hierarchy manager, but have found no documentation of it at all, and thus have no clue how to use it.

Thanks
    Isaac.

Re: Get recursive list of children

Posted: Fri Feb 26, 2010 2:31 pm
by Peciura
You could start with UDT "all_content"

Code: Select all

/*$params['assign']*/

if (!empty($params['assign'])){
	global $gCms;
	$contentops =& $gCms->GetContentOperations();
	$assign = $contentops->GetAllContent(false);
	$smarty = $gCms->GetSmarty();
	$smarty->assign($params['assign'], $assign);
}
Look what it outputs

Code: Select all

{all_content assign='all_content'}

<pre>
{$all_content|var_dump}
</pre>

Re: Get recursive list of children

Posted: Sat Feb 27, 2010 4:48 pm
by isaacd
Okay, well I've got that, but I don't know quite what to do from there. What I am looking for is an array of all the aliases that have a position of greater than or equal to one, but less than two. So if I had this structure:

1 Home
-- 1.1 About
---- 1.1.1 Our Founders
---- 1.1.2 Our Future
--1.2 Mission
2 Calendar

Then I should have these aliases in the array:

home
about
our-founders
our-future
mission

Thanks.
    Isaac.

Re: Get recursive list of children

Posted: Sat Feb 27, 2010 5:54 pm
by Peciura
Try to modify UDT "all_content" as follows:

Code: Select all

/*$params['assign']*/
/*$params['parent_hierarchy']*/

if (!empty($params['assign']) && !empty($params['parent_hierarchy'])){
	global $gCms;
	$contentops =& $gCms->GetContentOperations();
	$all_content = $contentops->GetAllContent(false);
	$assign = array();
	foreach($all_content as $one_page){
		if (strpos($one_page->mHierarchy, $params['parent_hierarchy']) === 0){
			$assign[] = $one_page->mAlias;
		}
	}
	$smarty = $gCms->GetSmarty();
	$smarty->assign($params['assign'], $assign);
}
Use it

Code: Select all

{all_content assign='all_content' parent_hierarchy='00001'}
<pre>
{$all_content|var_dump}
</pre>

Re: Get recursive list of children

Posted: Sun Feb 28, 2010 4:15 am
by isaacd
That worked well. Thanks Peciura.