]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/remove.pm
form tweaks; use title
[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:", size => 60,
48                 validate => sub {
49                 # Validate page by checking that the page exists, and that
50                 # the user is allowed to edit(/delete) it.
51                 my $page=IkiWiki::titlepage(shift);
52                 if (! exists $pagesources{$page}) {
53                         $f->field(name => "page", message => gettext("page does not exist"));
54                         return 0;
55                 }
56                 else {
57                         IkiWiki::check_canedit($page, $q, $session);
58                         return 1;
59                 }
60         });
62         return $f, ["Remove", "Cancel"];
63 } #}}}
65 sub formbuilder (@) { #{{{
66         my %params=@_;
67         my $form=$params{form};
69         if (defined $form->field("do") && $form->field("do") eq "edit" &&
70             $form->submitted eq "Remove") {
71                 my $q=$params{cgi};
72                 my $session=$params{session};
74                 # Save current form state to allow returning to it later
75                 # without losing any edits.
76                 # (But don't save what button was submitted, to avoid
77                 # looping back to here.)
78                 # Note: "_submit" is CGI::FormBuilder internals.
79                 $q->param(-name => "_submit", -value => "");
80                 $session->param(postremove => scalar $q->Vars);
81                 IkiWiki::cgi_savesession($session);
83                 # Display a small confirmation form.
84                 my ($f, $buttons)=confirmation_form($q, $session);
85                 $f->field(name => "page", 
86                         value => IkiWiki::pagetitle($form->field("page")),
87                         force => 1);
88                 IkiWiki::showform($f, $buttons, $session, $q);
89                 exit 0;
90         }
91 } #}}}
93 sub sessioncgi ($$) { #{{{
94         my $q=shift;
96         if ($q->param("do") eq 'remove') {
97                 my $session=shift;
98                 my ($form, $buttons)=confirmation_form($q, $session);
99                 IkiWiki::decode_form_utf8($form);
100                 if ($form->submitted eq 'Cancel') {
101                         # Load saved form state and return to edit form.
102                         my $postremove=CGI->new($session->param("postremove"));
103                         $session->clear("postremove");
104                         IkiWiki::cgi_savesession($session);
105                         IkiWiki::cgi($postremove, $session);
106                         exit 0;
107                 }
108                 elsif ($form->submitted eq 'Remove' && $form->validate) {
109                         error("removal not yet implemented"); # TODO
110                 }
111                 else {
112                         IkiWiki::showform($form, $buttons, $session, $q);
113                         exit 0;
114                 }
115         }