import torrus 1.0.9
[freeside.git] / torrus / perllib / Torrus / Renderer / Frontpage.pm
1 #  Copyright (C) 2002  Stanislav Sinyagin
2 #
3 #  This program is free software; you can redistribute it and/or modify
4 #  it under the terms of the GNU General Public License as published by
5 #  the Free Software Foundation; either version 2 of the License, or
6 #  (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software
15 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16
17 # $Id: Frontpage.pm,v 1.1 2010-12-27 00:03:44 ivan Exp $
18 # Stanislav Sinyagin <ssinyagin@yahoo.com>
19
20 package Torrus::Renderer::Frontpage;
21
22 use strict;
23
24 use Torrus::ConfigTree;
25 use Torrus::Search;
26 use Torrus::Log;
27
28 use Template;
29 use URI::Escape;
30
31 # All our methods are imported by Torrus::Renderer;
32
33 sub renderUserLogin
34 {
35     my $self = shift;
36     my %new_options = @_;
37
38     if( %new_options )
39     {
40         $self->{'options'} = \%new_options;
41     }
42
43     my($t_render, $t_expires, $filename, $mime_type);
44
45     my $cachekey = $self->cacheKey( 'LOGINSCREEN' );
46
47     ($t_render, $t_expires, $filename, $mime_type) =
48         $self->getCache( $cachekey );
49
50     # We don't check the expiration time for login screen
51     if( not defined( $filename ) )
52     {
53         $filename = Torrus::Renderer::newCacheFileName( $cachekey );
54     }
55
56     my $outfile = $Torrus::Global::cacheDir.'/'.$filename;
57
58     $t_expires = time();
59     $mime_type = $Torrus::Renderer::LoginScreen::mimeType;
60     my $tmplfile = $Torrus::Renderer::LoginScreen::template;
61
62     # Create the Template Toolkit processor once, and reuse
63     # it in subsequent render() calls
64
65     if( not defined( $self->{'tt'} ) )
66     {
67         $self->{'tt'} =
68             new Template(INCLUDE_PATH => $Torrus::Global::templateDirs,
69                          TRIM => 1);
70     }
71
72     my $url = $Torrus::Renderer::rendererURL;
73     if( length( $self->{'options'}->{'urlPassTree'} ) > 0 )
74     {
75         $url .= '/' . $self->{'options'}->{'urlPassTree'};
76     }
77     
78     my $ttvars =
79     {
80         'url'        => $url,
81         'plainURL'   => $Torrus::Renderer::plainURL,
82         'style'      => sub { return $self->style($_[0]); },
83         'companyName'=> $Torrus::Renderer::companyName,
84         'companyLogo'=> $Torrus::Renderer::companyLogo,
85         'companyURL' => $Torrus::Renderer::companyURL,
86         'lostPasswordURL' => $Torrus::Renderer::lostPasswordURL,
87         'siteInfo'   => $Torrus::Renderer::siteInfo,
88         'version'    => $Torrus::Global::version,
89         'xmlnorm'    => \&Torrus::Renderer::xmlnormalize
90         };
91
92
93     # Pass the options from Torrus::Renderer::render() to Template
94     while( my( $opt, $val ) = each( %{$self->{'options'}} ) )
95     {
96         $ttvars->{$opt} = $val;
97     }
98
99     my $result = $self->{'tt'}->process( $tmplfile, $ttvars, $outfile );
100
101     undef $ttvars;
102
103     my @ret;
104     if( not $result )
105     {
106         Error("Error while rendering login screen: " .
107               $self->{'tt'}->error());
108     }
109     else
110     {
111         $self->setCache($cachekey, time(), $t_expires, $filename, $mime_type);
112         @ret = ($outfile, $mime_type, $t_expires - time());
113     }
114
115     $self->{'options'} = undef;   
116
117     return @ret;
118 }
119
120
121 sub renderTreeChooser
122 {
123     my $self = shift;
124     my %new_options = @_;
125
126     if( %new_options )
127     {
128         $self->{'options'} = \%new_options;
129     }
130
131     my($t_render, $t_expires, $filename, $mime_type);
132
133     my $uid = '';
134     if( $self->{'options'}->{'uid'} )
135     {
136         $uid = $self->{'options'}->{'uid'};
137     }
138
139     my $cachekey = $self->cacheKey( $uid . ':' . 'TREECHOOSER' );
140
141     ($t_render, $t_expires, $filename, $mime_type) =
142         $self->getCache( $cachekey );
143
144     if( defined( $filename ) )
145     {
146         if( $t_expires >= time() )
147         {
148             return ($Torrus::Global::cacheDir.'/'.$filename,
149                     $mime_type, $t_expires - time());
150         }
151         # Else reuse the old filename
152     }
153     else
154     {
155         $filename = Torrus::Renderer::newCacheFileName( $cachekey );
156     }
157
158     my $outfile = $Torrus::Global::cacheDir.'/'.$filename;
159
160     $t_expires = time() + $Torrus::Renderer::Chooser::expires;
161     $mime_type = $Torrus::Renderer::Chooser::mimeType;
162     
163     my $tmplfile;
164     if( defined( $self->{'options'}{'variables'}{'SEARCH'} ) and
165         $self->mayGlobalSearch() )
166     {
167         $tmplfile = $Torrus::Renderer::Chooser::searchTemplate;
168     }
169     else
170     {
171         $tmplfile = $Torrus::Renderer::Chooser::template;
172     }
173
174     # Create the Template Toolkit processor once, and reuse
175     # it in subsequent render() calls
176
177     if( not defined( $self->{'tt'} ) )
178     {
179         $self->{'tt'} =
180             new Template(INCLUDE_PATH => $Torrus::Global::templateDirs,
181                          TRIM => 1);
182     }
183
184     my $ttvars =
185     {
186         'treeNames' => sub{ return Torrus::SiteConfig::listTreeNames() },
187         'treeDescr' => sub{ return
188                                 Torrus::SiteConfig::treeDescription($_[0]) }
189         ,
190         'url'  => sub { return $Torrus::Renderer::rendererURL . '/' . $_[0] },
191         'plainURL'   => $Torrus::Renderer::plainURL,
192         'persistentUrl'  => sub { return $Torrus::Renderer::rendererURL . '/' .
193                                   $_[0] . '?path=' . uri_escape($_[1])}
194         ,
195         'clearVar'   => sub { delete $self->{'options'}{'variables'}{$_[0]};
196                               return undef;},
197         'style'      => sub { return $self->style($_[0]); },
198         'companyName'=> $Torrus::Renderer::companyName,
199         'companyLogo'=> $Torrus::Renderer::companyLogo,
200         'companyURL' => $Torrus::Renderer::companyURL,
201         'siteInfo'   => $Torrus::Renderer::siteInfo,
202         'version'    => $Torrus::Global::version,
203         'xmlnorm'    => \&Torrus::Renderer::xmlnormalize,
204         'userAuth'   => $Torrus::CGI::authorizeUsers,
205         'uid'        => $self->{'options'}->{'uid'},
206         'userAttr'   => sub { return $self->userAttribute( $_[0] ) },
207         'mayDisplayTree' => sub { return $self->
208                                       hasPrivilege( $_[0], 'DisplayTree' ) }
209         ,
210         'mayGlobalSearch' => sub { return $self->mayGlobalSearch(); },        
211         'searchResults'   => sub { return $self->doGlobalSearch($_[0]); }
212     };
213
214
215     # Pass the options from Torrus::Renderer::render() to Template
216     while( my( $opt, $val ) = each( %{$self->{'options'}} ) )
217     {
218         $ttvars->{$opt} = $val;
219     }
220
221     my $result = $self->{'tt'}->process( $tmplfile, $ttvars, $outfile );
222
223     undef $ttvars;
224
225     my @ret;
226     if( not $result )
227     {
228         Error("Error while rendering tree chooser: " .
229               $self->{'tt'}->error());
230     }
231     else
232     {
233         $self->setCache($cachekey, time(), $t_expires, $filename, $mime_type);
234         @ret = ($outfile, $mime_type, $t_expires - time());
235     }
236
237     $self->{'options'} = undef;   
238
239     return @ret;
240 }
241
242
243 sub mayGlobalSearch
244 {
245     my $self = shift;
246     
247     return ( $Torrus::Renderer::globalSearchEnabled and
248              ( not $Torrus::CGI::authorizeUsers or
249                ( $self->hasPrivilege( '*', 'GlobalSearch' ) ) ) );
250 }
251
252 sub doGlobalSearch
253 {
254     my $self = shift;
255     my $string = shift;
256     
257     my $sr = new Torrus::Search;
258     $sr->openGlobal();
259     my $result = $sr->searchPrefix( $string );
260
261     my $sorted = [];
262     push( @{$sorted}, sort {$a->[0] cmp $b->[0]} @{$result} );
263
264     # remove duplicating entries
265     my %seen;
266     my $ret = [];
267     
268     foreach my $element ( @{$sorted} )
269     {
270         my $string = join( ':', $element->[0], $element->[1] );
271         if( not $seen{$string} )
272         {
273             $seen{$string} = 1;
274             push( @{$ret}, $element );
275         }
276     }
277         
278     return $ret;
279 }
280
281
282
283
284 1;
285
286
287 # Local Variables:
288 # mode: perl
289 # indent-tabs-mode: nil
290 # perl-indent-level: 4
291 # End: