X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=Memories%2FPhoto.pm;h=57a53c46d93b9c68471195f0ac070e3206444fa3;hb=090e12c1f7141a6d5a25014fc33e9b1cc382c0b7;hp=ce0cd2003a777277b06e5e535389e14c1be033ba;hpb=0462f25b10e5adb4eacc0e11f4ced5165784f38f;p=memories.git diff --git a/Memories/Photo.pm b/Memories/Photo.pm index ce0cd20..57a53c4 100644 --- a/Memories/Photo.pm +++ b/Memories/Photo.pm @@ -2,25 +2,38 @@ 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", + loved => "rating/(1+rated)", + interesting => INTERESTINGNESS_ALGORITHM, + random => "rand()" +); + +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", @@ -32,7 +45,6 @@ sub do_upload :Exported { my ($self, $r) = @_; my %upload = $r->upload("photo"); - # XXX Stop anonymous uploads! my $photo = $self->create({ uploader => $r->user, uploaded => Time::Piece->new(), @@ -139,16 +151,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"); @@ -303,7 +305,6 @@ sub scale { } } -use Text::Balanced qw(extract_multiple extract_quotelike); sub edit_tags :Exported { my ($self, $r) = @_; my $photo = $r->objects->[0]; @@ -321,7 +322,7 @@ sub edit_tags :Exported { sub add_tags { my ($photo, $tagstring) = @_; - for my $tag (map { s/^"|"$//g; $_} extract_multiple(lc $tagstring, [ \&extract_quotelike, qr/([^\s]+)/ ], undef,1)) { + for my $tag (Tagtools->separate_tags($tagstring)) { $photo->add_to_tags({tag => Memories::Tag->find_or_create({name =>$tag}) }) } }