websetup form display done
[ikiwiki.git] / IkiWiki / Plugin / lockedit.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::lockedit;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 sub import { #{{{
9         hook(type => "getsetup", id => "lockedit", call => \&getsetup);
10         hook(type => "canedit", id => "lockedit", call => \&canedit);
11         hook(type => "formbuilder_setup", id => "lockedit",
12              call => \&formbuilder_setup);
13 } # }}}
14
15 sub getsetup () { #{{{
16         return
17                 locked_pages => {
18                         type => "pagespec",
19                         example => "!*/Discussion",
20                         description => "PageSpec controlling which pages are locked",
21                         description_html => htmllink("", "", "ikiwiki/PageSpec", noimageinline => 1).
22                                 " controlling which pages are locked",
23                         safe => 1,
24                         rebuild => 0,
25                 },
26 } #}}}
27
28 sub canedit ($$) { #{{{
29         my $page=shift;
30         my $cgi=shift;
31         my $session=shift;
32
33         my $user=$session->param("name");
34         return undef if defined $user && IkiWiki::is_admin($user);
35
36         if (defined $config{locked_pages} && length $config{locked_pages} &&
37             pagespec_match($page, $config{locked_pages})) {
38                 if (! defined $user ||
39                     ! IkiWiki::userinfo_get($session->param("name"), "regdate")) {
40                         return sub { IkiWiki::needsignin($cgi, $session) };
41                 }
42                 else {
43                         return sprintf(gettext("%s is locked and cannot be edited"),
44                                 htmllink("", "", $page, noimageinline => 1));
45                         
46                 }
47         }
48
49         # XXX deprecated, should be removed eventually
50         foreach my $admin (@{$config{adminuser}}) {
51                 if (pagespec_match($page, IkiWiki::userinfo_get($admin, "locked_pages"))) {
52                         if (! defined $user ||
53                             ! IkiWiki::userinfo_get($session->param("name"), "regdate")) {
54                                 return sub { IkiWiki::needsignin($cgi, $session) };
55                         }
56                         else {
57                                 return sprintf(gettext("%s is locked and cannot be edited"),
58                                         htmllink("", "", $page, noimageinline => 1));
59                         }
60                 }
61         }
62
63         return undef;
64 } #}}}
65
66 sub formbuilder_setup (@) { #{{{
67         my %params=@_;
68
69         # XXX deprecated, should be removed eventually  
70         my $form=$params{form};
71         if ($form->title eq "preferences") {
72                 my $session=$params{session};
73                 my $cgi=$params{cgi};
74                 my $user_name=$session->param("name");
75
76                 $form->field(name => "locked_pages", size => 50,
77                         fieldset => "admin",
78                         comment => "deprecated; please move to locked_pages in setup file"
79                 );
80                 if (! IkiWiki::is_admin($user_name)) {
81                         $form->field(name => "locked_pages", type => "hidden");
82                 }
83                 if (! $form->submitted) {
84                         my $value=IkiWiki::userinfo_get($user_name, "locked_pages");
85                         if (length $value) {
86                                 $form->field(name => "locked_pages", force => 1, value => $value);
87                         }
88                         else {
89                                 $form->field(name => "locked_pages", type => "hidden");
90                         }
91                 }
92                 if ($form->submitted && $form->submitted eq 'Save Preferences') {
93                         if (defined $form->field("locked_pages")) {
94                                 IkiWiki::userinfo_set($user_name, "locked_pages",
95                                         $form->field("locked_pages")) ||
96                                                 error("failed to set locked_pages");
97                                 if (! length $form->field("locked_pages")) {
98                                         $form->field(name => "locked_pages", type => "hidden");
99                                 }
100                         }
101                 }
102         }
103 } #}}}
104
105 1