summaryrefslogtreecommitdiff
path: root/fml/lib/FML/CGI/Thread.pm
blob: 173fa8827d67568c0e44ccad7fada706cf25a827 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#-*- perl -*-
#
#  Copyright (C) 2004,2005 Ken'ichi Fukamachi
#   All rights reserved. This program is free software; you can
#   redistribute it and/or modify it under the same terms as Perl itself.
#
# $FML: Thread.pm,v 1.6 2005/08/17 10:33:16 fukachan Exp $
#

package FML::CGI::Thread;
use strict;
use Carp;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use CGI qw/:standard/; # load standard CGI routines

use FML::Process::CGI;
@ISA = qw(FML::Process::CGI);


=head1 NAME

FML::CGI::Thread - CGI details to control thread system

=head1 SYNOPSIS

    $obj = new FML::CGI::Thread;
    $obj->prepare();
    $obj->verify_request();
    $obj->run();
    $obj->finish();

run() executes html_start(), run_cgi() and html_end() described below.

See L<FML::Process::Flow> for flow details.

=head1 DESCRIPTION

=head2 CLASS HIERARCHY

C<FML::CGI::Thread> is a subclass of C<FML::Process::CGI>.

=head1 METHODS

Almost methods common for CGI or HTML are forwarded to
C<FML::Process::CGI> base class.

=cut


# Descriptions: print out HTML header + body former part
#    Arguments: OBJ($curproc)
# Side Effects: none
# Return Value: none
sub html_start
{
    my ($curproc) = @_;
    my $config    = $curproc->config();
    my $ml_name   = $curproc->cgi_var_ml_name();
    my $ml_domain = $curproc->cgi_var_ml_domain();
    my $name_ui   = $curproc->message_nl('term.thread_interface');
    my $title     = "${ml_name}\@${ml_domain} $name_ui";
    my $color     = $config->{ thread_cgi_bgcolor } || '#E6E6FA';
    my $charset   = $curproc->langinfo_get_charset("cgi");

    # o.k start html
    print start_html(-title   => $title,
		     -lang    => $charset,
		     -BGCOLOR => $color);
    print "\n";
}


# Descriptions: print out body latter part
#    Arguments: OBJ($curproc)
# Side Effects: none
# Return Value: none
sub html_end
{
    my ($curproc) = @_;

    # o.k. end of html
    print end_html;
    print "\n";
}


# Descriptions: currently, dummy.
#               this routine is executed before table based navigation.
#    Arguments: OBJ($curproc)
# Side Effects: none
# Return Value: none
sub run_cgi_main
{
    my ($curproc) = @_;
    my $max_id    = $curproc->article_get_max_id();
    my $cur_id    = $curproc->safe_param_article_id() || 0;
    my $range     = $cur_id;
    my $th_args   = {
	last_id => $max_id,
    };

    if ($cur_id) {
	use FML::Article::Thread;
	my $article_thread = new FML::Article::Thread $curproc;
	$article_thread->set_print_style('html');

	# interpret subcommand e.g. "close" / "open".
	my $command = $curproc->safe_param_command() || '';
	if ($command eq 'close') {
	    $th_args->{ range } = $range || '';
	    $article_thread->close_thread_status($th_args);
	}
    }
}


# Descriptions: main routine for thread control.
#               this output is shown at table center.
#               run_cgi() can process request: list, show, change_status
#    Arguments: OBJ($curproc)
# Side Effects: none
# Return Value: none
sub run_cgi_menu
{
    my ($curproc)     = @_;
    my $myname        = $curproc->myname();
    my $ml_name       = $curproc->cgi_var_ml_name() || '';
    my $max_id        = $curproc->article_get_max_id();
    my $cur_id        = $curproc->safe_param_article_id();
    my $range         = $cur_id;
    my $default_range = 'last:10';
    my $th_args       = {
	last_id => $max_id,
    };

    # specified command, we need to identify
    # the command specifined in the cgi_navigation and cgi_mein.
    my $navi_command = $curproc->safe_param_navi_command() || '';
    my $command      = $curproc->safe_param_command()      || 'summary';

    print "<!-- exec run_cgi_menu start -->\n";

    unless ($ml_name) {
	print "<!-- ml_name unspecified, so show help and exit -->\n";
    }
    else {
	use FML::Article::Thread;
	my $article_thread = new FML::Article::Thread $curproc;
	$article_thread->set_print_style('html');

	# set print engine to my owe one.
	my $fp = \&__psw_message_queue_html_summary_print;
	$article_thread->set_print_function('summary', $fp);

	if ($command eq 'one_line_summary') {
	    $th_args->{ range } = $range || $default_range;
	    $article_thread->print_one_line_summary($th_args);
	}
	elsif ($command eq 'summary') {
	    $th_args->{ range } = $range || $default_range;
	    $article_thread->print_summary($th_args);
	}
	elsif ($command eq 'close' || $command eq 'open' ||
	       $command eq 'reopen' ) {
	    $th_args->{ range } = $default_range;
	    $article_thread->print_summary($th_args);
	}
	elsif ($command eq 'list') {
	    $th_args->{ range } = $range || '';
	    $article_thread->print_list($th_args);
	    # $article_thread->print_one_line_summary($th_args);
	}
	else {
	    my $r = "unknown subcommand: thread $command";
	    $curproc->logerror($r);
	    $curproc->ui_message("error: $r");
	}
    }

    print "<!-- exec run_cgi_menu end -->\n";
}


