]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/remove.pm
implemented form state saving and retrieval
[git.ikiwiki.info.git] / IkiWiki / Plugin / remove.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::remove;
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
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);
13 } # }}}
15 sub formbuilder_setup (@) { #{{{
16         my %params=@_;
17         my $form=$params{form};
18         my $q=$params{cgi};
20         if (defined $form->field("do") && $form->field("do") eq "edit") {
21                 push @{$params{buttons}}, "Remove";
22                 # TODO button for attachments
23         }
24 } #}}}
26 sub confirmation_form ($$) { #{{{ 
27         my $q=shift;
28         my $session=shift;
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         });
59         return $f, ["Remove", "Cancel"];
60 } #}}}
62 sub formbuilder (@) { #{{{
63         my %params=@_;
64         my $form=$params{form};
66         if (defined $form->field("do") && $form->field("do") eq "edit" &&
67             $form->submitted eq "Remove") {
68                 my $q=$params{cgi};
69                 my $session=$params{session};
70                 # Save current form state to allow returning to it later
71                 # without losing any edits.
72                 # (But don't save what button was submitted.)
73                 $q->param(-name => "_submit", -value => "");
74                 $session->param(postremove => scalar $q->Vars);
75                 IkiWiki::cgi_savesession($session);
77                 # Display a small confirmation form.
78                 my ($f, $buttons)=confirmation_form($q, $session);
79                 $f->field(name => "page", value => $form->field("page"),
80                         force => 1);
81                 IkiWiki::showform($f, $buttons, $session, $q);
82                 exit 0;
83         }
84 } #}}}
86 sub sessioncgi ($$) { #{{{
87         my $q=shift;
89         if ($q->param("do") eq 'remove') {
90                 my $session=shift;
91                 my ($form, $buttons)=confirmation_form($q, $session);
92                 IkiWiki::decode_form_utf8($form);
93                 if ($form->submitted eq 'Cancel') {
94                         # Load saved form state and return to edit form.
95                         my $postremove=CGI->new($session->param("postremove"));
96                         $session->clear("postremove");
97                         IkiWiki::cgi_savesession($session);
98                         IkiWiki::cgi($postremove, $session);
99                         exit 0;
100                 }
101                 elsif ($form->submitted eq 'Remove' && $form->validate) {
102                         error("removal not yet implemented"); # TODO
103                 }
104                 else {
105                         IkiWiki::showform($form, $buttons, $session, $q);
106                         exit 0;
107                 }
108         }