| 1 | # Object to handle data communication between domain logic and presentation | ||
| 2 | # it will stash the input in a hash that the presentation can retrieve | ||
| 3 | |||
| 4 | package RWDE::Web::Helper; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use vars qw($VERSION); | ||
| 10 | $VERSION = sprintf "%d", q$Revision: 509 $ =~ /(\d+)/; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my ($proto, $params) = @_; | ||
| 14 | |||
| 15 | my $class = ref($proto) || $proto; | ||
| 16 | |||
| 17 | my $self = { _STASH => {}, }; | ||
| 18 | |||
| 19 | bless $self, $class; | ||
| 20 | |||
| 21 | return $self; | ||
| 22 | } | ||
| 23 | |||
| 24 | =pod | ||
| 25 | |||
| 26 | =head2 FIELDNAME($field[,$value]) | ||
| 27 | |||
| 28 | All field names of the record are accessible via the field name. If a | ||
| 29 | second parameter is provided, that value is stored as the data, | ||
| 30 | otherwise the existing value if any is returned. Throws an 'undef' | ||
| 31 | exception on error. It is intended to be called by an F<AUTOLOAD()> | ||
| 32 | method from the subclass. | ||
| 33 | |||
| 34 | Example: | ||
| 35 | |||
| 36 | $rec->owner_email('new@add.ress'); | ||
| 37 | $rec->user_addr2(undef); | ||
| 38 | print $rec->user_fname(); | ||
| 39 | |||
| 40 | Would be converted by F<AUTOLOAD()> in the subclass to calls like | ||
| 41 | |||
| 42 | $rec->FIELDNAME('owner_email','new@add.ress'); | ||
| 43 | |||
| 44 | and so forth. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | sub FIELDNAME { | ||
| 49 | my $self = shift; | ||
| 50 | my $name = shift; | ||
| 51 | |||
| 52 | $name =~ s/.*://; # strip fully-qualified portion | ||
| 53 | |||
| 54 | unless (exists $self->{_STASH}->{$name}) { | ||
| 55 | my ($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask) = caller(1); | ||
| 56 | my $info = " for $package=>$subroutine"; | ||
| 57 | |||
| 58 | throw RWDE::DevelException({ info => "Sorry, RWDE::Web::Helper does not have '$name' in class $self $info" }); | ||
| 59 | } | ||
| 60 | |||
| 61 | return $self->{_STASH}->{$name}; | ||
| 62 | } | ||
| 63 | |||
| 64 | use vars qw($AUTOLOAD); | ||
| 65 | |||
| 66 | sub AUTOLOAD { | ||
| 67 | my ($self, @args) = @_; | ||
| 68 | if (not ref $self) { | ||
| 69 | my ($package, $filename, $line) = caller(); | ||
| 70 | throw RWDE::DevelException( | ||
| 71 | { info => "Record::AUTOLOAD invoked with the fieldname: $AUTOLOAD; probably static access to an undefined field/method from $filename Line: $line " . join(':', @args) . "\n" }); | ||
| 72 | } | ||
| 73 | |||
| 74 | return $self->FIELDNAME($AUTOLOAD, @args); | ||
| 75 | } | ||
| 76 | |||
| 77 | # do nothing. here just to shut up TT when AUTOLOAD is present | ||
| 78 | sub DESTROY { | ||
| 79 | |||
| 80 | } | ||
| 81 | |||
| 82 | sub get_stash { | ||
| 83 | my ($self, $params) = @_; | ||
| 84 | return $self->{_STASH}; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub get_session { | ||
| 88 | my ($self, $params) = @_; | ||
| 89 | return $self->{_STASH}->{s}; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub get_req { | ||
| 93 | my ($self, $params) = @_; | ||
| 94 | return $self->{_STASH}->{req}; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub get_uri { | ||
| 98 | my ($self, $params) = @_; | ||
| 99 | return $self->{_STASH}->{uri}; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub get_pagetype { | ||
| 103 | my ($self, $params) = @_; | ||
| 104 | |||
| 105 | $self->{_STASH}->{uri} =~ m/\.(\w+)$/; | ||
| 106 | |||
| 107 | return $1; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub get_formdata { | ||
| 111 | my ($self, $params) = @_; | ||
| 112 | return $self->{_STASH}->{formdata}; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub set_stash { | ||
| 116 | my ($self, $params) = @_; | ||
| 117 | foreach my $key (keys %{$params}) { | ||
| 118 | $self->{_STASH}->{$key} = $$params{$key}; | ||
| 119 | } | ||
| 120 | |||
| 121 | return (); | ||
| 122 | } | ||
| 123 | |||
| 124 | 1; |