1 |
use strict; |
2 |
use warnings; |
3 |
|
4 |
=head1 NAME |
5 |
|
6 |
Grep::Action::Fetch |
7 |
|
8 |
=cut |
9 |
|
10 |
package Grep::Action::Fetch; |
11 |
use base qw/Grep::Action Jifty::Action/; |
12 |
|
13 |
use XML::Feed; |
14 |
use LWP::UserAgent; |
15 |
|
16 |
use Grep::Search; |
17 |
use Grep::Source; |
18 |
|
19 |
use Data::Dump qw/dump/; |
20 |
|
21 |
use Jifty::Param::Schema; |
22 |
use Jifty::Action schema { |
23 |
|
24 |
param q => |
25 |
type is 'text', |
26 |
label is 'Search for', |
27 |
hint is 'enter few words to search for'; |
28 |
|
29 |
param feed => |
30 |
label is 'From feed', |
31 |
render as 'select', |
32 |
available are defer { |
33 |
my $feeds = Grep::Model::FeedCollection->new; |
34 |
$feeds->order_by({ column => 'title', order => 'ASC' }); |
35 |
$feeds->unlimit; |
36 |
warn "feeds ", $feeds->build_select_query; |
37 |
[{ |
38 |
display_from => 'title', |
39 |
value_from => 'id', |
40 |
collection => $feeds, |
41 |
}]; |
42 |
}; |
43 |
|
44 |
param item_fragment => |
45 |
label is 'Show', |
46 |
render as 'select', |
47 |
# valid are qw/result result_short/, |
48 |
# available are qw/result result_short/; |
49 |
available are qw/long short title/; |
50 |
|
51 |
}; |
52 |
|
53 |
=head2 take_action |
54 |
|
55 |
Returns C<Grep::Model::ItemCollection> of fatched items from Feed which will |
56 |
also be stored in local cache. |
57 |
|
58 |
=cut |
59 |
|
60 |
sub take_action { |
61 |
my $self = shift; |
62 |
|
63 |
# Custom action code |
64 |
|
65 |
my $feed_id = $self->argument_value('feed') or die "no feed?"; |
66 |
my $q = $self->argument_value('q') or die "no q?"; |
67 |
|
68 |
my $feed = Grep::Model::Feed->new(); |
69 |
$feed->load_by_cols( id => $feed_id ); |
70 |
die "can't load feed ", $feed_id unless ($feed->id); |
71 |
|
72 |
my $source = Grep::Source->new({ feed => $feed }); |
73 |
my $items = $source->search( $q ); |
74 |
my $new = $source->new_items; |
75 |
|
76 |
if ( my $count = $items->count ) { |
77 |
|
78 |
# construct a proper sentence :-) |
79 |
my $message = $count |
80 |
. ( $new == $count ? ' new' : '' ) |
81 |
. ( $new == 0 ? ' old' : '' ) |
82 |
. ' results ' |
83 |
. ( $new && $new < $count ? "of which $new new " : '' ) |
84 |
. "for '$q' in " . $source->feed->title; |
85 |
|
86 |
$self->result->message( $message ); |
87 |
|
88 |
$self->result->content( items => $items ); |
89 |
$self->result->content( count => $items->count ); |
90 |
|
91 |
return $items; |
92 |
|
93 |
} else { |
94 |
return $self->result->error( "No results for '$q' in " . $feed->title ); |
95 |
} |
96 |
} |
97 |
|
98 |
|
99 |
1; |
100 |
|