package Memories::SystemTag;
use strict;
use base qw(Memories::DBI Maypole::Model::CDBI::Plain);
__PACKAGE__->columns(Essential => qw/id name/);
__PACKAGE__->table("system_tag");

Memories::Photo->set_sql(sorted_by_system_tag => q/
SELECT photo.id as id, title, uploader, uploaded, x, y
FROM photo, system_tagging
WHERE system_tagging.photo = photo.id
    AND system_tagging.tag = ?
ORDER BY photo.uploaded DESC
/
);
sub view :Exported {
    my ($self, $r) = @_;
    my $tag;
    my $page = $r->params->{page} || 1;
    my $pager = Class::DBI::Pager::_pager("Memories::Photo",
        Memories->config->{photos_per_page}, $page);
    $r->{template_args}{pager} = $pager;

    if (!$r->objects) {
        $tag = $self->search(name => $r->{args}->[0])->first;
    } else {
        $tag = $r->objects->[0]; # Should hardly happen
    }
    $r->{template_args}{tag} = $tag;
    $r->{template_args}{tags} = [$tag]; # For selector
    $r->{template_args}{photos} =
        [$pager->search_sorted_by_system_tag($tag->id)];
    $r->last_search;
}

package Memories::SystemTagging;
use base qw(Memories::DBI);
use Class::DBI::Pager;
1;
