1 package PciIds::Html::Util;
7 use Apache2::Const qw(:common :http);
10 our @EXPORT = qw(&genHtmlHead &htmlDiv &genHtmlTail &genTableHead &genTableTail &parseArgs &buildExcept &buildArgs &genMenu &genCustomMenu &encode &setAddrPrefix &HTTPRedirect &genPath &logItem &genLocMenu &genCustomHead &genPathBare);
13 return encode_entities( shift, "\"'&<>" );
16 sub genHtmlHead( $$$ ) {
17 my( $req, $caption, $metas ) = @_;
18 $req->content_type( 'text/html; charset=utf-8' );
19 $req->headers_out->add( 'Cache-control' => 'no-cache' );
20 print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'."\n";
21 print '<html lang="en"><head><title>'.encode( $caption )."</title>\n";
22 print "<link rel='stylesheet' type='text/css' media='screen' href='/static/screen.css'>\n";
23 print "<link rel='stylesheet' type='text/css' media='print' href='/static/print.css'>\n";
24 print $metas if( defined( $metas ) );
25 print "</head><body>\n";
29 print '</body></html>';
33 my( $class, $text ) = @_;
34 return '<div class="'.$class.'">'.$text.'</div>';
38 my( $url, $label ) = @_;
39 print " <li><a href='".$url."'>$label</a>\n";
42 sub genCustomMenu( $$$$ ) {
43 my( $req, $address, $args, $list ) = @_;
45 if( defined $address ) {
46 $url = '/'.$address->get().buildExcept( 'action', $args ).'?action=';
48 $url = '/read/?action=';
52 my( $label, $action, $param ) = @{$_};
53 if( $action eq 'jump' ) {
55 require PciIds::Html::Jump;
56 PciIds::Html::Jump::jumpWindow( $req, $args );
59 $prefix = '/read' if( !defined( $action ) or ( $action eq 'list' ) or ( $action eq '' ) or ( $action eq 'help' ) );
61 $suffix = '?help='.$param if( $action eq 'help' );
62 item( 'http://'.$req->hostname().$prefix.$url.$action.$suffix, $label );
70 if( defined( $auth->{'authid'} ) ) {
71 return [ 'Log out ('.encode( $auth->{'name'} ).')', 'logout' ];
73 return [ 'Log in', 'login' ];
77 sub genMenu( $$$$$ ) {
78 my( $req, $address, $args, $auth, $append ) = @_;
80 if( defined $address ) {
81 push @list, [ 'Add item', 'newitem' ] if( $address->canAddItem() );
82 push @list, [ 'Discuss', 'newhistory' ] if( $address->canDiscuss() );
84 push @list, [ 'Administer', 'admin' ] if( hasRight( $auth->{'accrights'}, 'validate' ) );
85 push @list, @{$append} if defined $append;
87 print "<div class='lmenu'>\n";
88 genCustomMenu( $req, $address, $args, \@list );
91 @list = ( logItem( $auth ) );
92 push @list, [ 'Profile', 'profile' ] if defined $auth->{'authid'};
93 push @list, [ 'Notifications', 'notifications' ] if defined $auth->{'authid'};
94 print "<div class='rmenu'>\n";
95 genCustomMenu( $req, $address, $args, \@list );
99 sub genTableHead( $$$ ) {
100 my( $class, $captions, $cols ) = @_;
101 print '<table class="'.$class.'">';
102 foreach( @{$cols} ) {
103 print "<col class='$_'>\n";
106 foreach( @{$captions} ) {
107 print '<th>'.$_."\n";
117 foreach( split /\?/, shift ) {
119 my( $name, $value ) = /^([^=]+)=(.*)$/;
120 $result{$name} = $value;
128 foreach( keys %{$args} ) {
129 $result .= "?$_=".$args->{$_} if( defined $args->{$_} );
134 sub buildExcept( $$ ) {
135 my( $except, $args ) = @_;
136 my %backup = %{$args};
137 delete $backup{$except};
138 return buildArgs( \%backup );
141 sub setAddrPrefix( $$ ) {
142 my( $addr, $prefix ) = @_;
143 $addr =~ s/\/(mods|read|static)//;
144 return "/$prefix$addr";
147 sub HTTPRedirect( $$ ) {
148 my( $req, $link ) = @_;
149 $req->headers_out->add( 'Location' => $link );
150 return HTTP_SEE_OTHER;
153 sub genPathBare( $$$$ ) {
154 my( $req, $address, $printAddr, $started ) = @_;
156 if( defined $address ) {
157 $path = $address->path();
158 unshift @{$path}, $address if( $printAddr );
162 foreach my $addr ( reverse @{$path} ) {
164 print " -> ";
168 print "<a href='http://".$req->hostname()."/read/".$addr->get()."'>".encode( $addr->pretty() )."</a>";
173 my( $req, $address, $printAddr ) = @_;
174 print "<div class='path'>\n";
175 print "<p><a href='http://".$req->hostname()."/index.html'>Main page</a>";
176 genPathBare( $req, $address, $printAddr, 1 );
180 sub genLocMenu( $$$$$ ) {
181 my( $req, $args, $addr, $lactions, $ractions ) = @_;
182 print "<div class='lmenu'>\n";
183 genCustomMenu( $req, $addr, $args, $lactions );
184 print "</div>\n<div class='rmenu'>\n";
185 genCustomMenu( $req, $addr, $args, $ractions );
189 sub genCustomHead( $$$$$$ ) {
190 my( $req, $args, $addr, $caption, $lactions, $ractions ) = @_;
191 print "<div class='top'>\n";
192 genLocMenu( $req, $args, $addr, $lactions, $ractions );
193 print "<h1>$caption</h1>\n";
194 print "<div class='clear'></div></div>\n";
195 genPath( $req, $addr, 1 );