<?php
//
//	file: includes/CH_functions_admin.php
//	author: ptirhiik
//	begin: 10/02/2006
//	version: 1.6.0 - 10/06/2006
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//

if ( !defined('IN_PHPBB') )
{
	die('Hacking attempt');
}

function make_forum_select($box_name, $ignore_forum = false, $select_forum = '')
{
	global $config, $forums, $user;

	$front_pic = $forums->get_front_pic();
	if ( !empty($front_pic) )
	{
		$forum_list = '';
		foreach ( $front_pic as $cur_id => $front )
		{
			$selected = !empty($select_forum) && ($select_forum == $cur_id) ? ' selected="selected"' : '';
			$forum_list .= '<option value="' . (($cur_id >= 0) ? $cur_id : -1) . '"' . $selected . '>';
			$count_front = strlen($front);
			for ( $i = 0; $i < $count_front; $i++ )
			{
				$forum_list .= $user->lang('tree_pic_' . $front[$i]);
			}
			if ( $cur_id >= 0 )
			{
				$forum_list .= $user->lang($forums->data[$cur_id]['forum_name']);
			}
			$forum_list .= '</option>';
		}
	}
	else
	{
		$forum_list = '<option value="-1">' . $user->lang('None') . '</option>';
	}
	$forum_list = '<select name="' . $box_name . '">' . $forum_list . '</select>';

	return $forum_list;
}

