Location: PHPKode > projects > Cynus > cynus/menu_func.php
<?php
/**
 * @file menu_func.php -- Provides functions to display menus
 * @Id $Id: menu_func.php,v 1.27 2004/07/08 19:13:35 jason Exp $
 *
 * Cynus - a web-based content manager
 * Copyright (C) 2003 Brett and Jason Profitt
 *
 * 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.
 *
 */
 
cynus_debug ("Loaded menu_func.php", 3);

/***************************************
List Modules: string list_modules()
This returns a string containing the main table of all items in 
the "modules" table as well as tacking on the logout button
***************************************/
function list_modules() {
	global $config, $user_config;
	cynus_debug ("Listing modules.", 3);
	
	$query="SELECT * from `$config[sql_prefix]modules` WHERE `min_level` <= '$user_config[level]' AND `type` != 'pseudo' ORDER by `friendly_name`";
	cynus_debug ($query);
	$result=mysql_query($query);
	$x=1;
	while($each_mod=mysql_fetch_assoc($result)) {
		cynus_debug ("Dealing with module: {$each_mod['friendly_name']} ({$each_mod['name']})");
		cynus_debug ($each_mod);
		
		if(in_array($each_mod['id'], $user_config['permissions_array'])) {
			cynus_debug ("User has access to \"{$each_mod['name']}\" module.");
			
			if($each_mod['type']=='module'){
				$main_menu[]="{$each_mod['name']}:{$each_mod['friendly_name']}:modules.php?module={$each_mod['location']}:{$each_mod['icon']}";
			}
			
			#"link"s redirect it to some place specific
			elseif($each_mod['type']=="link" || $each_mod['type']=="base") {
				$main_menu[]="{$each_mod['name']}:{$each_mod['friendly_name']}:{$each_mod['location']}:{$each_mod['icon']}";
			}
		}
	}
			
	#return $content;
	return cynus_menu ($main_menu, 'base');
}




/********************************
Cynus Menu: string cynus_menu(array $menu [, string $section])
Creates a menu honoring the main_columns var.
$menu is an array with the elements in the form of:
	module_name:text_to_display:link:icon_file
"link" is appended to the current URL.  it should be params in 
var=value separated by a &.  You will probably want it to start 
off saying "file=" or "op="
This returns the proper html for the menu.
The second optional parameter is used to define which part of Cynus
this menu is for. If you do not pass it one, it will assume the menu
is for a module and make links for it. If you pass it "base" then it will
make the links in respect to the base directory of Cynus
*********************************/
function cynus_menu($menu) {
	global $config;
	cynus_debug ("Creating Cynus Menu", 3);
	cynus_debug ($menu);
	
	#If they pass an optional second option, we'll use it to see if
	#it's for the base program or for a module
	if(func_num_args() >= 2) {
		$second_param=func_get_arg(1);
	}
	$section=set_default("module", $second_param);

	#Let's start making the table!
	$content="\n<table cellspacing=\"0\" cellpadding=\"5\" class=\"main-table\">\n";	
	
	$x=1;
	if(!is_array($menu)) {$menu=array();}
	foreach ($menu as $menu_info) {
		list ($mod_name, $text_temp, $link, $icon) = explode (":", $menu_info);
		$icon_alt = $text_temp;
		$text=$text_temp;
		
		if($x==1) {$content.= "\t<tr>\n";}
		#Now to determine which section this is for, the base program
		#or a module. we'll make links and icons accordingly
		if($section=='base') {
			$the_link="<a accesskey=\"$access_key\" href=\"$config[url_base_dir]/$link\">";
			
			#This is because i can't stand the damn "//" in a path name....gah!!
			if(substr($icon, 0, 1) == '/') {$slash='';}
			else{$slash='/';}
			
			#this is *such* an ugly hack
			#check if we're making an icon for a module by seeing if the link is pointing to
			#something with modules.php
			if(stristr($link, 'modules.php')) {
				$the_icon="$config[url_base_dir]/modules/$mod_name/$icon";
			}
			else{
				$the_icon="$config[url_base_dir]/themes/$config[style]$slash$icon";
			}
		}
		elseif($section=='external') {
			list ($mod_name_hack, $text_hack, $link_hack, $icon, $more_icon) = explode (":", $menu_info);
			
			#list ($icon, $more_icon) = explode (":", $icon);
			$the_icon=$icon . ':' . $more_icon;
			$the_link="<a accesskey=\"$access_key\" href=\"$config[url_base_dir]/$link\">";
		}
		elseif($section=='mod_list') {
			$the_link="<a accesskey=\"$access_key\" href=\"$config[url_base_dir]/$link\">";
			$the_icon="$config[url_base_dir]/modules/$icon";
		}
		else{
			$the_link="<a accesskey=\"$access_key\" href=\"modules.php?module=$mod_name&$link\">";
			$the_icon="$config[url_base_dir]/modules/$mod_name/$icon";
		}
		
		$content.= "\t\t<td class=\"module-cell\">";
		$content.= $the_link;
		if($icon != '') {
			$content.= "<img src=\"$the_icon\" class=\"module-icon\" alt=\"$icon_alt\" /><br />";
		}
		
		#now to print out the module name and close the link
		$content .= "<div class=\"module-text\">$text</div></a></td>\n";
		$x++;
		if($x>$config['main_columns']) {
			$x=1;
			$content .="</tr>\n";
		}
		$the_link=$the_icon='';
		
	}
		#now to list the logout button
		if($x==1) {$content.="\t<tr>\n";}
		while($x < $config['main_columns']) {
			$content.= "\t\t<td class=\"module-cell\">&nbsp;</td>\n";
			$x++;
		}
		$content.=<<<___eofh
			<td>
				<a href="logout.php"><img src="$config[url_base_dir]/themes/$config[style]/images/logout.png" class="module-icon" alt="Logout"/></a>
			</td>
		<tr>
	</table>\n
___eofh;

	return $content;
}


/******************************
Cynus Submenu: string cynus_submenu(array $submenu)
Takes the array and makes a submenu out of it in the style
Menu -> Another Link -> Where you are now
Where "Menu" and "Another Link" are links, but "Where you are
now" isn't.  The array needs to be formatted like
"Text" => "link" 
"Text 2" => ""

******************************/
function cynus_submenu($submenu) {
	global $config;
	cynus_debug ("Creating submenu.", 3);
	cynus_debug ($submenu);
	
	if(SUBMENU_DONE != 1) {
		if(func_num_args() >= 2) {
			$second_param=func_get_arg(1);
		}
		$section=set_default("module", $second_param);
		
		$content = '<div class="submenu">' . "\n";
		$menu_array=array();
		foreach($submenu as $text_temp => $link) {
			if(/*$section=="base" && */$link != "") {
				$text=$text_temp;
				$the_link="<a accesskey=\"" . strtoupper ($access_key) . "\" href=\"$config[url_base_dir]/$link\" class=\"submenu-link\">$text</a>";
			}
			//elseif($link != ""){
			//	list($mod_name, $link) = split(":", $link);
			//	$the_link="<a href=\"$config[url_base_dir]/modules.php?module=$mod_name&$link\">$text</a>";
			//}
			else{
				$the_link=$text_temp;
			}
			array_push($menu_array, "$the_link");
		}
		$content .= implode(" -&gt; ", $menu_array) . "</div><br />\n";
		define(SUBMENU_DONE, 1);
		return $content;
	}
}

?>
Return current item: Cynus