]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/List.pm
bc5866a3c89dd9a5648b1ae2104f7d06d897122f
[pciids.git] / PciIds / Html / List.pm
1 #       PciIds web database
2 #       Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
3 #
4 #       This program is free software; you can redistribute it and/or modify
5 #       it under the terms of the GNU General Public License as published by
6 #       he Free Software Foundation; either version 2 of the License, or
7 #       (at your option) any later version.
8 #
9 #       This program is distributed in the hope that it will be useful,
10 #       but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #
13 #       GNU General Public License for more details.
14 #
15 #       You should have received a copy of the GNU General Public License
16 #       along with this program; if not, write to the Free Software
17 #       Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19 package PciIds::Html::List;
20 use strict;
21 use warnings;
22 use PciIds::Address;
23 use PciIds::Html::Util;
24 use Apache2::Const qw(:common :http);
25 use base 'Exporter';
26
27 our @EXPORT = qw(&loadItem);
28
29 sub loadItem( $$ ) {
30         my( $tables, $uri ) = @_;
31         my $address = PciIds::Address::new( $uri );
32         return ( 0 ) unless( defined $address );
33         my $item = $tables->item( $address->get() );
34         return ( 0 ) unless( defined $item );
35         my( $parent, $name, $note, $mainhistory ) = @{$item};
36         return ( 1, $parent, $name, $note, $address, $mainhistory );
37 }
38
39 sub list( $$$$ ) {
40         my( $req, $args, $tables, $auth ) = @_;
41         my( $ok, $parent, $name, $note, $address, $mid ) = loadItem( $tables, $req->uri() );
42         return NOT_FOUND unless( $ok );
43         my $id = $address->top()->pretty();
44         genHtmlHead( $req, $id, "<style type='text/css' media='screen,print'>col.id-col { width: ".$address->subIdSize()*1.25."ex; }</style>\n" );
45         print "<div class='top'>\n";
46         genMenu( $req, $address, $args, $auth, [ [ 'Help', 'help', 'list' ], $address->helpName() ? [ 'ID syntax', 'help', $address->helpName() ] : (), [ '', 'jump' ] ] );
47         print '<h1>'.encode( $id ).'</h1>';
48         print "<div class='clear'></div>\n";
49         print "</div\n>";
50         genPath( $req, $address, 0 );
51         print htmlDiv( 'name', '<p>Name: '.encode( $name ) ) if defined $name && $name ne '';
52         print htmlDiv( 'note', '<p>Note: '.encode( $note ) ) if defined $note && $note ne '';
53         my $diss = 0;
54         my $history;
55         foreach $history ( @{$tables->history( $address->get() )} ) {
56                 unless( $diss ) {
57                         print "<div class='discussion'>\n<h2>Discussion</h2>";
58                         $diss = 1;
59                 }
60                 my( $id, $text, $time, $name, $note, $seen, $user, $email ) = @{$history};
61                 my $type = $seen ? 'history' : 'unseen-history';
62                 $type = 'main-history' if( defined( $mid ) && ( $id == $mid ) );
63                 print "<div class='$type'>\n";
64                 print "<p class='itemname'>Name: ".encode( $name )."\n" if( defined( $name ) && ( $name ne '' ) );
65                 print "<p class='itemname'>Deletion request\n" if( defined $name && $name eq '' );
66                 print "<p class='itemnote'>Note: ".encode( $note )."\n" if( defined( $note ) && ( $note ne '' ) );
67                 if( defined( $text ) && ( $text ne '' ) ) {
68                         $text = encode( $text );
69                         $text =~ s/\n/<br>/g;
70                         print "<p class='discussion-text'>$text\n";
71                 }
72                 ( $user ) = ( $email =~ /^(.*)@/ ) if defined $email && !defined $user;
73                 print "<p class='author'>".encode( $user )."\n" if( defined( $user ) );
74                 print "<p class='time'>".encode( $time )."\n";
75                 print "</div>\n";
76         }
77         if( $diss ) {
78                 print "<p><a href='".buildExcept( 'action', $args )."?action=newhistory'>Discuss</a>\n";
79                 print "</div>\n" if( $diss );
80         }
81         unless( $address->leaf() ) {
82                 print "<h2>".encode( $address->subName() )."</h2>\n";
83                 my $restricts = $address->defaultRestrictList();
84                 if( scalar @{$restricts} ) {
85                         print "<p>";
86                         my $url = '/read/'.$address->get().buildExcept( 'restrict', $args ).'?restrict=';
87                         foreach( @{$restricts} ) {
88                                 print "<a href='".$url.$_->[0]."'>".$_->[1]."</a> ";
89                         }
90                 }
91                 my $url = '/read/'.$address->get().buildExcept( 'sort', $args );
92                 my $sort = ( $args->{'sort'} or 'id' );
93                 my( $sort_id, $sort_name ) = ( ( $sort eq 'id' ? 'rid' : 'id' ), ( $sort eq 'name' ? 'rname' : 'name' ) );
94                 genTableHead( 'subnodes', [ '<a href="'.$url.'?sort='.$sort_id.'">Id</a>', '<a href="'.$url.'?sort='.$sort_name.'">Name</a>', 'Note' ], [ 'id-col', 'name-col', 'note-col' ] );
95                 $args->{'restrict'} = $address->defaultRestrict() unless( defined( $args->{'restrict'} ) );
96                 $tables->nodes( $address->get(), $args );
97                 genTableTail();
98                 print "<p><a href='".buildExcept( 'action', $args )."?action=newitem'>Add item</a>\n";
99         }
100         genHtmlTail();
101         return OK;
102 }
103
104 1;