// $debug=yes;
/**********************
sub category page for the directory .....
will need to show a list of items in the category as well as the categories beneath it
**********************/
require'../commonfiles/Functions.php';
require'../commonfiles/MyFunctions.php';
require'../commonfiles/brandconfig.php';
require'../commonfiles/newtemplate.php';
// connect mysql
$crm = new MySQL_class;
$crm->Create($crm_db,$crm_user,$crm_host,$crm_pass);
$gc_sql = $crm;
$cdc = new MySQL_class;
$cdc->Create($cdc_db,$cdc_user,$cdc_host,$cdc_pass);
$tmp = new MySQL_class;
$tmp->Create($template_db,$template_user,$template_host,$template_pass);
$dir = new MySQL_class;
$dir->Create($directory_db,$directory_user,$directory_host,$directory_pass);
$brnd = new MySQL_class;
$brnd->Create($db,$user,$host2,$pass);
// foil hackers
unset($u_type,$u_id,$session_email);
// start session
start_session($php_sessid);
// log it
app_log('directory',$PHP_SELF,$search_word,$id,'view',$code,$array);
// start template engine
$tpl = new rFastTemplate(".");
$tpl->no_strict();
$tpl->quiet();
if(page_auth(3,$u_type,$cdc) == "yes")
{
	$staging = "yes";
}
unset($u_id);
start_session($sessid);
// getting the header and other needed pages
$main = get_template('2',$TEMPLATE,$tmp,$code,$staging);
$body = get_template('4',$TEMPLATE,$tmp,$code,$staging);
$tpl->define_raw( array( main => $main,
			body => $body ));
$tpl = show_login($u_id,$tpl);
$tpl->assign( array( SELF => $PHP_SELF,
			CATEGORY_ID => $id ));
// list directory categories
$tpl = list_dir_cats($tpl,'COMPANY',$code,$dir);
// building the location ....
$id = strip_tags(ass($id));
$query = "select name, level, extra_text_1, extra_text_1, order_by 
		from $DIR_CATEGORY_NAME 
		where category_id = '$id' 
		and brand = '$code' ";
$data = $dir->mydata($query);
$location_name = stripslashes($data[name][0]);
$prepend  = " : $location_name";
$location_id = $id;
$order_by = $data[order_by][0];
$tpl->assign(array( CATEGORY_NAME => $location_name,
	EXTRA_TEXT_1 => ss($data[extra_text_1][0]),
	EXTRA_TEXT_2 => ss($data[extra_text_2][0]) ));
// now loop through till I get to zero
$level = $data[level][0];
for($a=0;$a<$level;$a++)
{
	$query = "select $DIR_CATEGORY_NAME.name, $DIR_CATEGORY_NAME.category_id 
			from $DIR_CATEGORY_NAME, $DIR_CAT_TO_CAT_ORGANIZATION
			where $DIR_CAT_TO_CAT_ORGANIZATION.category_id = $DIR_CATEGORY_NAME.category_id
			and $DIR_CAT_TO_CAT_ORGANIZATION.lesser_category_id = '$location_id'
			and $DIR_CAT_TO_CAT_ORGANIZATION.brand = '$code' ";
	$location_data = $dir->mydata($query);
	if($a!=0)
	{
		$prepend  = " : $location_name$prepend";
	}
	$location_id = $location_data[category_id][0];
	$location_name = $location_data[name][0];
}
$tpl->assign(array( LOCATION => $prepend ));
// lets get a list of categories beneath this category
$query = "select $DIR_CATEGORY_NAME.name, $DIR_CATEGORY_NAME.category_id 
		from $DIR_CATEGORY_NAME, $DIR_CAT_TO_CAT_ORGANIZATION
		where $DIR_CATEGORY_NAME.category_id = $DIR_CAT_TO_CAT_ORGANIZATION.lesser_category_id
		and $DIR_CAT_TO_CAT_ORGANIZATION.category_id = '$id'
		and $DIR_CATEGORY_NAME.hide != 'Y'
		and $DIR_CAT_TO_CAT_ORGANIZATION.brand = '$code'
		and $DIR_CATEGORY_NAME.brand = '$code'
		order by $DIR_CATEGORY_NAME.name ";
