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>
64 <div class="campaign"><a href="http://sourceforge.net/"><img src="http://sourceforge.net/sflogo.php?group_id=35833" width="88" height="31" alt="SourceForge"></a></div>
65 <div class="campaign"><a href="http://www.anybrowser.org/campaign/"><img src="/static/anybrowser.png" width=88 height=31 alt="Best viewed with any browser."></a></div>
67 Maintained by <a href="http://mj.ucw.cz/">Martin Mares</a> and <a href="http://vorner.pretel.cz/">Michal
68 Vaner</a> with great help by volunteers from the <a href="http://www.sourceforge.net/projects/pciids/">pciids project</a>
69 at <a href="http://www.sourceforge.net/">SourceForge</a>.
70 <p class="maintain">No counters, no frames, no syntax errors.
76 my( $class, $text ) = @_;
77 return '<div class="'.$class.'">'.$text.'</div>';
81 my( $url, $label ) = @_;
82 print " <li><a href='".$url."'>$label</a>\n";
85 sub genCustomMenu( $$$$ ) {
86 my( $req, $address, $args, $list ) = @_;
88 if( defined $address ) {
89 $url = '/'.$address->get().buildExcept( 'action', $args ).'?action=';
95 my( $label, $action, $param ) = @{$_};
96 if( $action eq 'jump' ) {
98 require PciIds::Html::Jump;
99 PciIds::Html::Jump::jumpWindow( $req, $args );
101 my $prefix = '/mods';
102 $prefix = '/read' if( !defined( $action ) or ( $action eq 'list' ) or ( $action eq '' ) or ( $action eq 'help' ) );
104 $suffix = '?help='.$param if( $action eq 'help' );
105 item( $prefix.$url.$action.$suffix, $label );
113 if( defined( $auth->{'authid'} ) ) {
114 return [ 'Log out ('.encode( $auth->{'name'} ).')', 'logout' ];
116 return [ 'Log in', 'login' ];
120 sub genMenu( $$$$$ ) {
121 my( $req, $address, $args, $auth, $append ) = @_;
123 if( defined $address ) {
124 push @list, [ 'Add item', 'newitem' ] if( $address->canAddItem() );
125 push @list, [ 'Discuss', 'newhistory' ] if( $address->canDiscuss() );
127 push @list, [ 'Administer', 'admin' ] if( hasRight( $auth->{'accrights'}, 'validate' ) );
128 push @list, @{$append} if defined $append;
130 print "<div class='lmenu'>\n";
131 genCustomMenu( $req, $address, $args, \@list );
134 @list = ( logItem( $auth ) );
135 push @list, [ 'Profile', 'profile' ] if defined $auth->{'authid'};
136 push @list, [ 'Notifications', 'notifications' ] if defined $auth->{'authid'};
137 print "<div class='rmenu'>\n";
138 genCustomMenu( $req, $address, $args, \@list );
142 sub genTableHead( $$$ ) {
143 my( $class, $captions, $cols ) = @_;
144 print '<table class="'.$class.'">';
145 foreach( @{$cols} ) {
146 print "<col class='$_'>\n";
149 foreach( @{$captions} ) {
150 print '<th>'.$_."\n";
160 foreach( split /\?/, shift ) {
162 my( $name, $value ) = /^([^=]+)=(.*)$/;
163 $result{$name} = $value;
171 foreach( keys %{$args} ) {
172 $result .= "?$_=".$args->{$_} if( defined $args->{$_} );
177 sub buildExcept( $$ ) {
178 my( $except, $args ) = @_;
179 my %backup = %{$args};
180 delete $backup{$except};
181 return buildArgs( \%backup );
184 sub setAddrPrefix( $$ ) {
185 my( $addr, $prefix ) = @_;
186 $addr =~ s/\/(mods|read|static)//;
187 return "/$prefix$addr";
190 sub HTTPRedirect( $$ ) {
191 my( $req, $link ) = @_;
192 $req->headers_out->add( 'Location' => $link );
193 return HTTP_SEE_OTHER;
196 sub genPathBare( $$$$ ) {
197 my( $req, $address, $printAddr, $started ) = @_;
199 if( defined $address ) {
200 $path = $address->path();
204 foreach my $item ( reverse @{$path} ) {
205 my( $addr, $exception, $myAddr ) = @{$item};
215 print "(" if( $exception );
216 if( !$printAddr && $myAddr ) {
217 print "<strong>".encode( $addr->pretty() )."</strong>";
219 print "<a href='/read/".$addr->get()."'>".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 );
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 );