| 1 | package Rapi::Blog::DB::ResultSet::Post; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Moo; | ||
| 7 | extends 'DBIx::Class::ResultSet'; | ||
| 8 | |||
| 9 | use RapidApp::Util ':all'; | ||
| 10 | use Rapi::Blog::Util; | ||
| 11 | |||
| 12 | __PACKAGE__->load_components(qw(Helper::ResultSet::CorrelateRelationship)); | ||
| 13 | |||
| 14 | sub published { | ||
| 15 | (shift) | ||
| 16 | ->search_rs({ 'me.published' => 1 }) | ||
| 17 | } | ||
| 18 | |||
| 19 | sub newest_first { | ||
| 20 | (shift) | ||
| 21 | ->search_rs(undef,{ | ||
| 22 | order_by => { -desc => 'me.ts' } | ||
| 23 | }) | ||
| 24 | } | ||
| 25 | |||
| 26 | sub newest_published_first { | ||
| 27 | (shift) | ||
| 28 | ->search_rs(undef,{ | ||
| 29 | order_by => { -desc => 'me.publish_ts' } | ||
| 30 | }) | ||
| 31 | } | ||
| 32 | |||
| 33 | sub most_hits_first { | ||
| 34 | my $self = shift; | ||
| 35 | $self->search_rs(undef, { | ||
| 36 | order_by => { '-desc' => $self->correlate('hits')->count_rs->as_query }, | ||
| 37 | }) | ||
| 38 | } | ||
| 39 | |||
| 40 | sub most_comments_first { | ||
| 41 | my $self = shift; | ||
| 42 | $self->search_rs(undef, { | ||
| 43 | order_by => { '-desc' => $self->correlate('comments')->count_rs->as_query }, | ||
| 44 | }) | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | sub permission_filtered { | ||
| 49 | my $self = shift; | ||
| 50 | my $User = Rapi::Blog::Util->get_User or return $self->published; | ||
| 51 | |||
| 52 | return $self if ($User->admin); | ||
| 53 | |||
| 54 | $self->search_rs({ -or => [ | ||
| 55 | { 'me.published' => 1 }, | ||
| 56 | { 'me.author_id' => $User->id } | ||
| 57 | ]}); | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | sub _all_columns_except { | ||
| 63 | my ($self, @exclude) = @_; | ||
| 64 | scalar(@exclude) > 0 or return $self; | ||
| 65 | |||
| 66 | my %excl = map {$_=>1} @exclude; | ||
| 67 | my @cols = grep { ! $excl{$_} } $self->result_source->columns; | ||
| 68 | |||
| 69 | $self->search_rs(undef,{ columns => \@cols }); | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | __PACKAGE__->load_components('+Rapi::Blog::DB::Component::ResultSet::ListAPI'); | ||
| 74 | |||
| 75 | sub _api_default_params {{ limit => 20 }} | ||
| 76 | sub _api_param_arg_order { [qw/search tag page limit sort category section_id/] } | ||
| 77 | sub _api_params_undef_map {{ section_id => 'none' }}; | ||
| 78 | |||
| 79 | # Method exposed to templates: | ||
| 80 | |||
| 81 | |||
| 82 | sub list_posts { | ||
| 83 | (shift) | ||
| 84 | ->_all_columns_except('body') | ||
| 85 | ->_list_posts_rs(@_) | ||
| 86 | ->_list_api | ||
| 87 | } | ||
| 88 | |||
| 89 | sub get_posts { | ||
| 90 | (shift) | ||
| 91 | ->_list_posts_rs(@_) | ||
| 92 | ->_list_api | ||
| 93 | } | ||
| 94 | |||
| 95 | sub _list_posts_rs { | ||
| 96 | my ($self, @args) = @_; | ||
| 97 | |||
| 98 | my $P = $self->_list_api_params(@args); | ||
| 99 | |||
| 100 | my $Rs = $self | ||
| 101 | ->published | ||
| 102 | ->newest_first | ||
| 103 | ->search_rs(undef, { | ||
| 104 | join => ['post_tags','post_categories'], | ||
| 105 | group_by => 'me.id' | ||
| 106 | }) | ||
| 107 | ; | ||
| 108 | |||
| 109 | if($P->{search}) { | ||
| 110 | my $as_tag = $P->{search}; | ||
| 111 | $as_tag =~ s/\s+/\-/g; | ||
| 112 | $as_tag =~ s/\_/\-/g; | ||
| 113 | |||
| 114 | $Rs = $Rs->search_rs({ -or => [ | ||
| 115 | { 'post_tags.tag_name' => lc($as_tag) }, | ||
| 116 | { 'post_categories.category_name' => { like => join('','%',$P->{search},'%') } }, | ||
| 117 | { 'me.name' => { like => join('','%',$P->{search},'%') } }, | ||
| 118 | { 'me.title' => { like => join('','%',$P->{search},'%') } }, | ||
| 119 | { 'me.summary' => { like => join('','%',$P->{search},'%') } }, | ||
| 120 | { 'me.body' => { like => join('','%',$P->{search},'%') } } | ||
| 121 | ]}); | ||
| 122 | } | ||
| 123 | |||
| 124 | $Rs = $Rs->search_rs({ 'post_tags.tag_name' => $P->{tag} }) if ($P->{tag}); | ||
| 125 | $Rs = $Rs->search_rs({ 'post_categories.category_name' => $P->{category} }) if ($P->{category}); | ||
| 126 | |||
| 127 | $Rs = $Rs->search_rs( | ||
| 128 | { 'author.username' => $P->{username} }, | ||
| 129 | { join => 'author' } | ||
| 130 | ) if ($P->{username}); | ||
| 131 | |||
| 132 | if(my $sort = $P->{sort}) { | ||
| 133 | if($sort eq 'newest') { | ||
| 134 | $Rs = $Rs->newest_first; | ||
| 135 | } | ||
| 136 | elsif($sort eq 'popularity') { | ||
| 137 | $Rs = $Rs->most_hits_first; | ||
| 138 | } | ||
| 139 | elsif($sort eq 'most_comments'){ | ||
| 140 | $Rs = $Rs->most_comments_first; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | if(exists $P->{section_id}) { | ||
| 145 | $Rs = $Rs->search_rs({ 'me.section_id' => $P->{section_id} }); | ||
| 146 | } | ||
| 147 | elsif($P->{under_section_id}) { | ||
| 148 | $Rs = $Rs->search_rs( | ||
| 149 | { 'trk_section_posts.section_id' => $P->{under_section_id} }, | ||
| 150 | { join => 'trk_section_posts' } | ||
| 151 | ); | ||
| 152 | } | ||
| 153 | |||
| 154 | return $Rs | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | 1; |