updates
[torrus-plus.git] / src / lib / Torrus / Renderer / Frontpage.pm
blob52e0a6a09f905cf2420d26a5c850c665f443c8da
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 # Stanislav Sinyagin <ssinyagin@yahoo.com>
19 package Torrus::Renderer::Frontpage;
20 use strict;
21 use warnings;
23 use Torrus::ConfigTree;
24 use Torrus::Search;
25 use Torrus::Log;
27 use URI::Escape qw( uri_escape );
28 use File::Spec;
30 our $VERSION = 1.0;
32 # All our methods are imported by Torrus::Renderer;
34 sub renderUserLogin
36 my $self = shift;
37 my %new_options = @_;
38 my $cache = $self->{cache};
40 $self->{'options'} = \%new_options
41 if( %new_options );
43 my($t_render, $t_expires, $filename, $mime_type);
45 my $cachekey = 'LOGINSCREEN';
47 ($t_render, $t_expires, $filename, $mime_type) =
48 $cache->getCache( $cachekey, $self->{options} );
50 $t_expires = time();
51 $mime_type = $Torrus::Renderer::LoginScreen::mimeType;
52 my $tmplfile = $Torrus::Renderer::LoginScreen::template;
54 my $url = $Torrus::Renderer::rendererURL;
55 if( length( $self->{'options'}->{'urlPassTree'} ) > 0 )
57 $url .= '/' . $self->{'options'}->{'urlPassTree'};
60 my $ttvars =
62 'url' => $url,
63 'plainURL' => $Torrus::Renderer::plainURL,
64 'style' => sub { return $self->style($_[0]); },
65 'companyName'=> $Torrus::Renderer::companyName,
66 'companyLogo'=> $Torrus::Renderer::companyLogo,
67 'companyURL' => $Torrus::Renderer::companyURL,
68 'lostPasswordURL' => $Torrus::Renderer::lostPasswordURL,
69 'siteInfo' => $Torrus::Renderer::siteInfo,
70 'version' => $Torrus::Global::version,
71 'xmlnorm' => \&Torrus::Renderer::xmlnormalize
75 # Pass the options from Torrus::Renderer::render() to Template
76 while( my( $opt, $val ) = each %{$self->{'options'}} )
78 $ttvars->{$opt} = $val;
81 my $content;
82 my $result = $self->{'tt'}->process( $tmplfile, $ttvars, \$content );
84 my @ret;
85 if( not $result )
87 Error('Error while rendering login screen: ' .
88 $self->{'tt'}->error());
90 else
92 $cache->setCache($cachekey, $self->{options}, time(), $t_expires, $content, $mime_type);
93 @ret = ($content, $mime_type, $t_expires - time());
96 $self->{'options'} = undef;
98 return @ret
103 sub renderTreeChooser
105 my $self = shift;
106 my %new_options = @_;
107 my $cache = $self->{cache};
109 $self->{'options'} = \%new_options
110 if( %new_options );
112 my($t_render, $t_expires, $content, $mime_type);
114 my $uid = $self->{'options'}->{'uid'} || '';
116 my $cachekey = $uid . ':' . 'TREECHOOSER';
118 ($t_render, $t_expires, $content, $mime_type) =
119 $cache->getCache( $cachekey, $self->{options} );
121 if( defined( $content ) )
123 if( $t_expires >= time() )
125 return ($content,$mime_type, $t_expires - time());
127 # Else reuse the old filename
130 $t_expires = time() + $Torrus::Renderer::Chooser::expires;
131 $mime_type = $Torrus::Renderer::Chooser::mimeType;
133 my $tmplfile;
134 if( defined( $self->{'options'}{'variables'}{'SEARCH'} ) and
135 $self->mayGlobalSearch() )
137 $tmplfile = $Torrus::Renderer::Chooser::searchTemplate;
139 else
141 $tmplfile = $Torrus::Renderer::Chooser::template;
144 my $ttvars =
146 'treeNames' => sub{ return Torrus::SiteConfig::listTreeNames() },
147 'treeDescr' => sub{ return
148 Torrus::SiteConfig::treeDescription($_[0]) }
150 'url' => sub { return $Torrus::Renderer::rendererURL . '/' . $_[0] },
151 'pathUrl' => sub { return $Torrus::Renderer::rendererURL . '/' .
152 $_[0] . '?path=' . $_[1] },
153 'plainURL' => $Torrus::Renderer::plainURL,
154 'clearVar' => sub { delete $self->{'options'}{'variables'}{$_[0]};
155 return},
156 'style' => sub { return $self->style($_[0]); },
157 'companyName'=> $Torrus::Renderer::companyName,
158 'companyLogo'=> $Torrus::Renderer::companyLogo,
159 'companyURL' => $Torrus::Renderer::companyURL,
160 'siteInfo' => $Torrus::Renderer::siteInfo,
161 'version' => $Torrus::Global::version,
162 'xmlnorm' => \&Torrus::Renderer::xmlnormalize,
163 'userAuth' => $Torrus::CGI::authorizeUsers,
164 'uid' => $self->{'options'}->{'uid'},
165 'userAttr' => sub { return $self->userAttribute( $_[0] ) },
166 'mayDisplayTree' => sub { return $self->
167 hasPrivilege( $_[0], 'DisplayTree' ) }
169 'mayGlobalSearch' => sub { return $self->mayGlobalSearch(); },
170 'searchResults' => sub { return $self->doGlobalSearch($_[0]); }
174 # Pass the options from Torrus::Renderer::render() to Template
175 while( my( $opt, $val ) = each( %{$self->{'options'}} ) )
177 $ttvars->{$opt} = $val;
180 my $result = $self->{'tt'}->process( $tmplfile, $ttvars, \$content );
182 my @ret;
183 if( not $result )
185 Error('Error while rendering tree chooser: ' .
186 $self->{'tt'}->error());
188 else
190 $cache->setCache($cachekey, $self->{options}, time(), $t_expires, $content, $mime_type);
191 @ret = ($content, $mime_type, $t_expires - time());
194 $self->{'options'} = undef;
196 return @ret
200 sub mayGlobalSearch
202 my $self = shift;
204 return ( $Torrus::Renderer::globalSearchEnabled and
205 ( not $Torrus::CGI::authorizeUsers or
206 ( $self->hasPrivilege( '*', 'GlobalSearch' ) ) ) )
209 =head2 doGlobalSearch
211 Takes two arguments: I<$self> and I<$string>, the later being the search
212 string. This $string is then searched for and returns an array reference
213 of results.
215 =cut
217 sub doGlobalSearch
219 my $self = shift;
220 my $string = shift;
222 my $sr = Torrus::Search->new();
223 $sr->openGlobal();
224 my $result = $sr->searchPrefix( $string );
226 my $sorted = [];
227 push( @{$sorted}, sort {$a->[0] cmp $b->[0]} @{$result} );
229 # remove duplicating entries
230 my %seen;
231 my $ret = [];
233 for my $element ( @{$sorted} )
235 my $str = join( ':', $element->[0], $element->[1] );
236 if( not $seen{$str} )
238 $seen{$str} = 1;
239 push( @{$ret}, $element );
243 return $ret
253 # Local Variables:
254 # mode: perl
255 # indent-tabs-mode: nil
256 # perl-indent-level: 4
257 # End: