basic form display done
[ikiwiki.git] / IkiWiki / Plugin / websetup.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::websetup;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 sub import { #{{{
9         hook(type => "sessioncgi", id => "websetup",
10              call => \&sessioncgi);
11         hook(type => "formbuilder_setup", id => "websetup",
12              call => \&formbuilder_setup);
13 } # }}}
14
15 sub addfields ($$@) {
16         my $form=shift;
17         my $section=shift;
18
19         while (@_) {
20                 my $key=shift;
21                 my %info=%{shift()};
22
23                 next if ! $info{safe} || $info{type} eq "internal";
24
25                 my $description=exists $info{description_html} ? $info{description_html} : $info{description};
26
27                 my $value=$config{$key};
28                 # multiple plugins can have the same key
29                 my $name=$section.".".$key;
30
31                 if ($info{type} eq "string") {
32                         $form->field(
33                                 name => $name,
34                                 label => $description,
35                                 comment => exists $info{example} && length $info{example} && $info{example} ne $value ? "<br/ ><small>Example: <tt>$info{example}</tt></small>" : "",
36                                 type => "text",
37                                 value => $value,
38                                 size => 60,
39                                 fieldset => $section,
40                         );
41                 }
42                 elsif ($info{type} eq "integer") {
43                         $form->field(
44                                 name => $name,
45                                 label => $description,
46                                 type => "text",
47                                 value => $value,
48                                 validate => '/^[0-9]+$/',
49                                 fieldset => $section,
50                         );
51                 }
52                 elsif ($info{type} eq "boolean") {
53                         $form->field(
54                                 name => $name,
55                                 label => "",
56                                 type => "checkbox",
57                                 value => $value,
58                                 options => [ [ 1 => $description ] ],
59                                 fieldset => $section,
60                         );
61                 }
62         }
63 }
64
65 sub showform ($$) { #{{{
66         my $cgi=shift;
67         my $session=shift;
68
69         if (! defined $session->param("name") || 
70             ! IkiWiki::is_admin($session->param("name"))) {
71                 error(gettext("you are not logged in as an admin"));
72         }
73
74         eval q{use CGI::FormBuilder};
75         error($@) if $@;
76
77         my $form = CGI::FormBuilder->new(
78                 title => "setup",
79                 name => "setup",
80                 header => 0,
81                 charset => "utf-8",
82                 method => 'POST',
83                 javascript => 0,
84                 params => $cgi,
85                 action => $config{cgiurl},
86                 template => {type => 'div'},
87                 stylesheet => IkiWiki::baseurl()."style.css",
88         );
89         my $buttons=["Save Setup", "Cancel"];
90
91         IkiWiki::decode_form_utf8($form);
92         IkiWiki::run_hooks(formbuilder_setup => sub {
93                 shift->(form => $form, cgi => $cgi, session => $session,
94                         buttons => $buttons);
95         });
96         IkiWiki::decode_form_utf8($form);
97
98         $form->field(name => "do", type => "hidden", value => "setup",
99                 force => 1);
100         addfields($form, gettext("main"), IkiWiki::getsetup());
101         require IkiWiki::Setup;
102         foreach my $pair (IkiWiki::Setup::getsetup()) {
103                 my $plugin=$pair->[0];
104                 my $setup=$pair->[1];
105                 addfields($form, $plugin." ".gettext("plugin"), @{$setup});
106         }
107
108         if ($form->submitted eq "Cancel") {
109                 IkiWiki::redirect($cgi, $config{url});
110                 return;
111         }
112         elsif ($form->submitted eq 'Save Setup' && $form->validate) {
113                 # TODO
114
115                 $form->text(gettext("Setup saved."));
116         }
117
118         IkiWiki::showform($form, $buttons, $session, $cgi);
119 } #}}}
120
121 sub sessioncgi ($$) { #{{{
122         my $cgi=shift;
123         my $session=shift;
124
125         if ($cgi->param("do") eq "setup") {
126                 showform($cgi, $session);
127                 exit;
128         }
129 } #}}}
130
131 sub formbuilder_setup (@) { #{{{
132         my %params=@_;
133
134         my $form=$params{form};
135         if ($form->title eq "preferences") {
136                 push @{$params{buttons}}, "Wiki Setup";
137                 if ($form->submitted && $form->submitted eq "Wiki Setup") {
138                         showform($params{cgi}, $params{session});
139                         exit;
140                 }
141         }
142 } #}}}
143
144 1