header("Pragma: no-cache");
// exit();
/********************************
publishing landing page
********************************/
// get needed files ....
include '../commonfiles/Functions.php';
require '../commonfiles/brandconfig.php';
require '../commonfiles/newtemplate.php';
require '../commonfiles/MyFunctions.php';
include '../commonfiles/publisher.inc';
// see if there is a cached file and if so
$dirh = '.dir';
$category_id = ass(str_replace('/','',str_replace("\n","",str_replace("\r","",trim($category_id)))));
if(!$category_id)
{
	echo"Invalid ID";
	exit();
}
if(@is_file("../cache_news/$category_id$dirh") && !$page && !$no_cache)
{
	$old = time() - filemtime("../cache_news/$category_id$dirh");
	if($old<'600')
	{
		echo file_get_contents("../cache_news/$category_id$dirh");
		app_log('publisher',$PHP_SELF,$search_word,$category_id,'view',$code,$array);
		exit();
	}
}
header( "Content-type: text/html; charset=iso-8859-1", false);
if($page)
{
	$no_cache='yes';
}
// connect mysql
$brnd = new MySQL_class;
$brnd->Create($db,$user,$host2,$pass);
$cdc = new MySQL_class;
$cdc->Create($cdc_db,$cdc_user,$cdc_host_2,$cdc_pass);
$crm = new MySQL_class;
$crm->Create($crm_db,$crm_user,$crm_host2,$crm_pass);
$gc_sql=$crm;
// foil hackers
unset($u_type,$u_id,$session_email);
// start session
start_session($php_sessid);
if(page_auth(3,$u_type,$cdc) == "yes")
{
	$staging=yes;
}
unset($u_id);
start_session($sessid);
lock_down("publisher",$u_id,$code,$array);
// log it
app_log('publisher',$PHP_SELF,$search_word,$category_id,'view',$code,$array);
$conf = get_conf2(3);
if($staging=='yes' && $code=='NCU')
{
        $conf[custom]='N';
}
// start template engine
$tpl = new rFastTemplate(".");
$tpl->no_strict();
$tpl->quiet();
if($u_id)
{
        $user = get_cdc($u_id,$code,$cdc);
        $tpl->assign(array( COMPANY_ID => $company_id,
                HLO1 => "",
                HLO2 => "",
                HLI1 => "",
                USER => "You are logged in as: $user[first_name] $user[last_name] 
 Logout - Manage Email " ));
} else {
        $tpl->assign(array( HLI1 => "",
                HLI2 => "",
                HLO1 => "" ));
}
// getting the header and other needed pages
if($conf[custom]=='Y')
{
	$main = get_template('4',$TEMPLATE,$brnd,$code,$staging);
} else {
	$main = get_template('10',$TEMPLATE,$brnd,$code,$staging);
	$body = get_template('4',$TEMPLATE,$brnd,$code,$staging);
}
//$tpl = $ms[tpl];
$tpl->define_raw( array( main => $main,
			body => $body ));
$tpl = show_login($u_id,$tpl);
if($code == "CALITODAY")
{
	$conf_cali = get_conf2(1);
	$tpl = most_popular($conf_cali,$code,$tpl);
	for($a=1;$a<=20;$a++)
	{
		$tpl = section_display($a,$brnd,$conf_cali);
	}
}
$tpl->assign( array( SELF => $PHP_SELF ));
$query = "select category_name, image_2, image_3, image_4, order_by 
	from $C_CATEGORY 
	where category_id = '$category_id' 
	and brand = '$code' ";
$data = $brnd->mydata($query);
// validate category id
if(!$data[category_name][0])
{
	echo"Invalid ID";
	exit();
}
$cat_name = $data[category_name][0]; 
$image_2 = $data[image_2][0]; 
$image_3 = $data[image_3][0]; 
$image_4 = $data[image_4][0]; 
$order_by = $data[order_by][0]; 
// lets get the issue info ....
$query = "select name, image, issue_id
	from $ISSUE
	where brand = '$code'
	and date <= '".date("Y-m-d")."'
	order by date desc limit 0,1";
//$data = $brnd->mydata($query);
//$tpl->assign(array( ISSUE_NAME => stripslashes($data[name][0]),
//			ISSUE_IMAGE => stripslashes($data[image][0]), 
//			ISSUE_ID => $data[issue_id][0] ));
// lets get the articles 
// *************** display all of todays or the top 20 articles for this category
$today = date("YmdHi00");
// this will break the total into a number of different pages ...
if($first=="")
{
	$first = "0";
	$last = $conf[number_display];
	$query = "select o.article_id as count 
        from $ORGANIZE o, $TITLE t
		where o.category_id = '$category_id' 
        and t.article_id = o.article_id
        and t.disable = 'T'
		and o.brand = '$code' 
		and o.time_stamp <= '$today'  ";
	$count_data=$brnd->mydata($query);
	$count = $data[count][0];
}
// lets make the counting pages foo
$query = "select o.article_id as count 
    from $ORGANIZE o, $TITLE t 
	where o.category_id = '$category_id'
    and t.article_id = o.article_id
    and t.disable = 'T'
	and o.brand = '$code' 
	and o.time_stamp <= '$today' ";
$count_data=$brnd->mydata($query);
$count = $count_data[count][0];
$number_of_pages = $count / $conf[number_display];
$foo = explode(".", $number_of_pages);
// decide if we need to add an extra page
$base = $foo[0];
if($base < $number_of_pages)
{
	$base = $base + 1;
}
$remainder = $foo[1];
if($page!=1)
{
	$a_pages = "> $cat_name ";
} else {
	$a_pages = "> $cat_name ";
}
for($a=0;$a<$base;$a++)
{
	if($page>=5)
	{
		// ***************** takes care of moving after ten 
		// the goal is to keep the page selected in the middle of the category pages 
		// but not put in the in middle if there is going to be less than ten displayed ..
		if(!$loop_one)
		{
			// inside loop one we make sure there are more than ten page	
			if($base>=11)
			{
				$a = $page - 5;
				$greater=yes;
				$b = $a + 1;
			}
			$loop_one='yes';
		}
		if(greater)
		{
			if($num_1=="")
			{
				$num_1 = $a * $conf[number_display];
			} else {
				$num_1 = $num_1 + $conf[number_display];
			}
			$less = $conf[number_display] - 1;
			$num_2 = $less + $num_1;
			$b = $a + 1;
			// so lets make our pages like google
			if($b!=1)
			{
				if($b!=$page)
				{
					$pages .= " > $b ";
				} else {
					$pages .= " > $b ";
				}
			}
			$c++;
			if($c==9)
			{
				break;
			}
			if($base==$b)
			{
				break;
			}
		} else {
			if($num_1=="")
			{
				$num_1 = "0";
			} else {
				$num_1 = $num_1 + $conf[number_display];
			}
			$less = $conf[number_display] - 1;
			$num_2 = $less + $num_1;
			$b = $a + 1;
			if($b!=1)
			{
				if($b!=$page)
				{
					$pages .= " > $b ";
				} else {
					$pages .= " > $b ";
				}
			}
			if($a==9)
			{
				break;
			}
		}
	} else {
		// ******************* first ten stories 
		if($num_1=="")
		{
			$num_1 = "0";
		} else {
			$num_1 = $num_1 + $conf[number_display];
		}
		$less = $conf[number_display] - 1;
		$num_2 = $less + $num_1;
		$b = $a + 1;
		if($b!=1)
		{
			if($b!=$page)
			{
				$pages .= " > $b ";
			} else {
				$pages .= " > $b ";
			}
		}
		if($a==9)
		{
			break;
		}
	}
}
$tpl->assign( array( PAGES => "$a_pages $pages", 
	O_PAGES => $pages ));
$tpl->assign( array( PAGE_TITLE => "$cat_name" ));
$tpl->assign( array( IMAGE_2 => stripslashes($image_2),
	IMAGE_3 => stripslashes($image_3),
	IMAGE_4 => stripslashes($image_4),
	THIS_CATEGORY_ID => $category_id ));
if($page!=1)
{
	$tpl->assign( array( PAGE_NUMBER => $page ));
}
// get a list of articles based on the cat ID
if($order_by=='DATE')
{
	/*$query = "select article_id from $ORGANIZE 
		where category_id = '$category_id' 
		and brand = '$code' 
		and time_stamp <= '$today' 
		order by time_stamp desc limit ".abs($first).", $conf[number_display]";*/
        
    $query = "select o.article_id
        from $ORGANIZE o, $TITLE t
    	where o.category_id = '$category_id'
        and t.article_id = o.article_id
        and t.disable = 'T'
		and o.brand = '$code' 
		and o.time_stamp <= '$today' 
		order by o.time_stamp desc limit ".abs((int)$first).", $conf[number_display]";        
        
	$cat_data = $brnd->mydata($query);
} else {
	$query = "select $ORGANIZE.article_id 
		from $ORGANIZE, $TITLE 
		where category_id = '$category_id' 
		and $ORGANIZE.brand = '$code' 
		and $ORGANIZE.article_id = $TITLE.article_id
		and $ORGANIZE.time_stamp <= '$today' 
		order by article_title
		limit ".abs((int)$first).", $conf[number_display] ";
	$cat_data = $brnd->mydata($query);
}
// number of page
// loop through and print
// will get the block to loop through
// no strict
$tpl->no_strict();
// will put the dates in long format 
$long_date=yes;
// the actual loop
for($a=0;$amydata($query);
	date_time($data[timestamp][0]);
	$tpl->assign( array( 
		TITLE => stripslashes($data[article_title][0]),
		ARTICLE_ID => $data[article_id][0],
		TIME => $ts_time,
		DATE => $ts_date ));
	if($a==0)
	{
		$tpl->assign( array( SPECIAL_TAG_1 => '',
			SPECIAL_TAG_2 => '' ));
	} else {
		$tpl->assign( array( SPECIAL_TAG_1 => '' ));
	}
	if($data[story_type][0]!='')
	{
		$tpl->assign( array( STORY_TYPE => stripslashes($data[story_type][0]).", " ));
	} else {
		$tpl->assign( array( STORY_TYPE => "" ));
	}
	if($data[article_publisher][0]!="")
	{
		$tpl->assign( array( PUBLISHER => stripslashes($data[article_publisher][0]).","));
	} else {
		$tpl->assign( array( PUBLISHER => "" ));
	}
	if($data[article_sub_title][0]!="")
	{
		$tpl->assign( array( SUB_BR => "
",
				SUB_TITLE => stripslashes($data[article_sub_title][0]) ));
	} else {
		$tpl->assign( array( SUB_BR => "",
				SUB_TITLE => "" ));
	}
	if($a==0)
	{
		if($data[article_large_image][0])
		{
			$tpl->assign( array( LARGE_IMAGE => stripslashes($data[article_large_image][0]) ));
			$tpl->assign( array( SMALL_IMAGE => "" ));
		} else {
			$tpl->assign( array( SMALL_IMAGE => stripslashes($data[article_small_image][0]) ));
			$tpl->assign( array( LARGE_IMAGE => "" ));
		}
	} else {
		$tpl->assign( array( LARGE_IMAGE => "" ));
		$tpl->assign( array( SMALL_IMAGE => stripslashes($data[article_small_image][0]) ));
	}
	if($data[article_author][0]!="")
	{
		$tpl->assign( array( BY => $conf[by] ));
		$tpl->assign( array( AUTHOR => stripslashes($data[article_author][0]) ));
	 } else {
		$tpl->assign( array( BY => "" ));
		$tpl->assign( array( AUTHOR => "" ));
	}
	// this is for not showing the description
	if($conf[description]=='Y')
	{
		$description_foo = stripslashes($data[article_description][0])."
";
	} else {
		if($first=="0")
		{	
			if($a < $conf[number_description])
			{
				$description_foo = stripslashes($data[article_description][0])."
";
			} else {
				unset($description_foo);
			}
			$tpl->assign( array( DESCRIPTION => $description_foo ));
		}
	}
	// if no content hide something ....
	$query = "select section_id from $CONTENT 
		where article_id = '".$cat_data[article_id][$a]."'
		and brand = '$code' ";
	$cdata = $brnd->mydata($query);
	if(!$cdata[section_id][0])
	{
		$tpl->assign(array( HID1 => '' ));
	} else {
		$tpl->assign(array( HID1 => '',
				HID2 => '' ));
	}
	$tpl->assign( array( DESCRIPTION => $description_foo ));
	$tpl->parse("ARTICLE_LIST",".article_list");
}
// **************************** display categories
$tpl = publisher_auto_category($tpl,$code,$brnd);
$tpl = publisher_auto_alt_category($tpl,$code,$brnd);
// *************** display all of todays or the top 20 articles
$now = date("YmdHi00");
$query = "select * from $TITLE 
		where brand = '$code' 
		and front_page = '1' 
		and disable = 'T' 
		and timestamp <= '$now' 
		order by timestamp desc limit 0, $conf[num_head_lines]";
$data=$brnd->mydata($query);
// loop through and print
// will get the block to loop through
// will put the dates in long format
$long_date=yes;
// the actual loop
for($a=0;$aassign( array( LS_TITLE => stripslashes($data[article_title][$a]),
		LS_ARTICLE_ID => $data[article_id][$a],
		LS_DATE => $ts_date ));
	if($data[article_publisher][$a]!="")
	{
		$tpl->assign( array( LS_PUBLISHER => stripslashes($data[article_publisher][$a]).","));
	} else {
		$tpl->assign( array( LS_PUBLISHER => "" ));
	}
	if($data[article_author][$a]!="")
	{
		$tpl->assign( array( BY => $conf[by] ));
		$tpl->assign( array( LS_AUTHOR => stripslashes($data[article_author][$a])."," ));
	 } else {
		$tpl->assign( array( BY => "" ));
		$tpl->assign( array( LS_AUTHOR => "" ));
	}
	$tpl->parse("LEAD_STORY",".lead_story");
}
// **************************** focus group
$query = "select image, category_name 
	from $C_FOCUS_CATEGORY 
	where brand = '$code' 
	and category_id = '$conf[focus_group_to_display]' ";
$data_i=$brnd->mydata($query);
$tpl->assign( array( F_IMAGE => stripslashes($data_i[image][0]),
	F_CATEGORY_NAME => stripslashes($data_i[category_name][0]),
	F_CATEGORY_ID => $conf[focus_group_to_display] ));
$now = date("YmdHi00");
$query = "select article_id from $FOCUS_ORGANIZE 
		where brand = '$code' 
		and category_id = '$conf[focus_group_to_display]' 
		and time_stamp <= '$now' 
		order by time_stamp desc limit 0, $conf[num_focus_group_items]";
$data_c=$brnd->mydata($query);
for($a=0;$amydata($query);
	date_time($data[timestamp][0]);
	$tpl->assign( array( F_ARTICLE_ID => $data[article_id][0],
		F_TITLE => stripslashes($data[article_title][0]),
		F_AUTHOR => stripslashes($data[article_author][0]),
		F_DATE => $ts_date )); 
	if($data[article_publisher][0]!="")
	{
		$tpl->assign( array( F_PUBLISHER => stripslashes($data[article_publisher][0]).","));
	} else {
		$tpl->assign( array( F_PUBLISHER => "" ));
	}
	$tpl->parse("FOCUS_GROUP",".focus_group");
}
$tpl = html_chunks($staging,$tpl,$code,$brnd);
if($conf[custom]=='Y')
{
	$tpl->parse( MAIN, main );
} else {
	$tpl->parse( MAIN, array( "body", "main" ));
}
// cache start
if(!$no_cache&&$category_id)
{
	$file_data = $tpl->Fetch();
	$fp = fopen("../cache_news/$category_id$dirh", 'w');
	fwrite($fp, $file_data);
	fclose($fp);
}
// cache end 
$tpl->FastPrint();
exit();
?>