<?php
// VERSION 4.0.8
/*======================================================================*\
|| #################################################################### ||
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2008+ King Kovifor. All Rights Reserved.              # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| #################################################################### ||
\*======================================================================*/
ini_set("display_errors", "1");
// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// ##################### DEFINE IMPORTANT CONSTANTS #######################
define('CVS_REVISION', '$RCSfile$ - $Revision: 1 $');

// #################### PRE-CACHE TEMPLATES AND DATA ######################
$phrasegroups = array('cpglobal');
$specialtemplates = array();

// ########################## REQUIRE BACK-END ############################
require_once('./global.php');

// ############################# CLEAN VARIABLES ###############################
$vbulletin->input->clean_array_gpc('g', array('do' => TYPE_NOHTML, 'mid' => TYPE_UINT, 'id' => TYPE_UINT));

print_cp_header($vbphrase['eg_tfh_tabs']);

if($vbulletin->GPC['do'] == '')
{
	
	$sql = $db->query_read("SELECT * FROM " . TABLE_PREFIX . "eg_tfh_tabs WHERE `iden`='t'");
	print_form_header('eg_tfh', 'organize');
	print_table_header($vbphrase['eg_tfh_your_tabs'], 7);
	
	// Needed for "Save Display Order"
	$taborder = array();
	
	print_cells_row(array($vbphrase['eg_tfh_name'], $vbphrase['eg_tfh_file'], $vbphrase['eg_tfh_forums'], $vbphrase['eg_tfh_cache'], $vbphrase['eg_tfh_limitedto'], $vbphrase['eg_tfh_displayorder'], $vbphrase['default']), true);
	if($db->num_rows($sql) > 0)
	{
		while($tab = $db->fetch_array($sql))
		{
			$cache = ($tab['cache'] == 'false') ? $vbphrase['no'] : $vbphrase['yes'];
			$limitto = ($tab['limitto'] == 'all') ? $vbphrase['eg_tfh_everyone'] : $tab['limitto'];
			$default = ($tab['default_tab'] == 0) ? '<a href="eg_tfh.php?do=default&id=' . $tab['tabid'] . '">' . $vbphrase['eg_tfh_default'] . '</a> - ' : $vbphrase['default'] . ' - ';
			$default = ($tab['limitto'] != 'all') ? '' : $default;
			if($tab['default_tab'] == 1)
			{
				$actions = '<a href="eg_tfh.php?do=edit&id=' . $tab['tabid'] . '">' . $vbphrase['eg_tfh_edit'] . '</a>';
			}
			else
			{
				$actions = '<a href="eg_tfh.php?do=edit&id=' . $tab['tabid'] . '">' . $vbphrase['eg_tfh_edit'] . '</a> - <a href="eg_tfh.php?do=delete&id=' . $tab['tabid'] . '">' . $vbphrase['eg_tfh_delete'] . '</a>';
			}
			print_cells_row(array($tab['name'], 'tabindex.php', $tab['forumids'], $cache, $limitto, "<input type=\"text\" size=\"3\" class=\"bginput\" name=\"order[$tab[tabid]]\" value=\"".$tab['displayorder']."\" accesskey=\"s\" >", $default . $actions), false);
		}
	}
	else
	{
		print_cells_row(array($vbphrase['eg_tfh_no_tabs'], '', '', '', '', '', ''), false);
	}

	$module_sql = $db->query_read("SELECT * FROM `" . TABLE_PREFIX . "eg_tfh_tabs` WHERE `status`='1' AND `iden`='m'");
	if($db->num_rows($module_sql) > 0)
	{
		print_cells_row(array($vbphrase['eg_tfh_m_active'], '', '', '', '', '', ''), true);
		while($module = $db->fetch_array($module_sql))
		{
			$default = ($module['default_tab'] == 0) ? '<a href="eg_tfh.php?do=default&id=' . $module['tabid'] . '">' . $vbphrase['eg_tfh_default'] . '</a>' : $vbphrase['default'];
			$default = ($module['limitto'] != 'all') ? '' : $default;
			$cache = ($module['cache'] == 'false') ? $vbphrase['no'] : $vbphrase['yes'];
			print_cells_row(array($module['name'], $module['file'], '', $cache, '', "<input type=\"text\" size=\"3\" class=\"bginput\" name=\"order[$module[tabid]]\" value=\"".$module['displayorder']."\" accesskey=\"s\" >", $default), false);
		}
	}
	
	print_table_footer(7, "<input type=\"submit\" class=\"button\" tabindex=\"1\" value=\"" .$vbphrase['save_display_order'] . "\" accesskey=\"s\" />");

	print '<br /><div style="text-align: center"><a href="eg_tfh.php?do=add">[' . $vbphrase['eg_tfh_add_tab'] . ']</a></div>';
}

