<?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();
$specialtemplates = array();

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

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

print_cp_header($vbphrase['eg_tfh_tab_modules']);

if($vbulletin->GPC['do'] == '')
{
	$enabled = array();
	$disabled = array();
	$di = 1;
	$ei = 1;
	
	$sql = $db->query_read("SELECT * FROM `" . TABLE_PREFIX . "eg_tfh_tabs` WHERE `iden`='m' ORDER BY `tabid`");
	while($module = $db->fetch_array($sql))
	{
		if($module['status'] == 0)
		{
			$disabled[$di++] = $module;
		}
		else
		{
			$enabled[$ei] = $module;
			$ei++;
		}
	}
	
	print_form_header('eg_tfh_modules', 'organize');
	print_table_header($vbphrase['eg_tfh_m_modules'], 7);
	
	// Needed for "Save Display Order"
	$taborder = array();

	if($ei != 1)
	{
		print_cells_row(array($vbphrase['eg_tfh_m_enabled'], $vbphrase['eg_tfh_description'], $vbphrase['eg_tfh_file'], $vbphrase['eg_tfh_cached'], $vbphrase['eg_tfh_limitedto'], $vbphrase['eg_tfh_displayorder'], ''), true);
	}

	foreach($enabled AS $emodule)
	{
		if($emodule['default_tab'] == 1)
		{
			$actions = '<a href="eg_tfh_modules.php?do=edit&module=' . $emodule['tabid'] . '">Edit</a>';
		}
		else
		{
			$actions = '<a href="eg_tfh_modules.php?do=disable&module=' . $emodule['tabid'] . '">' . $vbphrase['eg_tfh_disable'] . '</a> - <a href="eg_tfh_modules.php?do=edit&module=' . $emodule['tabid'] . '">' . $vbphrase['eg_tfh_edit'] . '</a> - <a href="eg_tfh_modules.php?do=delete&module=' . $emodule['tabid'] . '">' . $vbphrase['eg_tfh_delete'] . '</a>';
		}
		
		$limited = ($emodule['limitto'] == 'all') ? 'Everyone can see.' : $emodule['limitto'];
		$cache = ($emodule['cache'] == 'false') ? 'No.' : 'Yes.';
		print_cells_row(array($emodule['name'], $emodule['description'], $emodule['file'], $cache, $limited, "<input type=\"text\" size=\"3\" class=\"bginput\" name=\"order[$emodule[tabid]]\" value=\"".$emodule['displayorder']."\" accesskey=\"s\" >", $actions), false);
	}

	if($di != 1)
	{
		print_cells_row(array($vbphrase['eg_tfh_m_disabled'], $vbphrase['eg_tfh_description'], $vbphrase['eg_tfh_file'], $vbphrase['eg_tfh_cached'], $vbphrase['eg_tfh_limitedto'], $vbphrase['eg_tfh_displayorder'], ''), true);
	}

	foreach($disabled AS $dmodule)
	{
		$limited = ($dmodule['limitto'] == 'all') ? 'Everyone can see.' : $dmodule['limitto'];
		$cache = ($dmodule['cache'] == 'false') ? 'No.' : 'Yes.';
		print_cells_row(array($dmodule['name'], $dmodule['description'], $dmodule['file'], $cache, $limited, "<input type=\"text\" size=\"3\" class=\"bginput\" name=\"order[$dmodule[tabid]]\" value=\"".$dmodule['displayorder']."\" accesskey=\"s\" >", '<a href="eg_tfh_modules.php?do=enable&module=' . $dmodule['tabid'] . '">' . $vbphrase['eg_tfh_enable'] . '</a> - <a href="eg_tfh_modules.php?do=edit&module=' . $dmodule['tabid'] . '">' . $vbphrase['eg_tfh_edit'] . '</a> - <a href="eg_tfh_modules.php?do=delete&module=' . $dmodule['tabid'] . '">' . $vbphrase['eg_tfh_delete'] . '</a>'), 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_modules.php?do=add">[' . $vbphrase['eg_tfh_m_add'] . ']</a></div>';
}

if($vbulletin->GPC['do'] == 'enable' AND !empty($vbulletin->GPC['module']))
{
	$db->query("UPDATE `" . TABLE_PREFIX . "eg_tfh_tabs` SET `status`='1' WHERE `tabid`='" . $vbulletin->GPC['module'] . "'");
	print_cp_message($vbphrase['eg_tfh_m_ms_enabled'], 'eg_tfh_modules.php', 2);
}

if($vbulletin->GPC['do'] == 'disable' AND !empty($vbulletin->GPC['module']))
{
	$default = $db->query("SELECT * FROM `" . TABLE_PREFIX . "eg_tfh_tabs` WHERE `default_tab`='1' AND `tabid`='" . $vbulletin->GPC['module'] . "'");
	if($db->num_rows($default) > 0)
	{
		print_cp_message($vbphrase['eg_tfh_m_ms_nodisabled_def'], 'eg_tfh.php', 2);
	}
	else
	{
		$db->query("UPDATE `" . TABLE_PREFIX . "eg_tfh_tabs` SET `status`='0' WHERE `tabid`='" . $vbulletin->GPC['module'] . "'");
		print_cells_row($vbphrase['eg_tfh_m_ms_disabled'], 'eg_tfh_modules.php', 2);
	}
}

// ==== 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_modules.php');
	print_stop_message('saved_display_order_successfully');
}

if($vbulletin->GPC['do'] == 'add')
{
	print_form_header('eg_tfh_modules', 'doadd');
	print_table_header($vbphrase['eg_tfh_m_add']);
	print_input_row($vbphrase['eg_tfh_m_name'], 'name');
	print_input_row($vbphrase['eg_tfh_varname'], 'varname');
	print_input_row($vbphrase['eg_tfh_description'], 'description');
	print_input_row($vbphrase['eg_tfh_m_file'], 'file');
	print_input_row($vbphrase['eg_tfh_displayorder'], 'displayorder');
	print_input_row($vbphrase['eg_tfh_limitto'], 'limitto');
	print_yes_no_row($vbphrase['eg_tfh_cache_data'], 'cache', 0);
	print_yes_no_row($vbphrase['eg_tfh_m_enable'], 'enabled');
	print_submit_row($vbphrase['eg_tfh_m_add']);
}

if($vbulletin->GPC['do'] == 'doadd')
{
	$vbulletin->input->clean_array_gpc('p', array('name' => TYPE_HTML, 'varname' => TYPE_NOHTML, 'file' => TYPE_HTML, 'cache' => TYPE_UINT, 'enabled' => TYPE_UINT, 'limitto' => TYPE_NOHTML, 'displayorder' => TYPE_UINT, 'description' => TYPE_NOHTML));
	$varname_tsql = $vbulletin->db->query("SELECT * FROM `" . TABLE_PREFIX . "eg_tfh_tabs` WHERE `varname` = '" . $vbulletin->GPC['varname'] . "'");

	if($vbulletin->db->num_rows($varname_tsql) > 0)
	{
		print_cp_message($vbuphrase['eg_tfh_ms_addf_var'], 'eg_tfh_modules.php?do=add', 2);
	}
	else
	{
		$cache = ($vbulletin->GPC['cache'] == 0) ? 'false' : 'true';
		$db->query("INSERT INTO `" . TABLE_PREFIX . "eg_tfh_tabs` (`iden`, `name`, `varname`, `file`, `cache`, `status`, `limitto`, `displayorder`, `description`) VALUES ('m', '" . $db->escape_string($vbulletin->GPC['name']) . "', '" . $vbulletin->GPC['varname'] ."', '" . $vbulletin->GPC['file'] . "', '" . $cache . "', '" . $vbulletin->GPC['enabled'] . "', '" . $vbulletin->GPC['limitto'] . "', '" . $vbulletin->GPC['displayorder'] . "', '" . $vbulletin->GPC['description'] . "')");
		print_cp_message($vbphrase['eg_tfh_m_ms_added'], 'eg_tfh_modules.php', 2);
	}
}

if($vbulletin->GPC['do'] == 'edit' AND !empty($vbulletin->GPC['module']))
{
	$sql = $db->query("SELECT * FROM `" . TABLE_PREFIX . "eg_tfh_tabs` WHERE `tabid`='" . $vbulletin->GPC['module'] . "'");
	$module = $db->fetch_array($sql);
	$cache = ($module['cache'] == 'true') ? 1 : 0;
	print_form_header('eg_tfh_modules', 'doedit');
	print_table_header($vbphrase['eg_tfh_m_edit']);
	echo "<input type=\"hidden\" name=\"tabid\" value=\"" . $vbulletin->GPC['module'] . "\" />\n";
	print_input_row($vbphrase['eg_tfh_m_name'], 'name', $module['name']);
	print_input_row($vbphrase['eg_tfh_varname'], 'varname', $module['varname']);
	print_input_row($vbphrase['eg_tfh_description'], 'description', $module['description']);	
	print_input_row($vbphrase['eg_tfh_m_file'], 'file', $module['file']);
	print_input_row($vbphrase['eg_tfh_displayorder'], 'displayorder', $module['displayorder']);
	print_input_row($vbphrase['eg_tfh_limitto'], 'limitto', $module['limitto']);
	print_yes_no_row($vbphrase['eg_tfh_cache_data'], 'cache', $cache);
	print_yes_no_row($vbphrase['eg_tfh_m_enable'], 'enabled', $module['status']);
	print_submit_row($vbphrase['eg_tfh_m_edit']);
}

if($vbulletin->GPC['do'] == 'doedit')
{
	$vbulletin->input->clean_array_gpc('p', array('name' => TYPE_HTML, 'varname' => TYPE_NOHTML, 'file' => TYPE_HTML, 'cache' => TYPE_UINT, 'enabled' => TYPE_UINT, 'tabid' => TYPE_UINT, 'limitto' => TYPE_NOHTML, 'displayorder' => TYPE_UINT, 'description' => TYPE_NOHTML));
	
	$varname_tsql = $vbulletin->db->query("SELECT * FROM `" . TABLE_PREFIX . "eg_tfh_tabs` WHERE `varname` = '" . $vbulletin->GPC['varname'] . "' AND `tabid` <> '" . $vbulletin->GPC['tabid'] . "'");
	$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['limitto'] != 'all' AND $default_t['tabid'] == $vbulletin->GPC['tabid'])
	{
		print_cp_message($vbphrase['eg_tfh_ms_editf_default'], 'eg_tfh_modules.php?do=edit&id=' . $vbulletin->GPC['tabid'], 2);
	}
	else if($vbulletin->db->num_rows($varname_tsql) > 0)
	{
		print_cp_message($vbphrase['eg_tfh_ms_editf_var'], 'eg_tfh_modules.php?do=edit&id=' . $vbulletin->GPC['tabid'], 2);
	}
	else
	{
		$cache = ($vbulletin->GPC['cache'] == 0) ? 'false' : 'true';
		$db->query("UPDATE `" . TABLE_PREFIX . "eg_tfh_tabs` SET `name`='" . $db->escape_string($vbulletin->GPC['name']) . "', `varname`='" . $vbulletin->GPC['varname'] . "', `file`='" . $vbulletin->GPC['file'] . "', `cache`='" . $cache . "', `status`='" . $vbulletin->GPC['enabled'] . "', `limitto`='" . $vbulletin->GPC['limitto'] . "', `displayorder`='" . $vbulletin->GPC['displayorder'] . "', `description`='" . $vbulletin->db->escape_string($vbulletin->GPC['description']) . "' WHERE `tabid`='" . $vbulletin->GPC['tabid'] . "'");
		print_cp_message($vbphrase['eg_tfh_m_ms_edited'], 'eg_tfh_modules.php', 2);
	}
}

if($vbulletin->GPC['do'] == 'edit' AND empty($vbulletin->GPC['module']))
{
	print_cp_message($vbphrase['eg_tfh_m_ms_edit_nid'], 'eg_tfh_modules.php', 2);
}

if($vbulletin->GPC['do'] == 'enable' AND empty($vbulletin->GPC['module']))
{
	print_cp_message($vbphrase['eg_tfh_m_ms_edit_nid'], 'eg_tfh_modules.php', 2);
}

if($vbulletin->GPC['do'] == 'disable' AND empty($vbulletin->GPC['module']))
{
	print_cp_message($vbphrase['eg_tfh_m_ms_edit_nid'], 'eg_tfh_modules.php', 2);
}

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

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_m_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_m_ms_delete_r'], 'eg_tfh_modules.php', 2);
		}	
	}
	else
	{
		print_cp_message($vbphrase['eg_tfh_m_ms_delete_nr'], 'eg_tfh_modules.php', 2);
	}
}

print_cp_footer();

?>