| 1 | package OpenInteract2::Manage::Website::ThemeInstall; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # $Id: ThemeInstall.pm,v 1.12 2005/03/17 14:58:04 sjn Exp $ | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 87 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 87 | ▶ | use base qw( OpenInteract2::Manage::Website ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 87 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 87 | ▶ | use OpenInteract2::Context qw( CTX ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 87 | $OpenInteract2::Manage::Website::ThemeInstall::VERSION = sprintf("%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 87 | ▶ | sub get_name { | ||||||||||||||||||||||||||
|
Subroutine
get_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | 87 | return 'install_theme'; | |||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub get_brief_description { | ||||||||||||||||||||||||||
|
Subroutine
get_brief_description: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | 0 | return 'Install a theme dumped from a website to your website'; | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | sub get_parameters { | ||||||||||||||||||||||||||
|
Subroutine
get_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 1 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 22 | return { | ||||||||||||||||||||||||||||
| 23 | 1 | website_dir => $self->_get_website_dir_param, | |||||||||||||||||||||||||||
| 24 | theme_file => { | ||||||||||||||||||||||||||||
| 25 | description => 'Name of file to import the theme from', | ||||||||||||||||||||||||||||
| 26 | is_required => 'yes', | ||||||||||||||||||||||||||||
| 27 | }, | ||||||||||||||||||||||||||||
| 28 | }; | ||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | sub validate_param { | ||||||||||||||||||||||||||
|
Subroutine
validate_param: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 0 | my ( $self, $name, $value ) = @_; | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | if ( $name eq 'theme_file' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | unless ( $value and -f $value ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$value and -f $value
MC/DC0%0/2
$value and -f $value
$value
-f $value
Truth table0%0/3
A and B
A= $valueB= -f $value
| |||||||||||||||||||||||||||||
| 35 | 0 | return "Must be a valid filename"; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 0 | my $data = eval { _open_file( $value ) }; | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | return "Error parsing file: $@"; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 0 | $self->param( theme_data => $data ); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 0 | return $self->SUPER::validate_param( $name, $value ); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub run_task { | ||||||||||||||||||||||||||
|
Subroutine
run_task: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 0 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 49 | 0 | my $action = 'install theme'; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | my $themeball = $self->param( 'theme_data' ); | |||||||||||||||||||||||||||
| 52 | 0 | my $theme_meta = shift @{ $themeball }; | |||||||||||||||||||||||||||
| 53 | my $theme_data = $self->_map_fields( | ||||||||||||||||||||||||||||
| 54 | 0 | $theme_meta->{theme_fields}, shift @{ $themeball } | |||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | my $default_parent = CTX->lookup_default_object_id( 'theme' ); | |||||||||||||||||||||||||||
| 58 | 0 | $theme_data->{parent} = $default_parent; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | my ( $theme ); | |||||||||||||||||||||||||||
| 61 | 0 | eval { | |||||||||||||||||||||||||||
| 62 | 0 | $theme = CTX->lookup_object( 'theme' )->new( $theme_data ); | |||||||||||||||||||||||||||
| 63 | 0 | $theme->save; | |||||||||||||||||||||||||||
| 64 | }; | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | $self->_fail( $action, "Cannot save theme: $@" ); | |||||||||||||||||||||||||||
| 67 | 0 | return; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | my $theme_id = $theme->id; | |||||||||||||||||||||||||||
| 71 | 0 | my $themeprop_class = CTX->lookup_object( 'themeprop' ); | |||||||||||||||||||||||||||
| 72 | 0 | my $props = 0; | |||||||||||||||||||||||||||
| 73 | 0 | foreach my $prop_raw ( @{ $themeball } ) { | |||||||||||||||||||||||||||
| 74 | my $prop_data = $self->_map_fields( | ||||||||||||||||||||||||||||
| 75 | 0 | $theme_meta->{theme_prop_fields}, $prop_raw | |||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||
| 77 | 0 | my $prop = $themeprop_class->new( $prop_data ); | |||||||||||||||||||||||||||
| 78 | 0 | $prop->{theme_id} = $theme_id; | |||||||||||||||||||||||||||
| 79 | 0 | eval { $prop->save }; | |||||||||||||||||||||||||||
| 80 | 0 | $props++; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 0 | $self->_ok( $action, | |||||||||||||||||||||||||||
| 83 | "New theme (ID: $theme_id) and $props properties saved ok" ); | ||||||||||||||||||||||||||||
| 84 | 0 | return; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | sub _map_fields { | ||||||||||||||||||||||||||
|
Subroutine
_map_fields: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 89 | 0 | my ( $self, $fields, $data ) = @_; | |||||||||||||||||||||||||||
| 90 | 0 | my %map = (); | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | for ( my $i = 0; $i < scalar @{ $fields }; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | $map{ $fields->[ $i ] } = $data->[ $i ]; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 0 | return \%map; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub _open_file { | ||||||||||||||||||||||||||
|
Subroutine
_open_file: not called CC=4 SCAR=26.4
| |||||||||||||||||||||||||||||
| 99 | 0 | my ( $theme_file ) = @_; | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | eval { open( THEMEBALL, "< $theme_file" ) || die $! }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | die "cannot open - $@" if ( $@ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | local $/ = undef; | |||||||||||||||||||||||||||
| 104 | 0 | my $contents = <THEMEBALL>; | |||||||||||||||||||||||||||
| 105 | 0 | my ( $data ); | |||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | no strict 'vars'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 108 | 0 | $data = eval $contents; | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | die "invalid data - $@" if ( $@ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 0 | close( THEMEBALL ); | |||||||||||||||||||||||||||
| 112 | 0 | return $data; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 87 | OpenInteract2::Manage->register_factory_type( get_name() => __PACKAGE__ ); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 87 | 1; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | __END__ | ||||||||||||||||||||||||||||