App-ZofCMS
view release on metacpan or search on metacpan
lib/App/ZofCMS/Plugin/StyleSwitcher.pm view on Meta::CPAN
package App::ZofCMS::Plugin::StyleSwitcher;
use warnings;
use strict;
our $VERSION = '1.001008'; # VERSION
use base 'App::ZofCMS::Plugin::Base';
use DBI;
sub _key { 'plug_style_switcher' }
sub _defaults {
return (
q_name => 'style',
q_ajax_name => 'plug_style_switcher_ajax',
t_prefix => 'style_switcher_',
table => 'style_switcher',
max_time => 2678400, # one month
default_style => 'main',
xhtml => 0,
# styles => {}
dsn => "DBI:mysql:database=test;host=localhost",
#user => 'test',
#pass => 'test',
create_table => 0,
opt => { RaiseError => 1, AutoCommit => 1 },
);
}
sub _do {
my ( $self, $conf, $template, $query, $config ) = @_;
my $dbh = DBI->connect_cached(
@$conf{ qw/dsn user pass opt/ },
);
$self->dbh( $dbh );
if ( $conf->{create_table} ) {
$dbh->do(
"CREATE TABLE $conf->{table} ( host VARCHAR(200), style TEXT, time VARCHAR(10) );",
);
}
if ( defined $query->{ $conf->{q_name} }
and length $query->{ $conf->{q_name} }
) {
$self->_set_style( $conf, $config->cgi, $query->{ $conf->{q_name} } );
}
my $style = $self->_get_current_style( $conf, $config->cgi );
my $tp = $conf->{t_prefix};
$template->{t}{ $tp . 'style' } = $self->_make_style_html( $conf, $style );
$template->{t}{ $tp . 'toggle' } = $self->_make_toggle_html(
$conf,
map +(defined() ? $_ : ''), @$query{qw/page dir/},
$style,
);
$dbh->disconnect;
print "Set-Cookie: plug_style_switcher=$style\n";
if ( $query->{ $conf->{q_ajax_name} } ) {
print "Content-type: text/plain\n\n";
exit;
}
}
sub _make_toggle_html {
my ( $self, $conf, $page, $dir, $style ) = @_;
my $styles_ref = $conf->{styles};
my $next;
my $last;
for ( sort keys %$styles_ref ) {
$next = $_
unless defined $next;
if ( defined $last and $last eq $style ) {
$next = $_;
last;
}
$last = $_;
}
return qq|<a id="plug_style_switcher" |
. qq|href="/index.pl?page=$page&dir=$dir&$conf->{q_name}=$next">|
. qq|toggle style</a>\n|;
}
sub _make_style_html {
my ( $self, $conf, $style ) = @_;
my $styles_ref = $conf->{styles};
my $ending = $conf->{xhtml} ? '/' : '';
my @out;
for my $key ( sort keys %$styles_ref ) {
my $rel = $key eq $style ? 'stylesheet' : 'alternate stylesheet';
my $styles = $styles_ref->{ $key };
$styles = [ $styles ]
unless ref $styles eq 'ARRAY';
for my $style ( @$styles ) {
if ( '[IE]' eq substr $style, 0, 4 ) {
$style = substr $style, 0, 4;
push @out,
qq|<!--[if IE]><link rel="$rel" type="text/css" href="$style" media="screen,tv,projection"$ending><![endif]-->|;
}
else {
push @out,
qq|<link rel="$rel" type="text/css" href="$style" media="screen,tv,projection"$ending>|;
}
}
}
return join "\n", @out;
}
sub _set_style {
my ( $self, $conf, $cgi, $style ) = @_;
my $host = $self->_make_host( $cgi );
( run in 0.723 second using v1.01-cache-2.11-cpan-39bf76dae61 )