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);
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, $action ) = @_;
39 print " <li><a href='".$url.$action."'>$label</a>\n";
42 sub genCustomMenu( $$$ ) {
43 my( $address, $args, $list ) = @_;
44 my $url = '/'.$address->get().buildExcept( 'action', $args ).'?action=';
45 print "<div class='menu'>\n<ul>\n";
47 my( $label, $action ) = @{$_};
49 $prefix = '/read' if( !defined( $action ) or ( $action eq 'list' ) or ( $action eq '' ) );
50 item( $prefix.$url, $label, $action );
52 print "</ul></div>\n";
56 my( $address, $args, $auth ) = @_;
58 if( defined( $auth->{'authid'} ) ) {
59 push @list, [ 'Log out', 'logout' ];
61 push @list, [ 'Log in', 'login' ];
63 push @list, [ 'Add item', 'newitem' ] if( $address->canAddItem() );
64 push @list, [ 'Discuss', 'newhistory' ] if( $address->canDiscuss() );
65 push @list, [ 'Administrate', 'admin' ] if( hasRight( $auth->{'accrights'}, 'validate' ) );
66 push @list, [ 'Profile', 'profile' ] if defined $auth->{'authid'};
67 push @list, [ 'Notifications', 'notifications' ] if defined $auth->{'authid'};
68 genCustomMenu( $address, $args, \@list );
71 sub genTableHead( $$$ ) {
72 my( $class, $captions, $cols ) = @_;
73 print '<table class="'.$class.'">';
75 print "<col class='$_'>\n";
78 foreach( @{$captions} ) {
89 foreach( split /\?/, shift ) {
91 my( $name, $value ) = /^([^=]+)=(.*)$/;
92 $result{$name} = $value;
100 $result .= "?$_=".$args->{$_} foreach( keys %{$args} );
104 sub buildExcept( $$ ) {
105 my( $except, $args ) = @_;
106 my %backup = %{$args};
107 delete $backup{$except};
108 return buildArgs( \%backup );
111 sub setAddrPrefix( $$ ) {
112 my( $addr, $prefix ) = @_;
113 $addr =~ s/\/(mods|read|static)//;
114 return "/$prefix$addr";
117 sub HTTPRedirect( $$ ) {
118 my( $req, $link ) = @_;
119 $req->headers_out->add( 'Location' => $link );
120 return HTTP_SEE_OTHER;
124 my( $address, $printAddr ) = @_;
125 my $path = $address->path();
126 push @{$path}, $address if( $printAddr );
127 print "<div class='navigation'><ul>\n";
128 foreach my $addr ( @{$path} ) {
129 print " <li><a href='/read/".$addr->get()."/'>".encode( $addr->pretty() )."</a>\n";
131 print "<li><a href='/index.html'>Main page</a>\n";