// ==== Save Display Order ===============
if ($_REQUEST['do'] == 'organize') 
{
	$vbulletin->input->clean_array_gpc('p', array('order' => TYPE_ARRAY));

	if (is_array($vbulletin->GPC['order']))
	{
		$tabs = $db->query_read("SELECT * FROM " . TABLE_PREFIX . "eg_tfh_tabs");
		while ($tab = $db->fetch_array($tabs))
		{
			if (!isset($vbulletin->GPC['order']["$tab[tabid]"]))
			{
				continue;
			}
			
			$displayorder = intval($vbulletin->GPC['order']["$tab[tabid]"]);
			if ($tab['displayorder'] != $displayorder)
			{
				$tabdm =& datamanager_init('Tabs', $vbulletin, ERRTYPE_SILENT);
				$tabdm->set_existing($tab);
				$tabdm->setr('displayorder', $displayorder);
				$tabdm->save();
				unset($tabdm);
			}
		}
	}
	
	define('CP_REDIRECT', 'eg_tfh.php');
	print_stop_message('saved_display_order_successfully');
}

if($vbulletin->GPC['do'] == 'default')
{
	if($vbulletin->GPC['id'] != 0)
	{
		// Remove Previous Default Tab
		$db->query("UPDATE `" . TABLE_PREFIX . "eg_tfh_tabs` SET `default_tab`='0' WHERE `default_tab`='1'");
		// Add New Default Tab
		$db->query("UPDATE `" . TABLE_PREFIX . "eg_tfh_tabs` SET `default_tab`='1' WHERE `tabid`='" . $vbulletin->GPC['id'] . "'");
		print_cp_message($vbphrase['eg_tfh_ms_default_tab'], 'eg_tfh.php', 2);
	}
	else
	{
		print_cp_message($vbphrase['eg_tfh_ms_default_tab_noid'], 'eg_tfh.php', 2);
	}
}

if($vbulletin->GPC['do'] == 'add')
{
	print_form_header('eg_tfh', 'doadd');
	print_table_header($vbphrase['eg_tfh_add_tab']);
	print_input_row($vbphrase['eg_tfh_name'], 'name');
	print_input_row($vbphrase['eg_tfh_varname'], 'varname');
	print_input_row($vbphrase['eg_tfh_forums_ids'], 'forumids');
	print_input_row($vbphrase['eg_tfh_displayorder'], 'displayorder');
	print_input_row($vbphrase['eg_tfh_limitto'], 'groups');
	print_yes_no_row($vbphrase['eg_tfh_cache_data'], 'cache', 0);
	print_submit_row($vbphrase['eg_tfh_add_tab']);
}

if($vbulletin->GPC['do'] == 'doadd')
{
	$vbulletin->input->clean_array_gpc('p', array('name' => TYPE_HTML, 'varname' => TYPE_NOHTML, 'forumids' => TYPE_NOHTML, 'displayorder' => TYPE_UINT, 'groups' => TYPE_NOHTML, 'cache' => TYPE_UINT));

	$varname_tsql = $vbulletin->db->query("SELECT * FROM `" . TABLE_PREFIX . "eg_tfh_tabs` WHERE `varname` = '" . $vbulletin->GPC['varname'] . "'");
	
	if(empty($vbulletin->GPC['name']) OR empty($vbulletin->GPC['forumids']) OR empty($vbulletin->GPC['groups']))
	{
		print_cp_message($vbphrase['eg_tfh_ms_missing_fields'], 'eg_tfh.php?do=add', 2);
	}
	else if($vbulletin->db->num_rows($varname_tsql) > 0)
	{
		print_cp_message($vbuphrase['eg_tfh_ms_addf_var'], 'eg_tfh.php?do=add', 2);
	}
	else
	{
		$cache = ($vbulletin->GPC['cache'] == 1) ? 'true' : 'false';
		$db->query("INSERT INTO `" . TABLE_PREFIX . "eg_tfh_tabs` (`name`, `varname`, `forumids`, `limitto`, `cache`, `displayorder`) VALUES ('" . $db->escape_string($vbulletin->GPC['name']) . "', '" . $vbulletin->GPC['varname'] . "', '" . $vbulletin->GPC['forumids'] . "', '" . $vbulletin->GPC['groups'] . "', '" . $cache . "', '" . $vbulletin->GPC['displayorder'] . "')");
		print_cp_message($vbphrase['eg_tfh_add_success'], 'eg_tfh.php', 2);
	}
}

if($vbulletin->GPC['do'] == 'edit' AND $vbulletin->GPC['id'] != 0)
{
	$sql = $db->query("SELECT * FROM `" . TABLE_PREFIX . "eg_tfh_tabs` WHERE `tabid`='" . $vbulletin->GPC['id'] . "'");
	$tab = $db->fetch_array($sql);
	$cache = ($tab['cache'] == 'true') ? '1' : '0';
	print_form_header('eg_tfh', 'doedit');
	print_table_header($vbphrase['eg_tfh_edit_tab']);
	echo "<input type=\"hidden\" name=\"tabid\" value=\"" . $vbulletin->GPC['id'] . "\" />\n";
	print_input_row($vbphrase['eg_tfh_name'], 'name', $tab['name']);
	print_input_row($vbphrase['eg_tfh_varname'], 'varname', $tab['varname']);
	print_input_row($vbphrase['eg_tfh_forums_ids'], 'forumids', $tab['forumids']);
	print_input_row($vbphrase['eg_tfh_displayorder'], 'displayorder', $tab['displayorder']);
	print_input_row($vbphrase['eg_tfh_limitto'], 'groups', $tab['limitto']);
	print_yes_no_row($vbphrase['eg_tfh_cache_data'], 'cache', $cache);
	print_submit_row($vbphrase['eg_tfh_edit_tab']);
}

