]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Util.pm
Removed references to SF.net
[pciids.git] / PciIds / Html / Util.pm
1 #       PciIds web database
2 #       Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
3 #
4 #       This program is free software; you can redistribute it and/or modify
5 #       it under the terms of the GNU General Public License as published by
6 #       he Free Software Foundation; either version 2 of the License, or
7 #       (at your option) any later version.
8 #
9 #       This program is distributed in the hope that it will be useful,
10 #       but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #
13 #       GNU General Public License for more details.
14 #
15 #       You should have received a copy of the GNU General Public License
16 #       along with this program; if not, write to the Free Software
17 #       Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19 package PciIds::Html::Util;
20 use strict;
21 use warnings;
22 use HTML::Entities;
23 use base 'Exporter';
24 use PciIds::Users;
25 use Apache2::Const qw(:common :http);
26 use APR::Table;
27
28 our @EXPORT = qw(&genHtmlHead &htmlDiv &genHtmlTail &genTableHead &genTableTail &parseArgs &buildExcept &buildArgs &genMenu &genCustomMenu &encode &setAddrPrefix &HTTPRedirect &genPath &logItem &genLocMenu &genCustomHead &genPathBare &protoName &genHtmlFooter);
29
30 sub encode( $ ) {
31         return encode_entities( shift, "\"'&<>" );
32 }
33
34 sub protoName( $ ) {
35         my( $hasSSL ) = ( @_ );
36         return 'http' . ( 's' x $hasSSL );
37 }
38
39 sub genHtmlHead( $$$ ) {
40         my( $req, $caption, $metas ) = @_;
41         $req->content_type( 'text/html; charset=utf-8' );
42         $req->headers_out->add( 'Cache-control' => 'no-cache' );
43         print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'."\n";
44         print '<html lang="en"><head><title>'.encode( $caption )."</title>\n";
45         print "<link rel='stylesheet' type='text/css' media='screen' href='/static/screen.css'>\n";
46         print "<link rel='stylesheet' type='text/css' media='print' href='/static/print.css'>\n";
47         print "<link rel='stylesheet' type='text/css' media='screen,print' href='/static/common.css'>\n";
48         print $metas if( defined( $metas ) );
49         print "</head><body>\n";
50 }
51
52 sub genHtmlTail() {
53         print '</body></html>';
54 }
55
56 sub genHtmlFooter( $$$ ) {
57         my( $jump, $req, $args ) = @_;
58         if( $jump ) {
59                 require PciIds::Html::Jump;
60                 PciIds::Html::Jump::jumpWindow( $req, $args );
61         }
62         print '
63 <div class="footer"><hr>
64 <p class="maintain">
65 See <a href="/">home page of the repository</a> for more information.
66 </div>';
67         genHtmlTail();
68 }
69
70 sub htmlDiv( $$ ) {
71         my( $class, $text ) = @_;
72         return '<div class="'.$class.'">'.$text.'</div>';
73 }
74
75 sub item( $$ ) {
76         my( $url, $label ) = @_;
77         print "  <li><a href='".$url."'>$label</a>\n";
78 }
79
80 sub genCustomMenu( $$$$ ) {
81         my( $req, $address, $args, $list ) = @_;
82         my $url;
83         if( defined $address ) {
84                 $url = '/'.$address->get().buildExcept( 'action', $args ).'?action=';
85         } else {
86                 $url = '/?action=';
87         }
88         print "<ul>\n";
89         foreach( @{$list} ) {
90                 my( $label, $action, $param ) = @{$_};
91                 if( $action eq 'jump' ) {
92                         print "<li>\n";
93                         require PciIds::Html::Jump;
94                         PciIds::Html::Jump::jumpWindow( $req, $args );
95                 } else {
96                         my $prefix = '/mods';
97                         $prefix = '/read' if( !defined( $action ) or ( $action eq 'list' ) or ( $action eq '' ) );
98                         my $suffix = '';
99                         $suffix = '?help='.$param if( $action eq 'help' );
100                         item( $prefix.$url.$action.$suffix, $label );
101                 }
102         }
103         print "</ul>\n";
104 }
105
106 sub logItem( $ ) {
107         my( $auth ) = @_;
108         if( defined( $auth->{'authid'} ) ) {
109                 return [ 'Log out ('.encode( $auth->{'name'} ).')', 'logout' ];
110         } else {
111                 return [ 'Log in', 'login' ];
112         }
113 }
114
115 sub genMenu( $$$$$ ) {
116         my( $req, $address, $args, $auth, $append ) = @_;
117         my @list;
118         if( defined $address ) {
119                 push @list, [ 'Add item', 'newitem' ] if( $address->canAddItem() );
120                 push @list, [ 'Discuss', 'newhistory' ] if( $address->canDiscuss() );
121         }
122         push @list, [ 'Administer', 'admin' ] if( hasRight( $auth->{'accrights'}, 'validate' ) );
123         push @list, @{$append} if defined $append;
124         if( @list ) {
125                 print "<div class='lmenu'>\n";
126                 genCustomMenu( $req, $address, $args, \@list );
127                 print "</div>\n";
128         }
129         @list = ( logItem( $auth ) );
130         push @list, [ 'Profile', 'profile' ] if defined $auth->{'authid'};
131         push @list, [ 'Notifications', 'notifications' ] if defined $auth->{'authid'};
132         print "<div class='rmenu'>\n";
133         genCustomMenu( $req, $address, $args, \@list );
134         print "</div>\n";
135 }
136
137 sub genTableHead( $$$ ) {
138         my( $class, $captions, $cols ) = @_;
139         print '<table class="'.$class.'">';
140         foreach( @{$cols} ) {
141                 print "<col class='$_'>\n";
142         }
143         print "<tr>\n";
144         foreach( @{$captions} ) {
145                 print '<th>'.$_."\n";
146         }
147 }
148
149 sub genTableTail() {
150         print '</table>';
151 }
152
153 sub parseArgs( $ ) {
154         my %result;
155         foreach( split /\?/, shift ) {
156                 next unless( /=/ );
157                 my( $name, $value ) = /^([^=]+)=(.*)$/;
158                 $result{$name} = $value;
159         }
160         return \%result;
161 }
162
163 sub buildArgs( $ ) {
164         my( $args ) = @_;
165         my $result = '';
166         foreach( keys %{$args} ) {
167                 $result .= "?$_=".$args->{$_} if( defined $args->{$_} );
168         }
169         return $result;
170 }
171
172 sub buildExcept( $$ ) {
173         my( $except, $args ) = @_;
174         my %backup = %{$args};
175         delete $backup{$except};
176         return buildArgs( \%backup );
177 }
178
179 sub setAddrPrefix( $$ ) {
180         my( $addr, $prefix ) = @_;
181         $addr =~ s/\/(mods|read|static)//;
182         return "/$prefix$addr";
183 }
184
185 sub HTTPRedirect( $$ ) {
186         my( $req, $link ) = @_;
187         $req->headers_out->add( 'Location' => $link );
188         return HTTP_SEE_OTHER;
189 }
190
191 sub genPathBare( $$$$$ ) {
192         my( $req, $address, $printAddr, $started, $tables ) = @_;
193         my $path;
194         if( defined $address ) {
195                 $path = $address->path();
196         } else {
197                 $path = [];
198         }
199         foreach my $item ( reverse @{$path} ) {
200                 my( $addr, $exception, $myAddr ) = @{$item};
201                 if( $started ) {
202                         if( $exception ) {
203                                 print ", ";
204                         } else {
205                                 print " -&gt; ";
206                         }
207                 } else {
208                         $started = 1;
209                 }
210                 print "(" if( $exception );
211                 if( !$printAddr && $myAddr ) {
212                         print "<strong>".encode( $addr->pretty() )."</strong>";
213                 } else {
214                         my $title = '';
215                         $title = ' title="'.encode( $tables->itemName( $addr->get() ) ).'"' if defined $tables;
216                         print "<a href='/read/".$addr->get()."'$title>".encode( $addr->pretty() )."</a>";
217                 }
218                 print ")" if( $exception );
219         }
220 }
221
222 sub genPath( $$$ ) {
223         my( $req, $address, $printAddr ) = @_;
224         print "<div class='path'>\n";
225         print "<p><a href='/'>Main</a>";
226         genPathBare( $req, $address, $printAddr, 1, undef );
227         print "</div>\n";
228 }
229
230 sub genLocMenu( $$$$$ ) {
231         my( $req, $args, $addr, $lactions, $ractions ) = @_;
232         print "<div class='lmenu'>\n";
233         genCustomMenu( $req, $addr, $args, $lactions );
234         print "</div>\n<div class='rmenu'>\n";
235         genCustomMenu( $req, $addr, $args, $ractions );
236         print "</div>\n";
237 }
238
239 sub genCustomHead( $$$$$$ ) {
240         my( $req, $args, $addr, $caption, $lactions, $ractions ) = @_;
241         print "<div class='top'>\n";
242         genLocMenu( $req, $args, $addr, $lactions, $ractions );
243         print "<div class='bluesquare'><h1>$caption</h1><p class='home'>The PCI ID Repository</div>\n";
244         print "<div class='clear'></div></div>\n";
245         genPath( $req, $addr, 1 );
246 }
247
248 1;