summaryrefslogtreecommitdiffstats
path: root/lib/Jaos/WebApp/Controller.pm
blob: d16abeffa6b70fe536bf56c0065aa80f8687d1eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package Jaos::WebApp::Controller;
use strict;
use warnings;

sub new
{
  my ($class, $app) = @_;
  my $self  = {
    app   => $app,
  };

  bless $self, $class;
}

sub app { shift->{app}; }
sub stash { shift->{app}->stash; }
sub session { shift->{app}->stash->{session}; }
sub param { shift->{app}->req->param(@_); }
sub upload { shift->{app}->req->upload(@_); }

sub url_for
{
  my($self, $path, $args) = @_;

  my $uri = $self->{app}->req->base;
  $uri->path($uri->path . $path);
  $uri->query_form(@$args) if $args;

  $uri;
}

sub render
{
  my $self      = shift;
  my $handler   = shift;

  if (my $handler = $self->{app}->renderer($handler)) {
    $handler->($self, @_);
  } else {
    my ($package, $filename, $line) = caller;
    $self->app->log->error("No handler found ($package) $filename:$line");
  }
}

sub redirect_to
{
  my $self = shift;
  my $uri = $self->url_for(@_);
  $self->app->log->debug("Redirecting to $uri");
  $self->{app}->res->redirect($uri); 
}

sub redirect_external
{
  my ($self, $destination) = @_;
  $self->app->log->debug("Redirecting to $destination");
  $self->{app}->res->redirect($destination); 
}


sub encode_html
{
    my ($self, $str) = @_;
    $str =~ s/&/&/g;
    $str =~ s/>/>/g;
    $str =~ s/</&lt;/g;
    $str =~ s/"/&quot;/g;
    $str =~ s/'/&#39;/g;
    return $str;
}

sub decode_html
{
    my ($self, $str) = @_;
    $str =~ s/&amp;/&/g;
    $str =~ s/&gt;/>/g;
    $str =~ s/&lt;/</g;
    $str =~ s/&quot;/"/g;
    $str =~ s/&#39;/'/g;
    return $str;
}


=head1 NAME

Jaos::WebApp::Controller - Base class for Jaos::WebApp controllers

=head1 SYNOPSIS

 package MyApp::ControllerName;
 use base Jaos::WebApp::Controller;

 sub my_custom_method 
 {
   my $self = shift;
   my $name = $self->param('name') || $self->session->{name};
 
   if ($self->stash->{name} = $name) {
     $self->stash->{profile_url} = $self->url_for("/profile/$name");
     $self->render(tt => 'tmpl.tt');
   } else {
     $self->redirect_to('/login');
   }
 }

 1;

=head1 DESCRIPTION

This is the base class for Jaos::WebApp controllers.  It provides convenience methods for accessing the L<Plack::Request> and L<Plack::Response> objects, the main application object, the controller stash, and a render method for dispatching to a particular L<Jaos::WebApp::Render> handler.

=head1 METHODS

=head2 new

Constructor.  This is passed the application, request, and response objects.

=head2 app

Return the main application object.

=head2 stash

Return a hashref to the controller data storage stash.  

=head2 session

Shortcut to B<$controller->stash->{session}>.

=head2 param

Shortcut to B<$controller->req->param>.

=head2 url_for

Generates the full URL for a particular path.

=head2 render

Dispatch to the specified render type handler.  Each handler is registered with the main L<Jaos::WebApp> object through the plugin mechanism.

=head2 redirect_to

Redirects to the specified location.  url_for is called internally for generating the full path to the mounted location.  This is a slight wrapper around L<Plack::Response>->redirect().

=head2 redirect_external

Redirects to the specified external location.  This is a wrapper around L<Plack::Response>->redirect().

=head2 encode_html

=head2 decode_html

=head1 AUTHOR

Jason Woodward <woodwardj@jaos.org>

=head1 LICENSE

Copyright (C) 2010 Jason Woodward
All rights reserved

This library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this library.  If not, see <http://www.gnu.org/licenses/>.

=cut

1;