############################################################## 
## MOD Title: Categories hierarchy - upgrade from v 2.0.0 to v 2.0.1
## MOD Author: Ptirhiik < admin@rpgnet-fr.com > (Pierre) http://rpgnet.clanmckeen.com
## MOD Description:
##		Upgrade Categories hierarchy from v 2.0.0 to v 2.0.1
## MOD Version: 2.0.1
## 
## Installation Level: (Easy/Moderate/Advanced)
## Installation Time: 15 Minutes
## Files To Edit:
##		viewforum.php
##		viewtopic.php
##
##		admin/admin_forums.php
##		admin/admin_ug_auth.php
##
##		includes/functions.php
##
##		templates/subSilver/index_box.tpl
##
## Included Files: n/a
############################################################## 
## For Security Purposes, Please Check: http://www.phpbb.com/mods/ for the 
## latest version of this MOD. Downloading this MOD from other sites could cause malicious code 
## to enter into your phpBB Forum. As such, phpBB will not offer support for MOD's not offered 
## in our MOD-Database, located at: http://www.phpbb.com/mods/ 
############################################################## 
## Author Notes: 
## 
############################################################## 
## MOD History: 
## 
##   2003-09-19 - Version 2.0.1
##      - enhance quickness on hughe forums tree
##	- fix spacer issue in admin
##	- clean index_box.tpl from th classes
##	- fix auth_read and auth_view confusion in phpBB
## 
############################################################## 
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD 
##############################################################
#
#-----[ OPEN ]------------------------------------------------
#
viewforum.php
#
#-----[ FIND ]------------------------------------------------
#
$is_auth = auth(AUTH_ALL, $forum_id, $userdata, $forum_row);
#
#-----[ REPLACE WITH ]---------------------------------------- 
#
//-- mod : categories hierarchy --------------------------------------------------------------------
//-- delete
// $is_auth = auth(AUTH_ALL, $forum_id, $userdata, $forum_row);
//-- add
$is_auth = $tree['auth'][POST_FORUM_URL . $forum_id];
//-- fin mod : categories hierarchy ----------------------------------------------------------------
#
#-----[ OPEN ]------------------------------------------------
#
viewtopic.php
#
#-----[ FIND ]------------------------------------------------
#
$is_auth = auth(AUTH_ALL, $forum_id, $userdata, $forum_topic_data);

if( !$is_auth['auth_view'] || !$is_auth['auth_read'] )
#
#-----[ REPLACE WITH ]---------------------------------------- 
#
//-- mod : categories hierarchy --------------------------------------------------------------------
//-- delete
// $is_auth = auth(AUTH_ALL, $forum_id, $userdata, $forum_topic_data);
//
// if( !$is_auth['auth_view'] || !$is_auth['auth_read'] )
//-- add
$is_auth = $tree['auth'][POST_FORUM_URL . $forum_id];

if ( !$is_auth['auth_read'] )
//-- fin mod : categories hierarchy ----------------------------------------------------------------
#
#-----[ FIND ]------------------------------------------------
#
	$message = ( !$is_auth['auth_view'] ) ? $lang['Topic_post_not_exist'] : sprintf($lang['Sorry_auth_read'], $is_auth['auth_read_type']);
#
#-----[ REPLACE WITH ]---------------------------------------- 
#
//-- mod : categories hierarchy --------------------------------------------------------------------
//-- delete
//	$message = ( !$is_auth['auth_view'] ) ? $lang['Topic_post_not_exist'] : sprintf($lang['Sorry_auth_read'], $is_auth['auth_read_type']);
//-- add
	$message = sprintf($lang['Sorry_auth_read'], $is_auth['auth_read_type']);
//-- fin mod : categories hierarchy ----------------------------------------------------------------
#
#-----[ OPEN ]------------------------------------------------
#
admin/admin_forums.php
#
#-----[ FIND ]------------------------------------------------
#
	// renum objects of the same level and regenerate all
	$cats = array();
	$forums = array();
	$order = 0;
