%doc>
###############################################################################
# A customized version of the generic dynamic_list_query.html
###############################################################################
%doc>
<%args>
$val => $ARGS{crit};
$caller => $ARGS{self};
%args>
<%init>
my $DEBUG = 0;
print "
", Dumper(%ARGS), "
" if $DEBUG;
%init>
<%perl>
if ( $DEBUG ){
use Data::Dumper;
print " ", Dumper(%ARGS), "
";
print &backbone_search("end_id", "137");
} else {
do "jsrsServer.pm";
jsrsDispatch("backbone_search");
}
%perl>
<%shared>
sub backbone_search {
# Arguments:
# - field: Form element to add the results into
# - crit: Criteria. A string with one or more keywords to search
my $field = shift;
my $crit = shift;
my $val = $crit;
my @terms;
if ( $val =~ /\w+/ ) {
if ( $val =~ /\w+\s+\w+/ ){
# if there's more than one word
@terms = split /\s+/, $val;
}else{
$val =~ s/\s+//;
push @terms, $val;
}
# print "terms are: ", join ', ', @terms, "
" if $DEBUG;
}
my $site_id = $terms[0] if ( scalar(@terms) );
my $site;
unless ( $site = Site->retrieve($site_id) ){
$m->comp("error.mhtml", error=>"Can't retrieve site id $site_id");
}
my @closets = $site->closets;
my @results;
my %stored;
my @backbones;
# get backbone cable for each closet
foreach my $closet ( @closets ){
my $c_id = $closet->id;
map { push (@backbones, $_) } (BackboneCable->search(start_closet=>$c_id),
BackboneCable->search(end_closet=>$c_id));
}
# and now go through all of our backbones and add sites to our list.
foreach my $backbone ( @backbones ){
my ($start_site, $end_site);
if ( $backbone->start_closet->room->floor && $backbone->start_closet->room->floor->site ){
$start_site = $backbone->start_closet->room->floor->site;
}
if ( $backbone->end_closet->room->floor && $backbone->end_closet->room->floor->site ){
$end_site = $backbone->end_closet->room->floor->site;
}
if ( $start_site && $end_site ){
if ($start_site->id != $site_id &&
!exists($stored{$start_site})) {
push(@results, $start_site);
$stored{$start_site} = 1;
}
if ($end_site->id != $site_id &&
!exists($stored{$end_site})) {
push(@results, $end_site);
$stored{$end_site} = 1;
}
}
}
# printf("var listdata = new Array();\n");
my $MAX_RESULTS = $ui->config->get('DEFAULT_SELECTMAX');
my $response = $field."&";
if ( scalar(@results ) < $MAX_RESULTS) {
@results = sort { $a->name cmp $b->name } @results;
$response .= "0=".$ui->url_encode("No matches") unless (scalar(@results));
foreach my $end_site ( @results ){
$response .= $end_site->id."=".$ui->url_encode($end_site->name)."&";
}
}else{
$response .= "0=".$ui->url_encode("More than ".$MAX_RESULTS." matches.")."&";
$response .= "0=".$ui->url_encode("Refine search.")."&";
}
return $response;
}
%shared>