# Descriptions: print navigation bar
#    Arguments: OBJ($curproc)
# Side Effects: none
# Return Value: none
sub run_cgi_navigator
{
    my ($curproc) = @_;
    my $config    = $curproc->config();
    my $action    = $curproc->cgi_var_action();
    my $target    = $curproc->cgi_var_frame_target();
    my $ml_list   = $curproc->cgi_var_ml_name_list();
    my $ml_name   = $curproc->cgi_var_ml_name();

    # natural language-ed name
    my $name_ml_name = $curproc->message_nl('term.ml_name', 'ml_name');
    my $name_command = $curproc->message_nl('term.command', 'command');
    my $name_change  = $curproc->message_nl('term.change',  'change');
    my $name_reset   = $curproc->message_nl('term.reset',   'reset');

    print start_form(-action=>$action, -target=>$target);
    print $curproc->cgi_hidden_info_language();
    print $name_ml_name, ":\n";
    print popup_menu(-name   => 'ml_name', -values => $ml_list);
    print "<BR>\n";

    if (0) {
	print "orderd by: ";
	my $order = [ 'cost', 'date', 'reverse date' ];
	print popup_menu(-name   => 'order', -values => $order );
	print "<BR>\n";
    }

    # 3. submit
    print submit(-name => $name_change);
    print reset(-name  => $name_reset);

    print end_form;
    print "<HR>\n";
}


# Descriptions: show help
#    Arguments: OBJ($curproc)
# Side Effects: none
# Return Value: none
sub run_cgi_help
{
    my ($curproc) = @_;
    my $ml_name   = $curproc->cgi_var_ml_name();
    my $ml_domain = $curproc->cgi_var_ml_domain();
    my $mode      = $curproc->cgi_var_cgi_mode();
    my $role      = $curproc->message_nl('term.thread_interface');
    my $msg_args  = $curproc->_gen_msg_args();

    print "<B>\n<CENTER>\n";
    if ($mode eq 'admin') {
	print "fml CGI $role for \@$ml_domain ML's\n";
    }
    else {
	print "fml CGI $role for $ml_name\@$ml_domain ML\n";
    }
    print "</CENTER><BR>\n</B>\n";

    # top level help message
    my $buf  = '';
    if ($mode eq 'admin') {
	$buf = $curproc->message_nl("cgi.admin.top", "", $msg_args);
    }
    else {
	$buf = $curproc->message_nl("cgi.ml-admin.top", "", $msg_args);
    }

    print $buf;
}


=head2 run_cgi_command_help()

command_help.

=cut


# Descriptions: show thread command dependent help.
#    Arguments: OBJ($curproc)
# Side Effects: none
# Return Value: none
sub run_cgi_command_help
{
    my ($curproc)    = @_;
    my $buf          = '';
    my $navi_command = $curproc->safe_param_navi_command() || '';
    my $command      = $curproc->safe_param_command() || 'summary';
    my $msg_args     = $curproc->_gen_msg_args();

    # re-define: open|close -> summary.
    if ($command =~ /close|open/) { $command = 'summary';}

    # natural language-ed name
    my $name_usage   = $curproc->message_nl('term.usage',  'usage');

    if ($navi_command) {
	print "[$name_usage]<br> <b> $navi_command </b> <br>\n";
	$buf = $curproc->message_nl("cgi.thread.$navi_command", '', $msg_args);
    }
    elsif ($command) {
	print "[$name_usage]<br> <b> $command </b> <br>\n";
	$buf = $curproc->message_nl("cgi.thread.$command", '', $msg_args);
    }

    print $buf;
}