function sync($type, $id = false, $differ=false)
{
	global $db, $config;

	switch($type)
	{
		case 'all forums':
			$sql = 'SELECT forum_id
						FROM ' . FORUMS_TABLE;
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				sync('forum', intval($row['forum_id']));
			}
			$db->sql_freeresult($result);
		   	break;

		case 'all topics':
			$sql = 'SELECT topic_id
						FROM ' . TOPICS_TABLE;
			while( $row = $db->sql_fetchrow($result) )
			{
				sync('topic', intval($row['topic_id']));
			}
			$db->sql_freeresult($result);
			break;

	  	case 'forum':
			if ( empty($id) )
			{
				global $config;

				// count topics
				$sql = 'SELECT COUNT(topic_id) AS total_topics
							FROM ' . TOPICS_TABLE;
				$result = $db->sql_query($sql, false, __LINE__, __FILE__);
				$row = $db->sql_fetchrow($result);
				$db->sql_freeresult($result);
				$total_topics = intval($row['total_topics']);

				// count posts
				$sql = 'SELECT COUNT(post_id) AS total_posts
							FROM ' . POSTS_TABLE;
				$result = $db->sql_query($sql, false, __LINE__, __FILE__);
				$row = $db->sql_fetchrow($result);
				$db->sql_freeresult($result);
				$total_posts = intval($row['total_posts']);

				$config->begin_transaction();
				$config->set('stat_total_topics', $total_topics);
				$config->set('stat_total_posts', $total_posts);
				$config->end_transaction();
			}
			else
			{
				// count topics
				$sql = 'SELECT COUNT(topic_id) AS total_topics
							FROM ' . TOPICS_TABLE . '
							WHERE forum_id = ' . intval($id);
				$result = $db->sql_query($sql, false, __LINE__, __FILE__);
				$row = $db->sql_fetchrow($result);
				$db->sql_freeresult($result);
				$total_topics = intval($row['total_topics']);

				// count posts
				$sql = 'SELECT COUNT(post_id) AS total_posts
							FROM ' . POSTS_TABLE . '
							WHERE forum_id = ' . intval($id);
				$result = $db->sql_query($sql, false, __LINE__, __FILE__);
				$row = $db->sql_fetchrow($result);
				$db->sql_freeresult($result);
				$total_posts = intval($row['total_posts']);

				// get last post
				$sql = 'SELECT t.topic_last_post_id, t.topic_title, t.topic_last_poster, t.topic_last_username, t.topic_last_time, u.username
							FROM ' . TOPICS_TABLE . ' t
								LEFT JOIN ' . USERS_TABLE . ' u ON u.user_id = t.topic_last_poster
							WHERE forum_id = ' . intval($id) . '
								AND topic_moved_id = 0
							ORDER BY topic_last_post_id DESC
							LIMIT 1';
				$result = $db->sql_query($sql, false, __LINE__, __FILE__);
				$row = $db->sql_fetchrow($result);
				$db->sql_freeresult($result);
				$fields = array(
					'forum_topics' => $total_topics,
					'forum_posts' => $total_posts,
					'forum_last_post_id' => intval($row['topic_last_post_id']),
					'forum_last_title' => $row['topic_title'],
					'forum_last_poster' => intval($row['topic_last_poster']),
					'forum_last_username' => (($row['topic_last_poster'] != ANONYMOUS) && !empty($row['username'])) ? $row['username'] : $row['topic_last_username'],
					'forum_last_time' => intval($row['topic_last_time']),
				);
				$db->sql_statement($fields);
				$sql = 'UPDATE ' . FORUMS_TABLE . '
							SET ' . $db->sql_update . '
							WHERE forum_id = ' . intval($id);
				$db->sql_query($sql, false, __LINE__, __FILE__);
				if ( !$differ )
				{
					sync('forum');
				}
			}
			break;

		case 'topic':
			if ( $config->data['mod_attachment_CH'] )
			{
				global $attach_config;

				if ( $id = intval($id) )
				{
					include_once($config->url('includes/class_attach_handler'));
					$attach_handler = new attach_handler(PAGE_INDEX);
					$attach_handler->sync($id);
					unset($attach_handler);
				}
			}

			// prepare return : we don't touch first post data until a first post exists because of shadow topics
			$fields = array(
				'topic_replies' => 0,
				'topic_first_post_id' => 0,
				'topic_last_post_id' => 0,
				'topic_last_poster' => 0,
				'topic_last_username' => '',
				'topic_last_time' => 0,
			);
			// read topic counts
			$sql = 'SELECT MAX(post_id) AS topic_last_post_id, MIN(post_id) AS topic_first_post_id, COUNT(post_id) AS topic_replies
						FROM ' . POSTS_TABLE . '
						WHERE topic_id = ' . intval($id);
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			if ( $row = $db->sql_fetchrow($result) )
			{
				$fields = array_merge($fields, $row);
				if ( !empty($fields['topic_replies']) )
				{
					$fields['topic_replies']--;
				}
				else
				{
					// remove shadow topics
					$sql = 'DELETE FROM ' . TOPICS_TABLE . '
								WHERE topic_id = ' . intval($id) . '
									AND topic_moved_id <> 0';
					$db->sql_query($sql, false, __LINE__, __FILE__);
					return;
				}
			}
			$db->sql_freeresult($result);

			// read topic first & last post
			if ( !empty($fields['topic_last_post_id']) || !empty($fields['topic_first_post_id']) )
			{
				$sql = 'SELECT post_id, poster_id, post_username, post_time
							FROM ' . POSTS_TABLE . '
							WHERE topic_id = ' . intval($id) . '
								AND post_id IN(' . intval($fields['topic_first_post_id']) . ', ' . intval($fields['topic_last_post_id']) . ')';
				$result = $db->sql_query($sql, false, __LINE__, __FILE__);
				while ( $row = $db->sql_fetchrow($result) )
				{
					if ( $row['post_id'] == intval($fields['topic_last_post_id']) )
					{
						$fields = array_merge($fields, array(
							'topic_last_poster' => $row['poster_id'],
							'topic_last_username' => $row['post_username'],
							'topic_last_time' => $row['post_time'],
						));
					}
					if ( $row['post_id'] == intval($fields['topic_first_post_id']) )
					{
						$fields = array_merge($fields, array(
							'topic_poster' => $row['poster_id'],
							'topic_first_username' => $row['post_username'],
							'topic_time' => $row['post_time'],
						));
					}
				}
				$db->sql_freeresult($result);
			}
			$db->sql_statement($fields);
			$sql = 'UPDATE ' . TOPICS_TABLE . '
						SET ' . $db->sql_update . '
						WHERE topic_id = ' . intval($id);
			$db->sql_query($sql, false, __LINE__, __FILE__);
			break;
	}
	return true;
}

?>