# MKDoc IMS Metadata Plugin
# Copyright Chris Croome, <chris@mkdoc.com>, 23 April 2001
# This script (total hack...) is released under the Perl Artistic Licence
#
# create an IMS Metadata file
# http://imsproject.com/metadata/index.html
package flo::plugin::XML::IMS;
use strict;
use lib::sql::Table;
use lib::cgi::CGI;
use flo::Standard;
use lib::Exception;
use lib::util::Text2Html;
use flo::Template::ISODate;
use lib::cgi::CGI;
use lib::sql::Condition;
use flo::Object;
use flo::CGI;
use vars qw /@ISA/;
@ISA = qw /flo::Object/;


sub main
{
    my $class = shift;
    my $cgi = lib::cgi::CGI->get;
    
    # if this module is not concerned, let's return ASAP
    my $config = $class->config;
    my $http_name = $config->{http_name};
    my $q_http_name = quotemeta ($http_name);
    my $raw_path_info = raw_path_info;
    return unless ( ($raw_path_info =~ /.*\/$q_http_name$/o) );

    # makes all uri's absolute
    $flo::CGI::FULL_URI = 1;
    
    # otherwise, let's process the request
    $raw_path_info =~ s/$q_http_name$//;
    my $path_info = $raw_path_info;
    
    # retrieve the 'Document' table object
    my $document_table = table ('Document');

    # retrieves the current document
    my $current_document = $document_table->get (Full_Path => $path_info);
    return unless (defined $current_document);
    
    # what is the ID of this document ?
    my $current_document_id = $current_document->{ID};
    
    # this stuff is to get the URI of the current document
    my $uri = $current_document->URI;
    
    # We need the name of the creator of the current document
    my $editor_table = table ('Editor');
    my $editor = $editor_table->get ( $current_document->{Editor_Created_ID} );
    my $creator_firstname   = $editor->{First_Name}; 
    my $creator_familyname  = $editor->{Family_Name};
    my $creator_email              = $editor->{Email};
    my $creator_vcard_n = "$creator_familyname" . ";" . "$creator_firstname";
    
    # From JM 2001.06.26
    # This can be written more elegantly
    my @document_contributor = $current_document->Contributors;
    
    # Subject/keywords need to loop rather than spliting with ,'s 
    my $keywords = $current_document->{Keywords};
    
    # Remove newlines and tabs 
    $keywords =~ s/\n/ /gsm;
    $keywords =~ s/\f/ /gsm;
    $keywords =~ s/\r/ /gsm;
    $keywords =~ s/\t/ /gsm;
    
    # Remove spaces after commers
    $keywords =~ s/, +/,/g;
    
    # Remove unneeded whitespace
    # $keywords =~ s/ +/ /g;
    # from JM 2001.04.24: what about tabs?
    $keywords =~ s/\s+/ /g;
    my @keywords = split /,/, $keywords;
    
    # Munge the MySQL date into the W3C format (http://www.w3.org/TR/NOTE-datetime)
    # this should really be done some better way... but it works...
    my $date = ($current_document->{Date_Created});
    $date   =~ s/ /T/;
    $date   =~ s/$/Z/;
    
    my $title         = $current_document->{Title};
    my $description   = $current_document->{Description};
    
    my $publisher = $current_document->{Publisher};
    
    # Remove newlines and tabs 
    $publisher =~ s/\n/ /gsm;
    $publisher =~ s/\f/ /gsm;
    $publisher =~ s/\r/ /gsm;
    $publisher =~ s/\t/ /gsm;
    
    # Remove spaces after commas
    $publisher =~ s/, +/,/g;
    
    # Remove unneeded whitespace
    $publisher =~ s/ +/ /g;

    my $rights = text2html ($current_document->{Rights});
    
    # Remove newlines and tabs 
    $rights =~ s/\n/ /gsm;
    $rights =~ s/\f/ /gsm;
    $rights =~ s/\r/ /gsm;
    $rights =~ s/\t/ /gsm;
    
    # Remove spaces after commas
    $rights =~ s/, +/,/g;
    
    
    # Remove unneeded whitespace
    $rights =~ s/ +/ /g;
    
    my $language = $current_document->{Lang};
    
    # JM: 2001.04.27. Yeeeeew! Think TEMPLATE.
    # ----------------------------------------
    my $hash = {
	Language            => $language,
	Title               => $title,
	Description         => $description,
	Publisher           => $publisher,
	Creator_FirstName   => $creator_firstname,
	Creator_FamilyName  => $creator_familyname,
	Creator_VCard_N     => $creator_vcard_n,
	Creator_Email       => $creator_email,
	URI                 => $uri,
	Rights              => $rights,
    };
    
    # loops are made using array of hashes
    # lets go for the Keywords fields
    my @dc_keywords = ();
    foreach my $keyword (@keywords)
    {
	push @dc_keywords, { Keyword => $keyword };
    }
    $hash->{DC_Keywords} = \@dc_keywords;
    
    # now the document contributors...
    my @contrib = ();
    foreach my $contributor (@document_contributor)
    {
	my $contributor_first_name = $contributor->{First_Name};
	my $contributor_family_name = $contributor->{Family_Name};
	my $contributor_email = $contributor->{Email};
	my $contributor_vcard_n = $contributor_family_name . ";" . $contributor_first_name;
	
	push @contrib, {
	    Contributor_FirstName        => $contributor_first_name,
	    Contributor_FamilyName       => $contributor_family_name,
	    Contributor_Email            => $contributor_email,
            Contributor_VCard_N          => $contributor_vcard_n,
#	    $hash->{Contributor_VCard_N} => $contributor_vcard_n,
	};
    }
    $hash->{Contributor} = \@contrib;
    
    # rock and roll!
  flo::HTTP::Header->out
      ( new flo::Template ( file => template ('ims', 'en'),
                            hash => $hash ) );
    
    # we don't want to run any further
    return 'TERMINATE';
}


1;



