1 |
package App::RoomReservation; |
2 |
use Moose; |
3 |
|
4 |
extends 'Frey'; |
5 |
with 'Frey::Web'; |
6 |
|
7 |
use lib 'lib'; |
8 |
use App::RoomReservation::Room; |
9 |
|
10 |
sub room { |
11 |
App::RoomReservation::Room->new; |
12 |
} |
13 |
|
14 |
sub form_header { |
15 |
my $self = shift; |
16 |
$self->room->description; |
17 |
} |
18 |
|
19 |
sub form_footer { |
20 |
qq| |
21 |
<div style="font-size:75%; color: #888; clear: left;"> |
22 |
Insert some standard disclamer for every page here |
23 |
</div> |
24 |
|; |
25 |
} |
26 |
|
27 |
sub dsn { 'DBI:Pg:dbname=room-reservation' } |
28 |
|
29 |
=head1 DEPLOY |
30 |
|
31 |
createdb --encoding=utf-8 room-reservation |
32 |
|
33 |
perl -Ilib -MFrey::Class::Schematize \ |
34 |
-e 'print Frey::Class::Schematize->new( class => "App::RoomReservation::Reservation" )->create_table,$/;' \ |
35 |
| psql room-reservation |
36 |
|
37 |
=cut |
38 |
|
39 |
our $dbh; |
40 |
sub dbh { |
41 |
my ($self) = @_; |
42 |
|
43 |
return $dbh if defined $dbh; |
44 |
|
45 |
$dbh = DBI->connect( $self->dsn, '', '', { RaiseError => 1 } ) || die $DBI::errstr; |
46 |
$dbh->do( qq{ set client_encoding='utf-8' } ) if $self->dsn =~ m{pg}i; |
47 |
|
48 |
return $dbh; |
49 |
} |
50 |
|
51 |
sub as_markup { |
52 |
my ($self) = @_; |
53 |
|
54 |
|
55 |
my $url = $self->url_for( 'Reservation/create_as_markup' ); |
56 |
|
57 |
return |
58 |
$self->room->room_as_markup |
59 |
. qq|<a target="App::RoomReservation::Reservation" href="$url">seat reservation</a>| |
60 |
; |
61 |
} |
62 |
|
63 |
# my $url = $self->uri_for( 'Reservation/create_as_makrup' ); |
64 |
|
65 |
sub url_mapping {{ |
66 |
"App::RoomReservation/as_markup" => 'zimbardo', |
67 |
"App::RoomReservation::Reservation/create_as_markup" => 'zimbardo/create', |
68 |
"App::RoomReservation::Confirmation/verify_as_markup?token=" => 'zimbardo/verify/', |
69 |
"App::RoomReservation::Confirmation/cancel_as_markup?token=" => 'zimbardo/cancel/', |
70 |
}} |
71 |
|
72 |
our $urls; |
73 |
use Data::Dump qw/dump/; |
74 |
|
75 |
sub url_for { |
76 |
my $self = shift; |
77 |
my $to = shift; |
78 |
my $args = join('', @_); |
79 |
my $app = ref($self); |
80 |
$app =~ s{^(App::[^:]+)::.+$}{$1}; |
81 |
$app .= '::' . $to; |
82 |
|
83 |
my ( $class, $method ) = split(m{/}, $app); |
84 |
$method =~ s{\?.+$}{}; # remove arguments |
85 |
Class::MOP::load_class( $class ); |
86 |
die "$class doesn't implement $method" unless $class->meta->has_method($method); |
87 |
|
88 |
$urls->{$app}++; |
89 |
warn "XXX urls = ",dump($urls); |
90 |
|
91 |
my $map_to = url_mapping->{$app}; |
92 |
$app = $map_to if $map_to; |
93 |
|
94 |
return |
95 |
$self->request_url->scheme . '://' . $self->request_url->authority . '/' . $app . $args; |
96 |
} |
97 |
|
98 |
__PACKAGE__->meta->make_immutable; |
99 |
no Moose; |
100 |
|
101 |
1; |