<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../core.php";
} 
saxue_checkpower( 'friendlink' );
if ( !isset( $_REQUEST['action'] ) ) $_REQUEST['action'] = '';
include_once( SAXUE_ROOT_PATH . "/common/cache.php" );
include_once( SAXUE_ROOT_PATH . "/model/friendlink.php" );
$data_handler = &saxuefriendlinkhandler :: getinstance( "saxuefriendlinkhandler" );
switch ( $_REQUEST['action'] ) {
		case 'add':
				$row = array();
				if ( isset( $_REQUEST['linkid'] ) && is_numeric( $_REQUEST['linkid'] ) ) {
						$obj = $data_handler -> get( $_REQUEST['linkid'] );
						if ( !is_object( $obj ) ) {
								saxue_printfail( '友情链接不存在' );
						}
						$row = $obj -> getvars( "n" );
				} 
				if ( isset( $_POST['dosubmit'] ) ) {
						$_POST['title'] = trim( $_POST['title'] );
						$_POST['url'] = trim( $_POST['url'] );
						$_POST['logo'] = trim( $_POST['logo'] );
						$_POST['type'] = intval( $_POST['type'] );
						$_POST['listorder'] = intval( $_POST['listorder'] );
						if ( empty( $_POST['logo'] ) ) {
								$_POST['type'] = 0;
						}
						if ( isset( $_REQUEST['linkid'] ) ) {
								$data = $data_handler -> create( false );
						} else {
								$_POST['addtime'] = SAXUE_NOW_TIME;
								$data = $data_handler -> create();
						} 
						if ( !$data_handler -> insert( $data ) ) {
								exit( json_encode( array( 'flag' => 0, 'msg' => LANG_ERROR_DATABASE ) ) );
						} 
						cache_friendlink();
						exit( json_encode( array( 'flag' => 1 ) ) );
				} 
				include_once( SAXUE_ADMIN_PATH . "/header.php" );
				$saxueTpl -> assign_by_ref( "row", $row );
				$saxueTset['saxue_contents_template'] = SAXUE_ADMIN_PATH . "/templates/friendlink_add.html";
				include_once( SAXUE_ADMIN_PATH . "/footer.php" );
				exit;
		case "display" :
				if ( isset( $_REQUEST['linkid'] ) && is_numeric( $_REQUEST['linkid'] ) && isset( $_REQUEST['display'] ) && is_numeric( $_REQUEST['display'] ) ) {
						$data_handler -> updatefields( 'display=' . $_REQUEST['display'], 'linkid=' . $_REQUEST['linkid'] );
						cache_friendlink();
				} 
				saxue_jumppage( 'friendlink.php', LANG_DO_SUCCESS );
		case 'listorder':
				if ( !empty( $_REQUEST['listorder'] ) ) {
						foreach ( $_REQUEST['listorder'] as $linkid => $listorder ) {
								$linkid = intval( $linkid );
								$listorder = intval( $listorder );
								$data_handler -> updatefields( array( 'listorder' => $listorder ), 'linkid=' . $linkid );
						}
						cache_friendlink();
				}
				saxue_jumppage( 'friendlink.php', LANG_DO_SUCCESS );
		case 'delete':
				if ( isset( $_REQUEST['linkid'] ) && is_numeric( $_REQUEST['linkid'] ) ) {
						$data_handler -> delete( $_REQUEST['linkid'] );
						cache_friendlink();
				} 
				saxue_jumppage( 'friendlink.php', LANG_DO_SUCCESS );
		case 'cache':
				cache_friendlink();
				saxue_jumppage( 'friendlink.php', LANG_DO_SUCCESS );
}
$criteria = new criteriacompo();
$criteria -> setsort( "listorder" );
$criteria -> setorder( "ASC" );
$data_handler -> queryobjects( $criteria );
$rows = array();
$k = 0;
while ( $v = $data_handler -> getobject() ) {
		$rows[$k] = $v -> getvars( "n" );
		++$k;
}
include_once( SAXUE_ADMIN_PATH . "/header.php" );
$saxueTpl -> assign( "rows", $rows );
$saxueTset['saxue_contents_template'] = SAXUE_ADMIN_PATH . "/templates/friendlink.html";
include_once( SAXUE_ADMIN_PATH . "/footer.php" );
