OSDN Git Service

4d549fea207de0dd5a5f6ace99c820c709529a2b
[keitairc/keitairc.git] / lib / Keitairc / View.pm
1 # -*-perl-*-
2 # Keitairc::View
3 # $Id: View.pm,v 1.8 2008-02-26 00:41:09 morimoto Exp $
4 # $Source: /home/ishikawa/work/keitairc/tmp/keitairc/lib/Keitairc/View.pm,v $
5 #
6 # Copyright (c) 2008 Jun Morimoto <morimoto@mrmt.net>
7 # This program is covered by the GNU General Public License 2
8
9 package Keitairc::View;
10 use strict;
11 use HTML::Template;
12 use HTTP::Response;
13 use Keitairc::Config;
14 use Keitairc::ClientInfo;
15
16 ################################################################
17 sub new{
18         my $proto = shift;
19         my $cf = shift;
20         my $ci = shift;
21         my $session_id = shift;
22         my $me = {};
23         $me->{Config} = $cf;
24         $me->{ClientInfo} = $ci;
25         if(defined $session_id){
26                 $me->{session_id} = $session_id;
27         }
28         bless $me;
29 }
30
31 ################################################################
32 sub redirect{
33         my $me = shift;
34         my $path = shift;
35
36         if($me->{Config}->web_root() eq '/'){
37                 $path = sprintf('http://%s:%d%s%s',
38                                 $me->{Config}->web_host(),
39                                 $me->{Config}->web_port(),
40                                 $path);
41         }else{
42                 # Softbank
43                 $path =~ s|^/||;
44                 $path = sprintf('http://%s%s%s',
45                                 $me->{Config}->web_host(),
46                                 $me->{Config}->web_root(),
47                                 $path);
48         }
49
50         ::log_debug("path [$path]");
51
52         my $response = HTTP::Response->new(302);
53         $response->push_header('Location', $path);
54         $response;
55 }
56
57 ################################################################
58 sub template_path{
59         my $me = shift;
60         my $template = shift;
61         for my $template_dir (split(':', $me->{Config}->template_dir())){
62                 if(-r "$template_dir/$template"){
63                         return "$template_dir/$template";
64                 }
65         }
66 }
67
68 ################################################################
69 sub template{
70         my $me = shift;
71         my $template = shift;
72         my $vars = shift;
73
74         my $p = {};
75         $p->{root} = $me->{Config}->web_root();
76         $p->{title} = $me->{Config}->web_title();
77         $p->{version} = $me->{Config}->version();
78         $p->{timestamp} = timestamp();
79
80         $p->{viewport} = $me->{ClientInfo}->is_ipod();
81         $p->{is_ipod} = $me->{ClientInfo}->is_ipod();
82         $p->{is_docomo} = $me->{ClientInfo}->is_docomo();
83         $p->{is_ezweb} = $me->{ClientInfo}->is_ezweb();
84         $p->{is_softbank} = $me->{ClientInfo}->is_softbank();
85
86         map {
87                 $p->{"plugin_$_"} = 1;
88         } $::pl->list_plugins();
89
90         my $t = new HTML::Template(
91                 filename => $me->template_path($template),
92                 die_on_bad_params => 0,
93                 cache => 1);
94         $t->param($p);
95
96         if(defined $vars){
97                 $t->param($vars);
98         }
99
100         $t->output();
101 }
102
103 ################################################################
104 sub render{
105         my $me = shift;
106         my $template = shift;
107         my $vars = shift;
108         my $response = HTTP::Response->new(200);
109         $vars->{content_type} |= 'text/html; charset=Shift_JIS';
110         $response->push_header('Content-type', $vars->{content_type});
111         $response->content($me->template($template, $vars));
112         $response;
113 }
114
115 ################################################################
116 sub timestamp{
117         my ($sec, $min, $hour, $day, $mon, $year) = localtime(time);
118         sprintf('%d/%d/%d %02d:%02d:%02d',
119                 $year+1900, $mon+1, $day, $hour, $min, $sec);
120 }
121
122 1;