<?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");
$upload_thumbnail_file = FileUploadUtil::getScalarFile($_FILES["upload_thumbnail_file"]);
$is_valid = ParamUtil::getRequestBoolean("is_valid", true);
$user_status_array = ParamUtil::getRequestArray("user_status", array());

$user_status_str = "|" . implode("|", $user_status_array) . "|";

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

$error_flg = false;
// 图片Check
$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 ="CATEGORY_" . date("YmdHis") . "." . $image_type->extention;
		$dest_file =  ARTICLE_IMAGES_PATH . "/" . $upload_thumbnail;
		move_uploaded_file($upload_thumbnail_file->tmp_name, $dest_file);
		chmod($dest_file, 0777);
		//删除老的文件
		@unlink(ARTICLE_IMAGES_PATH . "/" . $category_mst->thumbnail);
		//更新文件名
		$category_mst->thumbnail = $upload_thumbnail;
	}
}

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

// 更新数据库
$category_mst->title = $title;
$category_mst->is_valid = $is_valid;
$category_mst->user_status = $user_status_str;
$category_mst->save();

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