#
#-----[ AFTER, ADD ]------------------------------------------
#
	$parents = array();
#
#-----[ FIND ]------------------------------------------------
#
		if ($tree['type'][$i] == POST_CAT_URL)
		{
			$cats[] = $tree['data'][$i];
		}
		else
		{
			$forums[] = $tree['data'][$i];
		}
#
#-----[ REPLACE WITH ]---------------------------------------- 
#
		if ($tree['type'][$i] == POST_CAT_URL)
		{
			$idx = count($cats);
			$cats[$idx] = $tree['data'][$i];
			$parents[POST_CAT_URL][ $tree['main'][$i] ][] = $idx;
		}
		else
		{
			$idx = count($forums);
			$forums[$idx] = $tree['data'][$i];
			$parents[POST_FORUM_URL][ $tree['main'][$i] ][] = $idx;
		}
#
#-----[ FIND ]------------------------------------------------
#
	build_tree($cats, $forums, $new_topic_data, $tracking_topic, $tracking_forums, $tracking_all);
#
#-----[ REPLACE WITH ]---------------------------------------- 
#
	build_tree($cats, $forums, $new_topic_data, $tracking_topic, $tracking_forums, $tracking_all, $parents);
#
#-----[ FIND ]------------------------------------------------
#
	'SPACER'			=> $images['spacer'],
#
#-----[ REPLACE WITH ]---------------------------------------- 
#
	'SPACER'			=> './../' . $images['spacer'],
#
#-----[ OPEN ]------------------------------------------------
#
admin/admin_ug_auth.php
#
#-----[ FIND ]------------------------------------------------
#
		'SPACER'		=> $images['spacer'],
#
#-----[ REPLACE WITH ]---------------------------------------- 
#
		'SPACER'		=> './../' . $images['spacer'],
#
#-----[ OPEN ]------------------------------------------------
#
includes/functions.php
#
#-----[ FIND ]------------------------------------------------
#
		$res = $tree['data'][$this][$field];
#
#-----[ REPLACE WITH ]---------------------------------------- 
#
		$res = ($tree['auth'][$cur]['auth_view']) ? $tree['data'][$this][$field] : '';
#
#-----[ FIND ]------------------------------------------------
#
function build_tree(&$cats, &$forums, &$new_topic_data, &$tracking_topics, &$tracking_forums, &$tracking_all, $level=-1, $main='Root')
#
#-----[ REPLACE WITH ]---------------------------------------- 
#
function build_tree(&$cats, &$forums, &$new_topic_data, &$tracking_topics, &$tracking_forums, &$tracking_all, &$parents, $level=-1, $main='Root')
#
#-----[ FIND ]------------------------------------------------
#
	// get the forums of the level
	for ($i=0; $i < count($forums); $i++)
	{
		if ($forums[$i]['main'] == $main)
		{
			$tree_level['type'][]	= POST_FORUM_URL;
			$tree_level['id'][]		= $forums[$i]['forum_id'];
			$tree_level['sort'][]	= $forums[$i]['forum_order'];
			$tree_level['data'][]	= $forums[$i];
		}
	}
	// add the categories of this level
	for ($i=0; $i < count($cats); $i++)
	{
		if ($cats[$i]['main'] == $main)
		{
			$tree_level['type'][]	= POST_CAT_URL;
			$tree_level['id'][]		= $cats[$i]['cat_id'];
			$tree_level['sort'][]	= $cats[$i]['cat_order'];
			$tree_level['data'][]	= $cats[$i];
		}
	}
