~ubuntu-branches/ubuntu/warty/swish-e/warty

« back to all changes in this revision

Viewing changes to example/modules/SWISH/TemplateFrame.pm

  • Committer: Bazaar Package Importer
  • Author(s): Ludovic Drolez
  • Date: 2004-03-11 08:41:07 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040311084107-7vp0mu82blq1qjvo
Tags: 2.4.1-3
Oops ! A comment was not removed to disable interactive compilation.
Closes: Bug#237332

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
=pod
 
2
 
 
3
SWISH::TemplateFrame
 
4
 
 
5
Use with a frameset:
 
6
 
 
7
<html>
 
8
<head>
 
9
<title>Search Swish-e Documenation</title>
 
10
<frameset rows="20%,*" frameborder="0" border="0" framespacing="0">
 
11
    <frame src="swish.cgi">
 
12
    <frame name="bottom">
 
13
</frameset>
 
14
</html>
 
15
 
 
16
=cut
 
17
 
 
18
#=====================================================================
 
19
# These routines format the HTML output.
 
20
#    $Id: TemplateFrame.pm,v 1.1 2003/10/03 23:50:06 whmoseley Exp $
 
21
#=====================================================================
 
22
package SWISH::TemplateFrame;
 
23
use strict;
 
24
 
 
25
use CGI;
 
26
 
 
27
sub show_template {
 
28
    my ( $class, $template_params, $results ) = @_;
 
29
 
 
30
 
 
31
    my $q = $results->CGI;
 
32
 
 
33
 
 
34
 
 
35
    my $output =  $q->header . page_header( $results );
 
36
    
 
37
 
 
38
    unless ( $results->results || $results->errstr ) {
 
39
        $output .= show_form( $results );
 
40
 
 
41
    } else {
 
42
        
 
43
        if ( $results->results ) {
 
44
            $output .= results_header( $results );
 
45
            $output .=  show_result( $results, $_ ) for @{ $results->results };
 
46
           
 
47
            if ( $results->{links} ) {
 
48
                $output .= "<table>$results->{links}</table>";
 
49
            }
 
50
        } else {
 
51
            $output .= '<font size="+2" color="red">'
 
52
                       . $results->errstr || 'unknown error' 
 
53
                       . '</font>';
 
54
        }
 
55
    }
 
56
 
 
57
    $output .=  '</body></html>';
 
58
 
 
59
    print $output;
 
60
 
 
61
}
 
62
 
 
63
#=====================================================================
 
64
# This generates the header
 
65
 
 
66
sub page_header {
 
67
    my $results = shift;
 
68
    my $title = $results->config('title') || 'Search our site with Swish-e';
 
69
    
 
70
 
 
71
    my $html_title = $results->results
 
72
        ? ( $results->navigation('hits')
 
73
            . ' Results for ['
 
74
            . CGI::escapeHTML( $results->{query_simple} )
 
75
            . ']'
 
76
           )
 
77
 
 
78
        : ( $results->errstr || $title );
 
79
 
 
80
    return <<EOF;
 
81
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
82
<html>
 
83
    <head>
 
84
       <title>
 
85
          $html_title
 
86
       </title>
 
87
    </head>
 
88
    <body>
 
89
EOF
 
90
}
 
91
 
 
92
#=====================================================================
 
93
# This generates the form
 
94
#
 
95
#   Pass:
 
96
#       $results hash
 
