X-Git-Url: https://git.p6c8.net/selfforum.git/blobdiff_plain/9f19cb68669296f87eefd27f8e9765deeef85106..d8a1fb31ed6883aef6a7ba78a117269218966a10:/selfforum-cgi/shared/Posting/Handle.pm diff --git a/selfforum-cgi/shared/Posting/Handle.pm b/selfforum-cgi/shared/Posting/Handle.pm index 67d5c0b..9d2a94a 100644 --- a/selfforum-cgi/shared/Posting/Handle.pm +++ b/selfforum-cgi/shared/Posting/Handle.pm @@ -1,5 +1,4 @@ -package Handle; -#package Posting::Handle; +package Posting::Handle; ################################################################################ # # @@ -16,9 +15,10 @@ use strict; use vars qw(@EXPORT); use base qw(Exporter); -@EXPORT = qw(hide_posting); +@EXPORT = qw(hide_posting recover_posting modify_posting); -#use Posting::_lib; +use Posting::_lib qw(get_message_node save_file get_all_threads + create_forum_xml_string); use XML::DOM; @@ -26,68 +26,175 @@ use XML::DOM; # # Hide a posting: set 'invisible' flag # +# Params: $forum Path and filename of forum +# $tpath Path to thread files +# \%info Hash reference: 'thread', 'posting', 'indexFile' +# Return: -none- +# +sub hide_posting($$$) +{ + my ($forum, $tpath, $info) = @_; + my ($tid, $mid, $indexFile) = ($info->{'thread'}, + $info->{'posting'}, + $info->{'indexFile'}); + + # Thread + my $tfile = $tpath . '/t' . $tid . '.xml'; + change_posting_visibility($tfile, 't'.$tid, 'm'.$mid, 1); + + # Forum + #change_posting_visibility($forum, 't'.$tid, 'm'.$mid, 1); # OBSOLETE + + my ($f, $lthread, $lmsg, $dtd, $zlev) = get_all_threads($forum, 1, 0); + + for (@{$f->{$tid}}) + { + if ($_->{'mid'} == $mid) + { + $_->{'deleted'} = 1; + } + } + + my %cfxs = ( + 'dtd' => $dtd, + 'lastMessage' => $lmsg, + 'lastThread' => $lthread + ); + create_forum_xml_string($f, \%cfxs); +} + +### recover_posting() ########################################################## +# +# Recover a posting: delete 'invisible' flag +# # Params: $forum Path and filename of forum # $tpath Path to thread files # \%hashref Reference: 'thread', 'posting', 'indexFile' -# Return: Boolean +# Return: -none- # -sub hide_posting($$$) +sub recover_posting($$$) { - my ($forum, $tpath, $info) = shift; - my ($tid, $pid, $indexFile) = ('t' . $info->{'thread'}, - 'm' . $info->{'posting'}, + my ($forum, $tpath, $info) = @_; + my ($tid, $mid, $indexFile) = ($info->{'thread'}, + $info->{'posting'}, $info->{'indexFile'}); - { - # Change flag in thread xml file - my $tfile = $tpath . '/' . $tid; + # Thread + my $tfile = $tpath . '/t' . $tid . '.xml'; + change_posting_visibility($tfile, 't'.$tid, 'm'.$mid, 0); - my $parser = new XML::DOM::Parser; - my $xml = $parser->parsefile($tfile); + # Forum + #change_posting_visibility($forum, 't'.$tid, 'm'.$mid, 0); # OBSOLETE - my $msgs = $xml->getElementsByTagName('Message'); + my ($f, $lthread, $lmsg, $dtd, $zlev) = get_all_threads($forum, 1, 0); - for (my $i = 0; $i < $msgs->getLength; $i++) + for (@{$f->{$tid}}) + { + if ($_->{'mid'} == $mid) { - my $msg = $msgs->item($i); - - if ($msg->getAttribute('id')->getValue == $pid) - { - $msg->setAttribute('invisible', '1'); - last; - } + $_->{'deleted'} = 0; } - - # Save thread xml file - $xml->printToFile($tfile . '.temp'); - rename $tfile . '.temp', $tfile; } - { - # Change flag in forum xml file - my $parser = new XML::DOM::Parser; - my $xml = $parser->parseFile($forum); + my %cfxs = ( + 'dtd' => $dtd, + 'lastMessage' => $lmsg, + 'lastThread' => $lthread + ); + create_forum_xml_string($f, \%cfxs); +} - my $msgs = $xml->getElementsByTagName('Message'); +### change_posting_visibility () ############################################### +# +# Set a postings visibility flag to $invisible +# +# Params: $fname Filename +# $tid Thread ID +# $mid Message ID +# $invisible 1 - invisible, 0 - visible +# Return: Status code +# +sub change_posting_visibility($$$$) +{ + my ($fname, $tid, $mid, $invisible) = @_; - for (my $i = 0; $i < $msgs->getLength; $i++) - { - my $msg = $msgs->item($i); + my $parser = new XML::DOM::Parser; + my $xml = $parser->parsefile($fname); - if ($msg->getAttribute('id')->getValue == $pid) - { - $msg->setAttribute('invisible', '1'); - last; - } - } + # Set flag in given msg + my $mnode = get_message_node($xml, $tid, $mid); + $mnode->setAttribute('invisible', $invisible); - # Save forum xml file - $xml->printToFile($forum . '.temp'); - rename $forum . '.temp', $forum; + # Set flag in sub nodes + for ($mnode->getElementsByTagName('Message')) + { + $_->setAttribute('invisible', $invisible); } + + return save_file($fname, \$xml->toString); +} + +### modify_posting () ########################################################## +# +# Modify a posting (only subject and category until now!) +# +# Params: $forum Path and filename of forum +# $tpath Path to thread files +# \%info Reference: 'thread', 'posting', 'indexFile', 'data' +# (\%hashref: 'subject', 'category', 'body') +# Return: -none- +# +sub modify_posting($$$) +{ + my ($forum, $tpath, $info) = @_; + my ($tid, $mid, $indexFile, $data) = ('t' . $info->{'thread'}, + 'm' . $info->{'posting'}, + $info->{'indexFile'}, + $info->{'data'}); + my ($subject, $category, $body) = ($data->{'subject'}, $data->{'category'}, $data->{'body'}); + + my %msgdata; + + # These values may be changed by change_posting_value() + $subject && $msgdata{'Subject'} = $subject; + $category && $msgdata{'Category'} = $category; + + # + my $tfile = $tpath . '/' . $tid . '.xml'; + change_posting_value($tfile, $tid, $mid, \$msgdata); + change_posting_value($forum, $tid, $mid, \$msgdata); } +### change_posting_value () #################################################### +# +# Change specific values of a posting +# +# Params: $fname Filename +# $tid Thread ID +# $mid Message ID +# \%values New values +# Return: Status code +# +sub change_posting_value($$$$) +{ + my ($fname, $tid, $mid, $values) = @_; + + my $parser = new XML::DOM::Parser; + my $xml = $parser->parsefile($fname); + + my $mnode = get_message_node($xml, $tid, $mid); + + for (keys %$values) + { + # Find first direct child node with name $_ + my $nodes = $mnode->getElementsByTagName($_, 0); + my $node = $nodes->item(0); + $node->setValue($values->{$_}); + } + return save_file($fname, \$xml->toString); +} +# Let it be true 1;