<?
require_once('include/global.php');
require_once("include/mail_lib.php");
require_once('include/pager.php');
require_once('include/addr_lib.php');
require_once('conf.inc.php');


// set
	$addr_title = ($_GET['share']) ? '공용주소록' : '개인주소록';
	$CFG['TITLE'] = '<b>'.$addr_title.'</b>';


	// [2006-07-11] 공용주소록 구분
	set_addr_no_isshare($_GET['s_adg_no'], $_GET['share']);

	// 주소록 색인
		if ($G_SYS['nm_lang'] == 'ko')
		{
			$addr_idxs_key = array('ㄱ', 'ㄴ', 'ㄷ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅅ', 'ㅇ', 'ㅈ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ', 'A~Z', '0~9'); 
			$addr_idxs_val = array('가', '나', '다', '라', '마', '바', '사', '아', '자', '차', '카', '타', '파', '하', 'A-Z', '1');
		} else 
		{

			$addr_idxs_key = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'ㄱ~ㅎ', '0~9'); 
			$addr_idxs_val = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '가-하', '1');
		}	// if()


	// [2006-07-14] 그룹별 주소록 수
	if (!$_GET['is_iframe'])
		$adg_cnt = get_addr_group_count();


// data

	// search
		$q_where = '';
		if ($_GET['s_word'])
		{
			set_q_search($_GET['s_word']);
			$q_where = " AND ( ad_name LIKE '%" . $_GET['s_word'] . "%' OR ad_email LIKE '%" . $_GET['s_word'] . "%' OR ad_mobile LIKE '%" . $_GET['s_word'] . "%' OR ad_office LIKE '%" . $_GET['s_word'] . "%' ) ";
			$v_s_word = htmlspecialchars_fix(stripslashes($_GET['s_word']));
		}	// if()

		if ($_GET['s_adg_no'])
		{
			limit_adg_no($_GET['s_adg_no']);
			$q_where .= " AND adg.adg_no = '" . $_GET['s_adg_no'] . "' ";			
		}	// if()

		if ($_GET['s_idx'])
		{
			$s_idx_key = array_search($_GET['s_idx'], $addr_idxs_val);
			if ($s_idx_key !== false && $s_idx_key !== NULL)
			{
				switch ($_GET['s_idx'])
				{
					case '하':
						$q_where .= " AND ad.ad_name >= '하' ";
						break;

					case 'Z':
						$q_where .= " AND ad.ad_name >= 'Z' AND ad.ad_name < '{' ";
						break;

					case '가-하':
						$q_where .= " AND ad.ad_name >= '가' ";
						break;

					case 'A-Z':
						$q_where .= " AND ad.ad_name >= 'A' AND ad.ad_name < '{' ";
						break;

					case '1':
						$q_where .= " AND ad.ad_name >= '0' AND ad.ad_name < ':' ";
						break;

					default:
						$q_where .= " AND ad.ad_name >= '" . $addr_idxs_val[$s_idx_key] . "' AND ad.ad_name < '" . $addr_idxs_val[$s_idx_key+1] . "' ";
						break;
				}	// switch()
			}	// if()
		}	// if()

	// sort
		$CFG['ADDR_LIST_FIELD'] = array(
				'adg.adg_name' => '그룹',
				'ad.ad_name' => '이름',
				'ad.ad_email' => '이메일',
				'ad.ad_mobile' => '휴대폰',
				'ad.ad_office' => '회사/학교'
				);
		// [2012-10-18] 기본 정렬을 등록일자 역순이 아닌, 이름순으로 변경.
		if (!$_GET['s_key2'])
			$_GET['s_key2'] = 'ASC';
		$order_set = get_order(array_keys($CFG['ADDR_LIST_FIELD']), 'ad.ad_name', $_GET['s_key'], $_GET['s_key2']);
		$order_img = get_order_img(array_keys($CFG['ADDR_LIST_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(ad.ad_no) 
			FROM 
				" . $TB['ADDR_GROUP'] . " adg, 
				" . $TB['ADDR'] . " ad 
			WHERE 
				adg." . get_addr_group_where() . " 
				AND adg.adg_no=ad.adg_no 
				" . $q_where
			);
		$pager_params['perPage'] = 20;

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

		$pager = &new Pager_Sliding($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
				adg.adg_name, ad.adg_no, ad.ad_no, ad.ad_name, ad.ad_email, ad.ad_mobile, ad.ad_office
			FROM
				" . $TB['ADDR_GROUP'] . " adg, 
				" . $TB['ADDR'] . " ad 
			WHERE
				adg." . get_addr_group_where() . "
				AND adg.adg_no=ad.adg_no 
				" . $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++)
		{
			if ($_GET['is_iframe'])
			{
				$rows[$i]['ad_email'] = get_mailto_url($rows[$i]['ad_email'], $rows[$i]['ad_name'], true, NULL, ' target="_parent" ');
			} else 
			{
				$rows[$i]['ad_email'] = get_mailto_url($rows[$i]['ad_email'], $rows[$i]['ad_name'], true);
			}	// if()
		}	// for()
		$rows = db_rows_fieldtokey($rows);
		$rows_cnt = getarraycount($rows['ad_name']);

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

	// 주소록수
		$addr_all_cnt = number_format($pager_params['totalItems']);

	// 주소록 그룹
		$addr_group = get_addr_group();

		//$select_addr_group = select_list_value($_GET['s_adg_no'], $_addr_group['adg_name'], $_addr_group['adg_no'], NULL, 0, 1);
		// [2012-10-15] 주소록 그룹 트리 지원
		$cg = get_addr_group_tree_class();
		$_addr_group = db_rows_keytofield($addr_group);
		$cg_dataall_option = $cg->getSelectOption($_addr_group, $_GET['s_adg_no']);
		$select_addr_group = select_list_value ($_GET['s_adg_no'], $cg_dataall_option['val'], $cg_dataall_option['key'], NULL, 0, 1);


// skin view
	$layout = ($_GET['is_iframe']) ? 'iframe' : 'self';
	nskinset('webmail', 'addr_list.html', $layout);
	include($NSKIN['view_layout']);

	// [2008-08-22] 주소록 그룹이 좌측에 보여지는 스킨일때
	if ($_GET['is_iframe'])
		nm_iframe_load('_nm_address_list_frame', '_nm_address_list_title', $CFG['TITLE']);

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