1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#!/usr/bin/perl
use strict 'vars';
use vars qw/$DB $URL %EQUIV/;
use Ace 1.51;
use CGI 2.42 qw/:standard :html3 escape/;
use CGI::Carp qw/fatalsToBrowser/;
use Ace::Browser::AceSubs qw(:DEFAULT DoRedirect);
use Ace::Browser::SearchSubs;
my $classlist = Configuration()->Basic_objects;
my @classlist = @{$classlist}[map {2*$_} (0..@$classlist/2-1)]; # keep keys, preserving the order
my $JSCRIPT=<<END;
function focussearch() {
document.SimpleForm.query.focus();
document.SimpleForm.query.select();
return (false);
}
END
# fetch database handle
$DB = OpenDatabase() || AceError("Couldn't open database.");
my $search_class = param('class');
my $search_pattern = param('query');
my $offset = AceSearchOffset();
$URL = url();
$URL=~s!^http://[^/]+!!;
my ($objs,$count);
if (defined $search_class) {
if ($search_class eq 'Any' && $search_pattern) {
($objs,$count) = do_grep ($search_pattern,$offset);
} else {
($objs,$count) = do_search($search_class,$search_pattern || '*',$offset);
}
param('query' => param('query') . '*') if !$count && param('query') !~ /\*$/; #autoadd
}
DoRedirect(@$objs) if $count==1;
PrintTop(undef,undef,img({-src=>SEARCH_ICON,-align=>CENTER}).'Simple Search');
print p({-class=>'small'},
"Select the type of object you are looking for and optionally",
"type in a name or a wildcard pattern",
"(? for any one character. * for zero or more characters).",
"If no name is entered, the search displays all objects of the selected type.",
i('Anything'),'searches for the entered text across the entire database.');
display_search_form();
display_search($objs,$count,$offset,$search_class) if $search_class;
PrintBottom();
sub display_search_form {
CGI::autoEscape(0);
print start_form(-name=>'SimpleForm'),
table(
TR({-valign=>TOP},
td(radio_group(-name=>'class',
-Values=>\@classlist,
-Labels=>{@$classlist},
-default=>'Any',
-rows=>3)),
td({-align=>LEFT,-class=>'large'},
b('Name:'),textfield(-name=>'query'),br,
submit(-name=>'Search')
)
),
);
CGI::autoEscape(1);
print end_form();
}
sub do_search {
my ($class,$pattern,$offset) = @_;
my $count;
my (@objs) = $DB->fetch(-class=>$class,-pattern=>$pattern,
-count=>MAXOBJECTS,-offset=>$offset,
-total=>\$count);
return unless @objs;
return (\@objs,$count);
}
sub display_search {
my ($objs,$count,$offset,$class) = @_;
my $label = $class eq 'Any' ? '' :$class;
if ($count > 0) {
print p(strong($count),"$label objects found");
} else {
print p(font{-color=>'red'},'No matching objects found.',
'Try searching again with a * wildcard before or after the name (already added for you).');
return;
}
my @objects;
if ($class eq 'Any') {
@objects = map { a({-href=>Object2URL($_)},$_->class . ": $_") }
sort { $a->class cmp $b->class } @$objs;
} else {
@objects = map { a({-href=>Object2URL($_)},"$_") } @$objs;
}
AceResultsTable(\@objects,$count,$offset);
}
sub do_grep {
my ($text,$offset) = @_;
my $count;
my (@objs) = $DB->grep(-pattern=> $text,
-count => MAXOBJECTS,
-offset => $offset,
-total => \$count,
);
return unless @objs;
return (\@objs,$count);
}
|