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