]> git.decadent.org.uk Git - memories.git/blobdiff - Memories/Photo.pm
Add interface to "most popular photos"
[memories.git] / Memories / Photo.pm
index a0c3f1bbc1d664a980454538029bcca78116f325..ce0cd2003a777277b06e5e535389e14c1be033ba 100644 (file)
@@ -5,7 +5,7 @@ use base qw(Memories::DBI Maypole::Model::CDBI::Plain);
 use Time::Piece;
 use Image::Seek;
 use constant PAGER_SYNTAX => "LimitXY";
-__PACKAGE__->columns(Essential => qw(id title uploader uploaded x y));
+__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{
@@ -14,6 +14,12 @@ FROM __TABLE__
 ORDER BY uploaded DESC
 LIMIT 4
 });
+__PACKAGE__->set_sql(popular => q{
+SELECT __ESSENTIAL__
+FROM __TABLE__
+ORDER BY hit_count DESC
+LIMIT 4
+});
 
 __PACKAGE__->has_many(comments => "Memories::Comment");
 __PACKAGE__->has_a(uploader => "Memories::User");
@@ -30,7 +36,10 @@ sub do_upload :Exported {
     my $photo = $self->create({
         uploader => $r->user,
         uploaded => Time::Piece->new(),
-        title => ($r->params->{title} || $upload{filename})
+        title => $r->params->{title},
+        hit_count => 0,
+        rating => 0,
+        rated => 0, # Oh, the potential for divide by zero errors...
     });
 
     # Dump content
@@ -45,9 +54,13 @@ sub do_upload :Exported {
     $photo->x($x); $photo->y($y);
 
     # Rotate?
-    $photo->unrotate();
+    $photo->unrotate(); 
+    if (!$photo->title){ 
+        $photo->title($photo->title_exif || $upload{filename});
+    }
 
     $photo->make_thumb;
+    $r->{params}{tags} ||= join " ", map { qq{"$_"} } $photo->tags_exif;
     $photo->add_tags($r->{params}{tags});
     $photo->add_to_imageseek_library;
     Memories->zap_cache();
@@ -66,10 +79,28 @@ 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];
+    $photo->hit_count($photo->hit_count()+1);
     if ($r->{session}{last_search}) {
-        my $photo = $r->{objects}[0];
         # This is slightly inefficient
         my @search = split/,/, $r->{session}{last_search};
         my $found = -1;
@@ -85,22 +116,37 @@ 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 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 {
@@ -179,7 +225,7 @@ my %banned_tags = map { $_ => 1 }
 
 sub _exif_info {
     my $exifTool = new Image::ExifTool;
-    $exifTool->Options(Group0 => ['IPTC', 'EXIF', 'MakerNotes', 'Composite']);
+    $exifTool->Options(Group0 => ['IPTC', 'EXIF', 'XMP', 'MakerNotes', 'Composite']);
     my $info = $exifTool->ImageInfo(shift->path);
     my $hash = {};
     foreach my $tag ($exifTool->GetFoundTags('Group0')) {
@@ -280,15 +326,50 @@ sub add_tags {
     }
 }
 
+# Work out some common properties from a set of potential photo metadata
+# tags
+sub _grovel_metadata {
+    my ($self, @tags) = @_;
+    my %md = map {%$_} values %{$self->exif_info};
+    for (@tags) {
+        if ($md{$_} and $md{$_} =~/[^ 0:]/) { return $md{$_} }
+    }
+    return;
+}
+
 sub shot {
-  my $self = shift;
-  my $exif = $self->exif_info->{EXIF};
-  my ($dt) =
-    grep {$_ and /[^ 0:]/} 
-        ($exif->{ 'Shooting Date/Time' },
-         $exif->{ 'Date/Time Of Digitization' },
-         $exif->{ 'Date/Time Of Last Modification' });
-  if (!$dt) { return $self->uploaded }
-  return Time::Piece->strptime($dt, "%Y:%m:%d %T") || $self->uploaded;
+    my $self = shift;
+    my $dt = $self->_grovel_metadata(
+        'Shooting Date/Time',
+        'Date/Time Of Digitization',
+        'Date/Time Of Last Modification'
+    );
+    if (!$dt) { return $self->uploaded }
+    return Time::Piece->strptime($dt, "%Y:%m:%d %T") || $self->uploaded;
+}
+
+sub description {
+    shift->_grovel_metadata(
+        'Description', 'Image Description', 'Caption-Abstract'
+    );
+}
+
+sub title_exif { shift->_grovel_metadata( 'Headline', 'Title'); }
+sub license { shift->_grovel_metadata( 'Rights Usage Terms', 'Usage Terms' ) }
+sub copyright { shift->_grovel_metadata( 'Rights', 'Copyright', 'Copyright Notice') }
+
+# This one's slightly different since we want everything we can get...
+sub tags_exif {
+    my $self = shift;
+    my %md = map {%$_} values %{$self->exif_info};
+    my %tags = 
+        map { s/\s+/-/g; lc $_ => 1  }
+        map { split /\s*,\s*/, $md{$_}}
+        grep {$md{$_} and $md{$_} =~/[^ 0:]/}
+        (qw(Keywords Subject City State Location Country Province-State), 
+        'Transmission Reference', 'Intellectual Genre', 
+        'Country-Primary Location Name'
+        );
+    return keys %tags;
 }
 1;