<?php
!defined('M_P') && exit('Forbidden');
require_once(R_P.'require/forum.php');
include_once(D_P.'data/bbscache/forum_cache.php');
require_once(R_P.'require/element.class.php');// Load Element Class
$element = new Element();

InitGP(array('cateid'),'GP',2);
!$cateid && Showmsg('no_cateid');
$fid = $cateid;
$forum[$fid]['type'] != 'category' && Showmsg('not_is_category');

$metakeyword = strip_tags($forum[$cateid]['name']);
$subject = $metakeyword.' - ';
$db_metakeyword = $metakeyword;

$forumdb = $showsub = $pwCacheTime = $pwPushInfo = array();

$sqlwhere = 'AND (f.fid='.pwEscape($cateid).' OR f.fup='.pwEscape($cateid).')';
if (!$db_showcms) $sqlwhere .= " AND f.cms!='1'";

$query  = $db->query("SELECT f.fid,f.childid,f.fup,f.logo,f.descrip,f.forumadmin,f.across,f.password,f.allowvisit,f.showsub,f.forumtype,f.f_type,fd.tpost,fd.topic,fd.article,fd.subtopic,fd.top1,fd.lastpost FROM pw_forums f LEFT JOIN pw_forumdata fd USING(fid) WHERE f.ifsub='0' $sqlwhere ORDER BY f.vieworder");
while ($forums = $db->fetch_array($query)) {
	$forums['name'] = $forum[$forums['fid']]['name'];
	$forums['type'] = $forum[$forums['fid']]['type'];
	if ($forums['type'] === 'category') {
		if ($forums['forumadmin']) {
			$forumadmin = explode(',',$forums['forumadmin']);
			foreach ($forumadmin as $key => $value) {
				if ($value) {
					if ($key==10) {
						$forums['admin'] .= '...';
						break;
					}
					$forums['admin'] .= '<a href="u.php?action=show&username='.rawurlencode($value)."\" class=\"cfont\">$value</a> ";
				}
			}
			if (strpos($forums['forumadmin'],",$windid,")!==false){
				$admincheck = 1;
			}
		}
		$cateinfo = array('fid' => $forums['fid'],'across' => $forums['across'],'name' => strip_tags($forums['name']),'admin' => $forums['admin']);
	} elseif ($forums['type'] === 'forum') {
		if ($forums['password'] || ($forums['allowvisit'] && !(allowcheck($forums['allowvisit'],$groupid, $winddb['groups'],$forums['fid'],$winddb['visit'])) && $groupid != 3) || $rt['f_type'] == 'hidden') {
			continue;
		}
		if ($forums['showsub'] && $forums['childid']) {
			$showsub[$forums['fid']] = '';
		}
		$forums['topics'] = $forums['topic']+$forums['subtopic'];
		if ($db_topped) {
			$forums['topics'] += $forums['top1'];
			$forums['article'] += $forums['top1'];
		}
		$article += $forums['article'];
		$topics += $forums['topics'];
		$tposts += $forums['tpost'];
		$forums['admin'] = '';

		$forumdb[$forums['fid']] = array('fid' => $forums['fid'],'name' => $forums['name'],'topics' => $forums['topics'],'article' => $forums['article'],'tpost' => $forums['tpost'],'descrip' => $forums['descrip'],'forumtype'=>$forums['forumtype']);
	}
}
$db->free_result($query);

foreach ($forum as $key => $value) {
	if (isset($showsub[$value['fup']]) && $value['f_type'] != 'hidden') {
		$showsub[$value['fup']] .= ($showsub[$value['fup']] ? '&nbsp; ' : '')."<a href=\"thread.php?fid=$value[fid]\" class=\"link-white\">$value[name]</a>";
	}
}

