]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Util.pm
Help index
[pciids.git] / PciIds / Html / Util.pm
1 package PciIds::Html::Util;
2 use strict;
3 use warnings;
4 use HTML::Entities;
5 use base 'Exporter';
6 use PciIds::Users;
7 use Apache2::Const qw(:common :http);
8 use APR::Table;
9
10 our @EXPORT = qw(&genHtmlHead &htmlDiv &genHtmlTail &genTableHead &genTableTail &parseArgs &buildExcept &buildArgs &genMenu &genCustomMenu &encode &setAddrPrefix &HTTPRedirect &genPath &logItem &genLocMenu);
11
12 sub encode( $ ) {
13         return encode_entities( shift, "\"'&<>" );
14 }
15
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";
26 }
27
28 sub genHtmlTail() {
29         print '</body></html>';
30 }
31
32 sub htmlDiv( $$ ) {
33         my( $class, $text ) = @_;
34         return '<div class="'.$class.'">'.$text.'</div>';
35 }
36
37 sub item( $$ ) {
38         my( $url, $label ) = @_;
39         print "  <li><a href='".$url."'>$label</a>\n";
40 }
41
42 sub genCustomMenu( $$$$ ) {
43         my( $req, $address, $args, $list ) = @_;
44         my $url;
45         if( defined $address ) {
46                 $url = '/'.$address->get().buildExcept( 'action', $args ).'?action=';
47         } else {
48                 $url = '/read/?action=';
49         }
50         print "<div class='menu'>\n<ul>\n";
51         foreach( @{$list} ) {
52                 my( $label, $action, $param ) = @{$_};
53                 my $prefix = '/mods';
54                 $prefix = '/read' if( !defined( $action ) or ( $action eq 'list' ) or ( $action eq '' ) or ( $action eq 'help' ) );
55                 my $suffix = '';
56                 $suffix = '?help='.$param if( $action eq 'help' );
57                 item( 'http://'.$req->hostname().$prefix.$url.$action.$suffix, $label );
58         }
59         print "</ul></div>\n";
60 }
61
62 sub logItem( $ ) {
63         my( $auth ) = @_;
64         if( defined( $auth->{'authid'} ) ) {
65                 return [ 'Log out ('.encode( $auth->{'name'} ).')', 'logout' ];
66         } else {
67                 return [ 'Log in', 'login' ];
68         }
69 }
70
71 sub genMenu( $$$$$ ) {
72         my( $req, $address, $args, $auth, $help ) = @_;
73         my @list = ( logItem( $auth ) );
74         if( defined $address ) {
75                 push @list, [ 'Add item', 'newitem' ] if( $address->canAddItem() );
76                 push @list, [ 'Discuss', 'newhistory' ] if( $address->canDiscuss() );
77         }
78         push @list, [ 'Administrate', 'admin' ] if( hasRight( $auth->{'accrights'}, 'validate' ) );
79         push @list, [ 'Profile', 'profile' ] if defined $auth->{'authid'};
80         push @list, [ 'Notifications', 'notifications' ] if defined $auth->{'authid'};
81         push @list, [ 'Help', 'help', $help ] if defined $help;
82         genCustomMenu( $req, $address, $args, \@list );
83 }
84
85 sub genTableHead( $$$ ) {
86         my( $class, $captions, $cols ) = @_;
87         print '<table class="'.$class.'">';
88         foreach( @{$cols} ) {
89                 print "<col class='$_'>\n";
90         }
91         print "<tr>\n";
92         foreach( @{$captions} ) {
93                 print '<th>'.$_."\n";
94         }
95 }
96
97 sub genTableTail() {
98         print '</table>';
99 }
100
101 sub parseArgs( $ ) {
102         my %result;
103         foreach( split /\?/, shift ) {
104                 next unless( /=/ );
105                 my( $name, $value ) = /^([^=]+)=(.*)$/;
106                 $result{$name} = $value;
107         }
108         return \%result;
109 }
110
111 sub buildArgs( $ ) {
112         my( $args ) = @_;
113         my $result = '';
114         $result .= "?$_=".$args->{$_} foreach( keys %{$args} );
115         return $result;
116 }
117
118 sub buildExcept( $$ ) {
119         my( $except, $args ) = @_;
120         my %backup = %{$args};
121         delete $backup{$except};
122         return buildArgs( \%backup );
123 }
124
125 sub setAddrPrefix( $$ ) {
126         my( $addr, $prefix ) = @_;
127         $addr =~ s/\/(mods|read|static)//;
128         return "/$prefix$addr";
129 }
130
131 sub HTTPRedirect( $$ ) {
132         my( $req, $link ) = @_;
133         $req->headers_out->add( 'Location' => $link );
134         return HTTP_SEE_OTHER;
135 }
136
137 sub genPath( $$$ ) {
138         my( $req, $address, $printAddr ) = @_;
139         my $path;
140         if( defined $address ) {
141                 $path = $address->path();
142                 unshift @{$path}, $address if( $printAddr );
143         } else {
144                 $path = [];
145         }
146         print "<div class='navigation'><ul>\n";
147         foreach my $addr ( @{$path} ) {
148                 print "  <li><a href='http://".$req->hostname()."/read/".$addr->get()."/'>".encode( $addr->pretty() )."</a>\n";
149         }
150         print "<li><a href='http://".$req->hostname()."/index.html'>Main page</a>\n";
151         print "</div>\n";
152 }
153
154 sub genLocMenu( $$$ ) {
155         my( $req, $args, $actions ) = @_;
156         my $addr = PciIds::Address::new( $req->uri() );
157         genCustomMenu( $req, $addr, $args, $actions );
158         genPath( $req, $addr, 1 );
159 }
160
161 1;