<? if ( !defined("_INCLUDED_GLOBAL") ) die("Access Denied");		// [Security]Allow_url_fopen = On ?>
<?


	/*
		-. 주소록 그룹 목록 가져오기

		ex)
			$addr_group = get_addr_group()

	*/
	function get_addr_group()
	{
		global $db, $TB, $AUTH, $G;

		if (!$addr_group)
		{
			/*
			$sql = "
				SELECT
					adg_no, adg_name
				FROM
					".$TB['ADDR_GROUP']."
				WHERE
					" . get_addr_group_where() . "
				ORDER BY
					adg_name
				";
			$addr_group = db_get_rows($sql);
			*/
			// [2012-10-15] 주소록 그룹 트리 지원
			$cg = get_addr_group_tree_class();
			$cg_dataall = $cg->getRowsByParent(0, 255, 1, "AND " . get_addr_group_where());
			$addr_group = db_rows_keytofield($cg_dataall);

			for ($i=0; $i<sizeof($addr_group); $i++)
			{
				$addr_group[$i]['adg_name'] = lang_conv($addr_group[$i]['adg_name']);				
			}	// for()
		}	// if()

		return $addr_group;
	}	// function()


	/*
		-. 로그인한 회원자신의 그룹번호가 아닐 경우 접근제한

		ex)
			limit_adg_no($adg_no);

	*/
	function limit_adg_no($adg_no)
	{
		global $db, $TB, $AUTH, $G;
		$ad_cnt = db_get_one("SELECT COUNT(*)  FROM " . $TB['ADDR_GROUP'] . " WHERE " . get_addr_group_where() . " AND adg_no='" . $adg_no . "'");
		if ($ad_cnt < 1)
			msg("올바른 URL로 접근하여 주십시오.");		
	}	// function()


	/*
		-. 주소록 등록/수정 폼 데이타 지정.

		ex)
			$view = setform_addr($row);

	*/
	function setform_addr($row)
	{
		global $db, $TB, $AUTH, $G;

		$addr_group = get_addr_group();
		//$_addr_group = db_rows_fieldtokey($addr_group);
		//$view['adg_no'] = select_list_value($row['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, $row['adg_no']);
		$view['adg_no'] = select_list_value($row['adg_no'], $cg_dataall_option['val'], $cg_dataall_option['key'], NULL, 0, 1);

		if (!$row['adg_name'])
		{
			$tmp = $G['adg_no_isshare'];
			$G['adg_no_isshare'] = true;
			$addr_group = get_addr_group();
			//$_addr_group = db_rows_fieldtokey($addr_group);
			//$view['adg_no_share'] = select_list_value('', $_addr_group['adg_name'], $_addr_group['adg_no'], lang_conv("지정안함"), 0, 1);
			$_addr_group = db_rows_keytofield($addr_group);
			$cg_dataall_option = $cg->getSelectOption($_addr_group);
			$view['adg_no_share'] = select_list_value('', $cg_dataall_option['val'], $cg_dataall_option['key'], lang_conv("지정안함"), 0, 1);
			$G['adg_no_isshare'] = $tmp;
		}	// if()

		$view['ad_name'] = input_value($row['ad_name'], 1);
		$view['ad_email'] = input_value($row['ad_email'], 1);
		$view['ad_mobile'] = input_value($row['ad_mobile'], 1);
		$view['ad_office'] = input_value($row['ad_office'], 1);
		$view['ad_addr'] = input_value($row['ad_addr'], 1);
		$view['ad_tel'] = input_value($row['ad_tel'], 1);
		$view['ad_fax'] = input_value($row['ad_fax'], 1);
		$view['ad_homepage'] = input_value($row['ad_homepage'], 1);
		$view['ad_memo'] = htmlspecialchars_fix($row['ad_memo']);
		$view['ad_share'] = radio_value("ad_share", $row['ad_share'], array('개인주소록', '공용주소록'), array('N', 'Y'), 0, 0, 1);

		return $view;
	}	// function()

	function get_addr_group_where()
	{
		global $AUTH, $G;

		if ($G['adg_no_isshare'])
		{
			$q_where = "d_no='" . $G['DINFO']['d_no'] . "' ";
		} else 
		{

			if ($_GET['isadmin'])
			{
				if (!$G['auth_limit_exec'])
				{
					auth_limit("admin");
					$G['auth_limit_exec'] = true;
				}	// if()
				$q_where = "d_no='" . $G['DINFO']['d_no'] . "' ";
			} else 
			{
				$q_where = "m_no='" . $AUTH['auth_m_no'] . "' ";
			}	// if()
		}	// if()

		return $q_where;
	}	// function()


	// [2006-07-11] 공용주소록 구분
	function set_addr_no_isshare($adg_no=0, $share=0)
	{
		global $TB, $G;

		// [2006-07-11] 공용주소록 구분
		if ($adg_no > 0)
		{
			$d_cnt = db_get_one("SELECT COUNT(*)  FROM " . $TB['ADDR_GROUP'] . " WHERE d_no='" . $G['DINFO']['d_no'] . "' AND adg_no='" . $adg_no . "'");
			if ($d_cnt > 0)
			{
				$G['adg_no_isshare'] = true;
				$_GET['share'] = 1;
			}	// if()
		} else if ($share)
		{
			$G['adg_no_isshare'] = true;			
		}	// if()
	}	// function()


	// [2006-07-14] 그룹별 주소록 수
	function get_addr_group_count()
	{
		global $db, $TB, $AUTH, $G, $G_SYS;

		if ($G_SYS[DB] == 'oracle')
		{
			$q = "
				SELECT
					adg.adg_no, COUNT(ad.ad_no) cnt
				FROM
					" . $TB['ADDR_GROUP'] . " adg,
					" . $TB['ADDR'] . " ad
				WHERE
					adg.m_no='" . $AUTH['auth_m_no'] . "'
					AND adg.adg_no=ad.adg_no(+)
				GROUP BY adg.adg_no
				";
		} else
		{
			$q = "
				SELECT
					adg.adg_no, COUNT(ad.ad_no) cnt
				FROM
					" . $TB['ADDR_GROUP'] . " adg LEFT JOIN
					" . $TB['ADDR'] . " ad ON (adg.adg_no=ad.adg_no)
				WHERE
					adg.m_no='" . $AUTH['auth_m_no'] . "'
				GROUP BY adg.adg_no
				";
		}	// if()

		$_adg1 = db_get_rows($q, true);
		$adg_cnt = array();
		$adg_cnt['P'] = 0;
		$adg_cnt['S'] = 0;
		for ($i=0; $i<sizeof($_adg1['adg_no']); $i++)
		{
			$adg_cnt[$_adg1['adg_no'][$i]] = $_adg1['cnt'][$i];								
			$adg_cnt['P'] += $_adg1['cnt'][$i];
		}	// for()

		if ($G_SYS[DB] == 'oracle')
		{
			$q = "
				SELECT
					adg.adg_no, COUNT(ad.ad_no) cnt
				FROM
					" . $TB['ADDR_GROUP'] . " adg,
					" . $TB['ADDR'] . " ad
				WHERE
					adg.d_no='" . $G['DINFO']['d_no'] . "'
					AND adg.adg_no=ad.adg_no
				GROUP BY adg.adg_no
				";
		} else
		{
			$q = "
				SELECT
					adg.adg_no, COUNT(ad.ad_no) cnt
				FROM
					" . $TB['ADDR_GROUP'] . " adg LEFT JOIN
					" . $TB['ADDR'] . " ad ON (adg.adg_no=ad.adg_no)
				WHERE
					adg.d_no='" . $G['DINFO']['d_no'] . "'
				GROUP BY adg.adg_no
				";
		}	// if()
		$_adg2 = db_get_rows($q, true);
		for ($i=0; $i<sizeof($_adg2['adg_no']); $i++)
		{
			$adg_cnt[$_adg2['adg_no'][$i]] = $_adg2['cnt'][$i];								
			$adg_cnt['S'] += $_adg2['cnt'][$i];
		}	// for()

		// [2008-07-26] 회원 조직
		// [2011-07-08] 가입대기, 탈퇴회원은 조직이 할당되어 있더라도 제외시킴.
		//$q = "SELECT COUNT(*) FROM ".$TB['MEMBER']." WHERE d_no='" . $G['DINFO']['d_no'] . "' AND mg_no > 0";
		$q = "SELECT COUNT(*) FROM ".$TB['MEMBER']." WHERE d_no='" . $G['DINFO']['d_no'] . "' AND mg_no > 0 AND m_usertype IS NULL AND m_level > '" . $G[LEVEL][wait] . "'";
		$adg_cnt['G'] = db_get_one($q);

		return $adg_cnt;
	}	// function()

	// 공유주소록일 경우 해당 공유주소록을 등록한 사용자가 맞는지 확인.
	function get_addr_owner_check($ad_no)
	{
		global $TB, $G, $AUTH;

		$q = "SELECT ad_no_share FROM " . $TB['ADDR'] . " WHERE ad_no='" . $ad_no . "'";
		$ad_no_share = db_get_one($q);		

		$q = "
			SELECT 
				COUNT(*) 
			FROM 
				" . $TB['ADDR'] . " ad,
				" . $TB['ADDR_GROUP'] . " adg
			WHERE
				ad.ad_no='".$ad_no_share."'
				AND ad.adg_no=adg.adg_no
				AND adg.m_no='" . $AUTH['auth_m_no'] . "'
			";
		$adr_mbr_cnt = db_get_one($q);

		return $adr_mbr_cnt;
	}	// function()


	// [2012-10-15] 주소록 그룹 트리 지원
	function get_addr_group_tree_class()
	{
		require_once('include/group_tree_lib.php');

		$_tree = new NmailGroupTree;

		$_tree->table = 'nmail_addr_group';
		$_tree->field_key = 'adg_no';
		$_tree->field_parent = 'adg_parent';
		$_tree->field_depth = 'adg_depth';
		$_tree->field_sort = 'adg_sort';
		$_tree->field_name = 'adg_name';
		$_tree->field_name_title = '그룹';

		return $_tree;
	}	// function()


?>