Hierarchical menu

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Locked
Manarth

Hierarchical menu

Post by Manarth »

The following code creates a hierarchical menu made up of nested lists.

It's similar to {bulletmenu} but doesn't show the whole site structure, it only descends down the sections you're in. It doesn't display seperators.
Section headers and links have classes applied to the li.

Code: Select all

<?php
#CMS - CMS Made Simple
#(c)2004 by Ted Kulp (wishy@users.sf.net)
#This project's homepage is: http://cmsmadesimple.sf.net
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

function smarty_cms_function_bulletmenu2($params, &$smarty) {

	global $db;
	global $gCms; 
	
	# getting menu parameters
	$showadmin = isset($params["showadmin"]) ? $params["showadmin"] : 1 ;
	
	# getting content hierarchy parameters
	$newparams["show"] = "menu";
	$thispage = $gCms->variables['page'];


	# track - identify all ancestors of current page
	$parent = $thispage;
	$parents = array();
	
	array_unshift($parents, $parent);
	while ($parent != 0) {
		$query	= "SELECT page_id, parent_id FROM ".cms_db_prefix()."pages WHERE page_id = '$parent'";
		$result = $db->Execute($query);
		if ($result && $result->RowCount() > 0) {
			$line	= $result->FetchRow();
			$parent	= $line["parent_id"];
		}
		array_unshift($parents, $parent);
	}


	# getting content - returns array of all items
	$content = db_get_menu_items($newparams);


	# defining variables
	$menu = "";
	$level = array(0);
	$changelevel=0;
	
	$menu .= "\n<ul>\n";
	# parse content, add to $menu
	foreach ($content as $menuitem) {
		# do we display this item?
		if (in_array($menuitem->parent_id, $parents) && $menuitem->page_type!='seperator') {
			
			# have we changed into another level?
			if ($menuitem->parent_id!=end($level)) {
				if ($menuitem->parent_id==prev($level)) {
					array_pop($level);
					$menu .= "</ul>\n</li>\n";
				} else {
					array_push($level, $menuitem->parent_id);
				}
			} else $changelevel=0;
			
			if ($menuitem->page_type=='sectionheader') $class=" class=\"sectionheader\""; 
			elseif ($menuitem->page_type='link') $class=" class=\"link\"";
			else $class="";
			
			$menu .= "<li$class>";
			if ($menuitem->page_id!=$thispage)
				$menu .= "<a href=\"$menuitem->url\">$menuitem->menu_text</a>";
			else
				$menu .= "<strong>$menuitem->menu_text</strong>";
			
			# any children?
			if ($menuitem->childs!="" && in_array($menuitem->page_id, $parents)) {
				$menu .= "\n<ul>\n";
			} else {
				$menu .= "</li>\n";
			}
		}
	}
	# descend to ground safely - close off all the tags
	while (array_pop($level)) $menu .= "\n</ul>\n</li>\n";
	#close off the menu
	$menu .= "</ul>\n";

	# add admin link if necessary
	if ($showadmin == 1) {
		$menu .= "<hr />\n<ul><li><a href='admin/'>Admin</a></li></ul>\n";
	}

	return $menu;

}

function smarty_cms_help_function_bulletmenu2() {
	?>
	<h3>What does this do?</h3>
	<p>Prints a hierachical bullet menu .</p>
	<h3>How do I use it?</h3>
	<p>Just insert the tag into your template/page like: <code>{bulletmenu2}</code></p>
	<h3>What parameters does it take?</h3>
	<p>
	<ul>
		<li><em>(optional)</em> <tt>showadmin</tt> - 1/0, whether you want to show or not the admin link.</li>
	</ul>
	</p>

	<?php
}

function smarty_cms_about_function_bulletmenu2() {
	?>
	<p>Author: Marcus Deglos <<a href="mailto:md@zioncore.com">md@zioncore.com</a>></p>
	<p>Version: 1.0</p>
	<p>
	Change History:<br/>
	None
	</p>
	<?php
}

# vim:ts=4 sw=4 noet
?>
Copy the code and save it as 'function.bulletmenu2.php' in your plugins folder. It will be automatically included as a plugin - simply insert the code {bulletmenu2} in a template or page.[/i]

Marcus.
Greg
Power Poster
Power Poster
Posts: 598
Joined: Sun Sep 26, 2004 6:15 pm

Hierarchical menu

Post by Greg »

I get this error:

Warning: Cannot modify header information - headers already sent by (output started at d:\aaawebsites\websites\cmsmadesimple.old\plugins\function.bulletmenu2.php:134) in d:\aaawebsites\websites\cmsmadesimple.old\lib\preview.functions.php on line 63
Greg
Manarth

Hmm...errors

Post by Manarth »

What are you trying to do when you get this error?

The plugin doesn't send any headers - the only time this error would occur is if php were trying to set a header when another plugin/script had already sent output.
Greg
Power Poster
Power Poster
Posts: 598
Joined: Sun Sep 26, 2004 6:15 pm

Hierarchical menu

Post by Greg »

Occurs when I'm doing a preview when editing page content.
Greg
Manarth

Unsure....

Post by Manarth »

Well, I can't replicate the problem...

The plugin does have issues with preview. It doesn't work! That's because it builds the menu based on knowing the current page, and the page's ancestors...and at the preview stage these variables aren't set in the same way. I'll work on that.

But as for the headers error, that's really unusual. I wonder if it's conflicting with another plugin / function you're running - I can't imagine how this plugin could cause the error. I just get an empty menu, not an error,
ntro

Cannot modify header information

Post by ntro »

Most likely Greg added an extra newline outside of the part when he pasted it into a file. That's a common cause of that error for files that are included before you send your headers.
bilgehan

Re: Hierarchical menu

Post by bilgehan »

I get this error:

Parse error: parse error, unexpected '&', expecting '(' in /usr/export/www/hosting/mysite/plugins/function.bulletmenu2.php on line 19
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm

Re: Hierarchical menu

Post by Ted »

Bulletmenu has this function built in now.  Just set collapse="1" in your bulletmenu definition on your template.
jah
Forum Members
Forum Members
Posts: 147
Joined: Thu Dec 30, 2004 9:09 am

Re: Hierarchical menu

Post by jah »

I discovered a small problem with the bulletmenu when setting collapse="1".

The children of the last menu item becomes visible when the first menu item is selected. (First and last menu items refer to items in the first level of the hierarchy.)
When other menu items are selected they will collapse.

Jon
DefenceTalk

Re: Hierarchical menu

Post by DefenceTalk »

jah wrote: I discovered a small problem with the bulletmenu when setting collapse="1".

The children of the last menu item becomes visible when the first menu item is selected. (First and last menu items refer to items in the first level of the hierarchy.)
When other menu items are selected they will collapse.

Jon
Hi,

Is there a fix for this?

thanks!
Locked

Return to “Modules/Add-Ons”