<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../core.php";
} 
saxue_checkpower( 'pages' );
if ( !isset( $_REQUEST['action'] ) ) $_REQUEST['action'] = '';
include_once( SAXUE_ROOT_PATH . "/common/cache.php" );
include_once( SAXUE_ROOT_PATH . "/common/funpost.php" );
include_once( SAXUE_ROOT_PATH . "/model/pages.php" );
$data_handler = &saxuepageshandler :: getinstance( "saxuepageshandler" );
switch ( $_REQUEST['action'] ) {
		case 'preview':
				$id = intval( $_REQUEST['id'] );
				if ( empty( $id ) ) {
						saxue_printfail( '参数错误' );
				}
				$obj = $data_handler -> get( $id );
				if ( !is_object( $obj ) ) {
						saxue_printfail( '单页不存在' );
				}
				$row = $obj -> getvars( "n" );
				saxue_getconfigs( 'pages' );
				include_once( SAXUE_ROOT_PATH . '/lib/template/template.php' );
				$saxueTpl = &saxuetpl :: getinstance();
				saxue_getconfigs( 'channel' );
				$saxueTpl -> assign_by_ref( "channel", $saxueChannel );
				$saxueTpl -> assign_by_ref( "row", $row );
				$saxueTpl -> assign_by_ref( "pages", $saxuePages );
				$saxueTpl -> setcaching( 0 );
				if ( !empty( $_REQUEST['mobile'] ) ) {
						if ( !empty( $row['mtemplate'] ) ) {
								if ( SAXUE_MTHEME_SET == '' ) {
										$mtemp_path = SAXUE_ROOT_PATH . '/templates/m/pages/' . $row['mtemplate'];
								} else {
										$mtemp_path = SAXUE_ROOT_PATH . '/templates/' . SAXUE_MTHEME_SET . '/pages/' . $row['mtemplate'];
								}
								if ( SAXUE_MSKIN_SET == '' ) {
										$saxueTpl -> assign( "saxue_skin_url", SAXUE_SKIN_SERVER . '/m' );
								} else {
										$saxueTpl -> assign( "saxue_skin_url", SAXUE_SKIN_SERVER . '/' . SAXUE_MSKIN_SET );
								}
								$saxueTpl -> display( $mtemp_path );
						} else {
								saxue_printfail( '没有设置手机端模版' );
						}
				} else {
						if ( !empty( $row['template'] ) ) {
								$saxueTpl -> display( SAXUE_THEME_PATH . '/pages/' . $row['template'] );
						} else {
								saxue_printfail( '没有设置PC端模版' );
						}
				}
				exit;
		case 'html':
				if ( !is_array( $_POST['ids'] ) || count( $_POST['ids'] ) == 0 ) {
						saxue_printfail( '请选择单页' );
				}
				saxue_getconfigs( 'pages' );
				include_once( SAXUE_ROOT_PATH . '/lib/template/template.php' );
				$saxueTpl = &saxuetpl :: getinstance();
				$saxueTpl -> setcaching( 0 );
				saxue_getconfigs( 'channel' );
				$saxueTpl -> assign_by_ref( "channel", $saxueChannel );
				$saxueTpl -> assign_by_ref( "pages", $saxuePages );
				$data_handler -> queryobjects( new criteria( 'id', '(' . implode( ',', $_POST['ids'] ) . ')', 'IN' ) );
				while ( $v = $data_handler -> getobject() ) {
						$row = $v -> getvars( "n" );
						$saxueTpl -> assign_by_ref( "row", $row );
						$filepath = saxue_filepath( $row['htmldir'] );
						saxue_checkdir( $filepath, true );
						// PC端
						if ( !empty( $row['template'] ) ) {
								if ( empty( $row['filename'] ) ) {
										$row['filename'] = $row['id'] . '.html';
								}
								$saxueTpl -> assign( "saxue_skin_url", SAXUE_SKIN_URL );
								saxue_writefile( $filepath . '/' . $row['filename'], $saxueTpl -> fetch( SAXUE_THEME_PATH . '/pages/' . $row['template'] ) );
						}
						// 手机端
						if ( !empty( $row['mtemplate'] ) ) {
								if ( empty( $row['mfilename'] ) ) {
										$row['mfilename'] = $row['id'] . '.htm';
								}
								if ( SAXUE_MTHEME_SET == '' ) {
										$mtemp_path = SAXUE_ROOT_PATH . '/templates/m/pages/' . $row['mtemplate'];
								} else {
										$mtemp_path = SAXUE_ROOT_PATH . '/templates/' . SAXUE_MTHEME_SET . '/pages/' . $row['mtemplate'];
								}
								if ( SAXUE_MSKIN_SET == '' ) {
										$saxueTpl -> assign( "saxue_skin_url", SAXUE_SKIN_SERVER . '/m' );
								} else {
										$saxueTpl -> assign( "saxue_skin_url", SAXUE_SKIN_SERVER . '/' . SAXUE_MSKIN_SET );
								}
								saxue_writefile( $filepath . '/' . $row['mfilename'], $saxueTpl -> fetch( $mtemp_path ) );
						}
				}
				saxue_jumppage( 'pages.php', LANG_DO_SUCCESS );
		case 'add':
				$row = array();
				if ( isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ) {
						$obj = $data_handler -> get( $_REQUEST['id'] );
						if ( !is_object( $obj ) ) {
								saxue_printfail( '单页不存在' );
						}
						$row = $obj -> getvars( "e" );
				} else {
						$obj = $data_handler -> getlast();
						if ( is_object( $obj ) ) {
								$row = $obj -> getvars( "n" );
								$row['id'] = 0;
								$row['title'] = $row['content'] = $row['filename'] = '';
						}
				}
				if ( isset( $_POST['dosubmit'] ) ) {
						$_POST['item'] = strtolower( trim( $_POST['item'] ) );
						$_POST['title'] = trim( $_POST['title'] );
						$_POST['content'] = trim( $_POST['content'] );
						$_POST['htmldir'] = trim( $_POST['htmldir'] );
						$_POST['htmlurl'] = trim( $_POST['htmlurl'] );
						$_POST['filename'] = trim( $_POST['filename'] );
						$_POST['template'] = trim( $_POST['template'] );
						$_POST['mfilename'] = trim( $_POST['mfilename'] );
						$_POST['mtemplate'] = trim( $_POST['mtemplate'] );
						if ( !empty( $_POST['template'] ) && $_POST['filename'] == $_POST['mfilename'] ) {
								saxue_printfail( 'PC端和手机端文件名不能相同' );
						}
						if ( isset( $_REQUEST['id'] ) ) {
								$data = $data_handler -> create( false );
						} else {
								$data = $data_handler -> create();
						} 
						if ( !$data_handler -> insert( $data ) ) {
								saxue_printfail( LANG_ERROR_DATABASE );
						} 
						cache_pages();
						saxue_jumppage( 'pages.php', LANG_DO_SUCCESS );
				} 
				$temp_path = SAXUE_THEME_PATH . '/pages';
				$temp_list = glob( $temp_path . '/*.html' );
				$temps = array();
				if ( $temp_list ) {
						foreach( $temp_list as $file ) {
								$temps[] = basename( $file );
						}
				}
				if ( SAXUE_MTHEME_SET == '' ) {
						$mtemp_path = SAXUE_ROOT_PATH . '/templates/m/pages';
				} else {
						$mtemp_path = SAXUE_ROOT_PATH . '/templates/' . SAXUE_MTHEME_SET . '/pages';
				}
				$mtemp_list = glob( $mtemp_path . '/*.html' );
				$mtemps = array();
				if ( $mtemp_list ) {
						foreach( $mtemp_list as $file ) {
								$mtemps[] = basename( $file );
						}
				}
				include_once( SAXUE_ADMIN_PATH . "/header.php" );
				$saxueTpl -> assign_by_ref( "row", $row );
				$saxueTpl -> assign_by_ref( "temps", $temps );
				$saxueTpl -> assign_by_ref( "mtemps", $mtemps );
				$saxueTpl -> assign( "mtemp_path", $mtemp_path );
				$saxueTset['saxue_contents_template'] = SAXUE_ADMIN_PATH . "/templates/pages_add.html";
				include_once( SAXUE_ADMIN_PATH . "/footer.php" );
				exit;
		case 'listorder':
				if ( !empty( $_REQUEST['listorder'] ) ) {
						foreach ( $_REQUEST['listorder'] as $id => $listorder ) {
								$id = intval( $id );
								$listorder = intval( $listorder );
								$data_handler -> updatefields( array( 'listorder' => $listorder ), 'id=' . $id );
						}
						cache_pages();
				}
				saxue_jumppage( 'pages.php', LANG_DO_SUCCESS );
		case 'delete':
				if ( isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ) {
						$data_handler -> delete( $_REQUEST['id'] );
						cache_pages();
				} 
				saxue_jumppage( 'pages.php', LANG_DO_SUCCESS );
		case 'cache':
				cache_pages();
				saxue_jumppage( 'pages.php', LANG_DO_SUCCESS );
}
$criteria = new criteriacompo();
$criteria -> setsort( "item ASC,listorder" );
$criteria -> setorder( "ASC" );
$data_handler -> queryobjects( $criteria );
$rows = array();
$k = 0;
while ( $v = $data_handler -> getobject() ) {
		$rows[$k] = $v -> getvars( "n" );
		if ( empty( $rows[$k]['filename'] ) ) {
				$rows[$k]['filename'] = $rows[$k]['id'] . '.html';
		}
		$rows[$k]['url'] = saxue_fileurl( $rows[$k]['htmldir'], $rows[$k]['htmlurl'] ) . '/' . $rows[$k]['filename'];
		++$k;
}
include_once( SAXUE_ADMIN_PATH . "/header.php" );
$saxueTpl -> assign( "rows", $rows );
$saxueTset['saxue_contents_template'] = SAXUE_ADMIN_PATH . "/templates/pages.html";
include_once( SAXUE_ADMIN_PATH . "/footer.php" );