#
#-----[ REPLACE WITH ]---------------------------------------- 
#
	// get the forums of the level
	for ($i=0; $i < count($parents[POST_FORUM_URL][$main]); $i++)
	{
		$idx = $parents[POST_FORUM_URL][$main][$i];
		$tree_level['type'][]	= POST_FORUM_URL;
		$tree_level['id'][]		= $forums[$idx]['forum_id'];
		$tree_level['sort'][]	= $forums[$idx]['forum_order'];
		$tree_level['data'][]	= $forums[$idx];
	}
	// add the categories of this level
	for ($i=0; $i < count($parents[POST_CAT_URL][$main]); $i++)
	{
		$idx = $parents[POST_CAT_URL][$main][$i];
		$tree_level['type'][]	= POST_CAT_URL;
		$tree_level['id'][]		= $cats[$idx]['cat_id'];
		$tree_level['sort'][]	= $cats[$idx]['cat_order'];
		$tree_level['data'][]	= $cats[$idx];
	}
#
#-----[ FIND ]------------------------------------------------
#
		// add sub levels
		build_tree($cats, $forums, $new_topic_data, $tracking_topics, $tracking_forums, $tracking_all, $level, $tree_level['type'][$i] . $tree_level['id'][$i]);
#
#-----[ REPLACE WITH ]---------------------------------------- 
#
		// add sub levels
		build_tree($cats, $forums, $new_topic_data, $tracking_topics, $tracking_forums, $tracking_all, $parents, $level, $tree_level['type'][$i] . $tree_level['id'][$i]);
#
#-----[ FIND ]------------------------------------------------
#
	// read cats
#
#-----[ BEFORE, ADD ]-----------------------------------------
#
	// mains
	$parents = array();

#
#-----[ FIND ]------------------------------------------------
#
		$cats[] = $row;
#
#-----[ REPLACE WITH ]---------------------------------------- 
#
		$idx = count($cats);
		$cats[$idx] = $row;
		$parents[POST_CAT_URL][ $row['main'] ][] = $idx;
#
#-----[ FIND ]------------------------------------------------
#
		$forums[] = $row;
#
#-----[ REPLACE WITH ]---------------------------------------- 
#
		$idx = count($forums);
		$forums[$idx] = $row;
		$parents[POST_FORUM_URL][ $row['main'] ][] = $idx;
#
#-----[ FIND ]------------------------------------------------
#
	// build the tree
	$tree = array();
	build_tree($cats, $forums, $new_topic_data, $tracking_topics, $tracking_forums, $tracking_all);
#
#-----[ REPLACE WITH ]---------------------------------------- 
#
	// build the tree
	$tree = array();
	build_tree($cats, $forums, $new_topic_data, $tracking_topics, $tracking_forums, $tracking_all, $parents);
