<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../../core.php";
} 
saxue_checkpower('membermenu');
if ( !isset( $_REQUEST['action'] ) ) $_REQUEST['action'] = '';
include SAXUE_ROOT_PATH . "/common/cache.php";
include SAXUE_ROOT_PATH . "/model/member_menu.php";
$data_handler = &saxuemembermenuhandler :: getinstance( "saxuemembermenuhandler" );
switch ( $_REQUEST['action'] ) {
		case "add" :
				$row = array();
				if ( isset( $_REQUEST['menuid'] ) && is_numeric( $_REQUEST['menuid'] ) ) {
						$obj = $data_handler -> get( $_REQUEST['menuid'] );
						if ( !is_object( $obj ) ) {
								saxue_printfail( LANG_ERROR_PARAMETER );
						} 
						$row = $obj -> getvars( "n" );
				} elseif ( isset( $_REQUEST['pid'] ) && is_numeric( $_REQUEST['pid'] ) && $_REQUEST['pid'] > 0 ) {
						$obj = $data_handler -> get( $_REQUEST['pid'] );
						if ( !is_object( $obj ) ) {
								saxue_printfail( LANG_ERROR_PARAMETER );
						} 
						$row['pid'] = $_REQUEST['pid'];
						$row['level'] = $obj -> getvar( 'level' ) + 1;
				} else {
						$row['pid'] = 0;
						$row['level'] = 1;
				} 
				if ( isset( $_POST['dosubmit'] ) ) {
						$_POST['caption'] = trim( $_POST['caption'] );
						$_POST['command'] = trim( $_POST['command'] );
						$_POST['listorder'] = intval( $_POST['listorder'] );
						if ( isset( $_REQUEST['menuid'] ) ) {
								$data = $data_handler -> create( false );
						} else {
								$data = $data_handler -> create();
								$data -> setvar( "pid", $row['pid'] );
								$data -> setvar( "level", $row['level'] );
						} 
						if ( !$data_handler -> insert( $data ) ) {
								exit( json_encode( array( 'flag' => 0, 'msg' => LANG_ERROR_DATABASE ) ) );
						} 
						cache_member_menu();
						exit( json_encode( array( 'flag' => 1 ) ) );
				} 
				include_once( SAXUE_ADMIN_PATH . "/header.php" );
				$saxueTpl -> assign_by_ref( "row", $row );
				$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/member/templates/menu_add.html";
				include_once( SAXUE_ADMIN_PATH . "/footer.php" );
				exit;
		case "del" :
				if ( isset( $_REQUEST['menuid'] ) && is_numeric( $_REQUEST['menuid'] ) ) {
						if ( 0 < $data_handler -> getcount( new criteria( 'pid', $_REQUEST['id'] ) ) ) {
								saxue_printfail( "请先删除子菜单" );
						} 
						$data_handler -> delete( $_REQUEST['menuid'] );
						cache_member_menu();
				} 
				saxue_jumppage( "menu.php", LANG_DO_SUCCESS );
				break;
		case "display" :
				if ( isset( $_REQUEST['menuid'] ) && is_numeric( $_REQUEST['menuid'] ) && isset( $_REQUEST['display'] ) && is_numeric( $_REQUEST['display'] ) ) {
						$data_handler -> updatefields( 'display=' . $_REQUEST['display'], 'menuid=' . $_REQUEST['menuid'] );
						cache_member_menu();
				} 
				saxue_jumppage( "menu.php", LANG_DO_SUCCESS );
				break;
		case "listorder" :
				if ( isset( $_POST['listorder'] ) && is_array( $_POST['listorder'] ) ) {
						foreach( $_POST['listorder'] as $k => $v ) {
								$data_handler -> updatefields( 'listorder=' . intval( $v ), 'menuid=' . $k );
						} 
						cache_member_menu();
				} 
				saxue_jumppage( "menu.php", LANG_DO_SUCCESS );
				break;
		case "cache" :
				cache_member_menu();
				saxue_jumppage( "menu.php", LANG_DO_SUCCESS );
				break;
} 

include_once( SAXUE_ADMIN_PATH . "/header.php" );
include_once( SAXUE_ROOT_PATH . "/lib/util/tree.php" );
$tree = new tree;
$tree -> icon = array( '&nbsp;&nbsp;&nbsp;<span class="f_tree">│</span>&nbsp;', '&nbsp;&nbsp;&nbsp;<span class="f_tree">├─</span> ', '&nbsp;&nbsp;&nbsp;<span class="f_tree">└─</span> ' );
$tree -> nbsp = '&nbsp;&nbsp;&nbsp;';
$tree -> mid = 'menuid';
$tree -> pid = 'pid';
$criteria = new criteriacompo();
$criteria -> setsort( "pid ASC,listorder ASC,menuid" );
$criteria -> setorder( "ASC" );
$data_handler -> queryobjects( $criteria );
$menurows = array();
while ( $v = $data_handler -> getobject() ) {
		$k = $v -> getvar( "menuid" );
		$menurows[$k]['menuid'] = $v -> getvar( "menuid" );
		$menurows[$k]['pid'] = $v -> getvar( "pid" );
		$menurows[$k]['listorder'] = $v -> getvar( "listorder" );
		$menurows[$k]['caption'] = $v -> getvar( "caption" );
		if ( $v -> getvar( "display" ) == 1 ) {
				$menurows[$k]['str_display'] = "<a href=\"?action=display&display=0&menuid=" . $k . "\">" . saxue_geticon( 'status', '显示' ) . "</a>";
		} else {
				$menurows[$k]['str_display'] = "<a href=\"?action=display&display=1&menuid=" . $k . "\">" . saxue_geticon( 'status', '不显示', 0 ) . "</a>";
		} 
		if ( $v -> getvar( "level" ) < 4 ) {
				$menurows[$k]['str_manage'] = "<a href=\"javascript:doDialog('" . SAXUE_URL . "/member/admin/menu.php?action=add&pid=" . $k . "','添加子菜单','640','200')\">" . saxue_geticon( 'add', '添加子菜单' ) . "</a>";
		} else {
				$menurows[$k]['str_manage'] = saxue_geticon( 'add', '添加子菜单', 0 );
		} 
		$menurows[$k]['str_manage'] .= "<a href=\"javascript:doDialog('" . SAXUE_URL . "/member/admin/menu.php?action=add&menuid=" . $k . "','修改菜单 —— " . $menurows[$k]['caption'] . "','640','200')\">" . saxue_geticon( 'edit', '修改菜单' ) . "</a><a href=\"" . SAXUE_URL . "/member/admin/menu.php?action=del&menuid=" . $k . "\" onclick=\"return confirm('您确定要删除此菜单吗？')\">" . saxue_geticon( 'del', '删除菜单' ) . "</a>";
} 
$str = "<tr class='rowbg'>
			<td><input class='text' name='listorder[\$menuid]' type='text' size='3' value='\$listorder' onkeyup=\\\"value=value.replace(/[^0-9]/g,'');\\\"></td>
			<td>\$menuid</td>
			<td align='left'>\$spacer\$caption</td>
			<td>\$str_display</td>
			<td>\$str_manage</td>
		</tr>";
$tree -> init( $menurows );
$menus = $tree -> get_tree( 0, $str );
$saxueTpl -> assign( "menus", $menus );
$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/member/templates/menu.html";
include_once( SAXUE_ADMIN_PATH . "/footer.php" );
