<?php
/**
 * 单词登陆
 * $Id: product_category_edit_result.php,v 1.1 2015/10/08 11:18:46 wanggb Exp $
 * @author wanggb
 * @package manager.public_html
 */

// 底层包含
require_once("manager_include.inc");

// 登录检查
require_once("check_login.inc");

// 权限检查
if (!checkAuthority("ADMIN,MANAGER,STAFF")) {
	// エラー表示
	$layout_pages = array();
	$layout_pages["footer"] = "footer.inc";
	$layout_pages["top"] = "menu.inc";
	$layout_pages["middle"] = "error.inc";
	$message = "权限不足，请联系系统管理员。";
	require_once(MANAGER_TEMPLATE_DIR_PATH . "/layout/topmenutopsub_layout.inc");
	exit;
}

// 参数取得
$id = ParamUtil::getRequestNumber("id", 0);
$category_id = ParamUtil::getRequestString("category_id");
$title = ParamUtil::getRequestString("title");
$selection1 = ParamUtil::getRequestString("selection1");
$selection2 = ParamUtil::getRequestString("selection2");
$selection3 = ParamUtil::getRequestString("selection3");
$selection4 = ParamUtil::getRequestString("selection4");
$comment = ParamUtil::getRequestString("comment");
$level = ParamUtil::getRequestString("level", "N5");
$point = ParamUtil::getRequestNumber("point", 0);
$right_answer = ParamUtil::getRequestNumber("right_answer", 1);
$upload_mp3_file = FileUploadUtil::getScalarFile($_FILES["upload_mp3_file"]);

$subject_dat = SubjectDat::getById($id);
if (empty($subject_dat)) {
	$subject_dat = new SubjectDat();
}

$error_flg = false;
// 图片Check
if ($upload_mp3_file->size > 0) {
	if ($upload_mp3_file->type == null || $upload_mp3_file->type != "audio/mpeg") {
		$error_flg = true;
		$thumbnail_error_msg = "只接受MP3类型。";
	} else {
		$upload_mp3 = date("YmdHis") . ".mp3";
		$dest_file =  SUBJECT_DATA_PATH . "/" . $upload_mp3;

		move_uploaded_file($upload_mp3_file->tmp_name, $dest_file);
		chmod($dest_file, 0777);
		//删除老的文件
		@unlink(SUBJECT_DATA_PATH . "/" . $subject_dat->mp3);
		//更新文件名
		if($category_id == "6") {
			$subject_dat->mp3 = $upload_mp3;
		}
	}
}

if($category_id != "6") {
	$subject_dat->mp3 = "";
}

// 更新数据库
$subject_dat->category_id = $category_id;
$subject_dat->title = $title;
$subject_dat->selection1 = $selection1;
$subject_dat->selection2 = $selection2;
$subject_dat->selection3 = $selection3;
$subject_dat->selection4 = $selection4;
$subject_dat->right_answer = $right_answer;
$subject_dat->comment = $comment;
$subject_dat->level = $level;
$subject_dat->point = $point;
$subject_dat->save();

// 跳到一览页
header("Location:subject_dat_list.php");
exit;