#
#-----[ FIND ]------------------------------------------------
#
function set_tree_user_auth($cur='Root')
{
	global $board_config, $userdata, $lang;
	global $tree;

	$this = isset($tree['keys'][$cur]) ? $tree['keys'][$cur] : -1;

	// last post information and auth
	$res = array();
	$res['auth_view']			= false;
	$res['locked']				= false;
	$res['topic_last_post_id']	= 0;
	$res['post_time']			= 0;
	$res['post_user_id']		= 0;
	$res['post_username']		= '';
	$res['topic_title']			= '';
	$res['forum_posts']			= 0;
	$res['forum_topics']		= 0;
	$res['unread_topics']		= false;

	// get the info from forums
	if ( ($this > -1) && $tree['auth'][$cur]['auth_view'] )
	{
		$res['auth_view']		= $tree['auth'][$cur]['auth_view'];
		$res['locked']			= ($tree['data'][$this]['forum_status'] == FORUM_LOCKED);
		$res['forum_posts']		= $tree['data'][$this]['forum_posts'];
		$res['forum_topics']	= $tree['data'][$this]['forum_topics'];

		if ( !empty($tree['data'][$this]['topic_last_post_id']) )
		{
			$res['topic_last_post_id']	= $tree['data'][$this]['topic_last_post_id'];
			$res['post_time']			= $tree['data'][$this]['post_time'];
			$res['post_user_id']		= $tree['data'][$this]['user_id'];
			$res['post_username']		= ($tree['data'][$this]['user_id'] != ANONYMOUS) ? $tree['data'][$this]['username'] : ( (!empty($tree['data'][$this]['post_username'])) ? $tree['data'][$this]['post_username'] : $lang['Guest'] );
			$res['topic_title']			= $tree['data'][$this]['topic_title'];
			$res['unread_topics']		= $tree['unread_topics'][$this];
		}
	}

	// read sub-levels
	$count = count($tree['sub'][$cur]);
	$sub_locked = true;
	for ($i=0; $i < $count; $i++)
	{
		// climb up the tree
		$res_sub = array();
		$res_sub = set_tree_user_auth($tree['sub'][$cur][$i]);

		// get the auth
		$res['auth_view'] = $res['auth_view'] || $res_sub['auth_view'];
		// locked status
		if (!$res_sub['locked']) $sub_locked = false;

		// count topics and posts
		$res['forum_posts']		= $res['forum_posts'] + $res_sub['forum_posts'];
		$res['forum_topics']	= $res['forum_topics'] + $res_sub['forum_topics'];

		// check if there is a more recent post in the sub-tree
		if ( !empty($res_sub['topic_last_post_id']) && (empty($res['topic_last_post_id']) || ($res_sub['post_time'] > $res['post_time'])) )
		{
			$res['topic_last_post_id']	= $res_sub['topic_last_post_id'];
			$res['post_time']			= $res_sub['post_time'];
			$res['post_user_id']		= $res_sub['post_user_id'];
			$res['post_username']		= $res_sub['post_username'];
			$res['topic_title']			= $res_sub['topic_title'];
			$res['unread_topics']		= $res_sub['unread_topics'] || $res['unread_topics'];
		}
	}

	// overide the level
	$tree['auth'][$cur]['tree.auth_view'] = $res['auth_view'];
	if ($this > -1)
	{
		if ($tree['type'][$this] == POST_FORUM_URL)
		{
			$res['locked'] = ($tree['data'][$this]['forum_status'] == FORUM_LOCKED) && $sub_locked;
		}
		else
		{
			$res['locked']	= $sub_locked;
		}
		$tree['data'][$this]['tree.locked']				= $res['locked'];
		$tree['data'][$this]['tree.topic_last_post_id']	= $res['topic_last_post_id'];
		$tree['data'][$this]['tree.post_time']			= $res['post_time'];
		$tree['data'][$this]['tree.post_user_id']		= $res['post_user_id'];
		$tree['data'][$this]['tree.post_username']		= $res['post_username'];
		$tree['data'][$this]['tree.topic_title']		= $res['topic_title'];
		$tree['data'][$this]['tree.forum_posts']		= $res['forum_posts'];
		$tree['data'][$this]['tree.forum_topics']		= $res['forum_topics'];
		$tree['data'][$this]['tree.unread_topics']		= $res['unread_topics'];
	}

	return $res;
}
#
#-----[ REPLACE WITH ]---------------------------------------- 
#
function set_tree_user_auth()
{
	global $board_config, $userdata, $lang;
	global $tree;

	// read the tree from the bottom
	for ($i = count($tree['data']) - 1; $i >= 0; $i--)
	{
		//---------------------
		// full ids
		//---------------------
		$cur = $tree['type'][$i] . $tree['id'][$i];
		$main = $tree['main'][$i];
		$main_idx = ($main == 'Root') ? -1 : $tree['keys'][$main];

		//---------------------
		// auth view
		//---------------------
		$auth_view = false;
		if ( isset($tree['auth'][$cur]['auth_view']) )
		{
			// forum auth
			$auth_view = $tree['auth'][$cur]['auth_view'];
		}
		else if ( isset($tree['auth'][$cur]['tree.auth_view']) )
		{
			// categorie auth : get the sub level one
			$auth_view = $tree['auth'][$cur]['tree.auth_view'];
		}
		$tree['auth'][$cur]['auth_view'] = $auth_view;
		if ( !isset($tree['auth'][$cur]['tree.auth_view']) )
		{
			$tree['auth'][$cur]['tree.auth_view'] = $auth_view;
		}

		// grant the main level
		if ($main != 'Root')
		{
			$tree['auth'][$main]['tree.auth_view'] = ($tree['auth'][$main]['tree.auth_view'] || $tree['auth'][$cur]['tree.auth_view']);
		}

		//---------------------
		// auth read
		//---------------------
		$auth_read = false;
		if ( isset($tree['auth'][$cur]['auth_read']) )
		{
			// forum auth
			$auth_read = $tree['auth'][$cur]['auth_read'];
		}
		$tree['auth'][$cur]['auth_read'] = $auth_read;

		//---------------------
		// forum information
		//---------------------
		// locked status
		$locked = true;
		if ( isset($tree['data'][$i]['forum_status']) )
		{
			// forum info
			$locked = ($tree['data'][$i]['forum_status'] == FORUM_LOCKED);
		}
		else if ( isset($tree['data'][$i]['tree.locked']) )
		{
			// category info : get the sub levels one
			$locked = $tree['data'][$i]['tree.locked'];
		}
		$tree['data'][$i]['locked'] = $locked;

		// force lock status if no sub levels
		if ( !isset($tree['data'][$i]['tree.locked']) )
		{
			$tree['data'][$i]['tree.locked'] = $locked;
		}
		$tree['data'][$i]['tree.locked'] = ($tree['data'][$i]['tree.locked'] && $locked);

		// number of posts and topics
		if (!isset($tree['data'][$i]['tree.forum_posts']))
		{
			$tree['data'][$i]['tree.forum_posts'] = 0;
			$tree['data'][$i]['tree.forum_topics'] = 0;
		}
		if ($auth_view)
		{
			$tree['data'][$i]['tree.forum_posts'] += $tree['data'][$i]['forum_posts'];
			$tree['data'][$i]['tree.forum_topics'] += $tree['data'][$i]['forum_topics'];
		}

		// grant the main level
		if ($main != 'Root')
		{
			if ( !isset($tree['data'][$main_idx]['tree.locked']) )
			{
				$tree['data'][$main_idx]['tree.locked'] = $tree['data'][$i]['tree.locked'];
			}
			$tree['data'][$main_idx]['tree.locked'] = ($tree['data'][$main_idx]['tree.locked'] && $tree['data'][$i]['tree.locked']);

			// number of posts and topics
			if (!isset($tree['data'][$main_idx]['tree.forum_posts']))
			{
				$tree['data'][$main_idx]['tree.forum_posts'] = 0;
				$tree['data'][$main_idx]['tree.forum_topics'] = 0;
			}
			if ($auth_view)
			{
				$tree['data'][$main_idx]['tree.forum_posts'] += $tree['data'][$i]['tree.forum_posts'];
				$tree['data'][$main_idx]['tree.forum_topics'] += $tree['data'][$i]['tree.forum_topics'];
			}
		}

		//---------------------
		// last post
		//---------------------
		if ($auth_read)
		{
			// fill the sub
			if ( empty($tree['data'][$i]['tree.topic_last_post_id']) || ($tree['data'][$i]['post_time'] > $tree['data'][$i]['tree.post_time']) )
			{
				$tree['data'][$i]['tree.topic_last_post_id']	= $tree['data'][$i]['topic_last_post_id'];
				$tree['data'][$i]['tree.post_time']				= $tree['data'][$i]['post_time'];
				$tree['data'][$i]['tree.post_user_id']			= $tree['data'][$i]['user_id'];
				$tree['data'][$i]['tree.post_username']			= ($tree['data'][$i]['user_id'] != ANONYMOUS) ? $tree['data'][$i]['username'] : ( (!empty($tree['data'][$i]['post_username'])) ? $tree['data'][$i]['post_username'] : $lang['Guest'] );
				$tree['data'][$i]['tree.topic_title']			= $tree['data'][$i]['topic_title'];
				$tree['data'][$i]['tree.unread_topics']			= $tree['unread_topics'][$i];
			}
		}

		// grant the main level
		if ($main != 'Root')
		{
			if ( empty($tree['data'][$main_idx]['tree.topic_last_post_id']) || ($tree['data'][$i]['tree.post_time'] > $tree['data'][$main_idx]['tree.post_time']) )
			{
				$tree['data'][$main_idx]['tree.topic_last_post_id']	= $tree['data'][$i]['tree.topic_last_post_id'];
				$tree['data'][$main_idx]['tree.post_time']			= $tree['data'][$i]['tree.post_time'];
				$tree['data'][$main_idx]['tree.post_user_id']		= $tree['data'][$i]['tree.post_user_id'];
				$tree['data'][$main_idx]['tree.post_username']		= $tree['data'][$i]['tree.post_username'];
				$tree['data'][$main_idx]['tree.topic_title']		= $tree['data'][$i]['tree.topic_title'];
				$tree['data'][$main_idx]['tree.unread_topics']		= $tree['data'][$i]['tree.unread_topics'];
			}
		}
	}
}
#
#-----[ FIND ]------------------------------------------------
#
	// use tree auth if auth view asked
	if ($auth_key == 'auth_view')
	{
		$auth_key = 'tree.' . $auth_key;
	}

	// add the level
