root/branches/release-34/lib/MT/Template/Context/Search.pm @ 1855

Revision 1855, 5.5 kB (checked in by fumiakiy, 20 months ago)

Stopped sorting search results by blog id in multi blog search thus mix search results from multiple blogs. BugId:75781

Added SearchResultsHeader and SearchResultsFooter template tags.

Gave up on resurrecting per blog limiting of number of results.

  • Property svn:keywords set to Id Date Author Revision
Line 
1# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
2# This program is distributed under the terms of the
3# GNU General Public License, version 2.
4#
5# $Id$
6
7package MT::Template::Context::Search;
8
9use strict;
10use base qw( MT::Template::Context );
11use MT::Util qw( encode_url decode_html );
12
13sub load_core_tags {
14    require MT::Template::Context;
15    return {
16        function => {
17            SearchString => \&_hdlr_search_string,
18            SearchResultCount => \&_hdlr_result_count,
19            MaxResults => \&_hdlr_max_results,
20            SearchIncludeBlogs => \&_hdlr_include_blogs,
21            SearchTemplateID => \&_hdlr_template_id,
22        },
23        block => {
24            SearchResults => \&_hdlr_results,
25            'IfTagSearch?' => sub { MT->instance->mode eq 'tag' },
26            'IfStraightSearch?' => sub { MT->instance->mode eq 'default' },
27            'NoSearchResults?' => sub { $_[0]->stash('count') ? 0 : 1; },
28            'NoSearch?' => sub { ( $_[0]->stash('search_string') &&
29                                   $_[0]->stash('search_string') =~ /\S/ ) ? 0 : 1 },
30            SearchResultsHeader => \&MT::Template::Context::_hdlr_pass_tokens,
31            SearchResultsFooter => \&MT::Template::Context::_hdlr_pass_tokens,
32            BlogResultHeader => \&MT::Template::Context::_hdlr_pass_tokens,
33            BlogResultFooter => \&MT::Template::Context::_hdlr_pass_tokens,
34            'IfMaxResultsCutoff?' => \&MT::Template::Context::_hdlr_pass_tokens,
35        },
36    };
37}
38
39sub _hdlr_include_blogs { $_[0]->stash('include_blogs') || '' }
40sub _hdlr_search_string { $_[0]->stash('search_string') || '' }
41sub _hdlr_template_id { $_[0]->stash('template_id') || '' }
42sub _hdlr_max_results { $_[0]->stash('maxresults') || '' }
43
44sub _hdlr_result_count {
45    my $results = $_[0]->stash('count');
46    $results ? $results : 0;
47}
48
49sub _hdlr_results {
50    my($ctx, $args, $cond) = @_;
51
52    ## If there are no results, return the empty string, knowing
53    ## that the handler for <MTNoSearchResults> will fill in the
54    ## no results message.
55    my $iter = $ctx->stash('results') or return '';
56    my $count = $ctx->stash('count') or return '';
57    my $max = $ctx->stash('maxresults');
58    my $stash_key = $ctx->stash('stash_key') || 'entry';
59
60    my $output = '';
61    my $build = $ctx->stash('builder');
62    my $tokens = $ctx->stash('tokens');
63    my $blog_header = 1;
64    my $blog_footer = 0;
65    my $footer = 0;
66    my $count_per_blog = 0;
67    my $max_reached = 0;
68    my ( $this_object, $next_object );
69    $this_object = $iter->();
70    return '' unless $this_object;
71    for ( my $i = 0; $i < $count; $i++) {
72        $count_per_blog++;
73        $ctx->stash($stash_key, $this_object);
74        local $ctx->{__stash}{blog} = $this_object->blog
75            if $this_object->can('blog');
76        my $ts;
77        if ( $this_object->isa('MT::Entry') ) {
78            $ts = $this_object->authored_on;
79        }
80        elsif ( $this_object->properties->{audit} ) {
81            $ts = $this_object->created_on;
82        }
83        local $ctx->{current_timestamp} = $ts;
84
85        # TODO: per blog max objects?
86        #if ( $count_per_blog >= $max ) {
87        #    while (1) {
88        #        if ( $count > $i + 1 ) {
89        #            $next_object = $results->[$i + 1];
90        #            if ( $next_object->blog_id ne $this_object->blog_id ) {
91        #                $blog_footer = 1;
92        #                last;
93        #            }
94        #            else {
95        #                $max_reached = 1;
96        #            }
97        #        }
98        #        else {
99        #            $next_object = undef;
100        #            $blog_footer = 1;
101        #            last;
102        #        }
103        #        $i++;
104        #    }
105        #}
106        #elsif ( $count > $i + 1 ) {
107        #    $next_object = $results->[$i + 1];
108        #    $blog_footer = $next_object->blog_id ne $this_object->blog_id ? 1 : 0;
109        #}
110        #else {
111        #    $blog_footer = 1;
112        #}
113        if ( $next_object = $iter->() ) {
114            $blog_footer = $next_object->blog_id ne $this_object->blog_id ? 1 : 0;
115        }
116        else {
117            $blog_footer = 1;
118            $footer      = 1;
119        }
120
121        defined(my $out = $build->build($ctx, $tokens,
122            { %$cond, 
123                SearchResultsHeader => $i == 0,
124                SearchResultsFooter => $footer,
125                BlogResultHeader => $blog_header,
126                BlogResultFooter => $blog_footer,
127                IfMaxResultsCutoff => $max_reached,
128            }
129            )) or return $ctx->error( $build->errstr );
130        $output .= $out;
131
132        $this_object = $next_object;
133        last unless $this_object;
134        $blog_header = $blog_footer ? 1 : 0;
135    }
136    $output;
137}
138
139sub context_script {
140        my ( $ctx, $args, $cond ) = @_;
141
142    my $search_string = decode_html( $ctx->stash('search_string') ) ;
143    my $cgipath = $ctx->_hdlr_cgi_path($args);
144    my $script = $ctx->{config}->SearchScript;
145    my $link = $cgipath.$script . '?search=' . encode_url( $search_string );
146    if ( my $mode = $ctx->stash('mode') ) {
147        $mode = encode_url($mode);
148        $link .= "&__mode=$mode";
149    }
150    if ( my $type = $ctx->stash('type') ) {
151        $type = encode_url($type);
152        $link .= "&type=$type";
153    }
154    if ( my $include_blogs = $ctx->stash('include_blogs') ) {
155        $link .= "&IncludeBlogs=$include_blogs";
156    }
157    elsif ( my $blog_id = $ctx->stash('blog_id') ) {
158        $link .= "&blog_id=$blog_id";
159    }
160    if ( my $format = $ctx->stash('format') ) {
161        $link .= '&format=' . encode_url($format);
162    }
163        $link;
164}
165
1661;
167__END__
168
Note: See TracBrowser for help on using the browser.