<?php
	require("../__common__/__define_common.php");
	require("../__common__/include-common-all.php");
	require("../__common__/include-common-html.php");
	my_session_start();
	require("../account/__logincheck.php");
	if ($_SESSION['システム管理者'] <> "YES") {
		error_exit("アクセス権がありません。", True);
	}

	html_header(HTML_TITLE, '', '#ffffff');
	page_header();
	echo '<div style="margin:10px;">';
	echo '<h3>添付ファイル移動</h3>';
	if ($_POST) {
		move_file_list('do');
		echo '<br><b>終了</b>';
	} else {
		move_file_list();
		input_form();
	}
	echo '</div>';
	page_footer();
	html_footer();
	exit();
?>
<?php
function move_file_list($do='') {
	upload_file_move(_DB_SCHEMA_calendar, 'm_schedule', 'id_schedule', ATTACH_FILE_FOLDER_calendar, $do);
	upload_file_move(_DB_SCHEMA_memo, 'm_memo', 'id_memo', ATTACH_FILE_FOLDER_memo, $do);
	upload_file_move(_DB_SCHEMA_bbs, 'm_bbs', 'id_bbs', ATTACH_FILE_FOLDER_bbs, $do);
}
function upload_file_move($db_schema, $table, $id_name, $folder, $do) {
	echo '<br><b>【'.$db_schema.'：'.$table.'】</b><br>';
	echo '<table style="margin-left:10px;">';
	$con = my_mysql_connect($db_schema);
	$sql = "select ".$id_name.", id_account, c_attachFile1, c_attachFile2, c_attachFile3, c_delete from ".$table;
	$rs = my_mysql_query($sql);
	while ($rec=mysql_fetch_array($rs)) {
		file_move_one(1, $rec, $id_name, $folder, $do);
		file_move_one(2, $rec, $id_name, $folder, $do);
		file_move_one(3, $rec, $id_name, $folder, $do);
	}
	$con = mysql_close($con);
	echo '</table>';
}
function file_move_one($num, $rec, $id_name, $folder, $do) {
	if ($rec['c_attachFile'.$num] <> '') {
		echo '<tr>';
		echo '<td><font color=blue>'.$rec[$id_name].'：&nbsp;</font></td>';
		echo '<td>'.$folder.$rec['c_attachFile'.$num].'</td>';
		echo '<td>';
		if ($rec['c_delete'] <> 0) echo '(REC削除)';
		echo '</td>';
		$sjis_from = myfile_ENCODE($folder.$rec['c_attachFile'.$num]);
		$to_folder = $folder.$rec['id_account'];
		$sjis_to = myfile_ENCODE($to_folder.'/'.$rec['c_attachFile'.$num]);
		if (file_exists($sjis_from)) {
			echo '<td><font color=red>&nbsp;→&nbsp;</font></td>';
			echo '<td>'.$folder.$rec['id_account'].'/'.$rec['c_attachFile'.$num].'</td>';
			if ($do == 'do') {
				if (!file_exists($to_folder)) {
					mkdir($to_folder);
				}
				rename($sjis_from, $sjis_to);
				if (file_exists($sjis_to)) {
					echo '<td><font color=blue>&nbsp;移動OK</font></td>';
				} else {
					echo '<td><font color=red>&nbsp;移動NG</font></td>';
				}
			}
		} elseif (file_exists($sjis_to)) {
			echo '<td><font color=black>&nbsp;→&nbsp;</font></td>';
			echo '<td>'.$folder.$rec['id_account'].'/'.$rec['c_attachFile'.$num].'</td>';
			echo '<td><font color=green>移動済み</font></td>';
		} else {
			echo '<td><font color=black>&nbsp;←&nbsp;</font></td>';
			echo '<td><font color=red>ファイルなし</font></td>';
		}
		echo '</tr>';
	}
}
function input_form() {
?>
	<form name="form0" method="POST" action="<?= $_SERVER['SCRIPT_NAME'] ?>" style="margin-left:10px;">
		<br>ファイルの移動を実行しますか？<br><br>
		<input type="submit" name="実行" value="実行">
	</form>
<?
}
?>
