<?php
include "../core.php";
if ( !isset( $_REQUEST['action'] ) )  $_REQUEST['action'] = '';
saxue_getconfigs( "reporttype", "article" );
switch ( $_REQUEST['action'] ) {
		case 'report':
				$_POST['aid'] = intval( $_POST['aid'] );
				if ( empty( $_POST['aid'] ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '文章ID错误' ) ) );
				}
				$_POST['url'] = trim( $_POST['url'] );
				if ( empty( $_POST['url'] ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '文章URL错误' ) ) );
				}
				$_POST['reporttype'] = intval( $_POST['reporttype'] );
				if ( empty( $_POST['reporttype'] ) || !isset( $saxueReporttype[$_POST['reporttype']] ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '请选择举报该文章理由' ) ) );
				}
				$_POST['note'] = trim( $_POST['note'] );
				if ( empty( $_POST['note'] ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '请列举相关证据或链接' ) ) );
				}
				include SAXUE_ROOT_PATH . "/model/article_report.php";
				$report_handler = &saxuearticlereporthandler :: getinstance( "saxuearticlereporthandler" );
				$report = $report_handler -> get( $_POST['aid'] );
				if ( is_object( $report ) ) {
						// 文章已举报
						$newreport = $report_handler -> create( false );
						$reporttimes = $report -> getvar( 'reporttimes' ) + 1;
						$newreport -> setvar( "reporttimes", $reporttimes );
						$newreport -> setvar( "ishandle", 0 );
				} else {
						// 文章首次举报
						$newreport = $report_handler -> create();
				}
				$newreport -> setvar( "reporttime", SAXUE_NOW_TIME );
				if ( !$report_handler -> insert( $newreport ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '举报失败' ) ) );
				} 
				exit( json_encode( array( 'flag' => 1, 'msg' => '举报成功' ) ) );
				break;
		case 'ajaxhtml':
				include SAXUE_WEB_PATH . "/header.php";
				$saxueTpl -> assign_by_ref( "reporttype", $saxueReporttype );
				$saxueTpl -> display( SAXUE_THEME_PATH . "/article/report_ajax.html" );
				break;
		default:
				break;
}