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

// 参数取得
$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("size-->" . filesize($upload_file->tmp_name));
// 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);

$course_media_dat = new CourseMediaDat();
$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);
//视频上传
$upload_file = FileUploadUtil::getScalarFile(@$_FILES["media"]);
$tmp_file_name = date("YmdHis") . "_" . trim(substr($upload_file->name, strrpos($upload_file->name, '/')+1));
$url = $aliHandler->uploadVideo($tmp_file_name, $upload_file->tmp_name);
$course_media_dat->media = $url;
$course_media_dat->size = filesize($upload_file->tmp_name);
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;
  $course_mst = CourseMst::getById($course_id);
  $course_mst->media_count += 1;
  $course_mst->save();
}
$course_media_dat->save();

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