<?php
	require("__include-common.php");
	require("../account/__logincheck.php");
	$_SESSION['current_permit_type'] = "w";		// current_idが他ユーザでも、is_write_permit()でFalseにならないため
	require("../__common__/include-common-mp-list.php");

	$table_name_view = "v_study";
	$table_name = "m_study";
	$id_item = "id_study";
	$must_item = "c_question";

	$mp_list_arg = array();
	$mp_list_arg['account_id']		= "ALL";		// 共通
	$mp_list_arg['update_account']		= $_SESSION['login_id'];
	$mp_list_arg['table_name_view']		= "v_study";
	$mp_list_arg['table_name_edit']		= "v_study";
	$mp_list_arg['table_name_update']	= "m_study";
	$mp_list_arg['id_item']			= "id_study";
	$mp_list_arg['must_item']		= "c_question";
	$mp_list_arg['template_view']		= "list-my-template.php";
	$mp_list_arg['input_new']		= "no";
	$mp_list_arg['use_privacy']		= "yes";
	if (study_MASTER_EDIT_ADMIN_ONLY == "YES") {
		if ($_SESSION['システム管理者'] <> "YES") {
			if (isset($_POST['登録']) or $_GET['edit'] == 'y') {
				error_exit("不正アクセス：編集権限がありません。", True);
			}
			$mp_list_arg['edit_list_all'] = "no";
		} else {
			$mp_list_arg['add_list'] = "yes";
		}
	}
	$item_tbl = array();
	$item_tbl[] = array(	"表示名"=>"カテゴリ",	"列名"=>"id_category", "http_arg_GET名"=>"cat",
				"下位フィルタ_GET名"=>"cat2",
				"type"=>"select", "参照テーブル"=>"v_category_valid", "参照テーブル表示列"=>"c_categoryName",
				"参照テーブル表示順"=>"c_categoryDisplayOrder, id_category");
	$item_tbl[] = array(	"表示名"=>"サブカテゴリ",	"列名"=>"id_category2", "http_arg_GET名"=>"cat2",
				"上位フィルタ_GET名"=>"cat", "上位フィルタ列名"=>"id_category",
				"上位フィルタ参照テーブル"=>"v_category_valid", "上位フィルタ表示順"=>"c_categoryDisplayOrder, id_category",
				"type"=>"select", "参照テーブル"=>"m_category2", "参照テーブル表示列"=>"c_category2Name",
				"参照テーブル表示順"=>"c_category2DisplayOrder");
	$item_tbl[] = array(	"表示名"=>"質問",	"列名"=>"c_question",
				"type"=>"textarea", "cols"=>50, "rows"=>3, "文字検索"=>"Y");
	$item_tbl[] = array(	"表示名"=>"回答",	"列名"=>"c_answer",
				"type"=>"textarea", "cols"=>50, "rows"=>3, "文字検索"=>"Y");
	$order_tbl = array();
	$order_tbl[] = array(   "表示名"=>"最新順", "get_order_name"=>"new",
				"order_by"=>"id_study desc");		/* default */
	$order_tbl[] = array(   "表示名"=>"登録順", "get_order_name"=>"old",
				"order_by"=>"id_study");
	$order_tbl[] = array(   "表示名"=>"カテゴリ順", "get_order_name"=>"cat",
				"order_by"=>"c_categoryDisplayOrder, id_category, c_category2DisplayOrder, id_category2");
	$order_tbl[] = array(   "表示名"=>"質問順", "get_order_name"=>"question",
				"order_by"=>"c_question");
	$order_tbl[] = array(   "表示名"=>"回答順", "get_order_name"=>"answer",
				"order_by"=>"c_answer");

	$http_arg = array();
	$http_arg['pl'] = PAGE_LINE_DEFAULT;		// mp_list 必須
	$http_arg['sort'] = '';				// mp_list 必須
	$http_arg['cat'] = '';				// mp_list 必須
	$http_arg['cat2'] = '';
	$http_arg['key'] = '';				// mp_list 必須
	$http_arg['cid'] = '';
	if (isset($_POST['登録'])) {
		check_post_account($_POST['login_id'], $_POST['current_id']);
		mp_list_update($mp_list_arg, $item_tbl, $order_tbl, $http_arg);
	} else {
		_GET_to_http_arg_pool($http_arg, $table_name, 'key,pl');
		if ($http_arg['sort'] == 'check' or $http_arg['sort'] == 'rand') {	// mycheck.phpのみのsort order
			$http_arg['sort'] = 'new';
		}
		html_header(HTML_TITLE, '', '#ffffff');
		page_header();
		contents_header('off');
		if ($_GET['edit'] == "a") {
			$mp_list_arg['sql_for_edit'] = "select * from v_study where id_study = 0";
			mp_list_edit($mp_list_arg, $item_tbl, $order_tbl, $http_arg);
		} elseif ($_GET['edit'] == "y") {
			mp_list_edit($mp_list_arg, $item_tbl, $order_tbl, $http_arg);
		} else {
			mp_list_view($mp_list_arg, $item_tbl, $order_tbl, $http_arg);
		}
		page_footer();
		html_footer();
	}
	exit();
