File Coverage

blib/lib/Apache/Voodoo/View/HTML/Theme.pm
Criterion Covered Total %
statement 12 93 12.9
branch 0 22 0.0
condition 0 14 0.0
subroutine 4 10 40.0
pod 0 4 0.0
total 16 143 11.1


line stmt bran cond sub pod time code
1             package Apache::Voodoo::View::HTML::Theme;
2              
3             $VERSION = "3.0200";
4              
5 1     1   1549 use strict;
  1         2  
  1         35  
6 1     1   5 use warnings;
  1         1  
  1         34  
7              
8 1     1   5 use Config::General;
  1         2  
  1         47  
9 1     1   5 use HTML::Template;
  1         2  
  1         995  
10              
11             sub new {
12 0     0 0   my $class = shift;
13 0           my $config = shift;
14              
15 0           my $self = {};
16              
17 0           $self->{default} = $config->{default};
18 0           $self->{user_set} = $config->{user_can_choose};
19              
20 0           foreach (@{$config->{'theme'}}) {
  0            
21 0           $self->{'themes'}->{$_->{'name'}} = $_->{'dir'};
22             }
23              
24 0           bless $self,$class;
25              
26 0           return $self;
27             }
28              
29             sub handle {
30 0     0 0   my $self = shift;
31 0           my $p = shift;
32              
33 0           my $chosen_theme = $self->choose_theme($p);
34              
35 0           my $return = {};
36              
37             # URL relative
38 0           $return->{'THEME_DIR'} = $self->{'themes'}->{$chosen_theme};
39              
40             # FILE system relative
41 0           my $theme_dir = $p->{'document_root'}."/".$self->{'themes'}->{$chosen_theme};
42              
43 0           my $tc = "$theme_dir/theme.conf";
44 0 0         if ($self->_is_stale($tc)) {
45 0           $self->{cache}->{$tc} = $self->_load($tc);
46             }
47              
48 0           my $conf = $self->{cache}->{$tc};
49              
50             # find which style section this page is under
51 0           my $style = $conf->{'pages'}->{$p->{'uri'}}->{'__style__'};
52 0           $self->{'skeleton'} = $self->{'themes'}->{$chosen_theme}."/";
53              
54 0 0         unless (defined($style)) {
55             # not listed. no theme for you!
56              
57             # assume default skeleton.
58 0           $self->{'skeleton'} .= "skeleton";
59 0           return $return;
60             }
61              
62 0   0       $self->{'skeleton'} .= $conf->{'style'}->{$style}->{'skeleton'} || 'skeleton';
63              
64 0 0         if (defined($conf->{'style'}->{$style}->{'includes'})) {
65 0           while (my ($k,$v) = each %{$conf->{'style'}->{$style}->{'includes'}}) {
  0            
66 0           my $template = HTML::Template->new('filename' => "$theme_dir/$v.tmpl",
67             'shared_cache' => 1,
68             'die_on_bad_params' => 0
69             );
70 0           $template->param($conf->{'pages'}->{$p->{'uri'}});
71              
72 0           $return->{$k} = $template->output();
73             }
74             }
75             else {
76 0           while (my ($k,$v) = each %{$conf->{'pages'}->{$p->{'uri'}}}) {
  0            
77 0           $return->{$k} = $v;
78             }
79             }
80              
81 0           return $return;
82             }
83              
84             sub choose_theme {
85 0     0 0   my $self = shift;
86 0           my $p = shift;
87              
88 0           my $session = $p->{'session'};
89              
90             # check for an override of what's in the template conf file.
91 0           my $sys_override = $p->{'document_root'}."/.theme_conf";
92              
93 0           my $chosen_theme = $self->{'default'};
94              
95 0 0 0       if (-e $sys_override && -s $sys_override) {
96 0           my $mtime = (stat($sys_override))[9];
97              
98 0 0 0       if (!defined($self->{'sys_theme'}->{'mtime'}) || $self->{'sys_theme'}->{'mtime'} ne $mtime) {
99 0 0         unless(open(T,$sys_override)) {
100 0           die "Can't open $sys_override: $!";
101             }
102 0           my $t = ;
103 0           chomp($t);
104 0           close(T);
105              
106 0 0 0       if ($t ne "default" && defined($self->{'themes'}->{$t})) {
107 0           $chosen_theme = $t;
108 0           $self->{'sys_theme'}->{'name'} = $t;
109             }
110              
111 0           $self->{'sys_theme'}->{'mtime'} = (stat($sys_override))[9];
112             }
113             else {
114 0           $chosen_theme = $self->{'sys_theme'}->{'name'};
115             }
116             }
117              
118 0 0         if ($self->{'user_set'}) {
119 0           my $user_theme = $session->{'user_theme'};
120 0 0 0       if (defined($user_theme) && $user_theme ne "default") {
121 0 0         if (defined($self->{'themes'}->{$user_theme})) {
122 0           $chosen_theme = $user_theme;
123             }
124             else {
125 0           delete ($session->{'user_theme'});
126             }
127             }
128             }
129              
130 0           return $chosen_theme;
131             }
132              
133             sub _load {
134 0     0     my $self = shift;
135 0           my $file = shift;
136              
137 0           my $record;
138              
139 0           my $config_general = Config::General->new($file);
140 0           my %conf = $config_general->getall;
141              
142 0           $record->{'mtime'} = (stat($file))[9];
143 0           foreach my $style (keys %{$conf{'style'}}) {
  0            
144 0           $record->{'style'}->{$style}->{'skeleton'} = $conf{'style'}->{$style}->{'skeleton'};
145 0           $record->{'style'}->{$style}->{'includes'} = $conf{'style'}->{$style}->{'includes'};
146 0           foreach my $page (keys %{$conf{'style'}->{$style}->{'pages'}}) {
  0            
147 0           $record->{'pages'}->{$page} = $conf{'style'}->{$style}->{'pages'}->{$page};
148 0           $record->{'pages'}->{$page}->{'__style__'} = $style;
149             }
150             }
151              
152 0           return $record;
153             }
154              
155             sub _is_stale {
156 0     0     my $self = shift;
157 0           my $file = shift;
158              
159 0 0         return 1 unless defined($self->{'cache'}->{$file});
160 0           return ($self->{'cache'}->{$file}->{'mtime'} != (stat($file))[9]);
161             }
162              
163             sub get_skeleton {
164 0     0 0   return shift->{'skeleton'};
165             }
166              
167             1;
168              
169             ################################################################################
170             # Copyright (c) 2005-2010 Steven Edwards (maverick@smurfbane.org).
171             # All rights reserved.
172             #
173             # You may use and distribute Apache::Voodoo under the terms described in the
174             # LICENSE file include in this package. The summary is it's a legalese version
175             # of the Artistic License :)
176             #
177             ################################################################################