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);
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>';
57 my( $class, $text ) = @_;
58 return '<div class="'.$class.'">'.$text.'</div>';
62 my( $url, $label ) = @_;
63 print " <li><a href='".$url."'>$label</a>\n";
66 sub genCustomMenu( $$$$ ) {
67 my( $req, $address, $args, $list ) = @_;
69 if( defined $address ) {
70 $url = '/'.$address->get().buildExcept( 'action', $args ).'?action=';
72 $url = '/read/?action=';
76 my( $label, $action, $param ) = @{$_};
77 if( $action eq 'jump' ) {
79 require PciIds::Html::Jump;
80 PciIds::Html::Jump::jumpWindow( $req, $args );
83 $prefix = '/read' if( !defined( $action ) or ( $action eq 'list' ) or ( $action eq '' ) or ( $action eq 'help' ) );
85 $suffix = '?help='.$param if( $action eq 'help' );
86 item( $prefix.$url.$action.$suffix, $label );
94 if( defined( $auth->{'authid'} ) ) {
95 return [ 'Log out ('.encode( $auth->{'name'} ).')', 'logout' ];
97 return [ 'Log in', 'login' ];
101 sub genMenu( $$$$$ ) {
102 my( $req, $address, $args, $auth, $append ) = @_;
104 if( defined $address ) {
105 push @list, [ 'Add item', 'newitem' ] if( $address->canAddItem() );
106 push @list, [ 'Discuss', 'newhistory' ] if( $address->canDiscuss() );
108 push @list, [ 'Administer', 'admin' ] if( hasRight( $auth->{'accrights'}, 'validate' ) );
109 push @list, @{$append} if defined $append;
111 print "<div class='lmenu'>\n";
112 genCustomMenu( $req, $address, $args, \@list );
115 @list = ( logItem( $auth ) );
116 push @list, [ 'Profile', 'profile' ] if defined $auth->{'authid'};
117 push @list, [ 'Notifications', 'notifications' ] if defined $auth->{'authid'};
118 print "<div class='rmenu'>\n";
119 genCustomMenu( $req, $address, $args, \@list );
123 sub genTableHead( $$$ ) {
124 my( $class, $captions, $cols ) = @_;
125 print '<table class="'.$class.'">';
126 foreach( @{$cols} ) {
127 print "<col class='$_'>\n";
130 foreach( @{$captions} ) {
131 print '<th>'.$_."\n";
141 foreach( split /\?/, shift ) {
143 my( $name, $value ) = /^([^=]+)=(.*)$/;
144 $result{$name} = $value;
152 foreach( keys %{$args} ) {
153 $result .= "?$_=".$args->{$_} if( defined $args->{$_} );
158 sub buildExcept( $$ ) {
159 my( $except, $args ) = @_;
160 my %backup = %{$args};
161 delete $backup{$except};
162 return buildArgs( \%backup );
165 sub setAddrPrefix( $$ ) {
166 my( $addr, $prefix ) = @_;
167 $addr =~ s/\/(mods|read|static)//;
168 return "/$prefix$addr";
171 sub HTTPRedirect( $$ ) {
172 my( $req, $link ) = @_;
173 $req->headers_out->add( 'Location' => $link );
174 return HTTP_SEE_OTHER;
177 sub genPathBare( $$$$ ) {
178 my( $req, $address, $printAddr, $started ) = @_;
180 if( defined $address ) {
181 $path = $address->path();
185 foreach my $item ( reverse @{$path} ) {
186 my( $addr, $exception, $myAddr ) = @{$item};
196 print "(" if( $exception );
197 if( !$printAddr && $myAddr ) {
198 print "<strong>".encode( $addr->pretty() )."</strong>";
200 print "<a href='/read/".$addr->get()."'>".encode( $addr->pretty() )."</a>";
202 print ")" if( $exception );
207 my( $req, $address, $printAddr ) = @_;
208 print "<div class='path'>\n";
209 print "<p><a href='/index.html'>Main</a>";
210 genPathBare( $req, $address, $printAddr, 1 );
214 sub genLocMenu( $$$$$ ) {
215 my( $req, $args, $addr, $lactions, $ractions ) = @_;
216 print "<div class='lmenu'>\n";
217 genCustomMenu( $req, $addr, $args, $lactions );
218 print "</div>\n<div class='rmenu'>\n";
219 genCustomMenu( $req, $addr, $args, $ractions );
223 sub genCustomHead( $$$$$$ ) {
224 my( $req, $args, $addr, $caption, $lactions, $ractions ) = @_;
225 print "<div class='top'>\n";
226 genLocMenu( $req, $args, $addr, $lactions, $ractions );
227 print "<h1>$caption</h1>\n";
228 print "<div class='clear'></div></div>\n";
229 genPath( $req, $addr, 1 );