--- Webpacus/lib/Webpacus/Controller/Search.pm 2005/11/25 01:24:31 143 +++ Webpacus/lib/Webpacus/Controller/Search.pm 2005/11/26 01:54:31 153 @@ -4,9 +4,12 @@ use warnings; use base 'Catalyst::Controller'; +use Data::Dumper; + use lib '/data/webpac2/lib'; use WebPAC::Search::Estraier; + =head1 NAME Webpacus::Controller::Search - Catalyst Controller @@ -79,12 +82,87 @@ $c->res->body( $c->prototype->auto_complete_result( \@suggestions ) ); } + +# specify all Hyper Estraier operators which should stop this module +# from splitting search query and joining it with default operator +my $hest_op_regex = qr/(:?\[(:?BW|EW|RX)\]|AND|OR|ANDNOT)/; + +=item results + +Mothod which uses C and returns results for search query + +=cut + +sub results : Local { + my ( $self, $c ) = @_; + + my $webpac = $c->comp('Model::WebPAC'); + my $params = $c->req->params; + my $log = $c->log; + + $log->debug("results got params: " . Dumper( $params ) ); + + my @attr; + my @words; + # default operator to join fields/words + my $operator = 'AND'; + + foreach my $f (keys %{ $params }) { + + next if ($f =~ m/^_/o); + + my $v = $params->{$f} || next; + + if (my $op = $params->{ '_' . $f}) { + if ($v =~ $hest_op_regex) { + # don't split words if there is Hyper Estraier + # operator in them + push @words, $v; + } else { + push @words, join(" $op ", split(/\s+/, $v) ); + } + } else { + push @words, $v; + } + + next if ($f eq 'all'); # don't add_attr for magic field all + + if ($v !~ /\s/) { + push @attr, "$f ISTRINC $v"; + } else { + map { + push @attr, "$f ISTRINC $_"; + } grep { ! $hest_op_regex } split(/\s+/, $v); + } + } + + my $q = join(" $operator ", @words); + + my $template = $params->{'_template'}; + $log->debug("using template $template"); + + $c->stash->{html_results} = sub { + my $res = $webpac->search( + phrase => $q, + template => $template, + add_attr => \@attr + ); + # $log->debug("controller got " . ( $#{$res} + 1 ) . " results for '$q' " . Dumper( $res )); + return $res; + }; + + $c->stash->{phrase} = $q; + $c->stash->{attr} = \@attr; + + $c->stash->{template} = 'results.tt'; +} + =back =head1 AUTHOR -Dobrica Pavlinusic,,, +Dobrica Pavlinusic C<< >> =head1 LICENSE