]> git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Posting/Handle.pm
modified 'get_all_threads', returns now additionally the dtd resource,
[selfforum.git] / selfforum-cgi / shared / Posting / Handle.pm
1 package Posting::Handle;
2
3 ################################################################################
4 # #
5 # File: shared/Posting/Handle.pm #
6 # #
7 # Authors: Frank Schoenmann <fs@tower.de>, 2001-03-08 #
8 # #
9 # Description: Allow modifications of postings #
10 # #
11 ################################################################################
12
13 use strict;
14
15 use vars qw(@EXPORT);
16 use base qw(Exporter);
17
18 @EXPORT = qw(hide_posting recover_posting modify_posting);
19
20 use Posting::_lib qw(get_message_node save_file get_all_threads
21 create_forum_xml_string);
22
23 use XML::DOM;
24
25 ### hide_posting () ############################################################
26 #
27 # Hide a posting: set 'invisible' flag
28 #
29 # Params: $forum Path and filename of forum
30 # $tpath Path to thread files
31 # \%info Hash reference: 'thread', 'posting', 'indexFile'
32 # Return: -none-
33 #
34 # Todo:
35 # * set flags recursive in forum xml
36 #
37 sub hide_posting($$$)
38 {
39 my ($forum, $tpath, $info) = @_;
40 my ($tid, $mid, $indexFile) = ($info->{'thread'},
41 $info->{'posting'},
42 $info->{'indexFile'});
43
44 # Thread
45 my $tfile = $tpath . '/t' . $tid . '.xml';
46 change_posting_visibility($tfile, 't'.$tid, 'm'.$mid, 1);
47
48 # Forum
49 #change_posting_visibility($forum, 't'.$tid, 'm'.$mid, 1); # OBSOLETE
50
51 my ($f, $lthread, $lmsg, $dtd, $zlev) = get_all_threads($forum, 1, 0);
52
53 for (@{$f->{$tid}})
54 {
55 if ($_->{'mid'} == $mid)
56 {
57 $_->{'deleted'} = 1;
58 }
59 }
60
61 my %cfxs = (
62 'dtd' => $dtd,
63 'lastMessage' => $lmsg,
64 'lastThread' => $lthread
65 );
66 my $xmlstring = create_forum_xml_string($f, \%cfxs);
67 save_file($forum, $$xmlstring);
68 }
69
70 ### recover_posting() ##########################################################
71 #
72 # Recover a posting: delete 'invisible' flag
73 #
74 # Params: $forum Path and filename of forum
75 # $tpath Path to thread files
76 # \%info Hash reference: 'thread', 'posting', 'indexFile'
77 # Return: -none-
78 #
79 # Todo:
80 # * set flags recursive in forum xml
81 #
82 sub recover_posting($$$)
83 {
84 my ($forum, $tpath, $info) = @_;
85 my ($tid, $mid, $indexFile) = ($info->{'thread'},
86 $info->{'posting'},
87 $info->{'indexFile'});
88
89 # Thread
90 my $tfile = $tpath . '/t' . $tid . '.xml';
91 change_posting_visibility($tfile, 't'.$tid, 'm'.$mid, 0);
92
93 # Forum
94 #change_posting_visibility($forum, 't'.$tid, 'm'.$mid, 0); # OBSOLETE
95
96 my ($f, $lthread, $lmsg, $dtd, $zlev) = get_all_threads($forum, 1, 0);
97
98 for (@{$f->{$tid}})
99 {
100 if ($_->{'mid'} == $mid)
101 {
102 $_->{'deleted'} = 0;
103 }
104 }
105
106 my %cfxs = (
107 'dtd' => $dtd,
108 'lastMessage' => $lmsg,
109 'lastThread' => $lthread
110 );
111 my $xmlstring = create_forum_xml_string($f, \%cfxs);
112 save_file($forum, $$xmlstring);
113 }
114
115 ### change_posting_visibility () ###############################################
116 #
117 # Set a postings visibility flag to $invisible
118 #
119 # Params: $fname Filename
120 # $tid Thread ID
121 # $mid Message ID
122 # $invisible 1 - invisible, 0 - visible
123 # Return: Status code
124 #
125 sub change_posting_visibility($$$$)
126 {
127 my ($fname, $tid, $mid, $invisible) = @_;
128
129 my $parser = new XML::DOM::Parser;
130 my $xml = $parser->parsefile($fname);
131
132 # Set flag in given msg
133 my $mnode = get_message_node($xml, $tid, $mid);
134 $mnode->setAttribute('invisible', $invisible);
135
136 # Set flag in sub nodes
137 for ($mnode->getElementsByTagName('Message'))
138 {
139 $_->setAttribute('invisible', $invisible);
140 }
141
142 return save_file($fname, \$xml->toString);
143 }
144
145 ### modify_posting () ##########################################################
146 #
147 # Modify a posting (only subject and category until now!)
148 #
149 # Params: $forum Path and filename of forum
150 # $tpath Path to thread files
151 # \%info Reference: 'thread', 'posting', 'indexFile', 'data'
152 # (data = \%hashref: 'subject', 'category', 'body')
153 # Return: -none-
154 #
155 sub modify_posting($$$)
156 {
157 my ($forum, $tpath, $info) = @_;
158 my ($tid, $mid, $indexFile, $data) = ($info->{'thread'},
159 $info->{'posting'},
160 $info->{'indexFile'},
161 $info->{'data'});
162 my ($subject, $category, $body) = ($data->{'subject'}, $data->{'category'}, $data->{'body'});
163
164 my %msgdata;
165
166 # These values may be changed by change_posting_value()
167 $subject && $msgdata{'Subject'} = $subject;
168 $category && $msgdata{'Category'} = $category;
169
170 # Thread
171 my $tfile = $tpath . '/t' . $tid . '.xml';
172 change_posting_value($tfile, 't'.$tid, 'm'.$mid, \$msgdata);
173 $body && change_posting_body($tfile, 't'.$tid, 'm'.$mid, $body);
174
175 # Forum (does not contain msg bodies)
176 #change_posting_value($forum, 't'.$tid, 'm'.$mid, \$msgdata);
177
178 my ($f, $lthread, $lmsg, $dtd, $zlev) = get_all_threads($forum, 1, 0);
179
180 for (@{$f->{$tid}})
181 {
182 if ($_->{'mid'} == $mid)
183 {
184 $subject && $_->{'subject'} = $subject;
185 $category && $_->{'cat'} = $category;
186 }
187 }
188
189 my %cfxs = (
190 'dtd' => $dtd,
191 'lastMessage' => $lmsg,
192 'lastThread' => $lthread
193 );
194 my $xmlstring = create_forum_xml_string($f, \%cfxs);
195 save_file($forum, $$xmlstring);
196 }
197
198 ### change_posting_value () ####################################################
199 #
200 # Change specific values of a posting
201 #
202 # Params: $fname Filename
203 # $tid Thread ID
204 # $mid Message ID
205 # \%values New values
206 # Return: Status code
207 #
208 sub change_posting_value($$$$)
209 {
210 my ($fname, $tid, $mid, $values) = @_;
211
212 my $parser = new XML::DOM::Parser;
213 my $xml = $parser->parsefile($fname);
214
215 my $mnode = get_message_node($xml, $tid, $mid);
216
217 for (keys %$values)
218 {
219 # Find first direct child node with name $_
220 my $nodes = $mnode->getElementsByTagName($_, 0);
221 my $node = $nodes->item(0);
222 $node->setValue($values->{$_});
223 }
224
225 return save_file($fname, \$xml->toString);
226 }
227
228 ### change_posting_body () #####################################################
229 #
230 # Change body of a posting
231 #
232 # Params: $fname Filename
233 # $tid Thread ID (unused, for compatibility purposes)
234 # $mid Message ID
235 # $body New body
236 # Return: Status code
237 #
238 sub change_posting_body($$$$)
239 {
240 my ($fname, $tid, $mid, $body) = @_;
241
242 my $parser = new XML::DOM::Parser;
243 my $xml = $parser->parsefile($fname);
244
245 my $mbnody = get_message_body($xml, $mid);
246
247 # todo: change body
248
249 return save_file($fname, \$xml->toString);
250 }
251
252
253 # Let it be true
254 1;

patrick-canterino.de