<?php
/**
 * 商品分类管理
 * $Id: product_category_edit_input.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;
}

// 参数取得
$action_type = ParamUtil::getRequestString("action_type");
$category_id = ParamUtil::getRequestNumber("category_id", 0);
$id = ParamUtil::getRequestNumber("id", 0);
$title = ParamUtil::getRequestString("title");
$video_url = ParamUtil::getRequestString("video_url");
$mp3 = ParamUtil::getRequestString("mp3");
$comment = ParamUtil::getRequestString("comment");

//图片getArrayFile
$upload_thumbnail_file = FileUploadUtil::getScalarFile($_FILES["upload_thumbnail_file"]);

//编辑
if($action_type == "new") {
	$article_dat = new ArticleDat();
	//获取最大ID
	$display_order = 1;
	$tmp_list = ArticleDat::getList(null,"id", "desc", 0, 1);
	if(!empty($tmp_list)) {
		$display_order = $tmp_list[0]->id + 1;
	}
	$article_dat->display_order = $display_order;
}
if($action_type == "edit") {
	$article_dat = ArticleDat::getById($id);
}
$article_dat->category_id = $category_id;
$article_dat->title = $title;
$article_dat->mp3 = $mp3;
$article_dat->video_url = $video_url;
$article_dat->comment = $comment;

$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 ="ARTICLE_" . 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 . "/" . $article_dat->front_image);
		//更新文件名
		$article_dat->front_image = $upload_thumbnail;
	}
}
$article_dat->save();

header("Location:article_dat_list.php?category_id=" . $article_dat->category_id);
exit;