if($vbulletin->GPC['do'] == 'doedit')
{
	$vbulletin->input->clean_array_gpc('p', array('tabid' => TYPE_UINT, 'name' => TYPE_HTML, 'varname' => TYPE_NOHTML, 'forumids' => TYPE_NOHTML, 'groups' => TYPE_NOHTML, 'cache' => TYPE_UINT, 'displayorder' => TYPE_UINT));

	$varname_tsql = $vbulletin->db->query("SELECT * FROM `" . TABLE_PREFIX . "eg_tfh_tabs` WHERE `varname` = '" . $vbulletin->GPC['varname'] . "' AND `tabid` <> '" . $vbulletin->GPC['tabid'] . "'");
	if(empty($vbulletin->GPC['name']) OR empty($vbulletin->GPC['forumids']) OR empty($vbulletin->GPC['groups']))
	{
		print_cp_message($vbphrase['eg_tfh_ms_missing_fields'], 'eg_tfh.php?do=edit&amp;id=' . $vbulletin->GPC['tabid'], 2);
	}
	else
	{
		$default_s = $vbulletin->db->query("SELECT `tabid`, `default_tab` FROM `" . TABLE_PREFIX . "eg_tfh_tabs` WHERE `default_tab`='1'");
		$default_t = $vbulletin->db->fetch_array($default_s);
		if($vbulletin->GPC['groups'] != 'all' AND $default_t['tabid'] == $vbulletin->GPC['tabid'])
		{
			print_cp_message($vbphrase['eg_tfh_ms_editf_default'], 'eg_tfh.php?do=edit&amp;id=' . $vbulletin->GPC['tabid'], 2);
		}
		else if($vbulletin->db->num_rows($varname_tsql) > 0)
		{
			print_cp_message($vbuphrase['eg_tfh_ms_editf_var'], 'eg_tfh.php?do=edit&amp;id=' . $vbulletin->GPC['tabid'], 2);
		}
		else
		{
			$cache = ($vbulletin->GPC['cache'] == 1) ? 'true' : 'false';
			$db->query("UPDATE `" . TABLE_PREFIX . "eg_tfh_tabs` SET `name`='" . $db->escape_string($vbulletin->GPC['name']) . "', `varname`='" . $vbulletin->GPC['varname'] . "', `forumids`='" . $vbulletin->GPC['forumids'] . "', `limitto`='" . $vbulletin->GPC['groups'] . "', `cache`='" . $cache . "', `displayorder`='" . $vbulletin->GPC['displayorder'] . "' WHERE `tabid`='" . $vbulletin->GPC['tabid'] ."'");
			print_cp_message($vbphrase['eg_tfh_ms_edits'], 'eg_tfh.php', 2);
		}		
	}
}

if($vbulletin->GPC['do'] == 'delete')
{
	print_form_header('eg_tfh', 'dodelete');
	print_table_header($vbphrase['eg_tfh_delete_tab']);
	echo "<input type=\"hidden\" name=\"tabid\" value=\"" . $vbulletin->GPC['id'] . "\" />\n";
	print_yes_no_row($vbphrase['eg_tfh_delete_rus'], 'delete', 0);
	print_submit_row($vbphrase['eg_tfh_delete_tab']);
}

if($vbulletin->GPC['do'] == 'dodelete')
{
	$vbulletin->input->clean_array_gpc('p', array('tabid' => TYPE_UINT, 'delete' => TYPE_UINT));
	if($vbulletin->GPC['delete'] == 1)
	{
		$default = $db->query("SELECT * FROM `" . TABLE_PREFIX . "eg_tfh_tabs` WHERE `default_tab`='1' AND `tabid`='" . $vbulletin->GPC['tabid'] . "'");
		if($db->num_rows($default) > 0)
		{
			print_cp_message($vbphrase['eg_tfh_ms_delete_def'], 'eg_tfh.php', 2);
		}
		else
		{
			$db->query("DELETE FROM `" . TABLE_PREFIX . "eg_tfh_tabs` WHERE `tabid`='" . $vbulletin->GPC['tabid'] . "'");
			print_cp_message($vbphrase['eg_tfh_ms_delete_r'], 'eg_tfh.php', 2);
		}		
	}
	else
	{
		print_cp_message($vbphrase['eg_tfh_ms_delete_nr'], 'eg_tfh.php', 2);
	}
}

print_cp_footer();

?>