2 # Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
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.
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
13 # GNU General Public License for more details.
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
19 package PciIds::Html::Util;
25 use Apache2::Const qw(:common :http);
28 our @EXPORT = qw(&genHtmlHead &htmlDiv &genHtmlTail &genTableHead &genTableTail &parseArgs &buildExcept &buildArgs &genMenu &genCustomMenu &encode &setAddrPrefix &HTTPRedirect &genPath &logItem &genLocMenu &genCustomHead &genPathBare &protoName &genHtmlFooter);
31 return encode_entities( shift, "\"'&<>" );
35 my( $hasSSL ) = ( @_ );
36 return 'http' . ( 's' x $hasSSL );
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";
53 print '</body></html>';
56 sub genHtmlFooter( $$$ ) {
57 my( $jump, $req, $args ) = @_;
59 require PciIds::Html::Jump;
60 PciIds::Html::Jump::jumpWindow( $req, $args );
63 <div class="footer"><hr>
65 Maintained by <a href="http://mj.ucw.cz/">Martin Mares</a> and <a href="http://vorner.cz/">Michal
66 Vaner</a> with great help by volunteers from the <a href="http://www.sourceforge.net/projects/pciids/">pciids project</a>
67 at <a href="http://www.sourceforge.net/">SourceForge</a>.
68 <p class="maintain">No counters, no frames, no syntax errors.
74 my( $class, $text ) = @_;
75 return '<div class="'.$class.'">'.$text.'</div>';
79 my( $url, $label ) = @_;
80 print " <li><a href='".$url."'>$label</a>\n";
83 sub genCustomMenu( $$$$ ) {
84 my( $req, $address, $args, $list ) = @_;
86 if( defined $address ) {
87 $url = '/'.$address->get().buildExcept( 'action', $args ).'?action=';
93 my( $label, $action, $param ) = @{$_};
94 if( $action eq 'jump' ) {
96 require PciIds::Html::Jump;
97 PciIds::Html::Jump::jumpWindow( $req, $args );
100 $prefix = '/read' if( !defined( $action ) or ( $action eq 'list' ) or ( $action eq '' ) );
102 $suffix = '?help='.$param if( $action eq 'help' );
103 item( $prefix.$url.$action.$suffix, $label );
111 if( defined( $auth->{'authid'} ) ) {
112 return [ 'Log out ('.encode( $auth->{'name'} ).')', 'logout' ];
114 return [ 'Log in', 'login' ];
118 sub genMenu( $$$$$ ) {
119 my( $req, $address, $args, $auth, $append ) = @_;
121 if( defined $address ) {
122 push @list, [ 'Add item', 'newitem' ] if( $address->canAddItem() );
123 push @list, [ 'Discuss', 'newhistory' ] if( $address->canDiscuss() );
125 push @list, [ 'Administer', 'admin' ] if( hasRight( $auth->{'accrights'}, 'validate' ) );
126 push @list, @{$append} if defined $append;
128 print "<div class='lmenu'>\n";
129 genCustomMenu( $req, $address, $args, \@list );
132 @list = ( logItem( $auth ) );
133 push @list, [ 'Profile', 'profile' ] if defined $auth->{'authid'};
134 push @list, [ 'Notifications', 'notifications' ] if defined $auth->{'authid'};
135 print "<div class='rmenu'>\n";
136 genCustomMenu( $req, $address, $args, \@list );
140 sub genTableHead( $$$ ) {
141 my( $class, $captions, $cols ) = @_;
142 print '<table class="'.$class.'">';
143 foreach( @{$cols} ) {
144 print "<col class='$_'>\n";
147 foreach( @{$captions} ) {
148 print '<th>'.$_."\n";
158 foreach( split /\?/, shift ) {
160 my( $name, $value ) = /^([^=]+)=(.*)$/;
161 $result{$name} = $value;
169 foreach( keys %{$args} ) {
170 $result .= "?$_=".$args->{$_} if( defined $args->{$_} );
175 sub buildExcept( $$ ) {
176 my( $except, $args ) = @_;
177 my %backup = %{$args};
178 delete $backup{$except};
179 return buildArgs( \%backup );
182 sub setAddrPrefix( $$ ) {
183 my( $addr, $prefix ) = @_;
184 $addr =~ s/\/(mods|read|static)//;
185 return "/$prefix$addr";
188 sub HTTPRedirect( $$ ) {
189 my( $req, $link ) = @_;
190 $req->headers_out->add( 'Location' => $link );
191 return HTTP_SEE_OTHER;
194 sub genPathBare( $$$$$ ) {
195 my( $req, $address, $printAddr, $started, $tables ) = @_;
197 if( defined $address ) {
198 $path = $address->path();
202 foreach my $item ( reverse @{$path} ) {
203 my( $addr, $exception, $myAddr ) = @{$item};
213 print "(" if( $exception );
214 if( !$printAddr && $myAddr ) {
215 print "<strong>".encode( $addr->pretty() )."</strong>";
218 $title = ' title="'.encode( $tables->itemName( $addr->get() ) ).'"' if defined $tables;
219 print "<a href='/read/".$addr->get()."'$title>".encode( $addr->pretty() )."</a>";
221 print ")" if( $exception );
226 my( $req, $address, $printAddr ) = @_;
227 print "<div class='path'>\n";
228 print "<p><a href='/'>Main</a>";
229 genPathBare( $req, $address, $printAddr, 1, undef );
233 sub genLocMenu( $$$$$ ) {
234 my( $req, $args, $addr, $lactions, $ractions ) = @_;
235 print "<div class='lmenu'>\n";
236 genCustomMenu( $req, $addr, $args, $lactions );
237 print "</div>\n<div class='rmenu'>\n";
238 genCustomMenu( $req, $addr, $args, $ractions );
242 sub genCustomHead( $$$$$$ ) {
243 my( $req, $args, $addr, $caption, $lactions, $ractions ) = @_;
244 print "<div class='top'>\n";
245 genLocMenu( $req, $args, $addr, $lactions, $ractions );
246 print "<div class='bluesquare'><h1>$caption</h1><p class='home'>The PCI ID Repository</div>\n";
247 print "<div class='clear'></div></div>\n";
248 genPath( $req, $addr, 1 );