#[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 functions which are common for all feed types.
#they are not part of any object and need a feed - object (no mather which type!)
#as their first param.



##################################################################
package dnfeeds_common;
require dnfeeds;
########################################################################
#creates error message if the feed is internal

sub make_sure_it_is_not_internal{
	my $feed = $_[0];		if($feed->{'dkvp'}->get_value(0,"internal","header") eq "yes"){				donstdlib::error("dnfeeds_internal_feed");
		}		
};

########################################################################
#checks, if comment feed for posting feed $_[0] and POST_ID $_[1] exists 
#1 is returned if it exists. 
#otherwise, 0 is returned.

sub check_for_comment_feed_existance{
	#for security...
	donstdlib::check_filename($_[1]);
	donstdlib::check_filename($_[2]);
		my $feed_file = "$dnfeeds::compath/$_[0].$_[1].dnfeed";				#(hypothetical) comment feed file name
		if(donstdlib::make_sure_that_exists($feed_file,"check") == 0){
			return 0;
		};
			return 1;
};


########################################################################
#deletes all comments (including feed) for feed $_[0] and posting $_[1];
#warning: wrong params may cause loss of data and a security hole!
sub delete_all_comments_for_id{
	#for security...
	donstdlib::check_filename($_[0]);
	donstdlib::check_filename($_[1]);
	dnfeeds->delete_feed($_[0],$_[1]);
};

########################################################################
#deletes all comments for feed $_[0]
#warning: wrong params may cause loss of data and a security hole!
sub delete_all_comments_for_feed{
	#for security...
	donstdlib::check_filename($_[0]);
	my @feeds = dnfeeds->get_list_of_feeds("","","","comments");
	my $cnt = 0;
	foreach $feed (@feeds){
		(my $pfeed, my $id) = split(/\./,$feed);
		if($pfeed eq $_[0]){
			dnfeeds->delete_feed($pfeed,$id);	
			$cnt++;					
	};
	};
	return $cnt;
};

########################################################################
#deletes all comments - a bit ugly implemented but working
sub delete_all_comments{
	my @feeds = dnfeeds->get_list_of_feeds("","","","comments");
	my $cnt = 0;
	foreach my $feed (@feeds){
		(my $pfeed, my $id) = split(/\./,$feed);
		dnfeeds->delete_feed($pfeed,$id);
		$cnt++;
	};
	return $cnt;
};


########################################################################
#deletes the comment feed for posting feed $_[0] and POST_ID $_[1] if it is empty.
sub delete_comment_feed_if_empty{
	donstdlib::check_filename($_[0]);
	donstdlib::check_filename($_[1]);
	my $int_feed = dnfeeds->new($_[0]);
	if($int_feed->get_number_of_comments($_[1]) == 0){
		delete_all_comments_for_id($_[0],$_[1]);
	}; 
};



1;

