root/branches/release-33/lib/MT/Template/Context/Search.pm @ 1708

Revision 1708, 5.2 kB (checked in by fumiakiy, 20 months ago)

Added ability to filter search by author and/or category. BugId:69286

  • 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            BlogResultHeader => \&MT::Template::Context::_hdlr_pass_tokens,
31            BlogResultFooter => \&MT::Template::Context::_hdlr_pass_tokens,
32            'IfMaxResultsCutoff?' => \&MT::Template::Context::_hdlr_pass_tokens,
33        },
34    };
35}
36
37sub _hdlr_include_blogs { $_[0]->stash('include_blogs') || '' }
38sub _hdlr_search_string { $_[0]->stash('search_string') || '' }
39sub _hdlr_template_id { $_[0]->stash('template_id') || '' }
40sub _hdlr_max_results { $_[0]->stash('maxresults') || '' }
41
42sub _hdlr_result_count {
43    my $results = $_[0]->stash('count');
44    $results ? $results : 0;
45}
46
47sub _hdlr_results {
48    my($ctx, $args, $cond) = @_;
49
50    ## If there are no results, return the empty string, knowing
51    ## that the handler for <MTNoSearchResults> will fill in the
52    ## no results message.
53    my $iter = $ctx->stash('results') or return '';
54    my $count = $ctx->stash('count') or return '';
55    my $max = $ctx->stash('maxresults');
56    my $stash_key = $ctx->stash('stash_key') || 'entry';
57
58    my $output = '';
59    my $build = $ctx->stash('builder');
60    my $tokens = $ctx->stash('tokens');
61    my $blog_header = 1;
62    my $blog_footer = 0;
63    my $count_per_blog = 0;
64    my $max_reached = 0;
65    my ( $this_object, $next_object );
66    $this_object = $iter->();
67    return '' unless $this_object;
68    for ( my $i = 0; $i < $count; $i++) {
69        $count_per_blog++;
70        $ctx->stash($stash_key, $this_object);
71        local $ctx->{__stash}{blog} = $this_object->blog
72            if $this_object->can('blog');
73        my $ts;
74        if ( $this_object->isa('MT::Entry') ) {
75            $ts = $this_object->authored_on;
76        }
77        elsif ( $this_object->properties->{audit} ) {
78            $ts = $this_object->created_on;
79        }
80        local $ctx->{current_timestamp} = $ts;
81
82        # TODO: per blog max objects?
83        #if ( $count_per_blog >= $max ) {
84        #    while (1) {
85        #        if ( $count > $i + 1 ) {
86        #            $next_object = $results->[$i + 1];
87        #            if ( $next_object->blog_id ne $this_object->blog_id ) {
88        #                $blog_footer = 1;
89        #                last;
90        #            }
91        #            else {
92        #                $max_reached = 1;
93        #            }
94        #        }
95        #        else {
96        #            $next_object = undef;
97        #            $blog_footer = 1;
98        #            last;
99        #        }
100        #        $i++;
101        #    }
102        #}
103        #elsif ( $count > $i + 1 ) {
104        #    $next_object = $results->[$i + 1];
105        #    $blog_footer = $next_object->blog_id ne $this_object->blog_id ? 1 : 0;
106        #}
107        #else {
108        #    $blog_footer = 1;
109        #}
110        if ( $next_object = $iter->() ) {
111            $blog_footer = $next_object->blog_id ne $this_object->blog_id ? 1 : 0;
112        }
113        else {
114            $blog_footer = 1;
115        }
116
117        defined(my $out = $build->build($ctx, $tokens,
118            { %$cond, 
119                BlogResultHeader => $blog_header,
120                BlogResultFooter => $blog_footer,
121                IfMaxResultsCutoff => $max_reached,
122            }
123            )) or return $ctx->error( $build->errstr );
124        $output .= $out;
125
126        $this_object = $next_object;
127        last unless $this_object;
128        $blog_header = $blog_footer ? 1 : 0;
129    }
130    $output;
131}
132
133sub context_script {
134        my ( $ctx, $args, $cond ) = @_;
135
136    my $search_string = decode_html( $ctx->stash('search_string') ) ;
137    my $cgipath = $ctx->_hdlr_cgi_path($args);
138    my $script = $ctx->{config}->SearchScript;
139    my $link = $cgipath.$script . '?search=' . encode_url( $search_string );
140    if ( my $mode = $ctx->stash('mode') ) {
141        $mode = encode_url($mode);
142        $link .= "&__mode=$mode";
143    }
144    if ( my $type = $ctx->stash('type') ) {
145        $type = encode_url($type);
146        $link .= "&type=$type";
147    }
148    if ( my $include_blogs = $ctx->stash('include_blogs') ) {
149        $link .= "&IncludeBlogs=$include_blogs";
150    }
151    elsif ( my $blog_id = $ctx->stash('blog_id') ) {
152        $link .= "&blog_id=$blog_id";
153    }
154    if ( my $format = $ctx->stash('format') ) {
155        $link .= '&format=' . encode_url($format);
156    }
157        $link;
158}
159
1601;
161__END__
162
Note: See TracBrowser for help on using the browser.