]> git.decadent.org.uk Git - memories.git/blobdiff - Memories/Photo.pm
Add "Highest rated".
[memories.git] / Memories / Photo.pm
index 98444a7bbe0c8a001835c076c610c4a4175cf3a7..e5cb9a0d51cad0d9c73f743ed312bf2f14459868 100644 (file)
@@ -2,19 +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
-});
 
+BEGIN {
+my %order_by = (
+     recent => "uploaded",
+     popular => "hit_count",
+     rated => "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",
@@ -73,6 +92,23 @@ sub do_upload :Exported {
     ); 
 }
 
+sub approx_rating {
+    my $self = shift;
+    $self->rated or return 0;
+    int($self->rating/$self->rated*10)/10;
+}
+
+sub add_rating :Exported {
+    my ($self, $r) = @_;
+    my $photo = $r->{objects}[0];
+    my $delta = $r->{params}{rating};
+    if ($delta < 0 or $delta > 5) { return; } # Scammer
+    # XXX Race
+    $photo->rating($photo->rating() + $delta);
+    $photo->rated($photo->rated() + 1);
+    $r->output(""); # Only used by ajax
+}
+
 sub view :Exported {
     my ($self, $r) = @_;
     my $photo = $r->{objects}[0];
@@ -93,22 +129,27 @@ sub view :Exported {
     }
 }
 sub upload :Exported {}
+sub exif :Exported {}
+sub comment :Exported {}
+sub tagedit :Exported {}
+sub similar :Exported {}
 
 use Class::DBI::Plugin::Pager;
 use Class::DBI::Plugin::AbstractCount;
 
-sub recent :Exported {
-    my ($self, $r) = @_;
+sub view_paged_ordered {
+    my ($self, $r, $how) = @_;
     my $page = $r->params->{page} || 1;
     my $pager = $self->pager(
         per_page => Memories->config->{photos_per_page}, 
         page => $page,
         syntax => PAGER_SYNTAX,
-        order_by => "uploaded desc"
+        order_by => $how
     );
     $r->objects([$pager->retrieve_all ]);
     $r->{template_args}{pager} = $pager;
     $r->last_search;
+    $r->template("paged"); # Set the what using the action name
 }
 
 sub add_comment :Exported {
@@ -265,7 +306,6 @@ sub scale {
     }
 }
 
-use Text::Balanced qw(extract_multiple extract_quotelike);
 sub edit_tags :Exported {
     my ($self, $r) = @_;
     my $photo = $r->objects->[0];
@@ -283,7 +323,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}) })
     }
 }