###############################################################################
# HelpCentre.pl                                                               #
###############################################################################
# YaBB: Yet another Bulletin Board                                            #
# Open-Source Community Software for Webmasters                               #
# Version:        YaBB 2 RC 1                                                 #
# Released:       December 27, 2004                                           #
# Distributed by: http://www.yabbforum.com                                    #
# =========================================================================== #
# Copyright (c) 2000-2005 YaBB (www.yabbforum.com) - All Rights Reserved.     #
# Software by: The YaBB Development Team                                      #
#              with assistance from the YaBB community.                       #
# Sponsored by: Xnull Internet Media, Inc. - http://www.ximinc.com            #
#               Your source for web hosting, web design, and domains.         #
###############################################################################

$helpcentreplver = 'YaBB 2 RC 1 $Revision: 1.53 $';
if($action eq 'detailedversion') { return 1; }

LoadLanguage("HelpCentre");

$yytitle = "$helptxt{'1'}";

sub SectionDecide {

## This bit decides what section we are in and sets the background accordingly
## Also sets the variables are used to open up the correct Help Directory
$moderator_class = "catbg";
$admin_class = "catbg";
$global_mod_class = "catbg";
$UserClass = "catbg";

if(-e("$vardir/HelpSettings.txt")) { require "$vardir/HelpSettings.txt"; }
else { $UseHelp_Perms = 0; }

if($UseHelp_Perms) {
$ismod = 0;
unless (exists $memberinfo{$username}) { &LoadUser($username);&LoadMiniUser($username); }
foreach $catid (@categoryorder){
	if($ismod) { last; }
	$boardlist = $cat{$catid};
	(@bdlist) = split(/\,/, $boardlist);
	foreach $curboard (@bdlist) {
		if($ismod) { last; }
		$bdmods = ${$uid.$curboard}{'mods'};
		$bdmods =~ s/ //g;
		foreach $curuser (split(/\,/, $bdmods)) {
			if($curuser eq $username) { $ismod = 1; last; }
		}
		$bdmodsgrp = ${$uid.$curboard}{'modgroups'};
		$bdmodsgrp =~ s/ //g;
		foreach $curusergrp (split(/\,/, $bdmodsgrp)) {
			if($curusergrp eq ${$uid.$username}{'position'}) { $ismod = 1; last; }
		}
	}
}
}

if($INFO{'section'} eq "admin") {
if($UseHelp_Perms && !$iamadmin) { &fatal_error("$maintxt{'1'}"); }
${$INFO{'section'}._class} = "titlebg";
$help_area = "Admin";
	} elsif($INFO{'section'} eq "moderator") {
if($UseHelp_Perms && !$ismod && !$iamgmod && !$iamadmin) { &fatal_error("$maintxt{'1'}"); }
${$INFO{'section'}._class} = "titlebg";
$help_area = "Moderator";
	} elsif($INFO{'section'} eq "global_mod") {
if($UseHelp_Perms && !$iamgmod && !$iamadmin) { &fatal_error("$maintxt{'1'}"); }
${$INFO{'section'}._class} = "titlebg";
$help_area = "Gmod";
	} else {
$UserClass = "titlebg";
$help_area = "User";
	}

}

sub SectionPrint {
## Prints the navigation bar for the help section
$userhlp = qq~<a href="$scripturl?action=help">$helptxt{'3'}</a>~;
if($UseHelp_Perms) {
	if(!$ismod && !$iamgmod && !$iamadmin) { return }
	if($ismod || $iamgmod || $iamadmin) {
		$modhlp = qq~<a href="$scripturl?action=help;section=moderator">$helptxt{'4'}</a>~;
	} else { $modhlp = $helptxt{'4'}; }
	if($iamgmod || $iamadmin) {
		$gmodhlp = qq~<a href="$scripturl?action=help;section=global_mod">$helptxt{'5'}</a>~;
	} else { $gmodhlp = $helptxt{'5'}; }
	if($iamadmin) {
		$adminhlp = qq~<a href="$scripturl?action=help;section=admin">$helptxt{'6'}</a>~;
	} else { $adminhlp = $helptxt{'6'}; }
}
else {
	$modhlp = qq~<a href="$scripturl?action=help;section=moderator">$helptxt{'4'}</a>~;
	$gmodhlp = qq~<a href="$scripturl?action=help;section=global_mod">$helptxt{'5'}</a>~;
	$adminhlp = qq~<a href="$scripturl?action=help;section=admin">$helptxt{'6'}</a>~;
}

$HelpNavBar =~ s/<user menu>/$userhlp/g;
$HelpNavBar =~ s/<moderator menu>/$modhlp/g;
$HelpNavBar =~ s/<global mod menu>/$gmodhlp/g;
$HelpNavBar =~ s/<admin menu>/$adminhlp/g;
$HelpNavBar =~ s/<user class>/$UserClass/g;
$HelpNavBar =~ s/<moderator class>/$moderator_class/g;
$HelpNavBar =~ s/<global mod class>/$global_mod_class/g;
$HelpNavBar =~ s/<admin class>/$admin_class/g;
$yymain .= $HelpNavBar;

}