97
 
 
98
sub show_form {
 
99
 
 
100
    my $results = shift;
 
101
    my $q = $results->{q};
 
102
 
 
103
 
 
104
    my $query = $q->param('query') || '';
 
105
 
 
106
    $query = CGI::escapeHTML( $query );  # May contain quotes
 
107
 
 
108
 
 
109
    # Here's some form components
 
110
    
 
111
    my $meta_select_list    = get_meta_name_limits( $results );
 
112
    my $sorts               = get_sort_select_list( $results );
 
113
    my $select_index        = get_index_select_list( $results );
 
114
    my $limit_select        = get_limit_select( $results );
 
115
    
 
116
    my $date_ranges_select  = $results->get_date_ranges;
 
117
 
 
118
    my $form = $q->script_name;
 
119
 
 
120
    my $advanced_link = qq[<small><a href="$form">advanced form</a></small>]; 
 
121
 
 
122
    my $advanced_form = $q->param('brief')
 
123
                        ? $advanced_link
 
124
                        : <<EOF;
 
125
        $meta_select_list
 
126
        $sorts
 
127
        $select_index
 
128
        $limit_select
 
129
        $date_ranges_select
 
130
EOF
 
131
 
 
132
    my $extra = $results->config('extra_fields');
 
133
    my $hidden = !$extra ? ''
 
134
                 : join "\n", map { $q->hidden($_) } @$extra; 
 
135
 
 
136
 
 
137
    my $title = $results->config('title') || 'Search our site with Swish-e';
 
138
    
 
139
    return <<EOF;
 
140
    
 
141
    <h2>$title</h2>
 
142
 
 
143
    <form method="get" action="$form" enctype="application/x-www-form-urlencoded" class="form" target="bottom">
 
144
        <input maxlength="200" value="$query" size="32" type="text" name="query"/>
 
145
        $hidden
 
146
        <input value="Search!" type="submit" name="submit"/><br>
 
147
 
 
148
        $advanced_form
 
149
    </form>
 
150
EOF
 
151
}
 
152
 
 
153
 
 
154
#=====================================================================
 
155
# This routine creates the results header display
 
156
# and navigation bar
 
157
#
 
158
#
 
159
#
 
160
 
 
161
sub results_header {
 
162
 
 
163
    my $results = shift;
 
164
    my $config = $results->{config};
 
165
    my $q = $results->{q};
 
166
 
 
167
 
 
168
 
 
169
    my $swr = $results->header('removed stopwords');
 
170
    my $stopwords = '';
 
171
 
 
172
 
 
173
    if ( $swr && ref $swr eq 'ARRAY' ) {
 
174
        $stopwords = @$swr > 1
 
175
        ? join( ', ', map { "<b>$_</b>" } @$swr ) . ' are very common words and were not included in your search'
 
176
        : join( ', ', map { "<b>$_</b>" } @$swr ) . ' is a very common word and was not included in your search';
 
177
    }
 
178
 
 
179
    my $limits = '';
 
180
 
 
181
    #  Ok, this is ugly.
 
182
 
 
183
 
 
184
    if ( $results->{DateRanges_time_low} && $results->{DateRanges_time_high} ) {
 
185
        my $low = scalar localtime $results->{DateRanges_time_low};
 
186
        my $high = scalar localtime $results->{DateRanges_time_high};
 
187
        $limits = <<EOF;
 
188
        <tr>
 
189
            <td colspan=2>
 
190
                <font size="-2" face="Geneva, Arial, Helvetica, San-Serif">
 
191
                &nbsp;Results limited to dates $low to $high
 
192
                </font>
 
193
            </td>
 
194
        </tr>
 
195
EOF
 
196
    }
 
197
 
 
198
    my $query_href = $results->{query_href};
 
199
    my $query_simple = CGI::escapeHTML( $results->{query_simple} );
 
200
    my $pages       = $results->navigation('pages');
 
201
 
 
202
    my $prev        = $results->navigation('prev');
 
203
    my $prev_count  = $results->navigation('prev_count');
 
204
    my $next        = $results->navigation('next');
 
205
    my $next_count  = $results->navigation('next_count');
 
206
 
 
207
    my $hits        = $results->navigation('hits');
 
208
    my $from        = $results->navigation('from');
 
209
    my $to          = $results->navigation('to');
 
210
 
 
211
    my $run_time    = $results->navigation('run_time');
 
212
    my $search_time = $results->navigation('search_time');
 
213
 
 
214
 
 
215
 
 
216
 
 
217
 
 
218
    my $links = '';
 
219
 
 
220
    $links .= '<font size="-1" face="Geneva, Arial, Helvetica, San-Serif">&nbsp;Page:</font>' . $pages
 
221
        if $pages;
 
222
 
 
223
    $links .= qq[ <a href="$query_href&amp;start=$prev">Previous $prev_count</a>]
 
224
        if $prev_count;
 
225
 
 
226
    $links .= qq[ <a href="$query_href&amp;start=$next">Next $next_count</a>]
 
227
        if $next_count;
 
228
 
 
229
 
 
230
    # Save for the bottom of the screen.
 
231
    $results->{LINKS} = $links;
 
232
 
 
233
    $links = qq[<tr><td colspan="2" bgcolor="#EEEEEE">$links</td></tr>] if $links;
 
234
 
 
235
    $query_simple = $query_simple
 
236
        ? "&nbsp;Results for <b>$query_simple</b>"
 
237
        : '';
 
238
 
 
239
 
 
240
    $results->{links} = $links if $links;
 
241
 
 
242
    return <<EOF;
 
243
 
 
244
    <table cellpadding="0" cellspacing="0" border="0" width="100%">
 
245
        <tr>
 
246
            <td height=20 bgcolor="#FF9999">
 
247
                <font size="-1" face="Geneva, Arial, Helvetica, San-Serif">
 
248
                $query_simple
 
249
                &nbsp; $from to $to of $hits results.
 
250
                </font>
 
251
            </td>
 
252
            <td align=right bgcolor="#FF9999">
 
253
                <font size="-2" face="Geneva, Arial, Helvetica, San-Serif">
 
254
                Run time: $run_time |
 
255
                Search time: $search_time &nbsp; &nbsp;
 
256
                </font>
 
257
            </td>
 
258
        </tr>
 
259
 
 
260
        $links
 
261
        $limits
 
262
        $stopwords
 
263
 
 
264
    </table>
 
265
 
 
266
EOF
 
267
 
 
268
}
 
