| 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
| |||||||||||||||||||||||||||||
| 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")
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")
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | my $action = $self->initial_action; | |||||||||||||||||||||||||||
| 35 | 0 | ▶ | if ( $log->is_debug ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | my $task = $action->task || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$action->task || ''
MC/DC0%0/1
$action->task || ''
$action->task
Truth table0%0/2
A
A= $action->task
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | $log->is_info && | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | $log->is_info && | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | $log->is_info && | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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'
MC/DC0%0/1
$self->main_template_key || 'main_template'
$self->main_template_key
Truth table0%0/2
A
A= $self->main_template_key
| |||||||||||||||||||||||||||||
| 83 | 0 | $template_name = CTX->request->theme_values->{ $template_key }; | |||||||||||||||||||||||||||
| 84 | 0 | ▶ | $log->is_info && | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$key and $value
MC/DC0%0/2
$key and $value
$key
$value
Truth table0%0/3
A and B
A= $keyB= $value
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$key and $value
MC/DC0%0/2
$key and $value
$key
$value
Truth table0%0/3
A and B
A= $keyB= $value
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||