<?php
/**
 * 视频编辑
 * $Id: media_upload_result.php,v 1.1 2015/10/08 11:18:46 wanggb Exp $
 * @author lixq
 * @package manager.public_html
 */

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

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

// 权限检查
if (!checkAuthority("11")) {
  // エラー表示
  $layout_pages = array();
  $layout_pages["left"] = "menu.inc";
  $layout_pages["right"] = "error.inc";
  $message = "权限不足，请联系系统管理员。";
  require_once(MANAGER_TEMPLATE_DIR_PATH . "/layout/leftmenu_layout.inc");
  exit;
}

// 参数取得
$id = ParamUtil::getRequestNumber("id");
$title = ParamUtil::getRequestString("title");
$tags = ParamUtil::getRequestString("tags");
$teacher = ParamUtil::getRequestString("teacher");
$media_type = ParamUtil::getRequestNumber("media_type");
$media_kind = ParamUtil::getRequestString("media_kind");
$price = ParamUtil::getRequestString("price");
$course_id = ParamUtil::getRequestNumber("course_id");

// error_log("title-->" . $title);
// error_log("tags-->" . $tags);
// error_log("teacher-->" . $teacher);
// error_log("media_type-->" . $media_type);
// error_log("media_kind-->" . $media_kind);
// error_log("price-->" . $price);
// error_log("course_id-->" . $course_id);
// exit;
$course_media_dat = CourseMediaDat::getById($id);
if ($course_media_dat == null) {
  // エラー表示
  $message = "对象不存在.";
  $layout_pages = array();
  $layout_pages["left"] = "menu.inc";
  $layout_pages["right"] = "error.inc";
  require_once(MANAGER_TEMPLATE_DIR_PATH . "/layout/leftmenu_layout.inc");
  exit;
}

if(!empty($_FILES['front_image']['tmp_name'])){
  $aliHandler = new AliUploadHandler();
  //图片上传
  $upload_file = FileUploadUtil::getScalarFile(@$_FILES["front_image"]);
  $tmp_file_name = date("YmdHis") . "_" . trim(substr($upload_file->name, strrpos($upload_file->name, '/')+1));
  $url = $aliHandler->uploadImg($tmp_file_name, $upload_file->tmp_name);
  $course_media_dat->front_image = $url;
  ErrorLogger::doOutput($url);
}

// 数据库更新
$course_media_dat->title = $title;
$course_media_dat->tags = implode("|", explode(",", $tags));
$course_media_dat->teacher = $teacher;
if($media_kind == 'true'){
  $course_media_dat->price = $price;
  $course_media_dat->is_free = false;
}else{
  $course_media_dat->is_free = true;
}
if($media_type == 1){
  $course_media_dat->course_id = $course_id;
}else{
  $course_media_dat->course_id = 0;
}
$course_media_dat->save();

// 页面表示
$layout_pages = array();
$layout_pages["left"] = "menu.inc";
$layout_pages["right"] = "media_edit_result.inc";
$layout_pages["menu_clicked"] = "4-2";
require_once(MANAGER_TEMPLATE_DIR_PATH . "/layout/leftmenu_layout.inc");
exit;