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";
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 );
47 sub itemExists( $$ ) {
48 my( $tables, $addr ) = @_;
49 return defined $tables->item( $addr );
52 sub tryDirect( $$$$$ ) {
53 my( $req, $args, $tables, $search, $hasSSL ) = @_;
54 my $address = PciIds::Address::new( $req->uri() );
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() ) );
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();
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 );
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";
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 {
98 my $address = PciIds::Address::new( $addr );
99 return "<a href='$prefix".$address->get()."'>".encode( $address->fullPretty() )."</a>";
100 } ], sub { 1; }, sub { ' class="item"'; } );
102 genHtmlFooter( 1, $req, $args );
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";
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 );