wiki.pl: Port some fixes from upstream
[Orgmuse.git] / modules / checkbox.pl
blob2625a3b8aca8b1ca649f9d02c5fa4313d1e0a547
1 # Copyright (C) 2006 Alex Schroeder <alex@emacswiki.org>
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.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the
15 # Free Software Foundation, Inc.
16 # 59 Temple Place, Suite 330
17 # Boston, MA 02111-1307 USA
19 $ModulesDescription .= '<p><a href="http://git.savannah.gnu.org/cgit/oddmuse.git/tree/modules/checkbox.pl">checkbox.pl</a>, see <a href="http://www.oddmuse.org/cgi-bin/oddmuse/Checklist_Extension">Checklist Extension</a></p>';
21 # [[ : To do]]
22 # [[X: Done]]
23 # [[save: Save]]
25 push(@MyRules, \&CheckBoxRule);
27 sub CheckBoxRule {
28 if ($bol and /\G\[\[( |x):([^]]+)\]\]/cgi) {
29 my $html;
30 if (not InElement('form')) {
31 # We want to use GetFormStart so we have to trade-off using
32 # AddHtmlEnvironment.
33 $html = CloseHtmlEnvironments()
34 . GetFormStart(undef, 'get', 'checkboxes');
35 unshift(@HtmlStack, 'form');
36 $html .= AddHtmlEnvironment('p');
38 $html .= $q->checkbox(-name => FreeToNormal($2),
39 -checked => ($1 eq ' '? 0 : 1),
40 -label => $2)
41 . $q->br();
42 return $html;
43 } elsif (/\G\[\[save:([^]]+)\]\]/cgi) {
44 if (InElement('form')) {
45 return ($q->input({-type => 'hidden',
46 -name => 'action',
47 -value => 'checkbox'})
48 . $q->input({-type => 'hidden',
49 -name => 'id',
50 -value => $OpenPageName})
51 . $q->submit(-name => $1));
52 } else {
53 return $1;
56 return undef;
59 $Action{checkbox} = \&DoCheckBox;
61 sub DoCheckBox{
62 my $id = shift;
63 OpenPage($id);
64 my $text = $Page{text};
65 my %summary;
66 $text =~ s{(\A|\n)\[\[( |x):([^]]+)\]\]}{
67 # no search and replace in this loop
68 if (GetParam(FreeToNormal($3))) {
69 $summary{$3} = 1 if $2 eq ' ';
70 "${1}[[x:${3}]]";
71 } else {
72 $summary{$3} = 0 if $2 eq 'x' or $2 eq 'X';
73 "${1}[[ :${3}]]";
75 }eig;
76 SetParam('text', $text);
77 SetParam('summary', join(', ', map {
78 if ($summary{$_}) {
79 Ts('set %s', $_);
80 } else {
81 Ts('unset %s', $_);
83 } keys %summary));
84 DoPost($id);