<?php
/**
 * 合集视频编辑
 * $Id: collection_media_edit_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");
$action_type = ParamUtil::getRequestString("action_type");
$title = ParamUtil::getRequestString("title");
$parent_category_id = ParamUtil::getRequestNumber("parent_category_id");
$sub_category_id = ParamUtil::getRequestNumber("sub_category_id");
$teacher_name = ParamUtil::getRequestString("teacher_name");
$teacher_profile = ParamUtil::getRequestString("teacher_profile");

//创建的时候
if($action_type == "new") {
  $course_mst = new CourseMst();
}

//编辑
if($action_type == "edit") {
  $course_mst = CourseMst::getById($id);
  if ($course_mst == null) {
    // エラー表示
    $message = "对象不存在";
    $layout_pages = array();
    $layout_pages["left"] = "menu.inc";
    $layout_pages["right"] = "error.inc";
    $layout_pages["menu_clicked"] = "4-1";
    require_once(MANAGER_TEMPLATE_DIR_PATH . "/layout/leftmenu_layout.inc");
    exit;
  }
}

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

// 数据库更新
$course_mst->title = $title;
$course_mst->parent_category_id = $parent_category_id;
$course_mst->sub_category_id = $sub_category_id;
$course_mst->teacher_name = $teacher_name;
$course_mst->teacher_profile = $teacher_profile;
$course_mst->save();

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