<?php
/**
 * 单词登陆
 * $Id: product_category_edit_result.php,v 1.1 2015/10/08 11:18:46 wanggb Exp $
 * @author wanggb
 * @package manager.public_html
 */

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

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

// 权限检查
if (!checkAuthority("ADMIN,MANAGER,STAFF")) {
	// エラー表示
	$layout_pages = array();
	$layout_pages["footer"] = "footer.inc";
	$layout_pages["top"] = "menu.inc";
	$layout_pages["middle"] = "error.inc";
	$message = "权限不足，请联系系统管理员。";
	require_once(MANAGER_TEMPLATE_DIR_PATH . "/layout/topmenutopsub_layout.inc");
	exit;
}

// 取得CSV文件
$csv_file = FileUploadUtil::getScalarFile($_FILES["csv_file"]);
$dest_file = "";
if($csv_file->size > 0) {
	$dest_file =date("YmdHis") . ".csv";
	$dest_file =  CSV_PATH . "/" . $dest_file;
	move_uploaded_file($csv_file->tmp_name, $dest_file);
}

$result_list = array();

//逐行解析数据
$reader = new CsvReader($dest_file, ",");
$reader->parse();
$rows_data = $reader->getResult();
$index = 0;
foreach($rows_data as $row) {
	//第一行错过
	$index++;
	if($index == 1) {
		continue;
	}
	$word = trim($row[0]);
	$hira_kana = trim($row[1]);
	$chinese = trim($row[2]);
	$sample = trim($row[3]);
	$comment = trim($row[4]);
	$mp3 = trim($row[5]);
	//是否存在
	$param = array();
	$param["word"] = $word;
	$param["delete_flg"] = false;
	$word_dat_list = WordDat::getList($param, "id", "desc", 0, 1);
	$word_dat = new WordDat();
	if(!empty($word_dat_list)) {
		$word_dat = $word_dat_list[0];
	}
	// 更新数据库
	$word_dat->word = $word;
	$word_dat->hira_kana = $hira_kana;
	$word_dat->chinese = $chinese;
	$word_dat->mp3 = $mp3;
	$word_dat->sample = $sample;
	$word_dat->comment = $comment;
	$word_dat->save();
	
	$result_list[] = $word_dat;
}
// 页面表示
$_SCRIPT_FILE = array("scripts/validators.js", "scripts/word.js");
$layout_pages = array();
$layout_pages["top"] = "menu.inc";
$layout_pages["menu_clicked"] = "li_data";
$layout_pages["middle"] = "word_data_batch_result.inc";
require_once(MANAGER_TEMPLATE_DIR_PATH . "/layout/topmenutopsub_layout.inc");
exit;