OpenInteract2/Controller/MainTemplate.pm

filter: Statementstmt 28.7% 23 / 80 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 42.8% 6 / 14 Podpod 0.0% 0 / 8 totaltotal 18.7% 29 / 155 CC 22 SCAR 46.7
CC
22
Cov
18%
execute
240.0
add_content_param
12.0
add_template_param
12.0
CRAP 106.2
« OpenInteract2/Cache.pm ↑ Summary OpenInteract2/Action/CommonUpdate.pm »
1 package OpenInteract2::Controller::MainTemplate;
2  
3 # $Id: MainTemplate.pm,v 1.9 2005/03/17 14:58:01 sjn Exp $
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base qw( OpenInteract2::Controller
Subroutine BEGIN: called CC=1 SCAR=0.0
7 OpenInteract2::Controller::ManageBoxes
8 1 OpenInteract2::Controller::ManageTemplates );
9 1 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use OpenInteract2::Context qw( CTX );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use OpenInteract2::Constants qw( :log );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use OpenInteract2::Exception qw( oi_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 $OpenInteract2::Controller::MainTemplate::VERSION = sprintf("%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/);
15  
16 1 my ( $log );
17  
18 1 my @FIELDS = qw( no_template main_template main_template_key page_title );
19 1 __PACKAGE__->mk_accessors( @FIELDS );
20  
21 0 ▶sub init {
Subroutine init: not called CC=2 SCAR=17.9
Pod undocumented
22 0 my ( $self ) = @_;
23 0 ▶ if ( CTX->request->param( 'no_template' ) eq 'yes' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (CTX()->request->param("no_template") eq "yes") 0 0 0
24 0 $self->no_template( 'yes' );
25 }
26 0 $self->init_boxes;
27 0 $self->init_templates;
28 }
29  
30 0 ▶sub execute {
Subroutine execute: not called CC=15 SCAR=54.8
Pod undocumented
31 0 my ( $self ) = @_;
32 0 ▶ $log ||= get_logger( LOG_ACTION );
Condition0%0/3
$log ||= get_logger("OI2.ACTION")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.ACTION")
$log get_logger("OI2.ACTION")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.ACTION")
ABresult
1X1
011
000
33  
34 0 my $action = $self->initial_action;
35 0 ▶ if ( $log->is_debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log->is_debug) 0 0 0
36 0 ▶ my $task = $action->task || '';
Condition0%0/2
$action->task || ''
l!l
00
MC/DC0%0/1
$action->task || ''
$action->task
Truth table0%0/2
A
A= $action->task
Aresult
11
00
37 0 $log->debug(
38 sprintf( "Executing top-level action '%s' with task '%s'",
39 $action->name, $task )
40 );
41 }
42  
43 0 my $content = eval { $action->execute };
44 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
45 0 $log->error( "Caught exception from action: $@" );
46  
47 # TODO: Set this error message from config file
48 0 $self->add_content_param( title => 'Action execution error' );
49 0 $content = $@;
50 }
51  
52 # If an action set a file to send back, we're done
53  
54 0 ▶ if ( my $send_file = CTX->response->send_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $send_file = CTX()->response->send_file) 0 0 0
55 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Action specified return file '${send_file}'") 0 0 0
56 $log->info( "Action specified return file '$send_file'" );
57 0 return;
58 }
59  
60 # If someone has specified not to use a main template return the
61 # content. (Just like ::Raw does)
62  
63 0 ▶ if ( $self->no_template eq 'yes' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->no_template eq "yes") 0 0 0
64 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Someone told us not to use a wrapper template; ", "returning raw content") 0 0 0
65 $log->info( "Someone told us not to use a wrapper template; ",
66 "returning raw content" );
67 0 CTX->response->content( \$content );
68 0 return;
69 }
70  
71 # All content params are just top-level template variables.
72  
73 0 $self->add_content_param( content => $content );
74  
75 0 my $template_name = $self->main_template;
76 0 ▶ if ( $template_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template_name) { } 0 0 0
77 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Using main template name '${template_name}' as ", "set directly in controller") 0 0 0
78 $log->info( "Using main template name '$template_name' as ",
79 "set directly in controller" );
80 }
81 else {
82 0 ▶ my $template_key = $self->main_template_key || 'main_template';
Condition0%0/2
$self->main_template_key || 'main_template'
l!l
00
MC/DC0%0/1
$self->main_template_key || 'main_template'
$self->main_template_key
Truth table0%0/2
A
A= $self->main_template_key
Aresult
11
00
83 0 $template_name = CTX->request->theme_values->{ $template_key };
84 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Assigned main template name '${template_name}' ", "using the template key '${template_key}' from ", "theme with ID '", CTX()->request->theme->id, "'") 0 0 0
85 $log->info( "Assigned main template name '$template_name' ",
86 "using the template key '$template_key' from ",
87 "theme with ID '", CTX->request->theme->id, "'" );
88 }
89  
90 0 my $generator = CTX->content_generator( $self->generator_type );
91 0 my $full_content = eval {
92 0 $generator->generate( $self->template_params,
93 $self->content_params,
94 { name => $template_name } )
95 };
96 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
97 0 my $msg = "Content generator failed to execute: $@";
98 0 $log->error( $msg );
99 0 oi_error $msg;
100 }
101 $log->is_debug &&
102 0 ▶ $log->debug( "Generated content ok, setting to response" );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Generated content ok, setting to response") 0 0 0
103 0 CTX->response->content( \$full_content );
104 0 return;
105 }
106  
107 ########################################
108 # CONTENT PARAMS
109  
110 # XXX: Should these be moved to a separate class?
111  
112 0 ▶sub add_content_param {
Subroutine add_content_param: not called CC=3 SCAR=24.8
Pod undocumented
113 0 my ( $self, $key, $value ) = @_;
114 0 ▶ return unless ( $key and $value );
Branch: 1 branch
BranchTrueFalseTotal
unless $key and $value 0 0 0
Condition0%0/3
$key and $value
!ll&&!rl&&r
000
MC/DC0%0/2
$key and $value
$key $value
Truth table0%0/3
A and B
A= $keyB= $value
ABresult
0X0
100
111
115 0 $self->{_content_params}{ $key } = $value;
116 }
117  
118 0 ▶sub remove_content_param {
Subroutine remove_content_param: not called CC=2 SCAR=17.9
Pod undocumented
119 0 my ( $self, $key ) = @_;
120 0 ▶ return unless ( $key );
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 0 0
121 0 delete $self->{_content_params}{ $key };
122 }
123  
124 # TODO: Make a copy before returning?
125 0 ▶sub content_params {
Subroutine content_params: not called CC=1 SCAR=6.9
Pod undocumented
126 0 my ( $self ) = @_;
127 0 return $self->{_content_params};
128 }
129  
130 ########################################
131 # TEMPLATE PARAMETERS
132  
133 # XXX: Should these be moved to a separate class?
134  
135 0 ▶sub add_template_param {
Subroutine add_template_param: not called CC=3 SCAR=24.8
Pod undocumented
136 0 my ( $self, $key, $value ) = @_;
137 0 ▶ return unless ( $key and $value );
Branch: 1 branch
BranchTrueFalseTotal
unless $key and $value 0 0 0
Condition0%0/3
$key and $value
!ll&&!rl&&r
000
MC/DC0%0/2
$key and $value
$key $value
Truth table0%0/3
A and B
A= $keyB= $value
ABresult
0X0
100
111
138 0 $self->{_template_params}{ $key } = $value;
139 }
140  
141 0 ▶sub remove_template_param {
Subroutine remove_template_param: not called CC=2 SCAR=17.9
Pod undocumented
142 0 my ( $self, $key ) = @_;
143 0 ▶ return unless ( $key );
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 0 0
144 0 delete $self->{_template_params}{ $key };
145 }
146  
147 # TODO: Make a copy before returning?
148 0 ▶sub template_params {
Subroutine template_params: not called CC=1 SCAR=6.9
Pod undocumented
149 0 my ( $self ) = @_;
150 0 return $self->{_template_params};
151 }
152  
153 1 1;
154  
155 __END__
« OpenInteract2/Cache.pm ↑ Summary OpenInteract2/Action/CommonUpdate.pm »