Show
Ignore:
Timestamp:
02/29/08 08:17:29 (21 months ago)
Author:
fumiakiy
Message:

Added more extensibility to the new MT::App::Search. BugId:68481

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/release-30/lib/MT/App/Search.pm

    r1421 r1431  
    1111 
    1212use MT::Util qw( encode_html ); 
    13 use MT::Entry qw( :constants ); 
    1413 
    1514sub id { 'new_search' } 
     
    2726    return { 
    2827        'search' => \&process, 
    29         'author' => \&process, 
    3028    }; 
    3129} 
     
    6664            $app->config->$key() : ($q->param($key) || $app->config->$key()); 
    6765    } 
     66 
     67    $app->{searchparam}{Type} = $q->param('type') || 'entry'; 
    6868} 
    6969 
     
    121121    my $app = shift; 
    122122 
    123     my ( $terms, $args ) = $app->search_terms(); 
     123    my @arguments = $app->search_terms(); 
    124124    return $app->error($app->errstr) if $app->errstr; 
    125125 
    126126    my $count = 0; 
    127127    my $iter; 
    128     if ( $terms && @$terms ) { 
    129         ( $count, $iter ) = $app->execute( $terms, $args ); 
     128    if ( @arguments ) { 
     129        ( $count, $iter ) = $app->execute( @arguments ); 
    130130        return $app->error($app->errstr) unless $iter; 
    131131 
     
    147147    my ( $terms, $args ) = @_; 
    148148 
    149     my $class = $app->model( $app->mode eq 'search' ? 'entry' : $app->mode ); 
     149    my $class = $app->model( $app->{searchparam}{Type} ) 
     150        or return $app->errtrans('Unsupported type: [_1]', $app->{searchparam}{Type}); 
    150151    my $count = $app->count( $class, $terms, $args ); 
    151152    #TODO: cache! 
     
    167168    $limit = $max if !$limit || ( $limit - $offset > $max ); 
    168169 
    169     my $type = $q->param('type'); 
    170     my $entry_type = $app->mode eq 'search' 
    171       ? 1 
    172       : $type 
    173         ? ( 'entry' eq lc($type) || 'page' eq lc($type) 
    174           ? 1 
    175           : 0 ) 
    176         : 0; 
    177  
    178     my $def_terms = { 
    179         $entry_type ? ( status => MT::Entry::RELEASE() ) : (), 
    180         $type ? ( class  => $type ) : (), 
    181         exists( $app->{searchparam}{IncludeBlogs} ) 
    182           ? ( blog_id => [ keys %{ $app->{searchparam}{IncludeBlogs} } ] ) 
    183           : (), 
    184     }; 
     170    my $params = $app->registry( $app->{searchparam}{Type} ); 
     171    my %def_terms = exists( $params->{terms} ) 
     172          ? %{ $params->{terms} } 
     173          : (); 
     174    delete $def_terms{'plugin'}; #FIXME: why is this in here? 
     175 
     176    if ( exists $app->{searchparam}{IncludeBlogs} ) { 
     177        $def_terms{blog_id} = [ keys %{ $app->{searchparam}{IncludeBlogs} } ]; 
     178    } 
     179 
    185180    my @terms; 
    186     push @terms, $def_terms if %$def_terms; 
    187  
    188     my $columns = $app->mode eq 'search' 
    189       ? [ [ qw( title keywords text text_more ) ] ] 
    190       : $app->registry( $app->mode, 'columns' ); 
    191     $columns = $columns->[0]; # FIXME: Why? 
    192     return $app->errtrans('No columns to search for was specified for [_1]', $app->mode) 
     181    push @terms, \%def_terms if %def_terms; 
     182 
     183    my $columns = $params->{columns}; 
     184    my $sort = $params->{'sort'}; 
     185    if ( $sort !~ /\w+\!$/  && exists($app->{searchparam}{Sort}) ) { 
     186        $sort = $app->{searchparam}{Sort}; 
     187    } 
     188 
     189    return $app->errtrans('No column was specified to search for [_1].', $app->{searchparam}{Type}) 
    193190        unless $columns && @$columns; 
     191 
    194192    my $number = scalar @$columns; 
    195193    my @and; 
     
    208206      'sort' => [ 
    209207        { desc   => 'descend' eq $app->{searchparam}{ResultDisplay} ? 'DESC' : 'ASC', 
    210           column => $entry_type ? 'authored_on' : 'created_on' } 
     208          column => $sort } 
    211209      ] 
    212210    ); 
     
    296294    $ctx->stash('results', $iter); 
    297295    $ctx->stash('count',   $count); 
    298     $ctx->stash('stash_key', $app->mode) 
    299         if 'search' ne $app->mode; 
     296    $ctx->stash('stash_key', $app->{searchparam}{Type} ); 
    300297    $ctx->stash('include_blogs', 
    301298        join ',', keys %{ $app->{searchparam}{IncludeBlogs} }); 
     
    313310    my $app = shift; 
    314311    my ( $tmpl ) = @_; 
    315     $tmpl 
     312    $tmpl; 
    316313} 
    317314 
     
    322319    my $tmpl = $app->load_search_tmpl( $count, $iter ); 
    323320    $tmpl = $app->pre_render( $tmpl ); 
    324  
    325321    $tmpl; 
    326322}