2 # Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
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.
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
13 # GNU General Public License for more details.
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
19 package PciIds::Html::Jump;
23 use PciIds::Html::Forms;
24 use PciIds::Html::Users;
25 use PciIds::Html::Util;
26 use PciIds::Html::Format;
28 use Apache2::Const qw(:common :http);
30 our @EXPORT = qw(&jumpWindow);
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";
36 print "<input type='text' class='jump' name='where'><input type='submit' value='Jump'>\n";
41 my( $req, $args, $addr ) = @_;
42 my $prefix = ( !defined $args->{'action'} || $args->{'action'} eq '' || $args->{'action'} eq 'list' ) ? 'read' : 'mods';
43 my $url = "http://".$req->hostname()."/$prefix/$addr".buildArgs( $args );
44 return HTTPRedirect( $req, $url );
47 sub itemExists( $$ ) {
48 my( $tables, $addr ) = @_;
49 return defined $tables->item( $addr );
52 sub tryDirect( $$$$ ) {
53 my( $req, $args, $tables, $search ) = @_;
54 my $address = PciIds::Address::new( $req->uri() );
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() ) 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() ) if( defined $saddr && itemExists( $tables, $saddr->get() ) );
65 $address = $address->parent();
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 );
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 );
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' ], [ '', 'jump' ] ] );
87 print "<div class='clear'></div>\n";
89 genPath( $req, $address, 1 );
90 print "<h2>Found items</h2>\n";
91 genTableHead( 'found', [ 'ID', 'Name', 'Parent' ], [] );
92 my $prefix = 'http://'.$req->hostname().'/'.( ( !defined $args->{'action'} || $args->{'action'} eq '' || $args->{'action'} eq 'list' ) ? 'read/' : 'mods/' );
93 my $suffix = buildArgs( $args );
94 htmlFormatTable( $result, 3, [], [ sub {
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"'; } );
104 genHtmlHead( $req, 'No matches', undef );
105 print "<div class='top'>\n";
106 print '<h1>No matches</h1>';
107 genMenu( $req, $address, $args, $auth, [ [ 'Help', 'help', 'jump' ] ] );
108 print "<div class='clear'></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 );