| 1 | package SAL::WebApplication; | ||
| 2 | |||
| 3 | # This module is licensed under the FDL (Free Document License) | ||
| 4 | # The complete license text can be found at http://www.gnu.org/copyleft/fdl.html | ||
| 5 | # Contains excerpts from various man pages, tutorials and books on perl | ||
| 6 | # UNDER CONSTRUCTION | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use CGI; | ||
| 10 | use CGI::Carp qw(warningsToBrowser fatalsToBrowser); | ||
| 11 | use SAL::DBI; | ||
| 12 | use SAL::Web; | ||
| 13 | |||
| 14 | BEGIN { | ||
| 15 | use Exporter (); | ||
| 16 | our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); | ||
| 17 | $VERSION = '3.03'; | ||
| 18 | @ISA = qw(Exporter); | ||
| 19 | @EXPORT = qw(); | ||
| 20 | %EXPORT_TAGS = (); | ||
| 21 | @EXPORT_OK = qw(); | ||
| 22 | } | ||
| 23 | our @EXPORT_OK; | ||
| 24 | |||
| 25 | END { } | ||
| 26 | |||
| 27 | our %WebApplication = ( | ||
| 28 | ###################################### | ||
| 29 | 'dbi_factory' => '', | ||
| 30 | 'cgi' => '', | ||
| 31 | 'gui' => '', | ||
| 32 | 'canvas' => '', | ||
| 33 | 'modes' => (), | ||
| 34 | 'default_mode' => '', | ||
| 35 | 'build_toolbar' => '', | ||
| 36 | 'build_html_header' => '', | ||
| 37 | ###################################### | ||
| 38 | ); | ||
| 39 | |||
| 40 | # Setup accessors via closure (from perltooc manpage) | ||
| 41 | sub _classobj { | ||
| 42 | my $obclass = shift || __PACKAGE__; | ||
| 43 | my $class = ref($obclass) || $obclass; | ||
| 44 | no strict "refs"; | ||
| 45 | return \%$class; | ||
| 46 | } | ||
| 47 | |||
| 48 | for my $datum (keys %{ _classobj() }) { | ||
| 49 | no strict "refs"; | ||
| 50 | *$datum = sub { | ||
| 51 | my $self = shift->_classobj(); | ||
| 52 | $self->{$datum} = shift if @_; | ||
| 53 | return $self->{$datum}; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | ########################################################################################################################## | ||
| 58 | # Constructors (Public) | ||
| 59 | sub new { | ||
| 60 | my $obclass = shift || __PACKAGE__; | ||
| 61 | my $class = ref($obclass) || $obclass; | ||
| 62 | my $self = {}; | ||
| 63 | |||
| 64 | bless($self, $class); | ||
| 65 | |||
| 66 | # Set default object properties | ||
| 67 | |||
| 68 | $self->{cgi} = new CGI; | ||
| 69 | $self->{dbo_factory} = new SAL::DBI; | ||
| 70 | $self->{gui} = new SAL::Web; | ||
| 71 | # $self->{default_mode} = \&throw_error; | ||
| 72 | |||
| 73 | # Send the mime header | ||
| 74 | print "Content-type: text/html\n\n"; | ||
| 75 | |||
| 76 | return $self; | ||
| 77 | } | ||
| 78 | |||
| 79 | ########################################################################################################################## | ||
| 80 | # Destructor (Public) | ||
| 81 | sub destruct { | ||
| 82 | my $self = shift; | ||
| 83 | |||
| 84 | } | ||
| 85 | |||
| 86 | ########################################################################################################################## | ||
| 87 | # Public Methods | ||
| 88 | sub register_default { | ||
| 89 | my $self = shift; | ||
| 90 | my $address = shift; | ||
| 91 | |||
| 92 | if (! $address) { return 0; } | ||
| 93 | |||
| 94 | $self->{default_mode} = $address; | ||
| 95 | |||
| 96 | return 1; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub register_mode { | ||
| 100 | my $self = shift; | ||
| 101 | my $mode = shift; | ||
| 102 | my $address = shift; | ||
| 103 | |||
| 104 | if (! $mode) { return 0; } | ||
| 105 | if (! $address) { return 0; } | ||
| 106 | |||
| 107 | $self->{modes}{$mode} = $address; | ||
| 108 | |||
| 109 | return 1; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub register_toolbar { | ||
| 113 | my $self = shift; | ||
| 114 | my $address = shift; | ||
| 115 | |||
| 116 | if (! $address) { return 0; } | ||
| 117 | |||
| 118 | $self->{build_toolbar} = $address; | ||
| 119 | |||
| 120 | return 1; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub register_html_header { | ||
| 124 | my $self = shift; | ||
| 125 | my $address = shift; | ||
| 126 | |||
| 127 | if (! $address) { return 0; } | ||
| 128 | |||
| 129 | $self->{build_html_header} = $address; | ||
| 130 | |||
| 131 | return 1; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub run { | ||
| 135 | my $self = shift; | ||
| 136 | my $mode = $self->{cgi}->param('mode') || '(unknown)'; | ||
| 137 | my $address; | ||
| 138 | |||
| 139 | if ($mode ne '(unknown)') { | ||
| 140 | $address = $self->{modes}{$mode}; | ||
| 141 | } else { | ||
| 142 | $address = $self->{default_mode}; | ||
| 143 | } | ||
| 144 | &$address(); | ||
| 145 | } | ||
| 146 | |||
| 147 | sub throw_error { | ||
| 148 | my $self = shift; | ||
| 149 | my $message = shift || 'An unknown error has occurred.'; | ||
| 150 | print qq[<h2>Error:</h2>\n<p align=left>$message</p>]; | ||
| 151 | exit; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub write { | ||
| 155 | my $self = shift; | ||
| 156 | my $data = shift; | ||
| 157 | $self->{canvas} .= $data; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub paint { | ||
| 161 | my $self = shift; | ||
| 162 | my $title = shift; | ||
| 163 | my $canvas = $self->{canvas}; | ||
| 164 | my $toolbar = ' '; | ||
| 165 | my $html_header = '<!-- HTML HEADER -->'; | ||
| 166 | |||
| 167 | if ($self->{build_toolbar} ne '') { | ||
| 168 | my $toolbar_constructor = $self->{build_toolbar}; | ||
| 169 | $toolbar = &$toolbar_constructor(); | ||
| 170 | } | ||
| 171 | |||
| 172 | if ($self->{build_html_header} ne '') { | ||
| 173 | my $html_header_constructor = $self->{build_html_header}; | ||
| 174 | $html_header = &$html_header_constructor(); | ||
| 175 | } | ||
| 176 | |||
| 177 | print qq[ | ||
| 178 | <html> | ||
| 179 | <head> | ||
| 180 | <title>$title</title> | ||
| 181 | $html_header | ||
| 182 | </head> | ||
| 183 | <body> | ||
| 184 | <table border=0 width=100% cellspacing=0 cellpadding=2> | ||
| 185 | <tr> | ||
| 186 | <td valign=top align=left style="border-bottom: 3px double #000;">$title</td> | ||
| 187 | <td valign=top align=right style="border-bottom: 3px double #000;">$toolbar</td> | ||
| 188 | </tr> | ||
| 189 | </table> | ||
| 190 | $canvas | ||
| 191 | </body> | ||
| 192 | </html> | ||
| 193 | ]; | ||
| 194 | } | ||
| 195 | |||
| 196 | ########################################################################################################################## | ||
| 197 | # Private Methods | ||
| 198 | |||
| 199 | 1; |