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