]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Util.pm
Remove useless directories
[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);
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, $action ) = @_;
39         print "  <li><a href='".$url.$action."'>$label</a>\n";
40 }
41
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";
46         foreach( @{$list} ) {
47                 my( $label, $action ) = @{$_};
48                 my $prefix = '/mods';
49                 $prefix = '/read' if( !defined( $action ) or ( $action eq 'list' ) or ( $action eq '' ) );
50                 item( $prefix.$url, $label, $action );
51         }
52         print "</ul></div>\n";
53 }
54
55 sub genMenu( $$$ ) {
56         my( $address, $args, $auth ) = @_;
57         my @list;
58         if( defined( $auth->{'authid'} ) ) {
59                 push @list, [ 'Log out', 'logout' ];
60         } else {
61                 push @list, [ 'Log in', 'login' ];
62         }
63         push @list, [ 'Add item', 'newitem' ] if( $address->canAddItem() );
64         push @list, [ 'Discuss', 'newcomment' ] if( $address->canAddComment() );
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 );
69 }
70
71 sub genTableHead( $$ ) {
72         my( $class, $captions ) = @_;
73         print '<table class="'.$class.'"><tr>';
74         foreach( @{$captions} ) {
75                 print '<th>'.$_;
76         }
77         print '</tr>';
78 }
79
80 sub genTableTail() {
81         print '</table>';
82 }
83
84 sub parseArgs( $ ) {
85         my %result;
86         foreach( split /\?/, shift ) {
87                 next unless( /=/ );
88                 my( $name, $value ) = /^([^=]+)=(.*)$/;
89                 $result{$name} = $value;
90         }
91         return \%result;
92 }
93
94 sub buildArgs( $ ) {
95         my( $args ) = @_;
96         my $result = '';
97         $result .= "?$_=".$args->{$_} foreach( keys %{$args} );
98         return $result;
99 }
100
101 sub buildExcept( $$ ) {
102         my( $except, $args ) = @_;
103         my %backup = %{$args};
104         delete $backup{$except};
105         return buildArgs( \%backup );
106 }
107
108 sub setAddrPrefix( $$ ) {
109         my( $addr, $prefix ) = @_;
110         $addr =~ s/\/(mods|read|static)//;
111         return "/$prefix$addr";
112 }
113
114 sub HTTPRedirect( $$ ) {
115         my( $req, $link ) = @_;
116         $req->headers_out->add( 'Location' => $link );
117         return HTTP_SEE_OTHER;
118 }
119
120 1;