OSDN Git Service

改行コードをLFに統一。
[fswiki/fswiki.git] / plugin / admin / AdminStyleHandler.pm
1 ###############################################################################
2 #
3 # FSWiki¤Î¥¹¥¿¥¤¥ëÀßÄê¤ò¹Ô¤¦¥¢¥¯¥·¥ç¥ó¥Ï¥ó¥É¥é
4 #
5 ###############################################################################
6 package plugin::admin::AdminStyleHandler;
7 use strict;
8 #==============================================================================
9 # ¥³¥ó¥¹¥È¥é¥¯¥¿
10 #==============================================================================
11 sub new {
12         my $class = shift;
13         my $self = {};
14         return bless $self,$class;
15 }
16
17 #==============================================================================
18 # ¥¢¥¯¥·¥ç¥ó¥Ï¥ó¥É¥é¥á¥½¥Ã¥É
19 #==============================================================================
20 sub do_action {
21         my $self  = shift;
22         my $wiki  = shift;
23         my $cgi   = $wiki->get_CGI();
24         
25         $wiki->set_title("¥¹¥¿¥¤¥ëÀßÄê");
26         
27         if($cgi->param("SAVE") ne ""){
28                 return $self->save_config($wiki);
29         } else {
30                 return $self->config_form($wiki);
31         }
32 }
33
34 #==============================================================================
35 # ÀßÄê¥Õ¥©¡¼¥à
36 #==============================================================================
37 sub config_form {
38         my $self = shift;
39         my $wiki = shift;
40         
41         my $config  = &Util::load_config_hash($wiki,$wiki->config('config_file'));
42         my $usercss = &Util::load_config_text($wiki,$wiki->config('usercss_file'));
43         
44         # ¥Æ¡¼¥Þ¤Î°ìÍ÷¤ò¼èÆÀ¤·¥Æ¥ó¥×¥ì¡¼¥ÈÍѤ˲ù©
45         my @buf   = $self->list_theme($wiki);
46         my @theme = $self->convert_template_list(\@buf, $config->{theme});
47         
48         # ¥µ¥¤¥È¥Æ¥ó¥×¥ì¡¼¥È¥Æ¡¼¥Þ¤Î°ìÍ÷¤ò¼èÆÀ¤·¥Æ¥ó¥×¥ì¡¼¥ÈÍѤ˲ù©
49         @buf = $self->list_site_tmpl_theme($wiki);
50         my @site_tmpl_theme =  $self->convert_template_list(\@buf,$config->{site_tmpl_theme});
51         
52         my $no_theme = 0;
53         if($config->{'theme'} eq ""){
54                 $no_theme = 1;
55         }
56         
57         # ¥Æ¥ó¥×¥ì¡¼¥È¤Ë¥Ñ¥é¥á¡¼¥¿¤ò¥»¥Ã¥È
58         my $tmpl = HTML::Template->new(filename=>$wiki->config('tmpl_dir')."/admin_style.tmpl",
59                                        die_on_bad_params => 0);
60         $tmpl->param(THEME           => \@theme,
61                      USERCSS         => $usercss,
62                      OUTER_CSS       => $config->{'outer_css'},
63                      NO_THEME        => $no_theme,
64                      SITE_TMPL_THEME => \@site_tmpl_theme);
65         
66         return "<form action=\"".$wiki->create_url()."\" method=\"POST\">\n".
67                $tmpl->output().
68                "<input type=\"hidden\" name=\"action\" value=\"ADMINSTYLE\">\n".
69                "</form>\n";
70 }
71
72 #==============================================================================
73 # ÀßÄê¤òÊݸ
74 #==============================================================================
75 sub save_config {
76         my $self = shift;
77         my $wiki = shift;
78         my $cgi  = $wiki->get_CGI;
79         my $config = &Util::load_config_hash($wiki,$wiki->config('config_file'));
80         
81         $config->{theme}           = $cgi->param("theme");
82         $config->{outer_css}       = $cgi->param("outer_css");
83         $config->{site_tmpl_theme} = $cgi->param("site_tmpl_theme");
84         
85         &Util::save_config_hash($wiki,$wiki->config('config_file'),$config);
86         
87         my $usercss = $cgi->param("usercss");
88         &Util::save_config_text($wiki,$wiki->config('usercss_file'),$usercss);
89         
90         return $wiki->redirectURL( $wiki->create_url({action=>"ADMINSTYLE"}) );
91         #return "ÀßÄê¤òÊݸ¤·¤Þ¤·¤¿¡£\n";
92
93 }
94
95 #==============================================================================
96 # ¥Ç¥£¥ì¥¯¥È¥ê¤Î°ìÍ÷¤ò¼èÆÀ
97 #==============================================================================
98 sub list_dir {
99         my $self = shift;
100         my $dir  = shift;
101         my @list = ();
102         opendir(DIR, $dir) or die $!;
103         while(my $entry = readdir(DIR)) {
104                 my $type = -d $dir."/$entry" ? "dir" : "file";
105                 if($type eq "dir" && $entry ne "." && $entry ne ".."){
106                         push(@list,$entry);
107                 }
108         }
109         closedir(DIR);
110         
111         return sort(@list);
112 }
113
114 #==============================================================================
115 # ¥Æ¡¼¥Þ¤Î°ìÍ÷¤ò¼èÆÀ
116 #==============================================================================
117 sub list_theme {
118         my $self = shift;
119         my $wiki = shift;
120         return $self->list_dir($wiki->config('theme_dir'));
121 }
122
123 #==============================================================================
124 # ¥µ¥¤¥È¥Æ¥ó¥×¥ì¡¼¥È¥Æ¡¼¥Þ¤Î°ìÍ÷¤ò¼èÆÀ
125 #==============================================================================
126 sub list_site_tmpl_theme {
127         my $self = shift;
128         my $wiki = shift;
129         return $self->list_dir($wiki->config('tmpl_dir').'/site');
130 }
131
132 #==============================================================================
133 # HTML::Template¤Î¥»¥ì¥¯¥È¥¿¥°ÍѤ˥ꥹ¥È¤òÊÑ´¹¤··ë²Ì¤ò¼èÆÀ
134 #==============================================================================
135 sub convert_template_list {
136         my $self = shift;
137         my $list = shift;
138         my $selected_value = shift;
139
140         my @ret = ();
141         foreach my $value (@$list){
142                 my $selected = 0;
143                 if($value eq $selected_value){
144                         $selected = 1;
145                 }
146                 push(@ret,{VALUE=>$value,SELECT=>$selected});
147         }
148
149         return @ret;
150 }
151
152 1;