]> git.decadent.org.uk Git - memories.git/blob - Memories/Tag.pm
1140185a307e45e07b328e919a463c9d4120c802
[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     $r->last_search();
42 }
43
44 sub multi_search {
45     my ($self, @tags) = @_;
46     my $counter = "tagaaa";
47     my @stuff;
48     for my $tag (@tags) {
49         if (!$tag) { return }
50         push @stuff, { tag => $tag, id => $tag->id, counter => $counter++ };
51     }
52     my $sql = "select photo.id as id, photo.title as title, uploader, uploaded, x, y
53 from photo, ". (join ",", map{ "tagging ".$_->{counter} } @stuff).
54 " where ". (join " AND ", map { "$_->{counter}.tag=$_->{id} and photo.id
55 = $_->{counter}.photo" } @stuff);
56
57 $sql .= " order by photo.uploaded desc";
58     my $sth = $self->db_Main->prepare($sql);
59 }
60
61 sub list :Exported {
62     my ($self, $r) = @_;
63     my $page = $r->params->{page} || 1;
64     my $pager = Memories::Tagging->pager(
65         Memories->config->{photos_per_page}, $page
66     );
67     $r->{template_args}{pager} = $pager;
68     $r->objects([map {$_->tag} $pager->search_all]);
69 }
70
71 sub list_js :Exported {
72     my ($self, $r) = @_;
73     $r->objects([ $self->retrieve_all ]);
74 }
75
76 package Memories::Tagging;
77 use base qw(Memories::DBI);
78 use Class::DBI::Pager;
79 __PACKAGE__->columns(TEMP => qw/count/);
80 __PACKAGE__->columns(Essential => qw/id tag photo/);
81 __PACKAGE__->set_sql(summary => qq/
82 SELECT  id, tag, count(*) AS count
83 FROM tagging
84 GROUP BY tag
85 ORDER BY count DESC
86 LIMIT 75
87     /);
88 __PACKAGE__->set_sql(all => qq/
89 SELECT  id, tag, count(*) AS count
90 FROM tagging
91 GROUP BY tag
92 ORDER BY count DESC
93     /);
94 __PACKAGE__->set_sql(user_summary => qq/
95 SELECT  tagging.id id, tag, count(*) AS count
96 FROM tagging, photo
97 WHERE tagging.photo = photo.id AND photo.uploader = ?
98 GROUP BY tag
99 ORDER BY count DESC
100 /);
101
102 Memories::Tagging->has_a("photo" => "Memories::Photo");
103 Memories::Tagging->has_a("tag" => "Memories::Tag");
104
105 Memories::Photo->has_many(tags => ["Memories::Tagging" => "tag"]);
106 Memories::Photo->has_many(taggings => "Memories::Tagging");
107 Memories::Tag->has_many(photos => ["Memories::Tagging" => "photo"] );
108 Memories::Tag->has_many(taggings => "Memories::Tagging");
109
110 1;