Get recursive list of children [SOLVED]

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
isaacd
Forum Members
Forum Members
Posts: 32
Joined: Wed Dec 16, 2009 12:54 am

Get recursive list of children [SOLVED]

Post 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.
Last edited by isaacd on Sun Feb 28, 2010 4:21 am, edited 1 time in total.
isaacd
Forum Members
Forum Members
Posts: 32
Joined: Wed Dec 16, 2009 12:54 am

Re: Get recursive list of children

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

Re: Get recursive list of children

Post 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>
isaacd
Forum Members
Forum Members
Posts: 32
Joined: Wed Dec 16, 2009 12:54 am

Re: Get recursive list of children

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

Re: Get recursive list of children

Post 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>
isaacd
Forum Members
Forum Members
Posts: 32
Joined: Wed Dec 16, 2009 12:54 am

Re: Get recursive list of children

Post by isaacd »

That worked well. Thanks Peciura.
Post Reply

Return to “CMSMS Core”