# Copyright 2001-2004 Six Apart. This code cannot be redistributed without
# permission from www.sixapart.com.
#
# $Id: Atom.pm,v 1.4 2004/05/11 02:26:40 hirata Exp $

package MT::Atom;
use strict;

package MT::Atom::Entry;
use base qw( XML::Atom::Entry );

sub new_with_entry {
    my $class = shift;
    my($entry) = @_;
    my $atom = $class->new;
    $atom->title($entry->title);
    $atom->summary($entry->excerpt);
    $atom->content($entry->text);
    my $mt_author = MT::Author->load($entry->author_id);
    my $atom_author = new XML::Atom::Person();
    $atom_author->set('name', $mt_author->name());
    $atom_author->set('url', $mt_author->url());
    $atom_author->set('email', $mt_author->email());
    $atom->author($atom_author);
    my $co = sprintf "%04d-%02d-%02dT%02d:%02d:%02d",
        unpack 'A4A2A2A2A2A2', $entry->created_on;
    my $blog = MT::Blog->load($entry->blog_id);
    my $so = $blog->server_offset;
    $so = sprintf "%s%02d00", $so < 0 ? '-' : '+', abs(int $so);
    $co .= $so;
    $atom->issued($co);
    $atom->link($entry->permalink);
    my ($host) = $blog->site_url =~ m!^https?://([^/:]+)(:\d+)?/!;
    $atom->id('tag:' . $host . ':post:' . $entry->id);
    $atom;
}

1;
