]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Jump.pm
Do not fail when jumping from help
[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";
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         if( defined $address ) {
58                 my( $top ) = $address->get() =~ /^([^\/]+)/;
59                 $search =~ s/^\//$top\//;
60                 #Is it absolute address?
61                 my $saddr = PciIds::Address::new( $search );
62                 return redirect( $req, $args, $saddr->get(), $hasSSL ) if( defined $saddr && itemExists( $tables, $saddr->get() ) );
63         }
64         while( defined $address ) {
65                 my $saddr = PciIds::Address::new( $address->get()."/$search" );
66                 return redirect( $req, $args, $saddr->get(), $hasSSL ) if( defined $saddr && itemExists( $tables, $saddr->get() ) );
67                 $address = $address->parent();
68         }
69         return undef;
70 }
71
72 sub jump( $$$$ ) {
73         my( $req, $args, $tables, $auth ) = @_;
74         $args->{'action'} = delete $args->{'origin'};
75         my $search = getFormValue( 'where', '' );
76         my $idOnly = $search =~ s/^#//;
77         my $direct = tryDirect( $req, $args, $tables, $search, $auth->{'ssl'} );
78         return $direct if defined $direct;
79         my $address = PciIds::Address::new( $req->uri() );
80         $address = PciIds::Address::new( 'PC' ) unless defined $address;
81         unless( $idOnly || length $search < 3 ) {#Try extended search
82                 my( $prefix ) = $address->get() =~ /^([^\/]+)/;
83                 $prefix = undef if $search =~ s/^\*//;
84                 my $result = $tables->searchName( $search, $prefix );
85                 if( @{$result} ) {
86                         genHtmlHead( $req, 'Search results', undef );
87                         print "<div class='top'>\n";
88                         print "<h1>Search results</h1>\n";
89                         genMenu( $req, $address, $args, $auth, [ [ 'Help', 'help', 'jump' ] ] );
90                         print "<div class='clear'></div>\n";
91                         print "</div>\n";
92                         genPath( $req, $address, 1 );
93                         print "<h2>Found items</h2>\n";
94                         genTableHead( 'found', [ 'ID', 'Name', 'Parent' ], [] );
95                         my $prefix = '/'.( ( !defined $args->{'action'} || $args->{'action'} eq '' || $args->{'action'} eq 'list' ) ? 'read/' : 'mods/' );
96                         htmlFormatTable( $result, 3, [], [ sub {
97                                 my $addr = shift;
98                                 my $address = PciIds::Address::new( $addr );
99                                 return "<a href='$prefix".$address->get()."'>".encode( $address->fullPretty() )."</a>";
100                         } ], sub { 1; }, sub { ' class="item"'; } );
101                         genTableTail();
102                         genHtmlFooter( 1, $req, $args );
103                         return OK;
104                 }
105         }
106         genHtmlHead( $req, 'No matches', undef );
107         print "<div class='top'>\n";
108         genMenu( $req, $address, $args, $auth, [ [ 'Help', 'help', 'jump' ] ] );
109         print '<h1>No matches</h1>';
110         print "<div class='clear'></div>\n";
111         print "</div\n>";
112         genPath( $req, $address, 1 );
113         print "<p>Your search request matches no item. Would you like to try again?\n<p>\n";
114         jumpWindow( $req, $args );
115         genHtmlTail();
116         return OK;
117 }
118
119 1;