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