]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/remove.pm
77ddd63ba7de731cefdef1702520c4268d3005ce
[ikiwiki.git] / IkiWiki / Plugin / remove.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::remove;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 sub import { #{{{
9         hook(type => "formbuilder_setup", id => "remove", call => \&formbuilder_setup);
10         hook(type => "formbuilder", id => "remove", call => \&formbuilder);
11         hook(type => "sessioncgi", id => "remove", call => \&sessioncgi);
12
13 } # }}}
14
15 sub formbuilder_setup (@) { #{{{
16         my %params=@_;
17         my $form=$params{form};
18         my $q=$params{cgi};
19
20         if (defined $form->field("do") && $form->field("do") eq "edit") {
21                 push @{$params{buttons}}, "Remove";
22                 # TODO button for attachments
23         }
24 } #}}}
25
26 sub confirmation_form ($$) { #{{{ 
27         my $q=shift;
28         my $session=shift;
29
30         eval q{use CGI::FormBuilder};
31         error($@) if $@;
32         my @fields=qw(do page);
33         my $f = CGI::FormBuilder->new(
34                 title => "confirm removal",
35                 name => "remove",
36                 header => 0,
37                 charset => "utf-8",
38                 method => 'POST',
39                 javascript => 0,
40                 params => $q,
41                 action => $config{cgiurl},
42                 stylesheet => IkiWiki::baseurl()."style.css",
43                 fields => \@fields,
44         );
45                 
46         $f->field(name => "do", type => "hidden", value => "remove", force => 1);
47         $f->field(name => "page", label => "Will remove:", validate => sub {
48                 my $page=shift;
49                 if (! exists $pagesources{$page}) {
50                         $f->field(name => "page", message => gettext("page does not exist"));
51                         return 0;
52                 }
53                 else {
54                         IkiWiki::check_canedit($page, $q, $session);
55                         return 1;
56                 }
57         });
58
59         return $f, ["Remove", "Cancel"];
60 } #}}}
61
62 sub formbuilder (@) { #{{{
63         my %params=@_;
64         my $form=$params{form};
65
66         if (defined $form->field("do") && $form->field("do") eq "edit" &&
67             $form->submitted eq "Remove") {
68                 # When the remove button is pressed on the edit form,
69                 # save the rest of the form state and generate a small
70                 # remove confirmation form.
71
72                 # TODO save state
73
74
75                 my $q=$params{cgi};
76                 my $session=$params{session};
77                 my ($f, $buttons)=confirmation_form($q, $session);
78                 $f->field(name => "page", value => $form->field("page"),
79                         force => 1);
80                 IkiWiki::showform($f, $buttons, $session, $q);
81                 exit 0;
82         }
83 } #}}}
84
85 sub sessioncgi ($$) { #{{{
86         my $q=shift;
87
88         if ($q->param("do") eq 'remove') {
89                 my $session=shift;
90                 my ($form, $buttons)=confirmation_form($q, $session);
91                 IkiWiki::decode_form_utf8($form);
92                 if ($form->submitted eq 'Cancel') {
93                         error("canceled"); # TODO load state
94                 }
95                 elsif ($form->submitted eq 'Remove' && $form->validate) {
96                         error("removal not yet implemented"); # TODO
97                 }
98                 else {
99                         IkiWiki::showform($form, $buttons, $session, $q);
100                         exit 0;
101                 }
102         }
103 }
104
105 1