# $Id: EntryArchiveLink.pl,v 1.4 2003/06/21 19:17:17 akosut Exp $ # by Alexei Kosut # # The MTEntryArchiveLink tag works like MTArchiveLink, but lets you link # to non-preferred archives by template name or ID. For example, if you # have an Individual archive template named "Comments RSS", you could # provide a link to it using # # <$MTEntryArchiveLink archive_type="Individual" template_name="Comments RSS"$> # # archive_type can be omitted if there is only one archive type using # the given template. # # template_id can be used instead of template_name to specify a template # by ID number. # # MTEntryArchiveLink will only work for archives that have explicitly # specified file names. use MT; use MT::Template::Context; use MT::TemplateMap; use strict; use warnings; MT::Template::Context->add_tag(EntryArchiveLink => sub { my ($ctx, $args) = @_; my $blog = $ctx->stash('blog'); my $archive_type = $args->{'archive_type'}; my $tmpl_id = $args->{'template_id'}; my $tmpl_name = $args->{'template_name'} or $tmpl_id or return $ctx->error('MTEntryArchiveLink: template_id or template_name parameter required'); unless ($tmpl_id) { my @tmpls = MT::Template->load({ blog_id => $blog->id, name => $tmpl_name }); unless (@tmpls) { return $ctx->error("MTEntryArchiveLink: template '$tmpl_name' not found"); } $tmpl_id = $tmpls[0]->id; } my @maps; if ($archive_type) { @maps = MT::TemplateMap->load({ blog_id => $blog->id, template_id => $tmpl_id, archive_type => $archive_type }); } else { @maps = MT::TemplateMap->load({ blog_id => $blog->id, template_id => $tmpl_id }); } if (@maps == 0) { return $ctx->error("MTEntryAchiveLink: archive for template $tmpl_id not found"); } elsif (@maps > 1) { return $ctx->error("MTEntryAchiveLink: multiple archives found for template $tmpl_id"); } # We only care (right now) about templates with explicit templates my $file_tmpl = $maps[0]->file_template or return $ctx->error("MTEntryArchiveLink: no file template for template $tmpl_id"); my $builder = $ctx->stash('builder'); my $file = $builder->build($ctx, $builder->compile($ctx, $file_tmpl)); return $blog->archive_url . $file; });