<?php
/***************************************************************************
 *                              admin/admin_classement_index.php
 *                            -------------------
 *   begin                : 30 mai 2004
 *   Fait par             : Dark_Génova (raph)
 *   email                : genovakiller@yahoo.fr
 *
 *
 *
 ***************************************************************************/

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

/*
 * On va commencer par chercher combien de tableau on doit afficher à cet endroit
 */

//
// on récupère les données de la table classement
//
$sql = 'SELECT classement_name, classement_value FROM ' . CLASSEMENT_TABLE;
if ( ! $result = $db->sql_query($sql) )
{
	message_die(GENERAL_ERROR, 'Impossible de récupérer les données de la table classement', '', __LINE__, __FILE__, $sql);
}

$classement_config = array();
while ( $row = $db->sql_fetchrow($result) )
{
	$classement_config[$row['classement_name']] = $row['classement_value'];
}

//
// Est on sur l'index?
//
$page_name = basename($HTTP_SERVER_VARS['PHP_SELF']);
$on_index = ( $page_name == 'index.' . $phpEx ) ? TRUE : FALSE;
$mode_classement_parse = ( $on_up ) ? 'mod_classement_parse_up' : 'mod_classement_parse_down';

//
// Combien doit on afficher de tableau pour cette partie? (0, 1, ou 2)
//
$count_table = 0;
for ( $a = 0; $a < 2; $a++ )
{
	$draw_name = ( $a == 0 ) ? 'draw_classement_0' : 'draw_classement_1';

	if ( !$classement_config['CI_desactivate_' . $a] )
	{
		$on_position = ( ( $classement_config['CI_position_' . $a] && $on_up ) || ( !$classement_config['CI_position_' . $a] && $on_down ) ) ? TRUE : FALSE;

		if ( $classement_config['CI_page_' . $a] && $on_position )
		{
			$$draw_name = TRUE;
			$count_table++;
		}
		else if ( !$classement_config['CI_page_' . $a] && $on_index && $on_position )
		{
			$$draw_name = TRUE;
			$count_table++;
		}
		else
		{
			$$draw_name = FALSE;
		}
	}
}

/*
 * On affiche les classements
 */

//
// On défini dans ce tableau les classements existants
//
$classement_module = array();
$z = 0;

// Modules innés à phpBB
$classement_module[$z] = 'date_register';
$z++;
$classement_module[$z] = 'nbr_post';
$z++;
$classement_module[$z] = 'date_topic';
$z++;
$classement_module[$z] = 'view_topic';
$z++;
$classement_module[$z] = 'reply_topic';
$z++;
$classement_module[$z] = 'date_post';
$z++;

// Modules extension en fonction d'autres MODS
$classement_module[$z] = ( @file_exists($phpbb_root_path . 'level_mod.' . $phpEx) ) ? 'rpg_level' : '-none-';
$z++;
$classement_module[$z] = ( $userdata['user_points'] != '' ) ? 'nbr_point' : '-none-';
$z++;
$classement_module[$z] = ( $userdata['user_totaltime'] != '' ) ? 'total_time' : '-none-';
$z++;
$classement_module[$z] = ( $userdata['user_totallogon'] != '' ) ? 'total_logon' : '-none-';
$z++;
$classement_module[$z] = ( $userdata['user_totalpages'] != '' ) ? 'total_page' : '-none-';
$z++;
$classement_module[$z] = ( @file_exists($phpbb_root_path . 'annuaire.' . $phpEx) ) ? 'annuaire_clic' : '-none-';
$z++;

// On vire les modules n'existants pas
$classement_tmp_module = array();
$j = 0;
for ( $i = 0; $i < count($classement_module); $i++ )
{
	if ( $classement_module[$i] != '-none-' )
	{
		$classement_tmp_module[$j] = $classement_module[$i];
		$j++;
	}
}
$classement_module = $classement_tmp_module;

//
// On affiche les tableaux s'il y en au moins 1 à afficher
//

