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);
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 ) = @{$_};
54 $prefix = '/read' if( !defined( $action ) or ( $action eq 'list' ) or ( $action eq '' ) or ( $action eq 'help' ) );
56 $suffix = '?help='.$param if( $action eq 'help' );
57 item( 'http://'.$req->hostname().$prefix.$url.$action.$suffix, $label );
64 if( defined( $auth->{'authid'} ) ) {
65 return [ 'Log out ('.encode( $auth->{'name'} ).')', 'logout' ];
67 return [ 'Log in', 'login' ];
71 sub genMenu( $$$$$ ) {
72 my( $req, $address, $args, $auth, $append ) = @_;
74 if( defined $address ) {
75 push @list, [ 'Add item', 'newitem' ] if( $address->canAddItem() );
76 push @list, [ 'Discuss', 'newhistory' ] if( $address->canDiscuss() );
78 push @list, [ 'Administrate', 'admin' ] if( hasRight( $auth->{'accrights'}, 'validate' ) );
79 push @list, @{$append} if defined $append;
81 print "<div class='lmenu'>\n";
82 genCustomMenu( $req, $address, $args, \@list );
85 @list = ( logItem( $auth ) );
86 push @list, [ 'Profile', 'profile' ] if defined $auth->{'authid'};
87 push @list, [ 'Notifications', 'notifications' ] if defined $auth->{'authid'};
88 print "<div class='rmenu'>\n";
89 genCustomMenu( $req, $address, $args, \@list );
93 sub genTableHead( $$$ ) {
94 my( $class, $captions, $cols ) = @_;
95 print '<table class="'.$class.'">';
97 print "<col class='$_'>\n";
100 foreach( @{$captions} ) {
101 print '<th>'.$_."\n";
111 foreach( split /\?/, shift ) {
113 my( $name, $value ) = /^([^=]+)=(.*)$/;
114 $result{$name} = $value;
122 $result .= "?$_=".$args->{$_} foreach( keys %{$args} );
126 sub buildExcept( $$ ) {
127 my( $except, $args ) = @_;
128 my %backup = %{$args};
129 delete $backup{$except};
130 delete $backup{'full_links'};#This one is internal
131 return buildArgs( \%backup );
134 sub setAddrPrefix( $$ ) {
135 my( $addr, $prefix ) = @_;
136 $addr =~ s/\/(mods|read|static)//;
137 return "/$prefix$addr";
140 sub HTTPRedirect( $$ ) {
141 my( $req, $link ) = @_;
142 $req->headers_out->add( 'Location' => $link );
143 return HTTP_SEE_OTHER;
147 my( $req, $address, $printAddr ) = @_;
149 if( defined $address ) {
150 $path = $address->path();
151 unshift @{$path}, $address if( $printAddr );
155 print "<div class='path'>\n";
156 print "<p><a href='http://".$req->hostname()."/index.html'>Main page</a>";
157 foreach my $addr ( reverse @{$path} ) {
158 print " -> <a href='http://".$req->hostname()."/read/".$addr->get()."'>".encode( $addr->pretty() )."</a>";
163 sub genLocMenu( $$$$$ ) {
164 my( $req, $args, $addr, $lactions, $ractions ) = @_;
165 print "<div class='lmenu'>\n";
166 genCustomMenu( $req, $addr, $args, $lactions );
167 print "</div>\n<div class='rmenu'>\n";
168 genCustomMenu( $req, $addr, $args, $ractions );
172 sub genCustomHead( $$$$$$ ) {
173 my( $req, $args, $addr, $caption, $lactions, $ractions ) = @_;
174 print "<div class='top'>\n";
175 print "<h1>$caption</h1>\n";
176 genLocMenu( $req, $args, $addr, $lactions, $ractions );
177 print "<div class='clear'></div></div>\n";
178 genPath( $req, $addr, 1 );