<?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");
$comment = ParamUtil::getRequestString("comment");
$teacher_profile = ParamUtil::getRequestString("teacher_profile");
$parent_category_id = ParamUtil::getRequestNumber("parent_category_id");
$sub_category_id = ParamUtil::getRequestNumber("sub_category_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("sub_category_id-->" . $sub_category_id);
// error_log("parent_category_id-->" . $parent_category_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;
$course_media_dat->comment = $comment;
$course_media_dat->teacher_profile = $teacher_profile;

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->parent_category_id = $course_mst->parent_category_id;
  $course_media_dat->sub_category_id = $course_mst->sub_category_id;
}else{
  $course_media_dat->parent_category_id = $parent_category_id;
  $course_media_dat->sub_category_id = $sub_category_id;
}

$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;