# MKDoc Dublin Core Metadata Plugin
# Copyright Chris Croome, <chris@mkdoc.com>, 23 April 2001
# This script (total hack...) is released under the Perl Artistic Licence
#
# create an DC Metadata file
# http://dublincore.org/
package flo::plugin::XML::RDF::DC;
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::Editor;
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);
    
    # Get the link components in this document
    # We want these for dcq:references
    my $components_editor = new flo::Editor;
    $components_editor->parse_xml ($current_document->{Body});
#    my @links = grep (/^http:\/\//i, $components_editor->access ( class => [ 'link' ] ));
    my @links = $components_editor->access ( class => [ 'link' ] );
    
    # what is the ID of this document ?
    my $current_document_id = $current_document->{ID};
    
    # This is to get the redirects that point to this document
    # We want to use these for dcq:replaces
    my $redirect_table = table ('Redirect');
    my @redirects =  $redirect_table->search ( New_Path => $current_document->{Full_Path} )->fetch_all;
    my $cgix = cgi->new;
    foreach ($cgix->param) { $cgix->delete ($_) }
    foreach my $redirect (@redirects)
    {
        $cgix->path_info ( $redirect->{Old_Path} );
        $redirect->{Old_URI} = $cgix->self_url;
    }

    # this stuff is to get the full, qualified 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_first_name   = text2xml ($editor->{First_Name}); 
    my $creator_family_name  = text2xml ($editor->{Family_Name});
    my $creator_email              = $editor->{Email};
    my $creator_vcard_n = "$creator_family_name" . ";" . "$creator_first_name";

    # We want the name of the editor who last contributed to the document
    my $last_modified_first_name   = text2xml ($editor->{First_Name});
    my $last_modified_family_name  = text2xml ($editor->{Family_Name});
    my $last_modified_email              = $editor->{Email};
    my $last_modified_vcard_n = "$last_modified_family_name" . ";" . "$last_modified_first_name";
    
    # 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 = text2xml ($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_created = ($current_document->{Date_Created});
    $date_created    =~ s/ /T/;
    $date_created    =~ s/$/Z/;

    my $date_last_modified = ($current_document->{Date_Last_Modified});
    $date_last_modified    =~ s/ /T/;
    $date_last_modified    =~ s/$/Z/;
    
    my $title         = text2xml ($current_document->{Title});
    my $description   = text2xml ($current_document->{Description});
    
    my $publisher = text2xml ($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 = text2xml ($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 $lang = $current_document->{Lang};
    my $language_config = new lib::Config ("flo.plugin.explorer.Admin_LANG");
    my $language = $language_config->get($lang); 

    
    ##
    # <% loop name="Links" %>
    #    <% var name="[url|title|description]" %>
    # <% end-loop %>
    # JM: 2001.04.27. Yeeeeew! Think TEMPLATE.
    # ----------------------------------------
    my $hash = {
        Document                   => $current_document,
        Redirects		   => \@redirects,
	Links                      => \@links,
        Lang                       => $lang,
	Language                   => $language,
	Title                      => $title,
	Description                => $description,
	Publisher                  => $publisher,
	Creator_First_Name         => $creator_first_name,
	Creator_Family_Name        => $creator_family_name,
	Creator_VCard_N            => $creator_vcard_n,
	Creator_Email              => $creator_email,
	URI                        => $uri,
	Rights                     => $rights,
        Date_Created               => $date_created,
        Date_Last_Modified         => $date_last_modified,
        Last_Modified_First_Name   => $last_modified_first_name,
        Last_Modified_Family_Name  => $last_modified_family_name,
        Last_Modified_VCard_N      => $last_modified_vcard_n,
        Last_Modified_Email        => $last_modified_email,
    };
    
    # 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 = text2xml ($contributor->{First_Name});
	my $contributor_family_name = text2xml ($contributor->{Family_Name});
	my $contributor_email = $contributor->{Email};
	my $contributor_vcard_n = $contributor_family_name . ";" . $contributor_first_name;
	
	push @contrib, {
	    Contributor_First_Name        => $contributor_first_name,
	    Contributor_Family_Name       => $contributor_family_name,
	    Contributor_Email            => $contributor_email,
            Contributor_VCard_N          => $contributor_vcard_n,
#	    $hash->{Contributor_VCard_N} => $contributor_vcard_n,
	};
    }
    $hash->{Contributors} = \@contrib;


    
    # rock and roll!
  flo::HTTP::Header->out
      ( new flo::Template ( file => template ('dc', 'en'),
                            hash => $hash ) );
    
    # we don't want to run any further
    return 'TERMINATE';
}


1;