$data = $dir->mydata($query);
// lets print a list of sub categories in this category 
for($a=0;$amydata($query);
	if($count_data[count][0]=="0")
	{
		$foo = "0";
	} else {
		$foo = $count_data[count][0];
	}
	$query = "select count(cdc_id) as count 
			from $DIR_CATEGORY_ITEM_ORGANIZATION 
			where category_id = '".$data[category_id][$a]."'
			and brand = '$code' ";
	$count_data = $dir->mydata($query);
	if($count_data[count][0]!='0' || $foo!='0')
	{
		if($foo=='' || $foo=='0')
		{
			$hide_count_1 = '';
			unset($foo);
		}
	
		if($count_data[count][0]=='' || $count_data[count][0]=='0')
		{
			$hide_orgcount_1 = '';
		}
		$tpl->assign(array( NAME => stripslashes($data[name][$a]),
			ID => $data[category_id][$a],
			COUNT => $foo,
			ORGCOUNT => $count_data[count][0],
			HIDE_ORGCOUNT_1 => $hide_orgcount_1,
			HIDE_ORGCOUNT_2 => $hide_orgcount_2,
			HIDE_COUNT_1 => $hide_count_1,
			HIDE_COUNT_2 => $hide_count_2 ));
		$tpl->parse("SUB_CATEGORY",".sub_category");
		unset($hide_count_1,$hide_count_2,$hide_orgcount_1,$hide_orgcount_2);
		$empty_cats='N';
	} 
}
if($data[name][0]=='')
{
	$hide_count_1 = '';
	$hide_orgcount_1 = '';
	$tpl->assign(array( 
		HIDE_ORGCOUNT_1 => $hide_orgcount_1,
		HIDE_ORGCOUNT_2 => $hide_orgcount_2,
		HIDE_COUNT_1 => $hide_count_1,
		HIDE_COUNT_2 => $hide_count_2 ));
	//$tpl->parse("SUB_CATEGORY",".sub_category");
}
if($data[name][0]=="")
{
	$tpl->assign(array( HIDE_1 => "" ));
}
// lets print a list of companies in this category
$query = "select distinct($DIR_CDC.cdc_id) 
		from $DIR_CDC, $DIR_CATEGORY_ITEM_ORGANIZATION
		where $DIR_CATEGORY_ITEM_ORGANIZATION.category_id = '$id'
		and $DIR_CATEGORY_ITEM_ORGANIZATION.cdc_id = $DIR_CDC.cdc_id
		and $DIR_CATEGORY_ITEM_ORGANIZATION.hide != '1'
		and $DIR_CDC.brand = '$code'
		order by $DIR_CDC.company_name ";
$data = $dir->mydata($query);
$tpl->assign(array( THIS_ORG_COUNT => count($data[cdc_id]) ));
for($a=0;$amydata($query);
	$new_array = get_cdc($data[cdc_id][$a],$code,$cdc);
	$contact = get_cdc($contact_data[contact_id][0],$code,$cdc);
	$tpl->assign(array( CDC_ID => $data[cdc_id][$a],
			COMPANY_NAME => $new_array[company_name],
			CONTACT_NAME => "$contact[first_name] $contact[last_name]", 
			ADDRESS_1_1 => $new_array[address_1_1],
			ADDRESS_1_2 => $new_array[address_1_2],
			CITY_1 => $new_array[city_1],
			PROVINCE_1 => $new_array[province_1],
			ZIPCODE_1 => $new_array[zipcode_1],
			COUNTRY_1 => $new_array[country_1],
			COMPANY_BLURB_1 => $new_array[company_blurb_1] ));
	$tpl->parse("ITEM",".item");
	
	$empty_items = 'N';
}
// list articles in this category ...
$query = "select article_id 
	from $DIR_CATEGORY_ITEM_ORGANIZATION
	where brand = '$code'
	and hide != '1'
	and article_id != ''
	and category_id = '$id' ";
$data = $dir->mydata($query);
// figure how how these articles should be ordered ....
switch($order_by)
{
	case"ALPHABETICAL":
	$order_by = 'order by article_title';
	break;
	case"CHRONOLOGICAL":
	$order_by = 'order by timestamp desc';
	break;
	default:
	$order_by = 'order by timestamp desc';
	break;
}
$in = make_in($data,'article_id'); 
if($data[article_id][0])
{
	$query = "select article_id, article_title, article_sub_title, article_description,
		date_format(timestamp,'%M %D, %Y') as date,
		registered, free, subscription, fee, file_subscription, file_purchased
		from $TITLE
		where article_id in ($in)
		and disable = 'T'
		and brand = '$code' 
		$order_by ";
	$data = $brnd->mydata($query);
	for($a=0;$aassign(array( ARTICLE_TITLE => ss($data[article_title][$a]),
			ARTICLE_DESCRIPTION => ss($data[article_description][$a]),
			ARTICLE_DATE => ss($data[date][$a]),
			ARTICLE_SUB_TITLE => ss($data[article_sub_title][$a]),
			ARTICLE_REGISTERED => $data[registered][$a],
			I => $a,
			ARTICLE_FREE => $data[free][$a],
			ARTICLE_SUBSCRIPTION => $data[subscription][$a],
			ARTICLE_FEE => $data[fee][$a],
			ARTICLE_FILE_SUBSCRIPTION => $data[file_subscription][$a],
			ARTICLE_FILE_FEE => $data[file_purchased][$a],
			ARTICLE_ID => $data[article_id][$a] ));
		$tpl->parse("ARTICLE",".article");
		$empty_items = 'N';
	}
}
header( "Content-type: text/html; charset=iso-8859-1", false);
$tpl = html_chunks($staging,$tpl,$code,$brnd);
if($empty_items!='N' && $empty_cats!='N')
{
	$empty = get_template('141',$TEMPLATE,$tmp,$code,$staging);
	$tpl->define_raw(array( is_empty => $empty ));
	$tpl->parse( MAIN, array( "is_empty", "main" ));
	$tpl->FastPrint();
	exit();
}
$tpl->parse( MAIN, array( "body", "main" ));
$tpl->FastPrint();
exit();
?>