<?
require_once('include/global.php');
require_once("include/admin_mailing_lib.php");
require_once("include/board_lib.php");
require_once('include/pager.php');
#require_once('sadmin_conf.inc.php');


// set
	if ($_GET['isadmin'])
	{
		auth_limit('admin');
		$CFG['TITLE'] = '<b>관리자메뉴</b> >> <b>전체 메일링 회원 관리</b>';
		$_GET['d_no'] = $G['DINFO']['d_no'];

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


// data

	// search
		$q_where = " ";
		if ($_GET['s_word'])
		{
			set_q_search($_GET['s_word']);
			if (strpos($_GET['s_word'], 'ml_email=') !== false) {
				$_tmp    = str_replace('ml_email=', '', $_GET['s_word']);
				$q_where .= " AND (ml.ml_email='" . $_tmp . "') ";
			} else {
				$q_where .= " AND ( d.d_name LIKE '%" . $_GET['s_word'] . "%' OR m.m_name LIKE '%" . $_GET['s_word']
					. "%' OR m.m_id LIKE '%" . $_GET['s_word'] . "%' OR ml.ml_email 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_name' => '메일링 이름',
				'm.m_id' => '메일링 주소',
//				'm.m_email' => '메일링 관리주소',
				'ml.ml_email' => '메일 주소',
				'm2_name' => '이름',
				'ml.ml_regdate' => '등록일자'
				);
		$order_set = get_order(array_keys($CFG['ADMIN_MEMBER_FIELD']), 'ml.m_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
		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
				" . $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
				m.*, d.d_name, 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
				" . $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'];

	// process data
		$DEBUG['db_fetch_all']['start'] = getmicrotime();
		for ($i=0; $i<sizeof($rows); $i++)
		{
			$rows[$i]['m_id_view'] = $rows[$i]['m_id'].'@'.$rows[$i]['d_name'];
			$rows[$i]['ml_regdate'] = substr($rows[$i]['m_regdate'], 0, 10);

			// 메일 수신 상태 추가
			list($rows[$i]['m_usertype'], $rows[$i]['status']) = get_email_status($rows[$i]['ml_email']);

		}	// for()
		$rows = db_rows_fieldtokey($rows);
		$rows_cnt = getarraycount($rows['m_name']);

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

	// 메일링 리스트 관리수
		$admin_mailing_all_cnt = number_format($pager_params['totalItems']);

	// view
		$row = array();
		$view = array();
		$view = setform_admin_mailing($row);

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


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