# Descriptions: prepare arguemnts for message handling.
#    Arguments: OBJ($curproc)
# Side Effects: none
# Return Value: HASH_REF
sub _gen_msg_args
{
    my ($curproc) = @_;

    # natural language-ed name
    my $name_submit = $curproc->message_nl('term.submit', 'submit');
    my $name_show   = $curproc->message_nl('term.show',   'show');
    my $name_map    = $curproc->message_nl('term.map',    'map');
    my $msg_args    = {
	_arg_button_submit => $name_submit,
	_arg_button_show   => $name_show,
	_arg_scroll_map    => $name_map,
    };

    return $msg_args;
}



#
# THIS MODULE SPECIFIC METHODS
#


# Descriptions: print summary.
#               See templates within FML::Article::Thread class.
#    Arguments: OBJ($self) OBJ($curproc) ARRAY_REF($queue)
# Side Effects: none
# Return Value: none
sub __psw_message_queue_html_summary_print
{
    my ($self, $curproc, $queue) = @_;
    my $q       = $queue->[ 0 ] || {};
    my $wh      = $q->{ output_channel } || \*STDOUT;
    my $target  = $curproc->cgi_var_frame_target();
    my $action  = $curproc->cgi_var_action();
    my $ml_name = $curproc->cgi_var_ml_name();

    # debug
    print "debug: queue length = ", ($#$queue + 1), "\n";

    # terms
    my $term_article = $curproc->message_nl('term.article', 'article(s)');
    my $term_status  = $curproc->message_nl('term.status', 'status');
    my $term_change  = $curproc->message_nl('term.status_change',
					    'change status');
    my $term_summary = $curproc->message_nl('term.thread_summary',
					    'thread summary');

    print $wh "<table border=4>\n";
    print $wh "<td> $term_article </td>\n";
    print $wh "<td> $term_status  </td>\n";
    print $wh "<td> $term_change  </td>\n";
    print $wh "<td> $term_summary </td>\n";
    print $wh "</tr>\n";

    my $buf;
    for my $q (@$queue) {
	my $cur_id  = $q->{ cur_id }  || '';
	my $head_id = $q->{ head_id } || '';
	my $id_list = $q->{ id_list } || [];
	my $status  = $q->{ status }  || 'unknown';
	my $summary = $q->{ summary } || '';
	my $msg     = $q->{ message } || undef;
	my $wh      = $q->{ output_channel } || \*STDOUT;
	my $prompt  = "";

	print $wh "<!-- cur_id=$cur_id head_id=$head_id -->\n";
	print $wh "<tr>\n";

	if ($cur_id && $head_id) {
	    if ($head_id == $cur_id) {
		$buf  = start_form(-action=>$action, -target=>$target);
		$buf .= hidden(-name=>'article_id',-value=>$head_id,
			       -override=>1);
		$buf .= hidden(-name=>'ml_name',-value=>$ml_name,-override=>1);
		$buf .= hidden(-name=>'command',-value=>'close',-override=>1);
		$buf .= submit(-name => '-> close');

		print $wh "<td>\n @$id_list \n</td>\n";
		print $wh "<td>\n $status   \n</td>\n";
		print $wh "<td>\n $buf      \n</td>\n";
	    }
	    else {
		print $wh "<td> </td>\n";
		print $wh "<td> </td>\n";
		print $wh "<td> </td>\n";
	    }

	    print $wh "<td>\n";
	    $summary =~ s/^\s*/   /;
	    $summary =~ s/\n\s*/\n   /g;
	    print $wh $summary;

	    print $wh "</td>\n";
	}
	else {
	    carp("psw_message_queue_html_summary_print: invalid data");
	}

	print $wh "</tr>\n";
    }

    print $wh "</table>\n";
}


=head1 SEE ALSO

L<CGI>,
L<FML::Process::CGI>
and
L<FML::Process::Flow>

=head1 CODING STYLE

See C<http://www.fml.org/software/FNF/> on fml coding style guide.

=head1 AUTHOR

Ken'ichi Fukamachi

=head1 COPYRIGHT

Copyright (C) 2004,2005 Ken'ichi Fukamachi

All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.

=head1 HISTORY

FML::CGI::Thread first appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.

=cut


1;