<?
require_once('include/global.php');
require_once('include/admin_lib.php');
require_once('include/pager.php');
require_once('include/file.php');


// set
	if ($_GET['isadmin'])
	{
		auth_limit('admin');
		$CFG[TITLE] = '<b>관리자메뉴</b> >> <b>메일 예약 발송 관리</b>';
		$_GET['d_no'] = $G['DINFO']['d_no'];
	} else
	{
		auth_limit('serveradmin');
		$CFG[TITLE] = '<b>메일서버관리</b> >> <b>메일 예약 발송 관리</b>';
	}	// if()

	$CFG['mrc_reserve_status'] = array(
		'W' => '예약',
	);
// data

	// search
	$q_where = "";

	if ($_GET['d_no'])
		$q_where .= " AND d.d_no='".$_GET['d_no']."' ";

	if ($_GET['s_word'])
	{
		set_q_search($_GET['s_word']);
		$q_where .= " AND  (m.m_id LIKE '%" . $_GET['s_word'] . "%' OR m.m_name LIKE '%" . $_GET['s_word']
				. "%' OR mrc.mrc_to LIKE '%" . $_GET['s_word'] . "%' OR mrc.mrc_subject LIKE '%" . $_GET['s_word'] . "%') ";
		$v_s_word = htmlspecialchars_fix(stripslashes($_GET['s_word']));
	}	// if()

	// sort
	$CFG['ADMIN_MEMBER_FIELD'] = array(
		'd.d_name' => '도메인',
		'm.m_id' => '아이디',
		'm.m_name' => '이름',
		'mrc.mrc_to' => '받은사람',
		'mrc.mrc_subject' => '제목',
		'mrc.mrc_senddate' => '보낸날짜',
	);

	if ($_GET['isadmin'])
	{
		unset($CFG['ADMIN_MEMBER_FIELD']['d_name']);
	}	// if()

	$order_set = get_order(array_keys($CFG['ADMIN_MEMBER_FIELD']), 'mrc.mrc_no ', $_GET['s_key'], $_GET['s_key2']);
	$order_img = get_order_img(array_keys($CFG['ADMIN_MEMBER_FIELD']), $_GET['s_key'], $_GET['s_key2'], false, 'common_skin');		// array

	// db - pager
		$DEBUG['db_cnt']['start'] = getmicrotime();
		$pager_params['totalItems'] = db_get_one("
			SELECT
				COUNT(*)
			FROM
			" . $TB['MAIL_RECEIVE_CHECK'] . " mrc   
			LEFT JOIN " . $TB['MEMBER'] . " m ON m.m_no = mrc.m_no
			LEFT JOIN " . $TB['DOMAIN'] . " d ON d.d_no = m.d_no
			WHERE 
				mrc.mrc_reserve_status = 'W'
			".$q_where);

        $pager_params['perPage'] = 20;

		$DEBUG['db_cnt']['exec'] += getmicrotime() - $DEBUG['db_cnt']['start'];

		// [2016-01-11] PHP 5버전대에서 Ampersand가 DEPRECATED처리되어 &삭제
		#$pager = &new Pager_Sliding($pager_params);
		// [2016-01-11] Ampersand가 DEPRECATED관련하여 PEAR Pager라이브러리 업그레이드 소스적용
		#$pager = new Pager_Sliding($pager_params);
		$pager = Pager::factory($pager_params);
		$pager_links = $pager->getLinks();
		$pager_limit = get_pager_limit($pager->getCurrentPageID(), $pager_params['perPage']);

	// db - select
		$DEBUG['db_select']['start'] = getmicrotime();
		$sql = "
		SELECT 
			mrc.mrc_no, m.m_no, m.m_id, m.m_name, d.d_no, d.d_name, mrc.mrc_to, mrc.mrc_subject, mrc.mrc_senddate, mrc.mrc_reserve_status
		FROM 
		" . $TB['MAIL_RECEIVE_CHECK'] . " mrc   
			LEFT JOIN " . $TB['MEMBER'] . " m ON m.m_no = mrc.m_no
			LEFT JOIN " . $TB['DOMAIN'] . " d ON d.d_no = m.d_no
			WHERE 
				mrc.mrc_reserve_status = 'W'
			".$q_where . "
			ORDER BY
				" . $order_set['query'] . "
		";
		$rows = db_get_rows($sql, $pager_limit['from'], $pager_limit['count']);
		$DEBUG['db_select']['exec'] += getmicrotime() - $DEBUG['db_select']['start'];

	// 도메인
		if ($_GET['isadmin'])
		{
			$mbr_domain_select = NULL;
		} else
		{
			$q = "SELECT * FROM ".$TB['DOMAIN']." WHERE d_is_use='Y' ORDER BY d_name";
			$mbr_domain = db_get_rows($q, true);
			$mbr_domain_select = select_list_value($_GET['d_no'], $mbr_domain['d_name'], $mbr_domain['d_no'], '전체 도메인', 0, 1);
		}	// if()

	// process data
		$DEBUG['db_fetch_all']['start'] = getmicrotime();
		for ($i=0; $i<sizeof($rows); $i++)
		{
			$q = "SELECT d_name FROM ".$TB['DOMAIN']." WHERE d_no='".$rows[$i]['d_no']."'";
			$rows[$i]['d_name'] = db_get_one($q);

			$rows[$i]['mrc_subject'] = (trim($rows[$i]['mrc_subject']) === '') ? "__제목없음__" : htmlspecialchars_fix($rows[$i]['mrc_subject']);

			$_ap = mime_address_parse($rows[$i]['mrc_to']);
			$rows[$i]['mrc_to_view'] = $_ap['display'];

			$rows[$i]['mrc_to'] = address_view($rows[$i]['mrc_to']);
		}	// for()
		$rows = db_rows_fieldtokey($rows);
		$rows_cnt = getarraycount($rows['mrc_senddate']);

		$DEBUG['db_fetch_all']['exec'] += getmicrotime() - $DEBUG['db_fetch_all']['start'];

	// 메일 포워딩 리스트 뷰어수
		$admin_mailing_all_cnt = number_format($pager_params['totalItems']);

// skin view
	nskinset('sadmin', 'admin_mail_send_reserve.html');		// 컨텐츠
	include($NSKIN['view_layout']);


require_once('include/global_end.php');
?>