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

// check
	if (!$_GET['m_no'])
		msg('올바른 URL로 접근하여 주십시오');

	$q_where = '';
	if ($_GET['isadmin'])
	{
		auth_limit('admin');
		$_GET['d_no'] = $G['DINFO']['d_no'];
		$q_where = " AND d_no='" . $_GET['d_no'] . "'";
		$CFG['TITLE'] = '<b>관리자메뉴</b> >> <b>메일링 리스트 관리</b> >> <b>회원관리</b>';

		if ($G['DINFO']['d_svc_mailing_list'] === 'N') {
			msg("메일링 리스트를 사용할 수 없습니다.\\n\\n서버관리자에게 문의하여 주십시오.");
		}
	} else 
	{
		auth_limit('serveradmin');
		$CFG['TITLE'] = '<b>메일서버관리</b> >> <b>전체 메일링 리스트 관리</b> >> <b>회원관리</b>';
	}	// if()


// get
	// 메일링 리스트 정보
		$q = "SELECT m_id, m_name, m_email, m_etc1 FROM " . $TB['MEMBER'] . " WHERE m_no='" . $_GET['m_no'] . "'".$q_where;
		$mbr = db_get_row($q);
		if (!$mbr)
			msg("존재하지 않는 메일링 주소입니다.");


// data
	// search
		$q_where = '';
		if ($_GET['s_word'])
		{
			set_q_search($_GET['s_word']);
			$q_where = " AND ml_email LIKE '%" . $_GET['s_word'] . "%' ";
			$v_s_word = htmlspecialchars_fix(stripslashes($_GET['s_word']));
		}	// if()

	// sort
		$CFG['WHITE_주소_LIST_FIELD'] = array(
				'ml_email' => '주소',
				'ml_perms' => '권한',
				'ml_regdate' => '등록일자'
				);
		#if (!$_GET['s_key2'])
		#	$_GET['s_key2'] = 'ASC';
		$order_set = get_order(array_keys($CFG['WHITE_주소_LIST_FIELD']), 'ml_regdate', $_GET['s_key'], $_GET['s_key2']);
		$order_img = get_order_img(array_keys($CFG['WHITE_주소_LIST_FIELD']), $_GET['s_key'], $_GET['s_key2'], false, 'common_skin');		// array

	// db - pager
		if ($G_SYS[DB] === 'oracle') {
			$join = " LEFT JOIN " . $TB['DOMAIN'] . " d2 ON REGEXP_SUBSTR(ml.ml_email, '@(.+)$', 1, 1, NULL, 1)  = d2.d_name 
					  LEFT JOIN " .$TB['MEMBER'] . " m2 ON REGEXP_SUBSTR(ml.ml_email, '^[^@]+', 1, 1) = m2.m_id and m2.d_no = d2.d_no ";
		} else {
			$join = " LEFT JOIN " . $TB['DOMAIN'] . " d2 ON SUBSTRING_INDEX(ml.ml_email, '@', -1) = d2.d_name 
				LEFT JOIN " .$TB['MEMBER'] . " m2 ON SUBSTRING_INDEX(ml.ml_email, '@', 1) = m2.m_id and m2.d_no = d2.d_no ";
		}

		$DEBUG['db_cnt']['start'] = getmicrotime();
		$pager_params['totalItems'] = db_get_one("
			SELECT
				COUNT(*)
			FROM
				" . $TB['MAILING_LIST'] . " ml
				JOIN " . $TB['MEMBER'] . " m ON ml.m_no=m.m_no
				JOIN " . $TB['DOMAIN'] . " d ON d.d_no=m.d_no 
				" . $join . "
			WHERE
				1=1
				AND ml.m_no='" . $_GET['m_no'] . "'
				$q_where
			");
		$pager_params['perPage'] = 40;

		$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
				m.*, d.d_name, ml.ml_perms, ml.ml_email, ml.ml_regdate, m2.m_no as m2_no, m2.m_name as m2_name, m2.m_id as m2_id
			FROM
				" . $TB['MAILING_LIST'] . " ml
				JOIN " . $TB['MEMBER'] . " m ON ml.m_no=m.m_no
				JOIN " . $TB['DOMAIN'] . " d ON d.d_no=m.d_no 
				" . $join . "
			WHERE
				1=1
				AND ml.m_no='" . $_GET['m_no'] . "'
				$q_where
			ORDER BY
				" . $order_set['query'] . "
			";
		$rows = db_get_rows($sql, $pager_limit['from'], $pager_limit['count'], true);
		$rows_cnt = getarraycount($rows['ml_email']);
		$DEBUG['db_select']['exec'] += getmicrotime() - $DEBUG['db_select']['start'];

		for ($i=0; $i<$rows_cnt; $i++)
		{
			$rows['ml_regdate'][$i] = date('y-m-d H:i', strtotime($rows['ml_regdate'][$i]));
			list($rows['m_usertype'][$i], $rows['status'][$i], $rows['m_name'][$i]) = get_email_status($rows['ml_email'][$i]);
		}	// for()

	// 전체갯수
		$ip_all_cnt = number_format($pager_params['totalItems']);


// skin view
	nskinset('sadmin', 'admin_mailing_addr.html');
	include($NSKIN['view_layout']);


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