<?php
/**
 *	Copyright © 2006-2008 CraKteR, crakter [at] gmail [dot] com
 *
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *	@version $Id: usergrouplegends.php 35 2008-08-14 19:14:12Z CraKteR $
 *	@copyright $LastChangedDate: 2008-08-14 21:14:12 +0200 (to, 14 aug 2008) $
 *	@author CraKteR <crakter@gmail.com>
 */

if(!defined("IN_MYBB"))
{
    die("This file cannot be accessed directly.");
}

$plugins->add_hook("index_start", "do_legend");

function usergrouplegends_info()
{
    return array(
        "name"				=> "Usergroup legends",
        "description"		=> "Shows Usergroups legends on the index.",
        "website"			=> "",
        "author"			=> "CraKteR",
        "authorsite"		=> "mailto:crakter@gmail.com",
        "version"			=> "2.0",
		"guid"				=> "d912aad6bc509657d0691d31ae8038a6",
		"compatibility" 	=> "14*",
    );
}

function usergrouplegends_activate()
{
	global $db;

	$setting_group = array(
		"gid" =>			NULL,
		"name" =>			"uot",
		"title" =>			"User group legends",
		"description" =>	"",
		"disporder" =>		"35",
		"isdefault" =>		"no"
	);
	$db->insert_query("settinggroups", $setting_group);
	$gid = $db->insert_id();

	$s_1 = array(
		"sid"			=> NULL,
		"name"			=> "legendsdontshow",
		"title"			=> "Groups to not show",
		"description"	=> "Groups not to be shown on usersgroup legends. Seperate with \',\'.",
		"optionscode"	=> "text",
		"value"			=> "",
		"disporder"		=> "2",
		"gid"			=> intval($gid)
	);
	$db->insert_query("settings", $s_1);

	$template = array(
		"tid"		=> NULL,
		"title"		=> "usergroup_legend",
		"template"	=> "<tr><td class=\"tcat\" colspan=\"2\"><strong>Group Legend</strong></td></tr>
<tr><td class=\"trow1\" colspan=\"2\">{\$usertitles}</td></tr>",
		"sid"		=> "-1"
	);
	$db->insert_query("templates", $template);
	
	require MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets("index_boardstats", '#{\$whosonline}#', "{\$whosonline}\n{\$user_legend}");
}

function usergrouplegends_deactivate()
{
	global $db;

	$db->query("DELETE FROM ".TABLE_PREFIX."templates WHERE title='usergroup_legend'");
	$db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='uot'");
	$db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name='legendsdontshow'");

	require MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets("index_boardstats", '#(\n?){\$user_legend}#', '', 0);
}

function do_legend()
{
	global $db, $mybb, $user_legend, $templates, $groupscache, $cache, $theme;

	if(!is_array($groupscache))
	{
		$groupscache = $cache->read("usergroups");
	}

	$usertitles = "";
	$dontshow = explode(',', $mybb->settings['legendsdontshow']);

	foreach($groupscache as $usertitle)
	{
		if(!in_array($usertitle['gid'], $dontshow))
		{
			$format = $usertitle['namestyle'];
			$userin = substr_count($format, "{username}");
			if($userin == 0)
			{
				$format = "{username}";
			}

			$format = stripslashes($format);
			$plugincache = $cache->read("plugins");
			$usergroup = str_replace("{username}", $usertitle['title'], $format);

			if($plugincache['active']['showgroup'])
			{
				$usergroup = "<a href=\"./groups.php?gid=".$usertitle['gid']."\">".$usergroup."</a>";
			}

			$usertitles .= "[".$usergroup."] ";
		}
	}
	eval("\$user_legend = \"".$templates->get("usergroup_legend")."\";"); 
}
?>