]> git.decadent.org.uk Git - memories.git/blob - Memories/Tag.pm
e1afdd2f544715c613c9e7a44742c04084055ed0
[memories.git] / Memories / Tag.pm
1 package Memories::Tag;
2 use strict;
3 use base qw(Memories::DBI Maypole::Model::CDBI::Plain);
4 __PACKAGE__->columns(Essential => qw/id name/);
5
6 Memories::Photo->set_sql(sorted_by_tag => q/
7 SELECT photo.id as id, title, uploader, uploaded, x, y
8 FROM photo, tag, tagging
9 WHERE tagging.photo = photo.id
10     AND tagging.tag = tag.id
11     AND tag.id = ?
12 ORDER BY photo.uploaded DESC
13 /
14 );
15
16 sub view :Exported {
17     my ($self, $r) = @_;
18     my $tag;
19     my $page = $r->params->{page} || 1;
20     my $pager = Class::DBI::Pager::_pager("Memories::Photo",
21         Memories->config->{photos_per_page}, $page);
22     $r->{template_args}{pager} = $pager;
23     my @tags = map { $self->search(name => $_)->first } @{$r->args};
24
25     if (@{$r->args} > 1) { # This is actually an n-level search
26         my $sth = $self->multi_search(@tags);
27         $r->{template_args}{photos} = [ $r->{template_args}{pager}->sth_to_objects($sth) ];
28         $sth->finish;
29         $r->{template_args}{tags} = \@tags;
30     } else {
31         if (!$r->objects) {
32             $tag = $tags[0];
33         } else {
34             $tag = $r->objects->[0];
35         }
36         $r->{template_args}{tag} = $tag;
37         $r->{template_args}{tags} = [$tag]; # For selector
38         $r->{template_args}{photos} =
39             [$pager->search_sorted_by_tag($tag->id)];
40     }
41 }
42
43 sub multi_search {
44     my ($self, @tags) = @_;
45     my $counter = "tagaaa";
46     my @stuff;
47     for my $tag (@tags) {
48         if (!$tag) { return }
49         push @stuff, { tag => $tag, id => $tag->id, counter => $counter++ };
50     }
51     my $sql = "select photo.id as id, photo.title as title, uploader, uploaded, x, y
52 from photo, ". (join ",", map{ "tagging ".$_->{counter} } @stuff).
53 " where ". (join " AND ", map { "$_->{counter}.tag=$_->{id} and photo.id
54 = $_->{counter}.photo" } @stuff);
55
56 $sql .= " order by photo.uploaded desc";
57     my $sth = $self->db_Main->prepare($sql);
58 }
59
60 sub list :Exported {
61     my ($self, $r) = @_;
62     my $page = $r->params->{page} || 1;
63     my $pager = Memories::Tagging->pager(
64         Memories->config->{photos_per_page}, $page
65     );
66     $r->{template_args}{pager} = $pager;
67     $r->objects([map {$_->tag} $pager->search_all]);
68 }
69
70 sub list_js :Exported {
71     my ($self, $r) = @_;
72     $r->objects([ $self->retrieve_all ]);
73 }
74
75 package Memories::Tagging;
76 use base qw(Memories::DBI);
77 use Class::DBI::Pager;
78 __PACKAGE__->columns(TEMP => qw/count/);
79 __PACKAGE__->columns(Essential => qw/id tag photo/);
80 __PACKAGE__->set_sql(summary => qq/
81 SELECT  id, tag, count(*) AS count
82 FROM tagging
83 GROUP BY tag
84 ORDER BY count DESC
85 LIMIT 75
86     /);
87 __PACKAGE__->set_sql(all => qq/
88 SELECT  id, tag, count(*) AS count
89 FROM tagging
90 GROUP BY tag
91 ORDER BY count DESC
92     /);
93 Memories::Tagging->has_a("photo" => "Memories::Photo");
94 Memories::Tagging->has_a("tag" => "Memories::Tag");
95
96 Memories::Photo->has_many(tags => ["Memories::Tagging" => "tag"]);
97 Memories::Photo->has_many(taggings => "Memories::Tagging");
98 Memories::Tag->has_many(photos => ["Memories::Tagging" => "photo"] );
99 Memories::Tag->has_many(taggings => "Memories::Tagging");
100
101 1;