]> git.decadent.org.uk Git - memories.git/blob - Memories/SystemTag.pm
Branched from version 1.1.
[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 }
34
35 package Memories::SystemTagging;
36 use base qw(Memories::DBI);
37 use Class::DBI::Pager;
38 __PACKAGE__->table("system_tagging");
39 __PACKAGE__->columns(TEMP => qw/count/);
40 __PACKAGE__->columns(Essential => qw/id tag photo/);
41 __PACKAGE__->set_sql(summary => qq/
42 SELECT  id, system_tag, count(*) AS count
43 FROM system_tagging
44 GROUP BY system_tag
45 ORDER BY count DESC
46 LIMIT 50
47     /);
48 __PACKAGE__->set_sql(all => qq/
49 SELECT  id, tag, count(*) AS count
50 FROM system_tagging
51 GROUP BY system_tag
52 ORDER BY count DESC
53     /);
54 Memories::SystemTagging->has_a("photo" => "Memories::Photo");
55 Memories::SystemTagging->has_a("tag" => "Memories::SystemTag");
56
57 Memories::Photo->has_many(system_tags => ["Memories::SystemTagging" => "system_tag"]);
58 Memories::Photo->has_many(system_taggings => "Memories::SystemTagging");
59 Memories::SystemTag->has_many(photos => ["Memories::SystemTagging" => "photo"] );
60 Memories::SystemTag->has_many(system_taggings => "Memories::SystemTagging");
61
62 1;