<?php
	require("__include-common.php");
	require("../account/__logincheck.php");
	require("../__common__/include-common-mp-list.php");

	$table_name_view = "v_mycheck";
	$table_name = "m_mycheck";
	$id_item = "id_mycheck";

	$mp_list_arg = array();
	$mp_list_arg['account_id']		= $_SESSION['current_id'];
	$mp_list_arg['table_name_view']		= "v_mycheck";
	$mp_list_arg['edit_list_all']		= "no";
	$mp_list_arg['id_item']			= "id_mycheck";
	$mp_list_arg['template_view']		= "mycheck-my-template.php";
	$mp_list_arg['input_new']		= "no";
	$mp_list_arg['sql_create_callback']	= "my_mp_list_sql_create";
	$mp_list_arg['add_filter']		= "mycheck-add-filter.php";

	$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",
				"type"=>"select", "参照テーブル"=>"m_category2", "参照テーブル表示列"=>"c_category2Name",
				"参照テーブル表示順"=>"c_category2DisplayOrder",
				"参照テーブルユーザー"=>"共有");
	$item_tbl[] = array(	"表示名"=>"チェック",	"列名"=>"c_check",
				"type"=>"text");
	$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, id_study");
	$order_tbl[] = array(   "表示名"=>"解答順", "get_order_name"=>"answer",
				"order_by"=>"c_answer, id_study");
	$order_tbl[] = array(   "表示名"=>"レベル順", "get_order_name"=>"check",
				"order_by"=>"c_check, id_study");
	$order_tbl[] = array(   "表示名"=>"ランダム", "get_order_name"=>"rand",
				"order_by"=>"");
	$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['ck0'] = '';				// チェックレベル
	$http_arg['ck1'] = '';				// チェックレベル
	$http_arg['ck2'] = '';				// チェックレベル
	$http_arg['ck3'] = '';				// チェックレベル
	$http_arg['cid'] = '';

	_GET_to_http_arg_pool($http_arg, 'm_study', 'key,pl');	// 'm_study': list.phpと共通化するため
	for ($ix=0; $ix<=3; $ix++) { 
		if ($_GET['ck'.$ix].'' <> '') {
			$_SESSION['study_list_check'.$ix] = $_GET['ck'.$ix];
		}
	}
	$_SESSION['study_list_check_all'] = '';
	for ($ix=0; $ix<=3; $ix++) { 
		if ($_SESSION['study_list_check'.$ix].'' == 'on') {
			$_SESSION['study_list_check_all'] = 'off';
		}
	}
	html_header(HTML_TITLE, '', '#ffffff');
	page_header();
	contents_header();
	mp_list_view($mp_list_arg, $item_tbl, $order_tbl, $http_arg);
	page_footer();
	html_footer();
	exit();
function my_mp_list_sql_create($account_id, $table_name, $item_tbl, $order_tbl, $sortorder, $keystring, $sel_filter, $use_privacy, $add_where="") {
	$sql = "SELECT T1.id_mycheck, T1.id_account, T1.c_check";
	$sql .= ", T1.c_registtime, T1.c_updatetime";
	$sql .= ", v_study.id_study, v_study.c_question, v_study.c_answer";
	$sql .= ", v_study.id_category, v_study.c_categoryName, v_study.c_categoryDisplayOrder";
	$sql .= ", v_study.id_category2, v_study.c_category2Name, v_study.c_category2DisplayOrder";
	$sql .= " FROM ( select * from m_mycheck where id_account = '".$_SESSION['current_id']."') T1";
	$sql .= " RIGHT OUTER JOIN v_study ON T1.id_study = v_study.id_study";
	$sql = "select * from ( " . $sql ." ) TBL ";
	$sql_where = "";
	if ($keystring <> '') {
		$select_item = array();
		foreach ($item_tbl as $id=>$item) {
			if ($item['文字検索'] == "Y") {
				$select_item[] = $item['列名'];
			}
		}
		$select_item_cnt = count($select_item);		//文字列検索対象列の数
		if (count($select_item) > 0) {
			$sql_where .= where_and($sql_where).'( '.query_and_or($select_item, $keystring).' )';
		}
	}
	foreach ($sel_filter as $id=>$filter) {
		if ($filter['data'] <> '') {
			$sql_where .= where_and($sql_where) . $filter['colname'] . " = '" . $filter['data'] . "'";
		}
	}
	$add_check = '';
	for ($ix=0; $ix<=3; $ix++) { 
		if ($_SESSION['study_list_check'.$ix].'' == 'on') {
			if ($add_check <> '') {
				$add_check .= ' or ';
			}
			$add_check .= " c_check = '".$ix."'";
			if ($ix == 0) {
				$add_check .= " or c_check is NULL";
			}
		}
	}
	if ($add_check <> '') {
		$sql_where .= where_and($sql_where) . ' ( ' . $add_check . ' )';
	}
	if ($sql_where <> '') {
		$sql .= " where " . $sql_where;
	}
	foreach ($order_tbl as $id=>$item) {
		if ($item['get_order_name'] == $sortorder) {
			$order_item = $item['order_by'];
		}
	}
	if ($order_item <> '') {
		$sql .= " order by ".$order_item;
	}
	return($sql);
}
function where_and($sql) {
	if ($sql == '') return '';
	return " and ";
}
