]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Jump.pm
Merge branch 'master' of /home/vorner/pciids
[pciids.git] / PciIds / Html / Jump.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::Jump;
20 use strict;
21 use warnings;
22 use base 'Exporter';
23 use PciIds::Html::Forms;
24 use PciIds::Html::Users;
25 use PciIds::Html::Util;
26 use PciIds::Html::Format;
27 use PciIds::Address;
28 use Apache2::Const qw(:common :http);
29
30 our @EXPORT = qw(&jumpWindow);
31
32 sub jumpWindow( $$ ) {
33         my( $req, $args ) = @_;
34         print "<form id='jump' class='jump' name='jump' method='POST' action='".buildExcept( 'action', $args )."?action=jump".( defined $args->{'action'} ? "?origin=".$args->{'action'} : "" )."'>\n";
35         print "<p>\n";
36         print "<input type='text' class='jump' name='where'><input type='submit' value='Jump'>\n";
37         print "</form>\n";
38 }
39
40 sub redirect( $$$$ ) {
41         my( $req, $args, $addr, $hasSSL ) = @_;
42         my $prefix = ( !defined $args->{'action'} || $args->{'action'} eq '' || $args->{'action'} eq 'list' ) ? 'read' : 'mods';
43         my $url = protoName( $hasSSL )."://".$req->hostname()."/$prefix/$addr".buildArgs( $args );
44         return HTTPRedirect( $req, $url );
45 }
46
47 sub itemExists( $$ ) {
48         my( $tables, $addr ) = @_;
49         return defined $tables->item( $addr );
50 }
51
52 sub tryDirect( $$$$$ ) {
53         my( $req, $args, $tables, $search, $hasSSL ) = @_;
54         my $address = PciIds::Address::new( $req->uri() );
55         $search =~ s/:/\//g;
56         $search =~ s/ //g;
57         my( $top ) = $address->get() =~ /^([^\/]+)/;
58         $search =~ s/^\//$top\//;
59         #Is it absolute address?
60         my $saddr = PciIds::Address::new( $search );
61         return redirect( $req, $args, $saddr->get(), $hasSSL ) if( defined $saddr && itemExists( $tables, $saddr->get() ) );
62         while( defined $address ) {
63                 $saddr = PciIds::Address::new( $address->get()."/$search" );
64                 return redirect( $req, $args, $saddr->get(), $hasSSL ) if( defined $saddr && itemExists( $tables, $saddr->get() ) );
65                 $address = $address->parent();
66         }
67         return undef;
68 }
69
70 sub jump( $$$$ ) {
71         my( $req, $args, $tables, $auth ) = @_;
72         $args->{'action'} = delete $args->{'origin'};
73         my $search = getFormValue( 'where', '' );
74         my $idOnly = $search =~ s/^#//;
75         my $direct = tryDirect( $req, $args, $tables, $search, $auth->{'ssl'} );
76         return $direct if defined $direct;
77         my $address = PciIds::Address::new( $req->uri() );
78         unless( $idOnly || length $search < 3 ) {#Try extended search
79                 my( $prefix ) = $address->get() =~ /^([^\/]+)/;
80                 $prefix = undef if $search =~ s/^\*//;
81                 my $result = $tables->searchName( $search, $prefix );
82                 if( @{$result} ) {
83                         genHtmlHead( $req, 'Search results', undef );
84                         print "<div class='top'>\n";
85                         print "<h1>Search results</h1>\n";
86                         genMenu( $req, $address, $args, $auth, [ [ 'Help', 'help', 'jump' ] ] );
87                         print "<div class='clear'></div>\n";
88                         print "</div>\n";
89                         genPath( $req, $address, 1 );
90                         print "<h2>Found items</h2>\n";
91                         genTableHead( 'found', [ 'ID', 'Name', 'Parent' ], [] );
92                         my $prefix = '/'.( ( !defined $args->{'action'} || $args->{'action'} eq '' || $args->{'action'} eq 'list' ) ? 'read/' : 'mods/' );
93                         my $suffix = buildArgs( $args );
94                         htmlFormatTable( $result, 3, [], [ sub {
95                                 my $addr = shift;
96                                 my $address = PciIds::Address::new( $addr );
97                                 return "<a href='$prefix".$address->get()."$suffix'>".encode( $address->fullPretty() )."</a>";
98                         } ], sub { 1; }, sub { ' class="item"'; } );
99                         genTableTail();
100                         genHtmlFooter( 1, $req, $args );
101                         return OK;
102                 }
103         }
104         genHtmlHead( $req, 'No matches', undef );
105         print "<div class='top'>\n";
106         genMenu( $req, $address, $args, $auth, [ [ 'Help', 'help', 'jump' ] ] );
107         print '<h1>No matches</h1>';
108         print "<div class='clear'></div>\n";
109         print "</div\n>";
110         genPath( $req, $address, 1 );
111         print "<p>Your search request matches no item. Would you like to try again?\n<p>\n";
112         jumpWindow( $req, $args );
113         genHtmlTail();
114         return OK;
115 }
116
117 1;