]> git.decadent.org.uk Git - memories.git/blob - Memories/SystemTag.pm
3a9fbbf1de72f6ee1d9d8f2ea272bddb6ddcfe5d
[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_tagging
10 WHERE system_tagging.photo = photo.id
11     AND system_tagging.tag = ?
12 ORDER BY photo.uploaded DESC
13 /
14 );
15 sub view :Exported {
16     my ($self, $r) = @_;
17     my $tag;
18     my $page = $r->params->{page} || 1;
19     my $pager = Class::DBI::Pager::_pager("Memories::Photo",
20         Memories->config->{photos_per_page}, $page);
21     $r->{template_args}{pager} = $pager;
22
23     if (!$r->objects) {
24         $tag = $self->search(name => $r->{args}->[0])->first;
25     } else {
26         $tag = $r->objects->[0]; # Should hardly happen
27     }
28     $r->{template_args}{tag} = $tag;
29     $r->{template_args}{tags} = [$tag]; # For selector
30     $r->{template_args}{photos} =
31         [$pager->search_sorted_by_system_tag($tag->id)];
32     $r->last_search;
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;