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


// set
	if ($_GET['isadmin'])
	{
		$CFG['TITLE'] = '<b>관리자메뉴</b> >> <b>공용주소록 그룹관리</b>';
	} else 
	{
		$CFG['TITLE'] = '<b>개인주소록</b> >> <b>그룹관리</b>';
	}	// if()


// data
	// db - select
		/*
		$DEBUG['db_select']['start'] = getmicrotime();
		$sql = "
			SELECT
				adg.adg_no, adg.adg_name, COUNT(*) ad_cnt
			FROM
				" . $TB['ADDR_GROUP'] . " adg, 
				" . $TB['ADDR'] . " ad 
			WHERE
				adg." . get_addr_group_where() . "				
				AND adg.adg_no=ad.adg_no
			GROUP BY
				adg.adg_no, adg.adg_name
			ORDER BY
				adg.adg_name
			";
		$rows = db_get_rows($sql);
		$DEBUG['db_select']['exec'] += getmicrotime() - $DEBUG['db_select']['start'];
		*/

	// process data
		$DEBUG['proc_data']['start'] = getmicrotime();

		/*
		// 그룹결과 가져오기
		$ag_grp = array();
		$ag_grp_cnt = 0;
		for ($i=0; $i<sizeof($rows); $i++)
		{
			$ag_grp[$ag_grp_cnt]['adg_no'] = $rows[$i]['adg_no'];
			$ag_grp[$ag_grp_cnt]['ad_cnt'] += $rows[$i]['ad_cnt'];

			// 동일한 그룹은 합산처리
			if ($ag_grp[$ag_grp_cnt]['adg_no'] != $rows[$i+1]['adg_no'])
				$ag_grp_cnt++;

		}	// for()

		// total
		$ag_total = array();
		for ($i=0; $i<sizeof($ag_grp); $i++)
		{
			$ag_total['ad_cnt'] += $ag_grp[$i]['ad_cnt'];
		}	// for()
		*/

		// 그룹별로 삽입하기.
		$abg = get_addr_group($_GET['isadmin']);

		$ag_total_view = array();
		for ($i=0; $i<sizeof($abg); $i++)
		{
			/*
			for ($j=0; $j<sizeof($ag_grp); $j++)
			{
				if ($abg[$i]['adg_no'] == $ag_grp[$j]['adg_no'])
				{
					$abg[$i]['ad_cnt'] = $ag_grp[$j]['ad_cnt'];
					break;
				}	// if()
			}	// for()
			*/
			// [2012-10-15] 주소록 그룹 트리 지원
			$q = "
				SELECT 
					COUNT(*) 
				FROM 
					".$TB['ADDR']."
				WHERE
					adg_no='".$abg[$i]['adg_no']."'
				";
			$ad_cnt = db_get_one($q);
			$abg[$i]['ad_cnt'] = number_format($ad_cnt);
			$ag_total_view['ad_cnt'] += $ad_cnt;
		}	// for()
		$ag_total_view['ad_cnt'] = number_format($ag_total_view['ad_cnt']);


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

		// 상위 그룹
		$cg = get_addr_group_tree_class();
		$cg_dataall = db_rows_fieldtokey($abg);
		$cg_dataall_option = $cg->getSelectOption($cg_dataall);
		$adg_parent = select_list_value ($_GET['adg_no'], $cg_dataall_option['val'], $cg_dataall_option['key'], NULL, 0, 1);

		// 마지막 출력순서
		$q = "SELECT MAX(adg_sort) FROM ".$TB['ADDR_GROUP']." WHERE ".get_addr_group_where();
		$adg_sort = db_get_one($q)+1;


// skin view
	$layout = ($_GET['is_iframe']) ? 'iframe' : 'self';
	nskinset('webmail', 'addr_group.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');
?>