<?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;
}

// 参数取得
$theme_type = ParamUtil::getRequestNumber("theme_type", 0);
$id = ParamUtil::getRequestNumber("id", 0);
$title = ParamUtil::getRequestString("title");
$upload_thumbnail_file = FileUploadUtil::getScalarFile($_FILES["upload_thumbnail_file"]);
$comment = ParamUtil::getRequestString("comment");

$word_theme_mst = WordThemeMst::getById($id);
if (empty($word_theme_mst)) {
	$word_theme_mst = new WordThemeMst();
	$word_theme_mst->display_order = 1;
}

$error_flg = false;
// 图片Check
$upload_thumbnail = null;
if ($upload_thumbnail_file->size > 0) {
	
	$image_type = ImageType::getByHeader($upload_thumbnail_file->type);
	print_r($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 ="THEME_" . date("YmdHis") . "." . $image_type->extention;
		$dest_file =  THEME_IMAGES_PATH . "/" . $upload_thumbnail;
		move_uploaded_file($upload_thumbnail_file->tmp_name, $dest_file);
		chmod($dest_file, 0777);
		//删除老的文件
		@unlink(THEME_IMAGES_PATH . "/" . $word_theme_mst->thumbnail);
		//更新文件名
		$word_theme_mst->front = $upload_thumbnail;
	}
}

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

// 更新数据库
$word_theme_mst->theme_type = $theme_type;
$word_theme_mst->title = $title;
$word_theme_mst->comment = $comment;
$word_theme_mst->save();

// 跳到一览页
header("Location:word_theme_list.php?theme_type=" . $word_theme_mst->theme_type);
exit;