| 1 | package OpenInteract::UI::Main; | ||
| 2 | |||
| 3 | # $Id: Main.pm,v 1.11 2003/08/13 03:16:44 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | |||
| 7 | $OpenInteract::UI::Main::VERSION = sprintf("%d.%02d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/); | ||
| 8 | |||
| 9 | sub handler { | ||
| 10 | my ( $class ) = @_; | ||
| 11 | my $R = OpenInteract::Request->instance; | ||
| 12 | |||
| 13 | # Put the Popup and other directives here. (See DIRECTIVES in POD.) | ||
| 14 | |||
| 15 | if ( my $directive = $R->{ui}{directive} ) { | ||
| 16 | if ( $directive =~ /^(NoTmpl|NoTemplate)$/ ) { | ||
| 17 | $R->{page}{_no_template_}++; | ||
| 18 | $R->DEBUG && $R->scrib( 1, "Using NO window template" ); | ||
| 19 | } | ||
| 20 | else { | ||
| 21 | $R->{page}{_template_key_} = $R->CONFIG->{page_directives}{ $directive }; | ||
| 22 | $R->DEBUG && $R->scrib( 1, "Using template key from directive: ", | ||
| 23 | "[$R->{page}{_template_key_}]" ); | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | # Parse the URL and the information for our first action | ||
| 28 | |||
| 29 | my $action_info = $R->lookup_action( undef, { return => 'info' } ); | ||
| 30 | my ( $action_class, $action_method ) = ( $action_info->{class}, | ||
| 31 | $action_info->{method} ); | ||
| 32 | $R->DEBUG && $R->scrib( 1, "Action info [$action_class] [$action_method]" ); | ||
| 33 | |||
| 34 | # Capture any die() commands thrown; note that any error handler that | ||
| 35 | # throws a die() needs to also return content to display; otherwise | ||
| 36 | # it will be a pretty boring (empty) page :) | ||
| 37 | |||
| 38 | $R->{page}{content} = eval { $action_class->$action_method({ | ||
| 39 | path => $R->{path}{current}, | ||
| 40 | ACTION => $action_info }) }; | ||
| 41 | if ( $@ ) { | ||
| 42 | $R->{page}{content} = $@; | ||
| 43 | $R->scrib( 0, "Action died. Here is what it left: $@" ); | ||
| 44 | } | ||
| 45 | |||
| 46 | # Check to see if we're supposed to send back a file (no content | ||
| 47 | # to return) or we're doing an HTTP redirect | ||
| 48 | |||
| 49 | if ( $R->{page}{send_file} || $R->{page}{http_redirect} ) { | ||
| 50 | return undef; | ||
| 51 | } | ||
| 52 | |||
| 53 | # Otherwise pick the template to wrap the content in | ||
| 54 | |||
| 55 | my $template_name = $class->choose_template; | ||
| 56 | $R->DEBUG && $R->scrib( 1, "Using template [$template_name] for full page" ); | ||
| 57 | return $R->{page}{content} unless ( $template_name ); | ||
| 58 | |||
| 59 | $R->{main_template_vars} ||= {}; | ||
| 60 | return $R->template->handler( {}, | ||
| 61 | { %{ $R->{main_template_vars} }, | ||
| 62 | page => $R->{page}, | ||
| 63 | path => $R->{path}{original} }, | ||
| 64 | { name => $template_name } ); | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | sub choose_template { | ||
| 69 | my ( $class ) = @_; | ||
| 70 | my $R = OpenInteract::Request->instance; | ||
| 71 | |||
| 72 | return undef if ( $R->{page}{_no_template_} ); | ||
| 73 | |||
| 74 | my $template_name = $R->{page}{_template_name_}; | ||
| 75 | |||
| 76 | # If the template name isn't specified by the request, look for a | ||
| 77 | # template key which we can use to find the template in the theme. | ||
| 78 | |||
| 79 | unless ( $template_name ) { | ||
| 80 | my $template_key = $R->{page}{_template_key_}; | ||
| 81 | $template_key ||= 'simple_template' if ( $R->{page}{_simple_} ); | ||
| 82 | $template_key ||= 'main_template'; | ||
| 83 | $template_name = $R->{theme}->property_value( $template_key ); | ||
| 84 | } | ||
| 85 | return $template_name; | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; | ||
| 89 | |||
| 90 | __END__ |