<?php
/**
 * File: $Id: system_menu.php,v 1.2 2004/10/13 11:58:52 Farsus Exp $
 *  Ciamos: Simple + Flexible + Advanced Content Management
 *  < http://www.ciamos.com >
 * @author Kazumi Ono (@link http://www.mywebaddons.com/)
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 *
* Description
*
* @param type $var description
* @return type description
*/
function b_system_main_dynshow($options)
{
    global $db, $xoopsUser, $xoopsModule, $xoopsConfig;

    $groups   = array();
    $block    = array();
    $sublinks = array();
    $subcont  = array();
    $groups   = $xoopsUser ? $xoopsUser->groups() : XoopsGroup::getByType('Anonymous');

    $block['title']    = _MB_SYSTEM_MMENU;
    $menu = 'var mainmenu =['."\n";
    $sql    = '
        SELECT M.mid, M.dirname
            FROM '.$db->prefix('modules').' M, '.$db->prefix('groups_modules_link').' L
            WHERE M.hasmain=1
                AND M.isactive=1
                AND M.weight>0
                AND M.mid = L.mid
                AND L.groupid IN('.join(',', $groups).')
            GROUP BY M.dirname
            ORDER BY M.weight ASC';
    $result = $db->query($sql);
    $j = 0;
    $count = $db->num_rows($result);
	if ( $count > 0  ) {
    	while ( $myrow = $db->fetch_array($result) ) {
        	$array[] = $myrow;
    	}
    	unset ($myrow);

    	//MAKE A NICE A ARRAY FOR THE MENU
		foreach ( $array as $key => $myrow ) {
			if ( @file_exists(XOOPS_ROOT_PATH.'/modules/'.$myrow['dirname'].'/xoops_version.php') ) {
				$module = new XoopsModule($myrow);
				$link = $module->mainLink2();
				$tree[$key]['image'] = '<img src="'.XOOPS_URL.'/images/menu/d_tree_close.gif">';
				$tree[$key]['link']  = $link['link'];
				$tree[$key]['name']  = $link['name'];
				$tree[$key]['mid']   = $module->mid();
				if ( $sublinks = $module->subLink2() ) {
					foreach($sublinks as $key2 => $sublink) {
						$tree[$key]['leaves'][$key2]['name']  = $sublink['name'];
						$tree[$key]['leaves'][$key2]['link']  = $sublink['link'];
						$tree[$key]['leaves'][$key2]['image'] = '<img src="'.XOOPS_URL.'/images/menu/pointer.gif">';
					}
				}
			}
		} 
		@array_unshift ($tree, array('image'=> '<img src="'.XOOPS_URL.'/images/menu/d_tree_home.gif">', 'name' => _MB_SYSTEM_HOME, 'link' => XOOPS_URL.'/'));

		switch ($options[0]) {
			
			case 1:
			
			$block['content'] = "
			<script type='text/javascript'>
			function expand_menu(id)  {
				if (xoopsGetElementById(id + '_submenu')) {
					xoopsGetElementById(id + '_submenu').style.display = 'inline';
				}
			}

			function collapse_menu(id)  {
				if (id != '".$dirname."') {
					if (xoopsGetElementById(id + '_submenu')) {
						xoopsGetElementById(id + '_submenu').style.display = 'none';
					}
				}
			}
			</script>";

			$block['content'] .= "<span id='hmenu' class='top'><a href='".XOOPS_URL."/'>"._MB_SYSTEM_HOME."</a></span>";
			
			foreach ($array as $myrow) {
				if ( @file_exists(XOOPS_ROOT_PATH.'/modules/'.$myrow['dirname'].'/xoops_version.php') ) {
					$module = new XoopsModule($myrow);
					$block['content'] .= "<span onmouseover,Duration=0.63='expand_menu(\"".$myrow['dirname']."\")' onmouseout,Duration=0.63='collapse_menu(\"".$myrow['dirname']."\")'>";
					if ($sublinks = $module->subLink()) {
						$subcont[] = $myrow['dirname'];
						// Main menu entry
						$block['content'] .= "<span id='hmenu' class='hmenu'>".$module->mainLink()."</span>";

						// Submenu entry, if any..
						$block['content'] .= "<span id='".$myrow['dirname']."_submenu' name='".$myrow['dirname']."_submenu' style='display:none;'>";
						foreach($sublinks as $sublink) {
							$block['content'] .= "<span id='hmenu2' class='sub'>$sublink</span>";
						}
						$block['content'] .= "</span>";
						} else {
							$block['content'] .= "<span id='hmenu' class='hmenu'>".$module->mainLink()."</span>";
						}
				$block['content'] .= "</span>";
			}
		}
			

			if ( !empty($subcont) ) {
				$block['content'] .= "<script type='text/javascript'>\n";
				foreach($subcont as $value) {
					if ($xoopsModule && ($xoopsModule->dirname() == $value)) {
						$block['content'] .= "expand_menu('$value');\n";
					}
				}
				$block['content'] .= "</script>";
			}
			
			break;

			case 2:
			$menu='';
			foreach ($tree as $nodekey => $node) {
				if ($xoopsModule) {
					if ((is_array($node['leaves'])) && ($node['mid'] == $xoopsModule->mid())) {
						$menu .= '<img src="'.XOOPS_URL.'/images/menu/tree_open.gif">';
						$menu .= '&nbsp;&nbsp;<a href="'.$node['link'].'">'.$node['name'].'</a>';
						$menu .='<br />';
						foreach ($node['leaves'] as $leavekey => $leave) {
							$menu .= '&nbsp;&nbsp;&nbsp;&nbsp;<img src="'.XOOPS_URL.'/images/menu/tree_content.gif">';
						$menu .= '&nbsp;&nbsp;<a href="'.$leave['link'].'">'.$leave['name'].'</a>';
							if ($leavekey+1 != count($node['leaves'])) $menu .='<br />';
						}
					} else {
						$menu .= '<img src="'.XOOPS_URL.'/images/menu/tree_close.gif">';
						$menu .= '&nbsp;&nbsp;<a href="'.$node['link'].'">'.$node['name'].'</a>';
					}
				} else {
					$menu .= '<img src="'.XOOPS_URL.'/images/menu/tree_close.gif">';
					$menu .= '&nbsp;&nbsp;<a href="'.$node['link'].'">'.$node['name'].'</a>';
				}
				if ($nodekey+1 != count($tree)) $menu .='<br />';
			}
			$block['content'] = $menu;
			break;


			case 3:
			$block['content'] = "
					<script type='text/javascript'>
					function toggle_dynmenu(id)  {
						if (xoopsGetElementById(id + '_sub')) {
							var dom = xoopsGetElementById(id + '_sub').style.display;
                            if (dom == 'none') {
				xoopsGetElementById(id + '_sub').style.display = 'inline';
                                xoopsGetElementById(id + '_sign').src   = '".XOOPS_URL."/images/menu/minus.gif';
                                xoopsGetElementById(id + '_folder').src = '".XOOPS_URL."/images/menu/tree_open.gif';
				} else if (dom == 'inline') {
					xoopsGetElementById(id + '_sub').style.display = 'none';
                                	xoopsGetElementById(id + '_sign').src   = '".XOOPS_URL."/images/menu/plus.gif';
                                	xoopsGetElementById(id + '_folder').src = '".XOOPS_URL."/images/menu/tree_close.gif';
                            }
                         }
                    }
                    </script>";

			$block['content'] .= "<img src='".XOOPS_URL."/images/menu/d_tree_home.gif' alt='' /> <a href='".XOOPS_URL."/'>"._MB_SYSTEM_HOME."</a>";

			foreach ($array as $myrow) {
				if ( @file_exists(XOOPS_ROOT_PATH.'/modules/'.$myrow['dirname'].'/xoops_version.php') ) {
					$module = new XoopsModule($myrow);
					if ($sublinks = $module->subLink()) {
						
						$subcont[] = $myrow['dirname'];
						$block['content'] .= "<br /><a href='javascript:toggle_dynmenu(\"".$myrow['dirname']."\");'><img id='".$myrow['dirname']."_sign' name='".$myrow['dirname']."_sign' src='".XOOPS_URL."/images/menu/minus.gif' height='9' width='9' align='absmiddle' border='0' /></a> ";
						$block['content'] .= "<img id='".$myrow['dirname']."_folder' name='".$myrow['dirname']."_folder' src='".XOOPS_URL."/images/menu/tree_open.gif' alt='' /> ".$module->mainLink()."";
						$block['content'] .= "<span id='".$myrow['dirname']."_sub' name='".$myrow['dirname']."_sub' style='display:inline;'>";
						foreach($sublinks as $sublink) {
                        $block['content'] .= "<br /> <img src='".XOOPS_URL."/images/menu/d_tree_content.gif' alt='' /> ".$sublink;
						}
						$block['content'] .= "</span>";
					} else {
						$block['content'] .= "<br /><img src='".XOOPS_URL."/images/menu/d_tree_close.gif' alt='' /> ".$module->mainLink()."\n";
					}
				}
			}

			if ( !empty($subcont) ) {
				$block['content'] .= "<script type='text/javascript'>\n";
				foreach($subcont as $value) {
					if (!$xoopsModule || ($xoopsModule->dirname() != $value)) {
                    $block['content'] .= "toggle_dynmenu('$value');\n";
					}
				}
				$block['content'] .= "</script>";
			}
			break;


			case 4:
				include_once(XOOPS_ROOT_PATH.'/class/jsmenu/jstree.class.php');
				$mainmenu = New jstree('mainmenu', $tree);
				$block['content'] = $mainmenu->render();
			break;
		}
    return $block;
	}
}


/**
 * @return unknown
 * @param unknown $options
 * @desc Enter description here...
*/
function b_system_main_edit($options)
{
    $form  = _MB_SYSTEM_MENU_LOOK."&nbsp;
        <select name='options[]' class='select'>";
    $chk = ( $options[0] == 1 ) ? " selected" : "";
    $form .= '<option value ="1"'.$chk.'>'._MB_SYSTEM_MENU_JAVADYN.'</option>';
    $chk = ( $options[0] == 2 ) ? " selected" : "";
    $form .= '<option value ="2"'.$chk.'>'._MB_SYSTEM_MENU_PLAIN.'</option>';
    $chk = ( $options[0] == 3 ) ? " selected" : "";
    $form .= '<option value ="3"'.$chk.'>'._MB_SYSTEM_MENU_OLDDYN.'</option>';
    $chk = ( $options[0] == 4 ) ? " selected" : "";
    $form .= '<option value ="4"'.$chk.'>'._MB_SYSTEM_MENU_NEWDYN.'</option>
        </select>';
    return $form;
}
?>