#[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 packages provides information about templates, such as if it was derived from a draft,
#the description, ...
#it needs a dntemplatemmanager-object provided with the new - constructor.
#short how 2 use example: new($dntemplatemanager, $template); get_data("short")  
#########################################################################################################


package dntempinfo;
require donlibs;

#path configuration
$basepath = "restricted/templates";				#base path for all tempates
$userpath = "$basepath/user";						#obsolete for descriptions as templates have to be derived from a draft in every
$internalpath = "$basepath/internal";
$draftpath = "$basepath/drafts";
$list_cat_path = "$draftpath/list_category_descriptions";

$desc_path = "$draftpath/descriptions";
$short_desc_path = "$draftpath/descriptions_short";
$conf_path = "$draftpath/configs";

$messagepath = "messages/various";

#########################################################################################################
#constructor;
#needs task ("user"/"internal"/"draft") in $_[1];
#needs template file (without ending and path) in $_[0];
sub retrive_embedded_information{
	my $path;
	my $rv = {};
	if(($_[1] eq "user")||($_[1] eq "")){
		$path = $userpath;
	}elsif($_[1] eq "internal"){
		$path = $internalpath;
	}elsif($_[1] eq "draft"){
		$path = $draftpath;
	};
		my $parser;
	
	if($path ne $internalpath){ 
		$parser = donparser->new($path);
		$parser->set_option_manually("dontparse","yes");
		$parser->open($_[0]);
		$parser->parse();
	};
	
	if($path eq $userpath){ 
		#if the file has been created by the user, the file has a "parent" which it is derived from
		#otherwise, the "parent" is the file itself 
	
		#when a template is derived, whe must take the parent template which is in the draft directory.
		#consequently, all descriptions,... must be taken from the draft subdirectories. the filename of the parent
		#draft must be used.
		my @dotinfo = $parser->get_dot_information("REMARK","DERIVED_FROM");
		#get parent
	
		if($dotinfo[0] ne ""){
			$rv->{'parent'} = donstdlib::array_to_string(\@dotinfo," ");
			$rv->{'derived'} = "yes";
		}else{
			$rv->{'parent'} = "default_template";
			$rv->{'derived'} = "no";
		};	
	};
	if(($path eq $userpath)||($path eq $draftpath)){ 

		#get list categroy
		@dotinfo = $parser->get_dot_information("REMARK","LIST_CATEGORY");
		if($dotinfo[0] eq ""){
			$rv->{'list_category'} = "miscellaneous";
		}else{
			$rv->{'list_category'} = donstdlib::array_to_string(\@dotinfo," ");
		};


		#get template type
		@dotinfo = $parser->get_dot_information("REMARK","TEMPLATE_TYPE");
		if($dotinfo[0] eq ""){
			$rv->{'template_type'} = "undefined";
		}else{
			$rv->{'template_type'} = donstdlib::array_to_string(\@dotinfo," ");
		};


		#get template's content-type
		@dotinfo = $parser->get_dot_information("OPTION","content-type");
		if($dotinfo[0] eq ""){
			$rv->{'content_type'} = "text/html";
		}else{
			$rv->{'content_type'} = donstdlib::array_to_string(\@dotinfo," ");
		};

	};

	return $rv;
};


#########################################################################################################
#retrives information that belongs to draft $_[0] (or list_category $_[0] in case of list_category_description; returns it as text.
#$_[1] can be "short", then the short description is returned
#if it is "config", the config is returned
#if it is "list_category_description", the list category description is returned.

sub get_data{

	my $template = $_[0];
	my $path;
	if($_[1] eq "short"){
		$path = $short_desc_path
	}elsif($_[1] eq "config"){
		$path = $conf_path;
	}elsif($_[1] eq "description"){
		$path = $desc_path;
	}else{
		$path = $list_cat_path;
	};
	
	if(donstdlib::make_sure_that_exists($path."/".$template.".dontemplate","check") != 1){
		if($_[1] ne "config"){		
			$self->{$var} = donstdlib::get_message("dntempinfo_not_available");
			return $self->{$var};
		}else{
			$template="dnconfig_default";					#when no configlet template is available, use the standard one
		};
		
	};
	
	my $parser;
	$parser = donparser->new($path);
	$parser->open($template);
	if($var eq "draft"){
		$parser->set_option_manually("dontparse","yes");
	};
	$parser->parse();
	return $parser->get_text();		
};



1;

