X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=Memories%2FPhoto.pm;h=5a9b91e56aa133ea4cda60c948a12a6d7e7a9bba;hb=006c40622b5547fcbb693fb37e2216e1d0e7bb6e;hp=bcfb59c39c8f7db2b3811c6e53fa1eff38f23b1f;hpb=f2b33d6a43cde2f4e9c1170f3ae15a5cfb62d09e;p=memories.git diff --git a/Memories/Photo.pm b/Memories/Photo.pm index bcfb59c..5a9b91e 100644 --- a/Memories/Photo.pm +++ b/Memories/Photo.pm @@ -2,25 +2,36 @@ package Memories::Photo; use strict; use Carp qw(cluck confess); use base qw(Memories::DBI Maypole::Model::CDBI::Plain); +use constant INTERESTINGNESS_ALGORITHM => '((rating+3)/(rated+1))*(1+rated/hit_count)*(1+hit_count/(select max(hit_count) from photo))'; use Time::Piece; use Image::Seek; use constant PAGER_SYNTAX => "LimitXY"; __PACKAGE__->columns(Essential => qw(id title uploader uploaded x y rating rated hit_count)); __PACKAGE__->untaint_columns(printable => [qw/title/]); __PACKAGE__->columns(TEMP => qw/exif_object/); -__PACKAGE__->set_sql(recent => q{ -SELECT __ESSENTIAL__ -FROM __TABLE__ -ORDER BY uploaded DESC -LIMIT 4 -}); -__PACKAGE__->set_sql(popular => q{ -SELECT __ESSENTIAL__ -FROM __TABLE__ -ORDER BY hit_count DESC -LIMIT 4 -}); +BEGIN { +my %order_by = ( + recent => "uploaded", + popular => "hit_count", + interesting => INTERESTINGNESS_ALGORITHM +); + +while (my($label, $how) = each %order_by) { + __PACKAGE__->set_sql($label => qq{ + SELECT __ESSENTIAL__ + FROM __TABLE__ + ORDER BY $how DESC + LIMIT 4 + }); + no strict; + *$label = sub { + my ($self, $r) = @_; + $self->view_paged_ordered($r, "$how desc"); + }; + __PACKAGE__->MODIFY_CODE_ATTRIBUTES(*{$label}{CODE}, "Exported"); # Hack +} +} __PACKAGE__->has_many(comments => "Memories::Comment"); __PACKAGE__->has_a(uploader => "Memories::User"); __PACKAGE__->has_a(uploaded => "Time::Piece", @@ -139,16 +150,6 @@ sub view_paged_ordered { $r->template("paged"); # Set the what using the action name } -sub recent :Exported { - my ($self, $r) = @_; - $self->view_paged_ordered($r, "uploaded desc"); -} - -sub popular :Exported { - my ($self, $r) = @_; - $self->view_paged_ordered($r, "hit_count desc"); -} - sub add_comment :Exported { my ($self, $r, $photo) = @_; $r->template("view");