]> git.decadent.org.uk Git - memories.git/blob - Memories/SystemTag.pm
cdb8e14169bb9d360f57977a3f92ccb86a994f50
[memories.git] / Memories / SystemTag.pm
1 package Memories::SystemTag;
2 use strict;
3 use base qw(Memories::DBI Maypole::Model::CDBI::Plain);
4 __PACKAGE__->columns(Essential => qw/id name/);
5 __PACKAGE__->table("system_tag");
6
7 Memories::Photo->set_sql(sorted_by_system_tag => q/
8 SELECT photo.id as id, title, uploader, uploaded, x, y
9 FROM photo, system_tag, system_tagging
10 WHERE system_tagging.photo = photo.id
11     AND system_tagging.tag = system_tag.id
12     AND system_tag.id = ?
13 ORDER BY photo.uploaded DESC
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->objects) {
25         $tag = $self->search(name => $r->{args}->[0])->first;
26     } else {
27         $tag = $r->objects->[0]; # Should hardly happen
28     }
29     $r->{template_args}{tag} = $tag;
30     $r->{template_args}{tags} = [$tag]; # For selector
31     $r->{template_args}{photos} =
32         [$pager->search_sorted_by_system_tag($tag->id)];
33     $r->last_search;
34 }
35
36 package Memories::SystemTagging;
37 use base qw(Memories::DBI);
38 use Class::DBI::Pager;
39 __PACKAGE__->table("system_tagging");
40 __PACKAGE__->columns(TEMP => qw/count/);
41 __PACKAGE__->columns(Essential => qw/id tag photo/);
42 __PACKAGE__->set_sql(summary => qq/
43 SELECT  id, system_tag, count(*) AS count
44 FROM system_tagging
45 GROUP BY system_tag
46 ORDER BY count DESC
47 LIMIT 50
48     /);
49 __PACKAGE__->set_sql(all => qq/
50 SELECT  id, tag, count(*) AS count
51 FROM system_tagging
52 GROUP BY system_tag
53 ORDER BY count DESC
54     /);
55 Memories::SystemTagging->has_a("photo" => "Memories::Photo");
56 Memories::SystemTagging->has_a("tag" => "Memories::SystemTag");
57
58 Memories::Photo->has_many(system_tags => ["Memories::SystemTagging" => "system_tag"]);
59 Memories::Photo->has_many(system_taggings => "Memories::SystemTagging");
60 Memories::SystemTag->has_many(photos => ["Memories::SystemTagging" => "photo"] );
61 Memories::SystemTag->has_many(system_taggings => "Memories::SystemTagging");
62
63 1;