if ( $count_table > 0 )
{
	//
	// On créé une nouvelle instance de la classe template
	//
	$style = ( $userdata['user_style'] == '' ) ? $board_config['default_style'] : $userdata['user_style'];
	$sql_template = 'SELECT *
		FROM ' . THEMES_TABLE . '
		WHERE themes_id = \'' . $style . '\'';
	if ( !($result_template = $db->sql_query($sql_template)) )
	{
		message_die(CRITICAL_ERROR, 'Could not query database for theme info', '', __LINE__, __FILE__, $sql_template);
	}

	if ( !($row_template = $db->sql_fetchrow($result_template)) )
	{
		message_die(CRITICAL_ERROR, 'Could not get theme data for themes_id [' . $userdata['user_style'] . ']');
	}

	$template_path = 'templates/';
	$template_name = $row_template['template_name'];

	$template_classement = new Template($phpbb_root_path . $template_path . $template_name);

	for ( $b = 0; $b < 2; $b++ )
	{
		$draw_col = ( $b == 0 ) ? 'draw_classement_0' : 'draw_classement_1';

		if ( $$draw_col )
		{
			//
			// Configuration du classement
			//
			$limit = $classement_config['CI_limit_' . $b];
			$random = $classement_config['CI_random_' . $b];

			//
			// Est on est mode random ou pas?
			//
			if ( $random )
			{
				//
				// En cas de random order on redéfini la liste des modules
				//
				$random_list = ( $classement_config['CI_random_order_' . $b] > 0 && strpos($classement_config['CI_random_order_' . $b], ',') ) ? TRUE : FALSE;

				if ( $random_list )
				{
					$random_list_array = explode(',', $classement_config['CI_random_order_' . $b]);
					$random_tmp_list_array = array();
					for ( $k = 0; $k < count($random_list_array); $k++ )
					{
						$random_tmp_list_array[$k] = $random_list_array[$k] - 1;
					}
					$random_list_array = $random_tmp_list_array;

					$classement_real_list = array();
					$i = 0;
					reset($classement_module);
					while ( list($key, $value) = each($classement_module) )
					{
						if ( @in_array($key, $random_list_array) )
						{
							$classement_real_list[$i] = $value;
							$i++;
						}
					}
				}
				else
				{
					$classement_real_list = $classement_module;
				}

				$random_classement_number = rand(0, ( count($classement_real_list) -1));
				$classement = $classement_real_list[$random_classement_number];
				$order = ( rand(0, 1) == 0 ) ? 'asc' : 'desc';
			}
			else
			{
				$order = $classement_config['CI_order_' . $b];
				$manual_order = $classement_config['CI_manual_order_' . $b];

				//
				// En cas de manual_order on sélectionne la liste des classements
				//
				$manual_list = ( $classement_config['CI_manual_order_' . $b] > 0 && strpos($classement_config['CI_manual_order_' . $b], ',') ) ? TRUE : FALSE;

				if ( $manual_list )
				{
					$manual_list_array = explode(',', $classement_config['CI_manual_order_' . $b]);
					$classement = trim($classement_module[($manual_list_array[0] -1)]);

					$manual_tmp_list_array = array();
					for ( $i = 0; $i < count($manual_list_array); $i++ )
					{
						if ( $i != 0 )
						{
							$manual_tmp_list_array[($i - 1)] = trim($manual_list_array[$i]);
						}
					}
					$manual_tmp_list_array[($i + 1)] = $manual_list_array[0];
					$new_manual_list = implode($manual_tmp_list_array, ', ');

					$sql = 'UPDATE ' . CLASSEMENT_TABLE . ' SET classement_value=\'' . $new_manual_list . '\' WHERE classement_name=\'CI_manual_order_' . $b . '\'';
					if ( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, 'Impossible de mettre la table classement à jour', __LINE__, __FILE__, $sql);
					}
				}
				else
				{
					$classement = $classement_config['CI_classement_' . $b];
				}
			}

			//
			// On sélectionne les données des classements
			//
			switch ( $classement )
			{
				case "date_register":
					$l_login = $lang['ci_login'];
					$sql_query = 'SELECT user_id, username, user_regdate, user_level FROM ' . USERS_TABLE . ' WHERE user_id<>\'-1\' ORDER BY user_regdate ' . $order . ' LIMIT ' . $limit;
					break;

				case "nbr_post":
					$l_login = $lang['ci_login'];
					$sql_query = 'SELECT user_id, username, user_posts, user_level FROM ' . USERS_TABLE . ' WHERE user_id<>\'-1\' ORDER BY user_posts ' . $order . ' LIMIT ' . $limit;
					break;

				case "nbr_point":
					$l_login = $lang['ci_login'];
					$sql_query = 'SELECT user_id, username, user_points, user_level FROM ' . USERS_TABLE . ' WHERE user_id<>\'-1\' ORDER BY user_points ' . $order . ' LIMIT ' . $limit;
					break;

				case "rpg_level":
					$l_login = $lang['ci_login'];
					$sql_query = 'SELECT user_id, username, user_posts, user_level FROM ' . USERS_TABLE . ' WHERE user_id<>\'-1\' ORDER BY user_posts ' . $order . ' LIMIT ' . $limit;
					break;

				case "total_time":
					$l_login = $lang['ci_login'];
					$sql_query = 'SELECT user_id, username, user_totaltime, user_level FROM ' . USERS_TABLE . ' WHERE user_id<>\'-1\' ORDER BY user_totaltime ' . $order . ' LIMIT ' . $limit;
					break;

				case "total_logon":
					$l_login = $lang['ci_login'];
					$sql_query = 'SELECT user_id, username, user_totallogon, user_level FROM ' . USERS_TABLE . ' WHERE user_id<>\'-1\' ORDER BY user_totallogon ' . $order . ' LIMIT ' . $limit;
					break;

				case "total_page":
					$l_login = $lang['ci_login'];
					$sql_query = 'SELECT user_id, username, user_totalpages, user_level FROM ' . USERS_TABLE . ' WHERE user_id<>\'-1\' ORDER BY user_totalpages ' . $order . ' LIMIT ' . $limit;
					break;

				case "date_topic":
					$l_login = $lang['ci_login_topic'];
					$sql_query = 'SELECT topic_id, topic_title, topic_time FROM ' . TOPICS_TABLE . ' ORDER BY topic_time ' . $order . ' LIMIT ' . $limit;
					break;

				case "view_topic":
					$l_login = $lang['ci_login_topic'];
					$sql_query = 'SELECT topic_id, topic_title, topic_views FROM ' . TOPICS_TABLE . ' ORDER BY topic_views ' . $order . ' LIMIT ' . $limit;
					break;

				case "reply_topic":
					$l_login = $lang['ci_login_topic'];
					$sql_query = 'SELECT topic_id, topic_title, topic_replies FROM ' . TOPICS_TABLE . ' ORDER BY topic_replies ' . $order . ' LIMIT ' . $limit;
					break;

				case "date_post":
					$l_login = $lang['ci_login_topic'];
					$sql_query = 'SELECT p.post_id, p.post_time, p.topic_id, t.topic_id, t.topic_title FROM ' . POSTS_TABLE . ' p, ' . TOPICS_TABLE . ' t WHERE t.topic_id = p.topic_id ORDER BY post_time ' . $order . ' LIMIT ' . $limit;
					break;

				case "annuaire_clic":
					$l_login = $lang['ci_url'];
					$sql_query = 'SELECT annu_lien_id, annu_lien_url, annu_lien_vu FROM ' . ANNU_LIEN_TABLE . ' ORDER BY annu_lien_vu ' . $order . ' LIMIT ' . $limit;
					break;

				default:
					message_die(GENERAL_ERROR, 'Les données du classement sont corrompues, classement: ' . $classement, '', __LINE__, __FILE__);
					break;

			}

			// variables de langues dynamiques
			$l_value = $lang_value = $lang['ci_value_' . $classement];
			$l_effect = $lang['ci_' . $classement . '_' . $order];

			$template_classement->set_filenames(array($mod_classement_parse => "mod_classement_body.tpl"));

			$template_classement->assign_block_vars('bloc', array(
				'L_CI_EFFECT' => 	$l_effect,
				'L_CI_LOGIN' =>	$l_login,
				'L_CI_VALUE' =>	$l_value
			));

			//
			// On lance les requètes SQL
			//
			if ( ! $result = $db->sql_query($sql_query) )
			{
				message_die(GENERAL_ERROR, 'Impossible de récupérer les données du classement', '', __LINE__, __FILE__, $sql_query);
			}

			while( $row = $db->sql_fetchrow($result) )
			{
				//
				// On récupère les valeurs
				switch ( $classement )
				{
					case "date_register":
						$ci_value = create_date($board_config['default_dateformat'], $row['user_regdate'], $board_config['board_timezone']);
						$template_classement_ci_login = '<a href="' . append_sid('profile.' . $phpEx . '?mode=viewprofile&u=' . $row['user_id']) . '" class="genmed">' . $row['username'] . '</a>';
						break;
					case "nbr_post":
						$ci_value = $row['user_posts'];
						$template_classement_ci_login = '<a href="' . append_sid('profile.' . $phpEx . '?mode=viewprofile&u=' . $row['user_id']) . '" class="genmed">' . $row['username'] . '</a>';
						break;
					case "nbr_point":
						$ci_value = $row['user_points'];
						$template_classement_ci_login = '<a href="' . append_sid('profile.' . $phpEx . '?mode=viewprofile&u=' . $row['user_id']) . '" class="genmed">' . $row['username'] . '</a>';
						break;
					case "rpg_level":
						$ci_value = ( $row['user_posts'] < 1 ) ? 0 : floor( pow( log10( $row['user_posts'] ), 3 ) ) + 1;
						$template_classement_ci_login = '<a href="' . append_sid('profile.' . $phpEx . '?mode=viewprofile&u=' . $row['user_id']) . '" class="genmed">' . $row['username'] . '</a>';
						break;
					case "total_time":
						$ci_value = make_hours($row['user_totaltime']);
						$template_classement_ci_login = '<a href="' . append_sid('profile.' . $phpEx . '?mode=viewprofile&u=' . $row['user_id']) . '" class="genmed">' . $row['username'] . '</a>';
						break;
					case "total_logon":
						$ci_value = $row['user_totallogon'];
						$template_classement_ci_login = '<a href="' . append_sid('profile.' . $phpEx . '?mode=viewprofile&u=' . $row['user_id']) . '" class="genmed">' . $row['username'] . '</a>';
						break;
					case "total_page":
						$ci_value = $row['user_totalpages'];
						$template_classement_ci_login = '<a href="' . append_sid('profile.' . $phpEx . '?mode=viewprofile&u=' . $row['user_id']) . '" class="genmed">' . $row['username'] . '</a>';
						break;
					case "date_topic":
						$ci_value = create_date($board_config['default_dateformat'], $row['topic_time'], $board_config['board_timezone']);
						$template_classement_ci_login = '<a href="' . append_sid('viewtopic.' . $phpEx . '?t=' . $row['topic_id']) . '" class="genmed">' . $row['topic_title'] . '</a>';
						break;
					case "view_topic":
						$ci_value = $row['topic_views'];
						$template_classement_ci_login = '<a href="' . append_sid('viewtopic.' . $phpEx . '?t=' . $row['topic_id']) . '" class="genmed">' . $row['topic_title'] . '</a>';
						break;
					case "reply_topic":
						$ci_value = $row['topic_replies'];
						$template_classement_ci_login = '<a href="' . append_sid('viewtopic.' . $phpEx . '?t=' . $row['topic_id']) . '" class="genmed">' . $row['topic_title'] . '</a>';
						break;
					case "date_post":
						$ci_value = create_date($board_config['default_dateformat'], $row['post_time'], $board_config['board_timezone']);
						$template_classement_ci_login = '<a href="' . append_sid('viewtopic.' . $phpEx . '?p=' . $row['post_id'] . '#' . $row['post_id']) . '" class="genmed">' . $row['topic_title'] . '</a>';
						break;
					case "annuaire_clic":
						$ci_value = $row['annu_lien_vu'];
						$template_classement_ci_login = '<a href="' . append_sid('annuaire.' . $phpEx . '?mode=view_url&url_id=' . $row['annu_lien_id']) . '" class="genmed">' . $row['annu_lien_url'] . '</a>';
						break;
				}

				$template_classement->assign_block_vars('bloc.ci', array(
					'CI_LOGIN' => 	$template_classement_ci_login,
					'CI_VALUE' => 	$ci_value
				));
			}
		}
	}

	$template_classement->pparse($mod_classement_parse);
}

?>