<?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::getRequestString("id");
$title = ParamUtil::getRequestString("title");
$comment = ParamUtil::getRequestString("comment");
$price = ParamUtil::getRequestNumber("price", 0);
$original_price = ParamUtil::getRequestNumber("original_price", 0);

$is_free3 = ParamUtil::getRequestNumber("is_free3", 0);
$is_free6 = ParamUtil::getRequestNumber("is_free6", 0);
$is_free12 = ParamUtil::getRequestNumber("is_free12", 0);

$upload_thumbnail_file = FileUploadUtil::getScalarFile($_FILES["upload_thumbnail_file"]);
$upload_thumbnail_file2 = FileUploadUtil::getScalarFile($_FILES["upload_thumbnail_file2"]);

$course_mst = CourseMst::getById($id);
if (empty($course_mst)) {
	$course_mst = new CourseMst();
	//获取最大的id
	$display_order = 1;
	$tmp_list = CourseMst::getList(null,"display_order", "desc", 0, 1);
	if(!empty($tmp_list)) {
		$display_order = $tmp_list[0]->display_order + 1;
	}
	$course_mst->display_order = $display_order;
}

$error_flg = false;
// 图片Check[banner]
$upload_thumbnail = null;
if ($upload_thumbnail_file->size > 0) {
	$image_type = ImageType::getByHeader($upload_thumbnail_file->type);
	if ($image_type == null || !in_array($image_type->name, array("GIF", "PNG", "JPG"))) {
		$error_flg = true;
		$thumbnail_error_msg = "图片不是指定类型（gif、png、jpg）请重新上传。";
	} else {
		$upload_thumbnail ="COURSE_" . date("YmdHis") . "." . $image_type->extention;
		$dest_file =  COURSE_IMAGES_PATH . "/" . $upload_thumbnail;
		move_uploaded_file($upload_thumbnail_file->tmp_name, $dest_file);
		chmod($dest_file, 0777);
		//删除老的文件
		@unlink(COURSE_IMAGES_PATH . "/" . $course_mst->thumbnail);
		//更新文件名
		$course_mst->thumbnail = $upload_thumbnail;
	}
}

// 图片Check[detail]
$upload_thumbnail2 = null;
if ($upload_thumbnail_file2->size > 0) {
	$image_type = ImageType::getByHeader($upload_thumbnail_file2->type);
	if ($image_type == null || !in_array($image_type->name, array("GIF", "PNG", "JPG"))) {
		$error_flg = true;
		$thumbnail_error_msg = "图片不是指定类型（gif、png、jpg）请重新上传。";
	} else {
		$upload_thumbnail2 ="COURSE_" . date("YmdHis") . "." . $image_type->extention;
		$dest_file =  COURSE_IMAGES_PATH . "/" . $upload_thumbnail2;
		move_uploaded_file($upload_thumbnail_file2->tmp_name, $dest_file);
		chmod($dest_file, 0777);
		//删除老的文件
		@unlink(COURSE_IMAGES_PATH . "/" . $course_mst->detail_image);
		//更新文件名
		$course_mst->detail_image = $upload_thumbnail2;
	}
}

// 有错误的时候表示错误
if ($error_flg) {
	$_SCRIPT_FILE = array("scripts/validators.js", "scripts/course.js");
	$layout_pages = array();
	$layout_pages["top"] = "menu.inc";
	$layout_pages["menu_clicked"] = "li_system";
	$layout_pages["middle"] = "course_edit_input.inc";
	require_once(MANAGER_TEMPLATE_DIR_PATH . "/layout/topmenutopsub_layout.inc");
	exit;
}

// 更新数据库
$course_mst->title = $title;
$course_mst->comment = $comment;
$course_mst->original_price = $original_price;
$course_mst->price = $price;
$course_mst->is_free3 = $is_free3;
$course_mst->is_free6 = $is_free6;
$course_mst->is_free12 = $is_free12;
$course_mst->save();

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