#
#-----[ REPLACE WITH ]---------------------------------------- 
#

	// add the level
#
#-----[ FIND ]------------------------------------------------
#
				for ($j=0; $j < count($tree['sub'][$cur]); $j++) if ($tree['auth'][ $tree['sub'][$cur][$j] ]['tree.auth_view'])
#
#-----[ IN-LINE FIND ]----------------------------------------
#
tree.auth_view
#
#-----[ IN-LINE REPLACE WITH ]--------------------------------
#
auth_view
#
#-----[ FIND ]------------------------------------------------
#
# this is a partial search : the full line is longer
#
		$res = '<a href="' . append_sid('./' . $pgm_name
#
#-----[ IN-LINE FIND ]----------------------------------------
#
$res =
#
#-----[ IN-LINE REPLACE WITH ]---------------------------------
#
if (!empty($field_name)) $res =
#
#-----[ OPEN ]------------------------------------------------
#
templates/subSilver/index_box.tpl
#
#-----[ FIND ]------------------------------------------------
#
# this is a partial search : the full lines are longer
#
	<th colspan="{catrow.tablehead.INC_SPAN}"
	<th width="50" class="thTop"
	<th width="50" class="thTop"
	<th width="150" class="thCornerR"
#
#-----[ IN-LINE FIND ]---------------------------------------- 
#
 class="thCornerL" nowrap="nowrap"
#
#-----[ IN-LINE REPLACE WITH ]--------------------------------
#
 nowrap="nowrap"
#
#-----[ IN-LINE FIND ]---------------------------------------- 
#
 class="thTop" nowrap="nowrap">&nbsp;{L_TOPICS}
#
#-----[ IN-LINE REPLACE WITH ]--------------------------------
#
 nowrap="nowrap">&nbsp;{L_TOPICS}
#
#-----[ IN-LINE FIND ]---------------------------------------- 
#
 class="thTop" nowrap="nowrap">&nbsp;{L_POSTS}
#
#-----[ IN-LINE REPLACE WITH ]--------------------------------
#
 nowrap="nowrap">&nbsp;{L_POSTS}
#
#-----[ IN-LINE FIND ]---------------------------------------- 
#
 class="thCornerR" nowrap="nowrap"
#
#-----[ IN-LINE REPLACE WITH ]--------------------------------
#
 nowrap="nowrap"
#
#-----[ SAVE/CLOSE ALL FILES ]--------------------------------
#
# EoM