@include_once Pcv(D_P.'data/bbscache/mode_'.$db_mode.'_cateforuminfo_'.$cateid.'.php');
$updatetime		= 600;
$isupdated		= 0;
$ifwriteover	= 0;
foreach ($forumdb as $key=>$value) {
	if ($value['forumtype']==6) continue;
	if ((@!array_key_exists('hot',$cateforuminfo[$key])) || ($cateforuminfo[$key]['updatetime'] && $cateforuminfo[$key]['updatetime']<$timestamp-$updatetime && $isupdated==0)) {
		switch ($value['forumtype']) {
			case 2:
				$forumdb[$key]['hot'] = $element->replySort('replysort',$key,12,$value['forumtype']);
				if ($forumdb[$key]['hot']) {
					foreach ($forumdb[$key]['hot'] as $k=>$val) {
						$forumdb[$key]['hot'][$k] = array(
														'tid'		=> $val['addition']['tid'],
														'subject'	=> substrs($val['title'],40),
														'deadline'	=> get_date($val['value'],'m-d h:i'),
														'location'  => $val['addition']['location'],
													);
					}
				}
				break;
			case 3:
				$forumdb[$key]['hot'] = $element->replySort('replysort',$key,8,$value['forumtype']);
				if ($forumdb[$key]['hot']) {
					foreach ($forumdb[$key]['hot'] as $k=>$val) {
						$forumdb[$key]['hot'][$k] = array(
														'tid'		=> $val['addition']['tid'],
														'subject'	=> substrs($val['title'],40),
														'cbval'		=> $val['value'],
														'authorid'  => $val['addition']['authorid'],
														'author'	=> $val['addition']['author'],
													);
					}
				}
				break;
			case 4:
				$forumdb[$key]['hot'] = $element->replySort('replysort',$key,6,$value['forumtype']);
				if ($forumdb[$key]['hot']) {
					foreach ($forumdb[$key]['hot'] as $k=>$val) {
						$forumdb[$key]['hot'][$k] = array(
														'tid'		=> $val['addition']['tid'],
														'name'		=> substrs($val['title'],40),
														'price'		=> $val['value'],
														'icon'		=> minImage($val['image'],120,90),
													);
					}
				}
				break;
			case 5:
				$forumdb[$key]['hot'] = $element->newPic(0,$key,9);
				if ($forumdb[$key]['hot']) {
					foreach ($forumdb[$key]['hot'] as $k=>$val) {
						$forumdb[$key]['hot'][$k] = array(
														'tid'		=> $val['addition']['tid'],
														'subject'	=> substrs($val['title'],15),
														'attachurl'  => minImage($val['image'],120,90),
														'content' => $val['value'],
														'replies' => $val['addition']['replies'],
													);
					}
				}
				break;
			default:
				$forumdb[$key]['hot'] = $element->replySort('replysort',$key,11);
				if ($forumdb[$key]['hot']) {
					foreach ($forumdb[$key]['hot'] as $k=>$val) {
						$forumdb[$key]['hot'][$k] = array(
														'tid'		=> $val['addition']['tid'],
														'subject'	=> substrs($val['title'],50),
														'postdate'	=> get_date($val['addition']['postdate'],'m-d'),
														'authorid'  => $val['addition']['authorid'],
														'author'	=> $val['addition']['author'],
													);
					}
				}
		}
		$cateforuminfo[$key]['hot'] = $forumdb[$key]['hot'];
		$cateforuminfo[$key]['updatetime'] = $timestamp;
		$isupdated	 = $key;
		$ifwriteover = 1;
	} else {
		$forumdb[$key]['hot'] = $cateforuminfo[$key]['hot'];
	}
	if (@!array_key_exists('newpic',$cateforuminfo[$key]) || $isupdated == $key) {
		if ($value['forumtype'] == 5) continue;
		$forumdb[$key]['newpic'] = $element->newPic(0,$key,3);
		if ($forumdb[$key]['newpic']) {
			foreach ($forumdb[$key]['newpic'] as $k=>$val) {
				$forumdb[$key]['newpic'][$k] = array(
												'tid'		=> $val['addition']['tid'],
												'subject'	=> substrs($val['title'],30,0),
												'attachurl'  => minImage($val['image'],120,90),
												'content' => $val['value'],
												'replies' => $val['addition']['replies'],
											);
			}
		}
		$cateforuminfo[$key]['newpic'] = $forumdb[$key]['newpic'];
	} else {
		$forumdb[$key]['newpic'] = $cateforuminfo[$key]['newpic'];
	}
}

if ($ifwriteover == 1) {
	writeover(Pcv(D_P.'data/bbscache/mode_'.$db_mode.'_cateforuminfo_'.$cateid.'.php'),"<?php\r\n\$cateforuminfo=".pw_var_export($cateforuminfo).";\r\n\$catehotsubjectdb=".pw_var_export($catehotsubjectdb).";\r\n\$pwPushInfo=".pw_var_export($pwPushInfo).";\r\n?>");
}

unset($cateforuminfo,$updatetime,$isupdated,$ifwriteover,$pwCacheTime,$tmpUpdateTime,$times,$forums);

require_once(R_P.'require/header.php');
require_once PrintEot('cate');footer();
?>