1 # Copyright (C) 2002 Stanislav Sinyagin
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.
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.
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.
17 # $Id: Frontpage.pm,v 1.2 2010-12-27 08:40:19 ivan Exp $
18 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20 package Torrus::Renderer::Frontpage;
24 use Torrus::ConfigTree;
31 # All our methods are imported by Torrus::Renderer;
40 $self->{'options'} = \%new_options;
43 my($t_render, $t_expires, $filename, $mime_type);
45 my $cachekey = $self->cacheKey( 'LOGINSCREEN' );
47 ($t_render, $t_expires, $filename, $mime_type) =
48 $self->getCache( $cachekey );
50 # We don't check the expiration time for login screen
51 if( not defined( $filename ) )
53 $filename = Torrus::Renderer::newCacheFileName( $cachekey );
56 my $outfile = $Torrus::Global::cacheDir.'/'.$filename;
59 $mime_type = $Torrus::Renderer::LoginScreen::mimeType;
60 my $tmplfile = $Torrus::Renderer::LoginScreen::template;
62 # Create the Template Toolkit processor once, and reuse
63 # it in subsequent render() calls
65 if( not defined( $self->{'tt'} ) )
68 new Template(INCLUDE_PATH => $Torrus::Global::templateDirs,
72 my $url = $Torrus::Renderer::rendererURL;
73 if( length( $self->{'options'}->{'urlPassTree'} ) > 0 )
75 $url .= '/' . $self->{'options'}->{'urlPassTree'};
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
93 # Pass the options from Torrus::Renderer::render() to Template
94 while( my( $opt, $val ) = each( %{$self->{'options'}} ) )
96 $ttvars->{$opt} = $val;
99 my $result = $self->{'tt'}->process( $tmplfile, $ttvars, $outfile );
106 Error("Error while rendering login screen: " .
107 $self->{'tt'}->error());
111 $self->setCache($cachekey, time(), $t_expires, $filename, $mime_type);
112 @ret = ($outfile, $mime_type, $t_expires - time());
115 $self->{'options'} = undef;
121 sub renderTreeChooser
124 my %new_options = @_;
128 $self->{'options'} = \%new_options;
131 my($t_render, $t_expires, $filename, $mime_type);
134 if( $self->{'options'}->{'uid'} )
136 $uid = $self->{'options'}->{'uid'};
139 my $cachekey = $self->cacheKey( $uid . ':' . 'TREECHOOSER' );
141 ($t_render, $t_expires, $filename, $mime_type) =
142 $self->getCache( $cachekey );
144 if( defined( $filename ) )
146 if( $t_expires >= time() )
148 return ($Torrus::Global::cacheDir.'/'.$filename,
149 $mime_type, $t_expires - time());
151 # Else reuse the old filename
155 $filename = Torrus::Renderer::newCacheFileName( $cachekey );
158 my $outfile = $Torrus::Global::cacheDir.'/'.$filename;
160 $t_expires = time() + $Torrus::Renderer::Chooser::expires;
161 $mime_type = $Torrus::Renderer::Chooser::mimeType;
164 if( defined( $self->{'options'}{'variables'}{'SEARCH'} ) and
165 $self->mayGlobalSearch() )
167 $tmplfile = $Torrus::Renderer::Chooser::searchTemplate;
171 $tmplfile = $Torrus::Renderer::Chooser::template;
174 # Create the Template Toolkit processor once, and reuse
175 # it in subsequent render() calls
177 if( not defined( $self->{'tt'} ) )
180 new Template(INCLUDE_PATH => $Torrus::Global::templateDirs,
186 'treeNames' => sub{ return Torrus::SiteConfig::listTreeNames() },
187 'treeDescr' => sub{ return
188 Torrus::SiteConfig::treeDescription($_[0]) }
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])}
195 'clearVar' => sub { delete $self->{'options'}{'variables'}{$_[0]};
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' ) }
210 'mayGlobalSearch' => sub { return $self->mayGlobalSearch(); },
211 'searchResults' => sub { return $self->doGlobalSearch($_[0]); },
214 'freesideHeader' => sub { return $self->freesideHeader(@_); },
215 'freesideFooter' => sub { return $self->freesideFooter(); },
219 # Pass the options from Torrus::Renderer::render() to Template
220 while( my( $opt, $val ) = each( %{$self->{'options'}} ) )
222 $ttvars->{$opt} = $val;
225 my $result = $self->{'tt'}->process( $tmplfile, $ttvars, $outfile );
232 Error("Error while rendering tree chooser: " .
233 $self->{'tt'}->error());
237 $self->setCache($cachekey, time(), $t_expires, $filename, $mime_type);
238 @ret = ($outfile, $mime_type, $t_expires - time());
241 $self->{'options'} = undef;
251 return ( $Torrus::Renderer::globalSearchEnabled and
252 ( not $Torrus::CGI::authorizeUsers or
253 ( $self->hasPrivilege( '*', 'GlobalSearch' ) ) ) );
261 my $sr = new Torrus::Search;
263 my $result = $sr->searchPrefix( $string );
266 push( @{$sorted}, sort {$a->[0] cmp $b->[0]} @{$result} );
268 # remove duplicating entries
272 foreach my $element ( @{$sorted} )
274 my $string = join( ':', $element->[0], $element->[1] );
275 if( not $seen{$string} )
278 push( @{$ret}, $element );
293 # indent-tabs-mode: nil
294 # perl-indent-level: 4