#[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 directly from the template files
#and is very sloooooow, but builds the databases for faster retrival
package dntemplate_db_builder;
require dntemplatemanager;
$dbpath = "$dntemplatemanager::dbpath";


########################################################################################################
#returns a hash ref /array combination of drafts sorted by list category
sub retrive_drafts_by_category{
	my $ret_hash;
	foreach my $template (dntemplatemanager::get_simple_template_list("draft")){
		my $ti = dntempinfo::retrive_embedded_information($template,"draft");
		push @{$ret_hash->{$ti->{'list_category'}}},$template;
	};
	return $ret_hash;
};

########################################################################################################
#builds drafts_by_category db
sub build_drafts_by_category{
	my $dkvp = donkvparser->new();
	$dkvp->set_filename("$dbpath/$dntemplatemanager::dbc");
	my $data = retrive_drafts_by_category();
	foreach my $category (keys(%{$data})){
		my $bid = $dkvp->add_block();
		my $block = {};	
		my $cnt = 0;	
		foreach my $template (@{$data->{$category}}){
			$block->{$cnt} = $template;
			$cnt++;			
		};
		$block->{'primary_key'} = $category;
		$dkvp->set_block($bid,$block);		
	};
		$dkvp->write();
};


########################################################################################################
#returns a hash - ref/array-combination of templates sorted by draft
sub retrive_templates_by_draft{
	my $ret_hash;
	foreach my $template (dntemplatemanager::get_simple_template_list("user")){
		my $ti = dntempinfo::retrive_embedded_information($template,"user");
		push @{$ret_hash->{$ti->{'parent'}}},$template;
	}; 
	return $ret_hash;
};
########################################################################################################
#builds templates_by_drafts db
sub build_templates_by_drafts{
	my $dkvp = donkvparser->new();
	$dkvp->set_filename("$dbpath/$dntemplatemanager::tbd");
	my $data = retrive_templates_by_draft();
	foreach my $draft (keys(%{$data})){
		my $bid = $dkvp->add_block();
		my $block = {};	
		my $cnt = 0;	
			foreach my $template (@{$data->{$draft}}){;
				$block->{$cnt} = $template;
				$cnt++;			
			};
		$block->{'primary_key'} = $draft;
		$dkvp->set_block($bid,$block);		
	};
		$dkvp->write();
};




########################################################################################################
#returns a hash - ref/array-combination of templates sorted by draft
sub retrive_templates_by_type{
	my $ret_hash;
	foreach my $template (dntemplatemanager::get_simple_template_list("user")){
		my $ti = dntempinfo::retrive_embedded_information($template,"user");
		push @{$ret_hash->{$ti->{'template_type'}}},$template;
	}; 
	return $ret_hash;
};


########################################################################################################
#builds templates_by_type db
sub build_templates_by_type{
	my $dkvp = donkvparser->new();
	$dkvp->set_filename("$dbpath/$dntemplatemanager::tbt");
	my $data = retrive_templates_by_type();
	foreach my $key (keys(%{$data})){
		my $bid = $dkvp->add_block();
		my $block = {};	
		my $cnt = 0;	
		
		foreach my $template (@{$data->{$key}}){
			$block->{$cnt} = $template;
			$cnt++;			
		};
		$block->{'primary_key'} = $key;
		$dkvp->set_block($bid,$block);		
	};
		$dkvp->write();
};

########################################################################################################
#removes template $_[0] from the db file $_[1];

sub remove_template_from_db{
	my $dkvp = donkvparser->new();
	$dkvp->read("$dbpath/$_[1]");
	my $nrofblocks = $dkvp->{'info'}->{'data'}->{'nrofblocks'};
	foreach (my $cnt = 0; $cnt < $nrofblocks; $cnt++){
		my $newblock;
		foreach my $key (keys(%{$dkvp->{'data'}->[$cnt]})){
			if(($key ne "primary_key")&&($key ne "DKVP_BID")){
				if($dkvp->{'data'}->[$cnt]->{$key} ne $_[0]){
					$newblock->{$key} = $dkvp->{'data'}->[$cnt]->{$key}; 
				}; 			
			}else{
					$newblock->{$key} = $dkvp->{'data'}->[$cnt]->{$key}; 	
			};		
		};
		$dkvp->set_block($newblock->{'DKVP_BID'},$newblock);
	};
	$dkvp->write();
};


########################################################################################################
#removes template $_[0] from all dbs

sub remove_template_from_dbs{
	remove_template_from_db($_[0],$dntemplatemanager::tbd);
	remove_template_from_db($_[0],$dntemplatemanager::tbt);
	
};

########################################################################################################
#adds template $_[0] to db file $_[1].
#primary key must be in $_[1] and can be "template_type" or "parent" 
sub add_template_to_db{
	my $ti = dntempinfo::retrive_embedded_information($_[0],"user");
	my $found = 0;
	my $dkvp = donkvparser->new();
	my $primkey = "parent";
	if($_[2] eq "template_type"){
		$primkey = "template_type";
	};
	$dkvp->read("$dbpath/$_[1]");
	my $nrofblocks = $dkvp->{'info'}->{'data'}->{'nrofblocks'};
	foreach (my $cnt = 0; $cnt < $nrofblocks; $cnt++){
		if($dkvp->{'data'}->[$cnt]->{'primary_key'} eq $ti->{$primkey}){
			my $biggest = 0;
			$found = 1;
			foreach my $key (keys(%{$dkvp->{'data'}->[$cnt]})){
				if(($key ne "primary_key")&&($key ne "DKVP_BID")){
					if($dkvp->{'data'}->[$cnt]->{$key} eq $_[0]){
						#template allready exists!
						return;
					};
					if($biggest < $key){
						$biggest = $key;
					};
				};
			};
			$dkvp->{'data'}->[$cnt]->{$biggest + 1} = $_[0];
		};
	};
	
	#unknown type:
	if($found == 0){
		my $newblock = {};
		my $bid = $dkvp->add_block();
		$newblock->{'primary_key'} = $ti->{$primkey};
		$newblock->{'0'} = $_[0];
		$dkvp->set_block($bid,$newblock);
	};
	$dkvp->write(); 		
};


########################################################################################################
#adds template $_[0] to all dbs
sub add_template_to_dbs{
	add_template_to_db($_[0],$dntemplatemanager::tbd, "parent");
	add_template_to_db($_[0],$dntemplatemanager::tbt,"template_type");
};

1;

