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 See <a href="/">home page of the repository</a> for more information.
71 my( $class, $text ) = @_;
72 return '<div class="'.$class.'">'.$text.'</div>';
76 my( $url, $label ) = @_;
77 print " <li><a href='".$url."'>$label</a>\n";
80 sub genCustomMenu( $$$$ ) {
81 my( $req, $address, $args, $list ) = @_;
83 if( defined $address ) {
84 $url = '/'.$address->get().buildExcept( 'action', $args ).'?action=';
90 my( $label, $action, $param ) = @{$_};
91 if( $action eq 'jump' ) {
93 require PciIds::Html::Jump;
94 PciIds::Html::Jump::jumpWindow( $req, $args );
97 $prefix = '/read' if( !defined( $action ) or ( $action eq 'list' ) or ( $action eq '' ) );
99 $suffix = '?help='.$param if( $action eq 'help' );
100 item( $prefix.$url.$action.$suffix, $label );
108 if( defined( $auth->{'authid'} ) ) {
109 return [ 'Log out ('.encode( $auth->{'name'} ).')', 'logout' ];
111 return [ 'Log in', 'login' ];
115 sub genMenu( $$$$$ ) {
116 my( $req, $address, $args, $auth, $append ) = @_;
118 if( defined $address ) {
119 push @list, [ 'Add item', 'newitem' ] if( $address->canAddItem() );
120 push @list, [ 'Discuss', 'newhistory' ] if( $address->canDiscuss() );
122 push @list, [ 'Administer', 'admin' ] if( hasRight( $auth->{'accrights'}, 'validate' ) );
123 push @list, @{$append} if defined $append;
125 print "<div class='lmenu'>\n";
126 genCustomMenu( $req, $address, $args, \@list );
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 );
137 sub genTableHead( $$$ ) {
138 my( $class, $captions, $cols ) = @_;
139 print '<table class="'.$class.'">';
140 foreach( @{$cols} ) {
141 print "<col class='$_'>\n";
144 foreach( @{$captions} ) {
145 print '<th>'.$_."\n";
155 foreach( split /\?/, shift ) {
157 my( $name, $value ) = /^([^=]+)=(.*)$/;
158 $result{$name} = $value;
166 foreach( keys %{$args} ) {
167 $result .= "?$_=".$args->{$_} if( defined $args->{$_} );
172 sub buildExcept( $$ ) {
173 my( $except, $args ) = @_;
174 my %backup = %{$args};
175 delete $backup{$except};
176 return buildArgs( \%backup );
179 sub setAddrPrefix( $$ ) {
180 my( $addr, $prefix ) = @_;
181 $addr =~ s/\/(mods|read|static)//;
182 return "/$prefix$addr";
185 sub HTTPRedirect( $$ ) {
186 my( $req, $link ) = @_;
187 $req->headers_out->add( 'Location' => $link );
188 return HTTP_SEE_OTHER;
191 sub genPathBare( $$$$$ ) {
192 my( $req, $address, $printAddr, $started, $tables ) = @_;
194 if( defined $address ) {
195 $path = $address->path();
199 foreach my $item ( reverse @{$path} ) {
200 my( $addr, $exception, $myAddr ) = @{$item};
210 print "(" if( $exception );
211 if( !$printAddr && $myAddr ) {
212 print "<strong>".encode( $addr->pretty() )."</strong>";
215 $title = ' title="'.encode( $tables->itemName( $addr->get() ) ).'"' if defined $tables;
216 print "<a href='/read/".$addr->get()."'$title>".encode( $addr->pretty() )."</a>";
218 print ")" if( $exception );
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 );
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 );
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 );