<?php
function saxue_socket_url( $_url ) {
		if ( !function_exists( "fsockopen" ) ) {
				return false;
		}
		$_method = "GET";
		$_arr = parse_url( $_url );
		$_port = isset( $_arr['port'] ) ? $_arr['port'] : 80;
		$_fp = fsockopen( $_arr['host'], $_port, $_errno, $_errstr, 30 );
		if ( !$_fp ) {
				return false;
		}
		$_path = $_arr['path'];
		if ( !empty( $_arr['query'] ) ) {
				$_path .= "?" . $_arr['query'];
		}
		$_str = $_method . " " . $_path;
		$_str .= " HTTP/1.1\r\n";
		$_str .= "Host: " . $_arr['host'] . "\r\n";
		$_str .= "Connection:Close\r\n\r\n";
		fwrite( $_fp, $_str );
		if ( !feof( $_fp ) ) {
				fgets( $_fp, 8 );
		}
		fclose( $_fp );
		return true;
}
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../../core.php";
} 
saxue_checkpower( 'topic' );
include SAXUE_ROOT_PATH . "/model/topic.php";
$topic_handler = &saxuetopichandler :: getinstance( "saxuetopichandler" );
saxue_getconfigs( "cate", "topic" );
saxue_getconfigs( "configs", "topic" );
if ( !empty( $_REQUEST['action'] ) ) {
		if ( empty( $_REQUEST['ids'] ) || !is_array( $_REQUEST['ids'] ) || count( $_REQUEST['ids'] ) == 0 ) {
				saxue_printfail( '请选择专题' );
		}
		$create = false;
		switch ( $_REQUEST['action'] ) {
				case 'setgood':
						$topic_handler -> updatefields( array( 'isgood' => 1 ), 'topicid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						break;
				case 'cacelgood':
						$topic_handler -> updatefields( array( 'isgood' => 0 ), 'topicid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						break;
				case 'setshow':
						$topic_handler -> updatefields( array( 'status' => 1 ), 'topicid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						$create = true;
						break;
				case 'sethide':
						$topic_handler -> updatefields( array( 'status' => 0 ), 'topicid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						break;
				case 'movecate':
						$tocid = intval( $_REQUEST['tocid'] );
						if ( isset( $saxueCate['topic'][$tocid] ) ) {
								$topic_handler -> updatefields( array( 'cid' => $tocid ), 'topicid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
								$create = true;
						}
						break;
				case 'delete':
						$topic_handler -> db -> query( "UPDATE " . saxue_dbprefix( "article" ) . " SET topicid=0 WHERE topicid IN(" . implode( ',', $_REQUEST['ids'] ) . ")" );
						$topic_handler -> db -> query( "DELETE FROM " . saxue_dbprefix( "topic_data" ) . " WHERE topicid IN(" . implode( ',', $_REQUEST['ids'] ) . ")" );
						$topic_handler -> db -> query( "DELETE FROM " . saxue_dbprefix( "topic" ) . " WHERE topicid IN(" . implode( ',', $_REQUEST['ids'] ) . ")" );
						break;
				case 'counts':
						$_topiccount = array();
						$_sql = "SELECT topicid,COUNT(*) as counts FROM " . saxue_dbprefix( "article" ) . " WHERE topicid IN(" . implode( ',', $_REQUEST['ids'] ) . ") GROUP BY topicid";
						$_res = $topic_handler -> db -> query( $_sql );
						while ( $v = $topic_handler -> getobject( $_res ) ) {
								$_topiccount[$v -> getvar( 'topicid' )] = $v -> getvar( 'counts' );
						}
						if ( count( $_topiccount ) > 0 ) {
								foreach ( $_topiccount as $topicid => $counts ) {
										$topic_handler -> updatefields( array( 'counts' => $counts ), 'topicid=' . $topicid );
								}
						}
						break;
		}
		if ( $create && $saxueConfigs['topic']['url_detail'] > 2 ) {
				saxue_socket_url( SAXUE_URL . '/topic/admin/html.php?action=ids&ids=' . implode( ',', $_REQUEST['ids'] ) );
		}
		if ( empty( $_REQUEST['jumpurl'] ) ) {
				saxue_jumppage( 'topic.php', LANG_DO_SUCCESS );
		} else {
				saxue_jumppage( $_REQUEST['jumpurl'], LANG_DO_SUCCESS );
		}
}
if ( empty( $_REQUEST['pagesize'] ) || !is_numeric( $_REQUEST['pagesize'] ) || !in_array( $_REQUEST['pagesize'], array( '20', '30', '50', '100' ) ) ) $_REQUEST['pagesize'] = 30;
if ( empty( $_REQUEST['page'] ) || !is_numeric( $_REQUEST['page'] ) ) $GLOBALS['_REQUEST']['page'] = 1;
$sql = "saxue_topic AS a,saxue_topic_data AS b WHERE 1";
if ( !empty( $_REQUEST['cid'] ) ) {
		$sql .= " AND a.cid=" . $_REQUEST['cid'];
} 
if ( isset( $_REQUEST['status'] ) && in_array( $_REQUEST['status'], array( '0', '1' ) ) ) {
		$sql .= " AND a.status=" . $_REQUEST['status'];
} 
if ( !empty( $_REQUEST['isgood'] ) ) {
		$sql .= " AND a.isgood=1";
} 
if ( !empty( $_REQUEST['title'] ) ) {
		$sql .= " AND b.title REGEXP'" . $_REQUEST['title'] . "'";
} 
$sql .= " AND b.topicid=a.topicid";
$_res = $topic_handler -> db -> query( "SELECT COUNT(*) FROM " . $sql );
if ( !$_res ) {
		$listrows = 0;
} else {
		list( $listrows ) = $topic_handler -> db -> fetchrow( $_res );
} 
$sql = "SELECT a.*,b.* FROM " . $sql;
if ( isset( $_REQUEST['form_sort'] ) && !empty( $_REQUEST['form_sort'] ) ) {
		$sql .= " ORDER BY a." . $_REQUEST['form_sort'];
} else {
		$sql .= " ORDER BY a.topicid";
} 
if ( isset( $_REQUEST['form_order'] ) && !empty( $_REQUEST['form_order'] ) ) {
		$sql .= " " . $_REQUEST['form_order'];
} else {
		$sql .= " DESC";
} 
$sql .= " LIMIT " . ( ( $_REQUEST['page'] - 1 ) * $_REQUEST['pagesize'] ) . ", " . $_REQUEST['pagesize'];
$res = $topic_handler -> db -> query( $sql );
$rows = array();
$k = 0;
while ( $v = $topic_handler -> getobject( $res ) ) {
		$rows[$k] = $v -> getvars( 'n' );
		$rows[$k]['cname'] = $saxueCate['topic'][$rows[$k]['cid']]['cname'];
		$rows[$k]['curl'] = $saxueCate['topic'][$rows[$k]['cid']]['curl'];
		++$k;
} 
include_once( SAXUE_ADMIN_PATH . "/header.php" );
$saxueTpl -> assign_by_ref( "rows", $rows );
$saxueTpl -> assign_by_ref("cate", $saxueCate['topic']);
include_once( SAXUE_ROOT_PATH . "/lib/util/page.php" );
$jumppage = new saxuepage( $listrows, $_REQUEST['pagesize'], $_REQUEST['page'] );
$jumppage -> setlink( "", true, true );
$saxueTpl -> assign( "url_jumppage", $jumppage -> whole_bar() );
$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/topic/templates/topic.html";
include_once( SAXUE_ADMIN_PATH . "/footer.php" );