Note that Mark added Braintree payment module
[interchange.git] / code / UserTag / forum.tag
1 # Copyright 2002-2010 Interchange Development Group and others
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.  See the LICENSE file for details.
7
8 UserTag forum-userlink PosNumber 0
9 UserTag forum-userlink addAttr
10 UserTag forum-userlink Version   1.7
11 UserTag forum-userlink Routine   <<EOR
12 sub {
13         my ($row) = @_;
14         return $row->{name} || $Variable->{FORUM_ANON_NAME} || 'Anonymous Coward'
15                 if $row->{anon} or ! $row->{username};
16         my $realname = tag_data('userdb', 'handle', $row->{username})
17                                  || tag_data('userdb', 'fname', $row->{username});
18         return $realname || $row->{username};
19 }
20 EOR
21
22 UserTag forum Order     top
23 UserTag forum addAttr 
24 UserTag forum hasEndTag 
25 UserTag forum NoReparse 1
26 UserTag forum Version   1.7
27 UserTag forum Routine   <<EOR
28 my @uls;
29 my $lastlevel;
30
31 sub {
32         my ($id, $opt, $tpl) = @_;
33
34         if(! $id) {
35           $id = '0';
36         }
37
38         my $forum_header;
39         my $forum_footer;
40         my $forum_link;
41         my $forum_scrub;
42
43         $tpl ||= '';
44         $tpl =~ s{\[forum[-_]header\](.*)\[/forum[-_]header\]}{}is
45                 and $forum_header = $1;
46         $tpl =~ s{\[forum[-_]footer\](.*)\[/forum[-_]footer\]}{}is
47                 and $forum_footer = $1;
48         $tpl =~ s{\[forum[-_]link\](.*)\[/forum[-_]link\]}{}is
49                 and $forum_link = $1;
50         $tpl =~ s{\[forum[-_]scrub\](.*)\[/forum[-_]scrub\]}{}is
51                 and $forum_scrub = $1;
52
53         $forum_header ||= $opt->{header_template} || <<EOF;
54 <table>
55   <tr>
56         <td class=contentbar1>
57           <b>{SUBJECT}</b>
58           by <b>{USERINFO}</b>
59           on {DATE}
60         </td>
61   </tr>
62   <tr>
63         <td>
64                 {COMMENT}
65         </td>
66   </tr>
67 {ADDITIONAL?}
68   <tr>
69         <td>
70                 {ADDITIONAL}
71         </td>
72   </tr>
73 {/ADDITIONAL?}
74         <tr>
75           <td>
76                  &#91; 
77                   {TOP_URL?}<A HREF="{TOP_URL}">Top</A> |{/TOP_URL?}
78                   {PARENT_URL?}<A HREF="{PARENT_URL}">Parent</A> |{/PARENT_URL?}
79                   <A HREF="{REPLY_URL}">Reply</A>
80                  &#93; 
81           </td>
82         </tr>
83         </table>
84 <hr>
85 EOF
86
87          $forum_link ||= $opt->{link_template} || <<EOF;
88 <A HREF="{DISPLAY_URL}">{SUBJECT}</a> by {USERINFO} on {DATE}
89 EOF
90
91          $opt->{threshold_message} ||= errmsg("Message below your threshold");
92          $forum_scrub ||= $opt->{scrub_template} || <<EOF;
93 <A HREF="{DISPLAY_URL}">$opt->{threshold_message}</a>
94 EOF
95
96         $tpl ||= $opt->{template} || <<EOF;
97 <table cellspacing=0 cellpadding=2>
98   <tr>
99         <td class=contentbar1>
100                 <A HREF="{DISPLAY_URL}"><b>{SUBJECT}</b></A>
101                 by <b>{USERINFO}</b>
102                 on {DATE}
103         </td>
104         <td class=contentbar1 align=right>
105                 <small>&#91; <A HREF="{REPLY_URL}"><b>Reply</b></A> &#93;</font></small>
106         </td>
107   </tr>
108   <tr>
109         <td colspan=2>
110         {COMMENT}
111         <!--
112                 prior to UL: {MSG1}
113                 prior to /UL: {MSG2}
114                 prior to END: {MSG3}
115         -->
116         </td>
117   </tr>
118 </table>
119 EOF
120
121         $forum_footer ||= <<EOF;
122 <!-- end of forum -->
123 EOF
124
125         my $lastlevel = 0;
126         my @uls;
127
128         my $Tag = new Vend::Tags;
129         my $row = shift;
130
131         $opt->{reply_page} ||= 'forum/reply';
132         $opt->{submit_page} ||= 'forum/submit';
133         $opt->{display_page} ||= $Global::Variable->{MV_PAGE};
134         $opt->{date_format} ||= '%B %e, %Y @%H:%M';
135         my $menu_row = sub {
136           shift;
137           my $row = shift;
138           $row->{reply_url} = $Tag->area({
139                                                                         href => $opt->{reply_page},
140                                                                         arg => $row->{code},
141                                                                         });
142           if($row->{code} ne $row->{artid}) {
143                   $row->{top_url} = $Tag->area( {
144                                                                                 href => $opt->{display_page},
145                                                                                 arg => $row->{artid},
146                                                                         });
147           }
148           if($row->{parent}) {
149                   $row->{parent_url} = $Tag->area( {
150                                                                                 href => $opt->{display_page},
151                                                                                 arg => $row->{parent},
152                                                                         });
153           }
154           $row->{display_url} = $Tag->area({
155                                                                         href => $opt->{display_page},
156                                                                         arg => $row->{code},
157                                                                         });
158           $row->{userinfo} = $Tag->forum_userlink($row);
159           $row->{date} = $Tag->convert_date({
160                                                                           fmt => $opt->{date_format},
161                                                                           body => $row->{created},
162                                                                   });
163           my $lev = $row->{mv_level};
164           my $children = $row->{mv_children};
165           my $last = $row->{mv_last};
166           my $pre = '';
167           my $post = '';
168           my $num_uls = scalar(@uls);
169           $row->{msg1} = "lastlevel=$lastlevel lev=$lev children=$children uls=$num_uls";
170           if(! $lev) {
171                   $pre .= join "", splice (@uls);
172           }
173           elsif ($lastlevel < $lev) {
174                   $lastlevel = $lev;
175           }
176           elsif ($lastlevel > $lev) {
177                   $lastlevel = $lev;
178                   $pre .= join "", splice (@uls,$lev);
179           }
180           if($children) {
181                   push @uls, '</ul>';
182           }
183           $num_uls = scalar(@uls);
184           $row->{msg2} = "lastlevel=$lastlevel lev=$lev children=$children uls=$num_uls";
185           if($children) {
186                   $post .= '<ul>';
187           }
188           elsif($last) {
189                   $post .= join "", splice (@uls, $lev);
190           }
191           $num_uls = scalar(@uls);
192           $row->{msg3} = "lastlevel=$lastlevel lev=$lev children=$children uls=$num_uls";
193           $row->{forum_prepend} = $pre;
194           $row->{forum_append} = $post;
195           return $row;
196         };
197
198         my $fdb = database_exists_ref('forum')
199                 or die "No forum DB!";
200
201         my $record = $fdb->row_hash($id);
202         return undef unless $record;
203
204         $menu_row->(undef, $record);
205         my @out;
206
207         $opt->{full} = 1 if ! defined $opt->{full};
208
209         push @out, $Tag->uc_attr_list($record, $forum_header);
210
211         my %o = (
212           table                 => 'forum',
213           start                 => $id,
214           master                => 'parent',
215           subordinate   => 'code',
216           full                  => $opt->{full},
217           sort                  => $opt->{sort} || 'code',
218           spacer                => "&nbsp;",
219           autodetect    => 1,
220           iterator              => $menu_row,
221           spacing               => 4,
222         );
223
224         $Tag->tree(\%o);
225
226         my $rows = $o{object}{mv_results};
227         $opt->{scrub_score} ||= 0;
228         $opt->{show_score} ||= 1;
229         if(! defined $opt->{show_level}) {
230                 if($record->{code} == $record->{artid}) {
231                         $opt->{show_level} = 0;
232                 }
233                 else {
234                         $opt->{show_level} = 2;
235                 }
236         }
237
238         for(\$tpl, \$forum_link, \$forum_scrub) {
239                 $$_ = "{FORUM_PREPEND}$$_" unless $$_ =~ /\{FORUM_PREPEND\}/;
240                 $$_ .= '{FORUM_APPEND}' unless $$_ =~ /\{FORUM_APPEND\}/;
241         }
242
243         for my $record (@$rows) {
244
245                 my $this_tpl;
246                 if($record->{score} <= $opt->{scrub_score}) {
247                         $this_tpl = $forum_scrub;
248                 }
249                 elsif($record->{score} >= $opt->{show_score}) {
250                         $this_tpl = $tpl;
251                 }
252                 elsif($record->{mv_level} <= $opt->{show_level}) {
253                         $this_tpl = $tpl;
254                 }
255                 else {
256                         $this_tpl = $forum_link;
257                 }
258                 push @out, $Tag->uc_attr_list($record, $this_tpl);
259         }
260         push @out, join "", @uls;
261         push @out, $Tag->uc_attr_list($opt, $forum_footer);
262         return join "\n", @out;
263 }
264 EOR