Document validate.pl

From EPrints Documentation
Jump to: navigation, search

EPrints 3 Reference: Directory Structure - Metadata Fields - Repository Configuration - XML Config Files - XML Export Format - EPrints data structure - Core API - Data Objects


Back to cfg.d

document_validate.pl contains configuration for validating Document data objects.

The validate_document function tests the validity of the values set for the metadata fields for a Document. This usually happens when a user submits the form (e.g. clicks on Previous, Save and Return or Next) on the Upload stage.

  • $document is the Document object being validated.
  • $repository is a Repository object.
  • $for_archive is a boolean indicating whether the current validation is a precursor to the item being moved to the live archive. Its value make no difference for the default validate_document function.

Example

In this example, the document is checked to validate:

  1. formatdesc is set if the format is set to other.
  2. security is not public if embargo_date is set or the document is not really emabargoed.
  3. A full embargo_date is set to ensure there is no ambiguity about when an embargo will be lifted.
  4. The embargo_date is set in the future.
$c->{validate_document} = sub
{
    my( $document, $repository, $for_archive ) = @_;

    my @problems = ();

    my $xml = $repository->xml();

    # CHECKS IN HERE

    # "other" documents must have a description set
    if( $document->value( "format" ) eq "other" &&
       !EPrints::Utils::is_set( $document->value( "formatdesc" ) ) )
    {
        my $fieldname = $xml->create_element( "span", class=>"ep_problem_field:documents" );
        push @problems, $repository->html_phrase(
                    "validate:need_description" ,
                    type=>$document->render_citation("brief"),
                    fieldname=>$fieldname );
    }

    # security can't be "public" if date embargo set
    if( $document->value( "security" ) eq "public" &&
        EPrints::Utils::is_set( $document->value( "date_embargo" ) ) )
    {
        my $fieldname = $xml->create_element( "span", class=>"ep_problem_field:documents" );
        push @problems, $repository->html_phrase(
                    "validate:embargo_check_security" ,
                    fieldname=>$fieldname );
    }

    # embargo expiry date must be a full year, month and day and must be in the future
    if( EPrints::Utils::is_set( $document->value( "date_embargo" ) ) )
    {
        my $value = $document->value( "date_embargo" );
        my ($year, $month, $day) = split( '-', $value );
        if ( !EPrints::Utils::is_set( $month ) || !EPrints::Utils::is_set( $day ) )
        {
            my $fieldname = $xml->create_element( "span", class=>"ep_problem_field:documents" );
                        push @problems, $repository->html_phrase( "validate:embargo_incomplete_date", fieldname=>$fieldname );
        }
        else {
            my ($thisyear, $thismonth, $thisday) = EPrints::Time::get_date_array();
            if( $year < $thisyear || ( $year == $thisyear && $month < $thismonth ) ||
                ( $year == $thisyear && $month == $thismonth && $day <= $thisday ) )
            {
                my $fieldname = $xml->create_element( "span", class=>"ep_problem_field:documents" );
                push @problems,
                    $repository->html_phrase( "validate:embargo_invalid_date",
                    fieldname=>$fieldname );
            }
        }
    }
    return( @problems );
};