<?php
!defined('P_W') && exit('Forbidden');

InitGP('step','P','1');

if ($step == 2) {
	InitGP(array('forumtype','catedbs'),'P',1);
	$typyallowtype = $updatelist = array();
	$query = $db->query("SELECT mark,allowtype FROM pw_forumtype");
	while ($rt = $db->fetch_array($query)) {
		$updatelist[$rt['mark']] = array();
		$typyallowtype[$rt['mark']] = (int)$rt['allowtype'];
	}
	foreach ($forumtype as $key => $value) {
		if ($value && array_key_exists($value,$updatelist)) {
			$updatelist[$value][] = $key;
		} else {
			$updatelist['0'][] = $key;
		}
	}
	foreach ($updatelist as $key => $value) {
		if (count($value)) {
			$sqladd = '';
			if ($typyallowtype[$key]) {
				$sqladd .= ',allowtype=allowtype|'.$typyallowtype[$key];
			}
			$db->update("UPDATE pw_forums SET forumtype=".pwEscape($key,false)."$sqladd WHERE fid IN(".pwImplode($value).")");
		}
	}
	writeover(D_P.'data/bbscache/catedbs_area_cache.php',"<?php\r\n\$catedbs=".pw_var_export($catedbs).";\r\n?>");
	adminmsg('operate_success');
} else {
	@include_once(D_P.'data/bbscache/catedbs_area_cache.php');
	if (!$catedbs) {
		$catedbs = array();
	}
	$forumtypes = '<select class="input" name="forumtype" ><option value="0"> </option>';
	$query = $db->query("SELECT * FROM pw_forumtype");
	while ($rt = $db->fetch_array($query)) {
		$forumtypes .= "<option value=\"$rt[mark]\">$rt[typename]</option>";
	}
	$forumtypes .= "</select>";
	$catedb = $forumdb = $subdb1 = $subdb2 = array();
	$space  = '<span class="fourm-two"></span>';

	$query = $db->query("SELECT fid,fup,type,name,vieworder,forumadmin,f_type,cms,forumtype FROM pw_forums WHERE cms!='1' ORDER BY vieworder");
	while ($forums = $db->fetch_array($query)) {
		$forums['name'] = preg_replace("/\<(.+?)\>/is","",$forums['name']);//„h°£html¼ŠĆ±
		$forums['name'] = str_replace("<","&lt;",$forums['name']);
		$forums['name'] = str_replace(">","&gt;",$forums['name']);
		if ($forums['type'] != 'category') {
			if ($forums['forumtype']) {
				$forums['forumtype'] = str_replace(array("value=\"$forums[forumtype]\"","name=\"forumtype\""),array("value=\"$forums[forumtype]\" selected=\"selected\"","name=\"forumtype[$forums[fid]]\""),$forumtypes);
			} else {
				$forums['forumtype'] = str_replace("name=\"forumtype\"","name=\"forumtype[$forums[fid]]\"",$forumtypes);
			}
		} else {
			$forums['forumtype'] = array_key_exists($forums['fid'],$catedbs) ? $catedbs[$forums['fid']] : $forums['name'];
		}
		if ($forums['type'] == 'category') {
			$catedb[] = $forums;
		} elseif ($forums['type'] == 'forum') {
			$forumdb[] = $forums;
		} elseif ($forums['type'] == 'sub') {
			//$subdb1[] = $forums;
		} else {
			//$subdb2[] = $forums;
		}
	}
	$threaddb = array();
	foreach ($catedb as $cate) {
		$threaddb[] = $cate;
		foreach ($forumdb as $key2 => $forumss) {
			if ($forumss['fup'] == $cate['fid']) {
				$threaddb[] = $forumss;
				unset($forumdb[$key2]);
				foreach ($subdb1 as $key3 => $sub1) {
					if ($sub1['fup'] == $forumss['fid']) {
						$threaddb[] = $sub1;
						unset($subdb1[$key3]);
						foreach ($subdb2 as $key4 => $sub2) {
							if ($sub2['fup'] == $sub1['fid']) {
								$threaddb[] = $sub2;
								unset($subdb2[$key4]);
							}
						}
					}
				}
			}
		}
	}
}
include PrintMode('forumtype');exit;
?>