sub GetHelpFiles {

unless ($HelpTemplateLoaded) {
	if(-e ("$templatesdir/$usestyle.HelpCentre.template")) {
		require "$templatesdir/$usestyle.HelpCentre.template";
	}
	else {
		require "$templatesdir/default.HelpCentre.template";
	}
}

&SectionDecide;

# This determines if the order file is present and if it isn't
# It creates a new one, in default alphabetical order

if(!-e ("$helpfile/$help_area.helporder")) {
	&CreateOrderFile;
}

fopen(HELPORDER, "$helpfile/$help_area.helporder"); 
@helporderlist = <HELPORDER>; 
fclose(HELPORDER);

foreach $line (@helporderlist){
	chomp $line;
	if(-e ("$helpfile/$language/$help_area/$line.help")) {
		require "$helpfile/$language/$help_area/$line.help";
	}
	elsif(-e ("$helpfile/English/$help_area/$line.help")) {
		require "$helpfile/English/$help_area/$line.help";
	}
	else {
		next;
	}

	&MainHelp;
	&DoContents;
}

&SectionPrint;
&ContentContainer;

&template;
}

sub MainHelp {

$TempParse = $BodyHeader;
$TempParse =~ s/<yabb section_anchor>/$SectionName/g;
$SectionNam = $SectionName;
$SectionNam =~ s/_/ /g;
$TempParse =~ s/<yabb section_name>/$SectionNam/g;
$Body .= qq~$TempParse~;

$i = 1;
while (${SectionSub.$i}) {

$TempParse = $BodySubHeader;
$SectionAnchor = ${SectionSub.$i};
$SectionSub = ${SectionSub.$i};
$SectionSub =~ s/_/ /g;
$TempParse =~ s/<yabb section_anchor>/$SectionAnchor/g;
$TempParse =~ s/<yabb section_sub>/$SectionSub/g;
$Body .= qq~$TempParse~;

$message = ${SectionBody.$i};
&FromHTML($message);
$message =~ s~&#64;~@~g;

&wrap;
if(!$yyYaBBCloaded) { require "$sourcedir/YaBBC.pl"; } &DoUBBC;
$message =~ s~\[yabbimg\](\S+?\.\S{3,4})\[/yabbimg\]~<img src="$imagesdir/$1" border="0" alt="" />~ig;
&wrap2;

$TempParse = $BodyItem;
$TempParse =~ s/<yabb item>/$message/g;
$Body .= qq~$TempParse~;
$i++;
}
$Body .= qq~$BodyFooter~;
}

sub ContentContainer {

$MainLayout =~ s/<yabb contents>/$Contents/g;
$MainLayout =~ s/<yabb body>/$Body/g;

$yymain .= qq~$MainLayout~;

}

sub DoContents {
$TempParse = $ContentHeader;

$TempParse =~ s/<yabb section_anchor>/$SectionName/g;
$SectionNam = $SectionName;
$SectionNam =~ s/_/ /g;
$TempParse =~ s/<yabb section_name>/$SectionNam/g;
$Contents .= qq~$TempParse~;

$Contents .= qq~<ul style="list-style: none; margin: 0; padding: 2px; border: none;">~;
$i = 1;
while (${SectionSub.$i}) {
$SectionAnchor = ${SectionSub.$i};
${SectionSub.$i} =~ s/_/ /g;

$TempParse = $ContentItem;
$TempParse =~ s/<yabb anchor>/$SectionAnchor/g;
$TempParse =~ s/<yabb content>/${SectionSub.$i}/g;

$Contents .= qq~$TempParse~;
${SectionSub.$i} = "";
$i++;
}
$Contents .= qq~</ul>~;
}

sub CreateOrderFile {
	opendir(HELPDIR, "$helpfile/$language/$help_area");
		@contents = readdir(HELPDIR);
	closedir(HELPDIR);

	foreach $line (sort {uc($a) cmp uc($b)} @contents){
	($name, $extension) = split (/\./, $line);
	if ($extension !~ /help/i) { next; }

	$order_list .= "$name\n";

	}

	fopen(HELPORDER, ">$helpfile/$help_area.helporder") || die("couldn't write order file - check permissions on $helpfile");
	print HELPORDER qq~$order_list~;
	fclose(HELPORDER);

}


1;