<?php
	require("__include-common.php");
	require("../account/__logincheck.php");
	require("__include-common-rss.php");

	my_session_start();
	setcookie('current_id', $_SESSION['current_id'], 0, MY_SESSION_PATH);	// for ajax-rss.php
	setcookie('login_id', $_SESSION['login_id'], 0, MY_SESSION_PATH);	// for ajax-rss.php

	$table_name = "m_rss";
	$http_arg = array();
	$http_arg['cat'] = '';
	_GET_to_http_arg_pool($http_arg, $table_name);

	html_header(HTML_TITLE, "_add_header_index.php");
	page_header();
	contents_header();
	ob_implicit_flush();	//自動フラッシュをオン(出力をバッファリングしない)
	rss_body($http_arg, $table_name);
	page_footer();
	html_footer();
	exit();
?>
<?php
function rss_body($http_arg, $table_name) {
	$con = my_mysqli_connect(_DB_SCHEMA);
?>
<div id="rss_navi_box">
<ul id="rss_navi">
<?php
	$sql_cat = "select * from m_category where c_delete = 0";
	$sql_cat .= " and id_account = '" . $_SESSION['current_id'] . "' order by c_categoryDisplayOrder asc";
	$rs_cat = my_mysqli_query($sql_cat);
	$cat_tbl = array();
	$cnt = 0;
	$cat_id = 0;
	while ($rec_cat = mysqli_fetch_array($rs_cat)) {
		if ($cnt == 0 || $rec_cat['id_category'] == intval($http_arg['cat'])) {
			$cat_id = $rec_cat['id_category'];	// カテゴリ選択必須。
		}
		$cat_tbl[] = array($rec_cat['id_category'], my_htmlspecialchars($rec_cat['c_categoryName']));
		$cnt++;
	}
	if ($cnt == 0) {
		echo '<br>サイトが未登録です。<br>最初に、カテゴリを登録してください。';
		return;
	}
	$http_arg['cat'] = $cat_id;
	arg_val_to_http_arg_pool($http_arg, $table_name);	// 追加/修正(一覧)で、同じカテゴリで開かせるため。
	foreach ($cat_tbl as $category) {
		$categoryName = my_htmlspecialchars($rec_cat['c_categoryName']);
?>
		<li<?= ($category[0] == $cat_id) ? ' id="rss_navi_current"' : ' class="rss_navi_tag"' ?> style="width: <?= mb_strwidth($category[1])+4 ?>ex;">
		<a href="<?= $_SERVER['SCRIPT_NAME'] ?>?cat=<?= $category[0] ?>"><?= $category[1] ?></a></li>
<?php
	}
?>
<script src="../scripts/jquery-ui.min.js"></script>
<script>
window.onload=function(){
	if(!NiftyCheck()) return;
	//RoundedTop("div#rss_navi li", "transparent", "#404020");
	RoundedTop("li.rss_navi_tag", "transparent", "#a9b0e0");
	AddTop(document.getElementById("rss_navi_current"), "transparent", "#ff8c00");
}
function ajax_rss(id) {
	data = "id="+id+"&width=<?= RSS_ARTICLE_TABLE_WIDTH ?>&pop=<?= RSS_ARTICLE_TABLE_WIDTH_POPUP ?>";
	$.ajax({
		type: "GET",
		url: "ajax-rss.php",
		data: data,
		async: true,
		success: function(res){
			$("#rss_"+id).html(res);
		}
	})
}
</script>
</ul>
</div>
<script>
$(function(){
	$("#rss_body").sortable({stop: function(event, ui) {
		id_nums = $("#rss_body").sortable("toArray");
		$.ajax({
			type: "GET",
			url: "rss-renum.php",
			data: "ids="+id_nums,
			async: false
		})
	}});
});
</script>
<div id="rss_body">
<?php
	$sql = "select * from v_rss where c_delete = 0 and id_account = '" . $_SESSION['current_id'] . "'";
	$sql .= " and id_category = " . $cat_id;
	if ($_SESSION['current_id'] != $_SESSION['login_id']) {
		$sql .= " and c_privacy = 0";
	}
	$sql .= " order by c_displayOrder, c_title";
	$rs = my_mysqli_query($sql);
	$ajax_get = '';
	while ($rec = mysqli_fetch_array($rs)) {
		$ajax_get .= 'ajax_rss('.$rec['id_rss'].');';
		rss_view_frame($rec, RSS_ARTICLE_TABLE_WIDTH, RSS_ARTICLE_VIEW_CNT, RSS_ARTICLE_TABLE_WIDTH_POPUP);
	}
?>
<div class="rss_thumbnail" style="height:100px;width:100px;"></div>
</div>
<script>
$(function(){
	<?= $ajax_get ?>
});
</script>
<?php
}
?>
