<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../core.php";
} 
saxue_checkpower( 'timthumb' );
if ( !isset( $_REQUEST['action'] ) ) $_REQUEST['action'] = '';
include_once( SAXUE_ROOT_PATH . "/common/cache.php" );
include_once( SAXUE_ROOT_PATH . "/model/timthumb.php" );
$data_handler = &saxuetimthumbhandler :: getinstance( "saxuetimthumbhandler" );
switch ( $_REQUEST['action'] ) {
		case 'add':
				$row = array();
				if ( isset( $_REQUEST['tid'] ) && is_numeric( $_REQUEST['tid'] ) ) {
						$obj = $data_handler -> get( $_REQUEST['tid'] );
						if ( !is_object( $obj ) ) {
								saxue_printfail( LANG_ERROR_PARAMETER );
						} 
						$row = $obj -> getvars( "n" );
				} 
				if ( isset( $_POST['dosubmit'] ) ) {
						$_POST['tname'] = strtolower( trim( $_POST['tname'] ) );
						if ( strlen( $_POST['tname'] ) == 0 ) {
								exit( json_encode( array( 'flag' => 0, 'msg' => '方案标识不能为空' ) ) );
						} elseif ( !preg_match( "/^[a-z]+$/i", $_POST['tname'] ) ) {
								exit( json_encode( array( 'flag' => 0, 'msg' => '方案标识格式错误' ) ) );
						} 
						if ( $_POST['tname'] != $row['tname'] && 0 < $data_handler -> getcount( new criteria( 'tname', $_POST['tname'] ) ) ) {
								exit( json_encode( array( 'flag' => 0, 'msg' => '方案标识已存在' ) ) );
						}
						$_POST['width'] = intval( $_POST['width'] );
						$_POST['height'] = intval( $_POST['height'] );
						if ( !$_POST['width'] && !$_POST['height'] ) {
								exit( json_encode( array( 'flag' => 0, 'msg' => '缩略图大小至少设置一个值' ) ) );
						}
						if ( isset( $_REQUEST['tid'] ) ) {
								$data = $data_handler -> create( false );
						} else {
								$data = $data_handler -> create();
						} 
						if ( !$data_handler -> insert( $data ) ) {
								exit( json_encode( array( 'flag' => 0, 'msg' => LANG_ERROR_DATABASE ) ) );
						} 
						cache_timthumb();
						exit( json_encode( array( 'flag' => 1 ) ) );
				} 
				include_once( SAXUE_ROOT_PATH . "/common/funpost.php" );
				include_once( SAXUE_ADMIN_PATH . "/header.php" );
				$saxueTpl -> assign_by_ref( "row", $row );
				$saxueTset['saxue_contents_template'] = SAXUE_ADMIN_PATH . "/templates/timthumb_add.html";
				include_once( SAXUE_ADMIN_PATH . "/footer.php" );
				exit;
				break;
		case "delete" :
				if ( isset( $_REQUEST['tid'] ) && is_numeric( $_REQUEST['tid'] ) ) {
						$obj = $data_handler -> get( $_REQUEST['tid'] );
						if ( !is_object( $obj ) ) {
								saxue_printfail( LANG_ERROR_PARAMETER );
						} 
						$dir = $obj -> getvar( 'tname' );
						$data_handler -> delete( $_REQUEST['tid'] );
						cache_timthumb();
						$path = saxue_uploadpath( $dir );
						if ( is_dir( $path ) ) {
								saxue_delfolder( $path, true );
						} 
				} 
				saxue_jumppage( "timthumb.php", LANG_DO_SUCCESS );
				break;
		case "clear" :
				if ( isset( $_REQUEST['tid'] ) && is_numeric( $_REQUEST['tid'] ) ) {
						$obj = $data_handler -> get( $_REQUEST['tid'] );
						if ( !is_object( $obj ) ) {
								saxue_printfail( LANG_ERROR_PARAMETER );
						} 
						$dir = $obj -> getvar( 'tname' );
						$path = saxue_uploadpath( $dir );
						if ( is_dir( $path ) ) {
								saxue_delfolder( $path, true );
						} 
				} 
				saxue_jumppage( "timthumb.php", LANG_DO_SUCCESS );
				break;
		case "status" :
				if ( isset( $_REQUEST['tid'] ) && is_numeric( $_REQUEST['tid'] ) && isset( $_REQUEST['status'] ) && is_numeric( $_REQUEST['status'] ) ) {
						$data_handler -> updatefields( 'status=' . $_REQUEST['status'], 'tid=' . $_REQUEST['tid'] );
						cache_timthumb();
				} 
				saxue_jumppage( "timthumb.php", LANG_DO_SUCCESS );
				break;
} 
include_once( SAXUE_ADMIN_PATH . "/header.php" );
$data_handler -> queryobjects( new criteriacompo() );
$rows = array();
$k = 0;
while ( $v = $data_handler -> getobject() ) {
		$rows[$k] = $v -> getvars( "n" );
		if ( $rows[$k]['status'] == 1 ) {
				$rows[$k]['str_status'] = "<a href=\"?action=status&status=0&tid=" . $rows[$k]['tid'] . "\">" . saxue_geticon( 'status', '启用' ) . "</a>";
		} else {
				$rows[$k]['str_status'] = "<a href=\"?action=status&status=1&tid=" . $rows[$k]['tid'] . "\">" . saxue_geticon( 'status', '禁用', 0 ) . "</a>";
		} 
		++$k;
} 
$saxueTpl -> assign( "rows", $rows );
$saxueTset['saxue_contents_template'] = SAXUE_ADMIN_PATH . "/templates/timthumb.html";
include_once( SAXUE_ADMIN_PATH . "/footer.php" );
