<?php
/**
 * 证书管理编辑
 * $Id: certificate_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("15")) {
  // エラー表示
  $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;
}

// 参数取得
$action_type = ParamUtil::getRequestString("action_type");//new edit
$id = ParamUtil::getRequestString("id");
$title = ParamUtil::getRequestString("title");
$display_order = ParamUtil::getRequestNumber("display_order");

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

//编辑
if($action_type=="edit") {
  $certificate_mst = CertificateMst::getById($id);
  if ($certificate_mst == null) {
    // エラー表示
    $message = "对象不存在";
    $layout_pages = array();
    $layout_pages["left"] = "menu.inc";
    $layout_pages["right"] = "error.inc";
    $layout_pages["menu_clicked"] = "5-4";
    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($certificate_mst->front_image)){
    $aliHandler->delete(substr ( $certificate_mst->front_image, strrpos ( $certificate_mst->front_image, "/" ) + 1 ));
  }
  $url = $aliHandler->uploadImg($tmp_file_name, $upload_file->tmp_name);
  ErrorLogger::doOutput($url);
}
// 数据库更新
$certificate_mst->title = $title;
$certificate_mst->display_order = $display_order;
if(isset($url)){
  $certificate_mst->front_image = $url;
}
$certificate_mst->save();

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