<?php
	require("__include-common.php");
	require("../account/__logincheck.php");
	if ($_POST) {
		post_done_proc();
	}
	html_header(HTML_TITLE, '', svg_BACKGROUND_COLOR);
	page_header();
	view_thread();
	page_footer();
	html_footer();
	exit();
?>
<?php
function view_thread() {
	if ($_GET['id'].'' <> '') {
		$id = intval($_GET['id']);
	} else {
		$id = 0;
	}
	if ($_GET['move'].'' <> '') {
		$move = intval($_GET['move']);
	} else {
		$move = -1;
	}
	if ($id == 0 and $move < 0) {
		error_exit("不正アクセス。(1)<br>", False);
	}
	if ($_GET['page'].'' <> '') {
		$page = intval($_GET['page']);
	}
	if ($_GET['row'].'' <> '') {
		$row = intval($_GET['row']);
	}
	if ($_GET['pl'].'' <> '') {
		$pageline = intval($_GET['pl']);
	}
	$con = my_mysql_connect(_DB_SCHEMA);
	if ($id == 0) {
		if ($_SESSION['svg_list_sql'].'' == '') {
			error_exit("不正アクセス。(2)<br>", False);
		}
		$rs = my_mysql_query($_SESSION['svg_list_sql']);
		if ($rs) {
			if (($row = mysql_num_rows($rs)) == 0) {
				rec_not_found('データがありません。');
				return;
			}
			if ($row < $move+1) {
				rec_not_found('データがありません。');
				return;
			}
			if (!mysql_data_seek($rs, $move)) {
				rec_not_found('データがありません。');
				return;
			}
			$rec = mysql_fetch_array($rs);
			$id = $rec['id_svg'];
			$input_id = '&id='.$id;
		}
	} else {
		$sql = "select * from v_svg where id_svg = ".$id;
		$rs = my_mysql_query($sql);
		if (mysql_num_rows($rs) == 0) {
			rec_not_found('データがありません。');
			return;
		}
		$rec = mysql_fetch_array($rs);
	}
	$svg = $rec['c_svgdata'];
?>
<div id="view_body">
	<p>
		<a class="a_view_cancel_back" href='list.php?ac=<?= $_GET['ac'] ?>&key=<?= urlencode($_GET['key']) ?>&sort=<?= $_GET['sort'] ?>&page=<?= $page ?>&pl=<?= $pageline ?>'>一覧に戻る</a>
	<?php if ($move <> -1) { ?>
		<span class="view_page"><span class="view_page_num"><?= $move+1 ?></span> / <span class="view_page_num"><?= $row ?></span></span>
		<?php if ($move <> 0) { ?>
		<a class="a_view_move" href="<?= $_SERVER['SCRIPT_NAME'] ?>?move=<?= $move-1 ?>&row=<?= $row ?>&ac=<?= $_GET['ac'] ?>&key=<?= urlencode($_GET['key']) ?>&sort=<?= $_GET['sort'] ?>&page=<?= $page ?>&pl=<?= $pageline ?>">[前←]</a>
		<?php } else { ?>
		<span class="view_link_off">[←前]</span>
		<?php } ?>
		<?php if ($move < $row-1) { ?>
		<a class="a_view_move" href="<?= $_SERVER['SCRIPT_NAME'] ?>?move=<?= $move+1 ?>&row=<?= $row ?>&ac=<?= $_GET['ac'] ?>&key=<?= urlencode($_GET['key']) ?>&sort=<?= $_GET['sort'] ?>&page=<?= $page ?>&pl=<?= $pageline ?>">[→次]</a>
		<?php } else { ?>
		<span class="view_link_off">[→次]</span>
		<?php } ?>
	<?php } ?>
	</p>
	<p id="view_title">
	<span id="view_no">No.<span id="view_id_svg"><?= $rec['id_svg'] ?></span>:</span>
	<span id="view_subject"><?= $rec['c_subject'] ?></span>
	<span id="view_handle">by <?= $rec['c_handle'] ?></span>
	<span id="view_registtime"><?= $rec['c_registtime'] ?>
		<?php if ($rec['c_registtime'] <> $rec['c_updatetime']) { ?> <span id="view_updatetime"><?= $rec['c_updatetime'] ?></span><?php } ?>
	</span>
	<?php	if ($rec['id_account'] == $_SESSION['login_id']) { ?>
		<input type="button" class="view_func_button" value="修正" onClick="location.href='input.php?id=<?= $rec['id_svg'] ?>&move=<?= $move ?>&row=<?= $row ?>&page=<?= $page ?>&ac=<?= $_GET['ac'] ?>'">
	<?php	} ?>
	<input type="button" class="view_func_button" value="コピー" onClick="copy_svg();return false;">
	<?php	if ($rec['id_account'] == $_SESSION['login_id']) { ?>
		<input type="button" class="view_func_button" value="削除" onClick="delete_svg();return false;">
	<?php	} ?>
	</p>
<script language="JavaScript">
function copy_svg(){
	if (subj = window.prompt('タイトルを入れてください。', '<?= $rec['c_subject'] ?>')) {
		location.href='svg-copy.php?id=<?= $rec['id_svg'] ?>&subj='+URLencode(subj);
	}
}
function delete_svg(id){
	if (window.confirm('このデータを削除しますか？')) {
		location.href='svg-delete.php?id=<?= $rec['id_svg'] ?>&page=<?= $_GET['page'] ?>&ac=<?= $_GET['ac'] ?>';
	}
}
</script>
	<div id="view_svg_data">
<?= $svg ?>
	</div>
	<div id="comment_area">
	<?php
		$sql = "select * from v_comment where id_svg = ".$id." order by id_comment asc";
		if ($rs_comment = my_mysql_query($sql)) {
			while ($rec_comment=mysql_fetch_array($rs_comment)) {
	?>
		<div class="comment">
			<p class="comment_posted">
			<span class="comment_handle"><?= $rec_comment['c_handle'] ?></span>
			<span class="comment_time">(<?= $rec_comment['c_registtime'] ?>)</span>
			</p>
			<p class="comment_message">
			<?= ins_atag_br($rec_comment['c_memo']) ?>
			</p>
		</div>
	<?php
			}
		}
		$con = mysql_close($con);
	?>
	<div class="comment">
	<a name="comment_new"></a>
	<form id="comment_new" method="POST" action="<?= $_SERVER['SCRIPT_NAME'] ?>?id=<?= $rec['id_svg'] ?>&move=<?= $move ?>&row=<?= $row ?>&page=<?= $page ?>&ac=<?= $_GET['ac'] ?>#comment_new">
		<input type="hidden" name="id_svg" value="<?= $id ?>">
		コメント：<br>
		<textarea id="c_memo" name="c_memo" cols=80 rows=5 wrap="soft"></textarea><br>
		<input type="submit" name="登録" value="送信"></font>
	</form>
	</div>
<?php
}
?>
<?php
function post_done_proc() {
	if ($_POST['id_svg'].'' == '') {
		return;
	}
	if (($c_memo = trim($_POST['c_memo'])) == '') {
		return;
	}
	$id_svg = intval($_POST['id_svg']);
	$con = my_mysql_connect(_DB_SCHEMA);
	$sql = "insert into m_comment";
	$sql .= " (id_account";
	$sql .= ", id_svg";
	$sql .= ", c_memo";
	$sql .= ", c_registtime";
	$sql .= ") values ";
	$sql .= "( '".$_SESSION['login_id']."'";
	$sql .= ", '".intval($_POST['id_svg'])."'";
	$sql .= ", '".post_to_mysql('c_memo')."'";
	$sql .= ", '".date("Y/m/d H:i:s")."'";
	$sql .= ")";
	$ret = my_mysql_query($sql, "登録できませんでした。");
	$con = mysql_close($con);
}
?>
<?php
function rec_not_found($msg) {
?>
	<h3 style="margin: 10px;"><?= $msg ?>
	<a class="a_cancel_back" href='list.php?ac=<?= $_GET['ac'] ?>&page=<?= $page ?>'>一覧に戻る</a>
	</h3>
<?php
}
?>