269
 
 
270
#=====================================================================
 
271
# This routine formats a single result
 
272
#
 
273
#
 
274
sub show_result {
 
275
    my ($results, $this_result ) = @_;
 
276
 
 
277
    my $conf = $results->{conf};
 
278
 
 
279
    my $DocTitle = $results->config('title_property') || 'swishtitle';
 
280
 
 
281
 
 
282
    my $title = $this_result->{$DocTitle} || $this_result->{swishdocpath} || '?';
 
283
 
 
284
    my $name_labels = $results->config('name_labels');
 
285
 
 
286
 
 
287
 
 
288
    # The the properties to display
 
289
 
 
290
    my $props = '';
 
291
 
 
292
    my $display_props = $results->config('display_props');
 
293
    if ( $display_props ) {
 
294
 
 
295
 
 
296
        $props = join "\n",
 
297
            '<br><table cellpadding="0" cellspacing="0">',
 
298
            map ( {
 
299
                '<tr><td><small>'
 
300
                . ( $name_labels->{$_} || $_ )
 
301
                . ':</small></td><td><small> '
 
302
                . '<b>'
 
303
                . ( defined $this_result->{$_} ?  $this_result->{$_} : '' ) 
 
304
                . '</b>'
 
305
                . '</small></td></tr>'
 
306
                 }   @$display_props
 
307
            ),
 
308
            '</table>';
 
309
    }
 
310
 
 
311
 
 
312
    my $description_prop = $results->config('description_prop');
 
313
 
 
314
    my $description = '';
 
315
    if ( $description_prop ) {
 
316
        $description = $this_result->{ $description_prop } || '';
 
317
    }
 
318
 
 
319
 
 
320
    return <<EOF;
 
321
    <dl>
 
322
        <dt>$this_result->{swishreccount} <a href="$this_result->{swishdocpath_href}">$title</a> <small>-- rank: <b>$this_result->{swishrank}</b></small></dt>
 
323
        <dd>$description
 
324
 
 
325
        $props
 
326
        </dd>
 
327
    </dl>
 
328
 
 
329
EOF
 
330
 
 
331
}
 
332
 
 
333
 
 
334
#==================================================================
 
335
#  Form setup for sorts and metas
 
336
#
 
337
#  This could be methods of $results object
 
338
#  (and then available for Template-Toolkit)
 
339
#  But that's too much HTML in the object, perhaps.
 
340
#
 
