#[BOFN]###############################################################################
#
#
#Pagenews - a free script to publish news on websites
#Copyright (C) 2004,2005,2006,2007,2008 Philipp Kindt
#
#This file is part of Pagenews.
#
# 	 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/>.
#[EOFN]###############################################################################



#this package contains some nifty functions to 
#choose the right files for parsing with the donparser package.
#this package retrives the infos for templates

package dntemplatemanager;
require dntempinfo;
$dbpath = "$dntempinfo::basepath/dbs";
$dbc = "drafts_by_category.txt";
$tbd = "templates_by_draft.txt";
$tbt = "templates_by_type.txt";

########################################################################################################

#determines and returns the path by string $_[0] - values see subroutine
sub determine_path{
	my $path;
	if($_[0] eq "user"){
		$path = $dntempinfo::userpath;
	}elsif($_[0] eq "draft"){
		$path = $dntempinfo::draftpath;
	}else{
		$path = $dntempinfo::internalpath;	
	};
	return $path;
};
########################################################################################################
#makes a hirarchical tree of templates. it can destinguish menu templates (in internal section) from normal ones.
#a more sophisticated tree cannot be built by this function as it does not look into the template file. 
#saves it as a hash reference ($rv) 
#!just to use for the internal task!
#it destinguishes the different filenames for  categorization:
#menu entries have the filename dnmenu_MENU CATEGORY_filename.dontemplate
#the arrays $rv->{'fulltemplates'}->{'dnmenu'}->{MENU CATEGORY}->[index] for the full template name (including
#dnmenu-[MENU CATEGORY] and $rv->{'shorttemplates'}->{'dnmenu'}->{MENU CATEGORY}->[index] for jus the names of the entry
#a category index is stored in %{$rv->{'dnmenu'}};
#are created for menu entries.
#
#non-menu templates are stored in @{$rv->{'fulltemplates'}->{'normal'}} and @{$rv->{'shorttemplates'}->{'normal'}}
#
#$rv is returned
sub get_internal_templates{
	my $rv = {};
	foreach my $template (get_simple_template_list("internal")){
		(my $first, my $second, my @thirds) = 	split(/\_/,$template);
		my $third = donstdlib::array_to_string(\@thirds,"_");
		if($first eq "dnmenu"){
			if($third ne ""){					#happens if template is additional category code.
				push @{$rv->{'fulltemplates'}->{'dnmenu'}->{$second}},$template;
				push @{$rv->{'shorttemplates'}->{'dnmenu'}->{$second}},$third;	
			};				
			$rv->{'dnmenu'}->{$second} = "";
		}else{
			push @{$rv->{'fulltemplates'}->{'normal'}},$template;
			push @{$rv->{'shorttemplates'}->{'normal'}},$template;				
		};
	};
	
	return $rv;
};
########################################################################################################
#reads a template db file $_[0] ($_[0] without path!) and returns template array
#the primary_key must be in $_[1];
sub get_db_templates{
	my $dkvp = donkvparser->new();
	$dkvp->read("$dbpath/$_[0]");
	my $nrofblocks = $dkvp->{'info'}->{'data'}->{'nrofblocks'};
	my @rv;	
	foreach (my $cnt = 0; $cnt < $nrofblocks; $cnt++){
		if($dkvp->{'data'}->[$cnt]->{'primary_key'} eq $_[1]){
			foreach my $key (keys(%{$dkvp->{'data'}->[$cnt]})){
				if(($key ne "primary_key")&&($key ne "DKVP_BID")){
					push @rv,$dkvp->{'data'}->[$cnt]->{$key}; 			
				};
			};
		};
	};
	return @rv;
};

########################################################################################################
#creates a list of unhirachic templates.
#$_[0] can be "user" for user templates or "draft" for drafts or "internal" for internal templates.
#return as an array
sub get_simple_template_list{
	my $path = determine_path($_[0]);
	return donstdlib::glob_directory($path,"cutoffendings");	

};
########################################################################################################

#checks the name of a template which is to be created.
#name must be in $_[0]
sub check_templatename{
	if($_[0] =~ /[^a-zA-Z_0-9]/){
		donstdlib::error("dntemplatemanager_invalidtemplatename",$_[0]);
	};
};

########################################################################################################
#returns an array of drafts that have the list_category $_[0]
sub get_drafts_by_category{
	return get_db_templates($dbc,$_[0]);
};

########################################################################################################
#returns an array of templates that are derived from draft $_[0]
sub get_templates_by_draft{
	return get_db_templates($tbd,$_[0]);
};
########################################################################################################
#returns an array of templates that are of type $_[0]
sub get_templates_by_type{
	return get_db_templates($tbt,$_[0]);
};


########################################################################################################
#returns an array of templates that have the template_type $_[0]
sub get_list_categories{
	my @ret_array = donstdlib::glob_directory($dntempinfo::list_cat_path,"cutoffendings");
	return @ret_array;
};

#######################################################################################################
#deletes template $_[1]
#path must be determined by string $_[0] (see abouve)
sub delete_template{
	donstdlib::check_filename($_[1]);
	donstdlib::delete_file(determine_path($_[0])."/$_[1].dontemplate");
};



#######################################################################################################
#when viewed in Xinha, all [DNSTANDARD_COLOUR_*]-handlers
#are replaced by their actual colour values. This is required by xinha.
#this function replaces all (!!!) colour values by their actual value 
#needs template text in $_[0], returns parsed template
sub repair_colours{
	my $txt = $_[0];
	$txt =~ s/$dnmain::settings->{'COLOUR_NORMAL'}/\[DNSTANDARD_COLOUR_NORMAL\]/gi;
	$txt =~ s/$dnmain::settings->{'COLOUR_HIGHLIGHT'}/\[DNSTANDARD_COLOUR_HIGHLIGHT\]/gi;
	$txt =~ s/$dnmain::settings->{'COLOUR_DOUBLEHIGHLIGHT'}/\[DNSTANDARD_COLOUR_DOUBLEHIGHLIGHT\]/gi;
	$txt =~ s/$dnmain::settings->{'COLOUR_BGTITLE'}/\[DNSTANDARD_COLOUR_BGTITLE\]/gi;
	
	return $txt;
}
########################################################################################################

1;