341
#
 
342
#==================================================================
 
343
 
 
344
sub get_meta_name_limits {
 
345
    my ( $results ) = @_;
 
346
 
 
347
    my $metanames = $results->config('metanames');
 
348
    return '' unless $metanames;
 
349
 
 
350
 
 
351
    my $name_labels = $results->config('name_labels');
 
352
    my $q = $results->CGI;
 
353
 
 
354
 
 
355
    return join "\n",
 
356
        'Limit search to:',
 
357
        $q->radio_group(
 
358
            -name   =>'metaname',
 
359
            -values => $metanames,
 
360
            -default=>$metanames->[0],
 
361
            -labels =>$name_labels
 
362
        ),
 
363
        '<br>';
 
364
}
 
365
 
 
366
sub get_sort_select_list {
 
367
    my ( $results ) = @_;
 
368
 
 
369
    my $sort_metas = $results->config('sorts');
 
370
    return '' unless $sort_metas;
 
371
 
 
372
    
 
373
    my $name_labels = $results->config('name_labels');
 
374
    my $q = $results->CGI;
 
375
 
 
376
 
 
377
 
 
378
    return join "\n",
 
379
        'Sort by:',
 
380
        $q->popup_menu(
 
381
            -name   =>'sort',
 
382
            -values => $sort_metas,
 
383
            -default=>$sort_metas->[0],
 
384
            -labels =>$name_labels
 
385
        ),
 
386
        $q->checkbox(
 
387
            -name   => 'reverse',
 
388
            -label  => 'Reverse Sort'
 
389
        );
 
390
}
 
391
 
 
392
 
 
393
 
 
394
sub get_index_select_list {
 
395
    my ( $results ) = @_;
 
396
    my $q = $results->CGI;
 
397
 
 
398
 
 
399
    my $indexes = $results->config('swish_index');
 
400
    return '' unless ref $indexes eq 'ARRAY';
 
401
 
 
402
    my $select_config = $results->config('select_indexes');
 
403
    return '' unless $select_config && ref $select_config eq 'HASH';
 
404
 
 
405
 
 
406
    # Should return a warning, as this might be a likely mistake
 
407
    # This jumps through hoops so that real index file name is not exposed
 
408
    
 
409
    return '' unless exists $select_config->{labels}
 
410
              && ref $select_config->{labels} eq 'ARRAY'
 
411
              && @$indexes == @{$select_config->{labels}};
 
412
 
 
413
 
 
414
    my @labels = @{$select_config->{labels}};
 
415
    my %map;
 
416
 
 
417
    for ( 0..$#labels ) {
 
418
        $map{$_} = $labels[$_];
 
419
    }
 
420
 
 
421
    my $method = $select_config->{method} || 'checkbox_group';
 
422
    my @cols = $select_config->{columns} ? ('-columns', $select_config->{columns}) : ();
 
423
 
 
424
    return join "\n",
 
425
        '<br>',
 
426
        ( $select_config->{description} || 'Select: '),
 
427
        $q->$method(
 
428
        -name   => 'si',
 
429
        -values => [0..$#labels],
 
430
        -default=> 0,
 
431
        -labels => \%map,
 
432
        @cols );
 
433
}
 
434
 
 
435
 
 
436
sub get_limit_select {
 
437
    my ( $results ) = @_;
 
438
    my $q = $results->CGI;
 
439
 
 
440
 
 
441
    my $limit = $results->config('select_by_meta');
 
442
    return '' unless ref $limit eq 'HASH';
 
443
 
 
444
    my $method = $limit->{method} || 'checkbox_group';
 
445
 
 
446
    my @options = (
 
447
        -name   => 'sbm',
 
448
        -values => $limit->{values},
 
449
        -labels => $limit->{labels} || {},
 
450
    );
 
451
 
 
452
    push @options, ( -columns=> $limit->{columns} ) if $limit->{columns};
 
453
    
 
454
 
 
455
    return join "\n",
 
456
        '<br>',
 
457
        ( $limit->{description} || 'Select: '),
 
458
        $q->$method( @options );
 
459
}
 
460
1;
 
461