package flo::plugin::explorer::Check;
use flo::Standard;
use strict;


##
# __PACKAGE__->document_add_check;
# --------------------------------
#   Checks wether the document that we are trying to add is valid
#   or not.
##
sub document_add_check
{
    my $class = shift;
    my $cgi   = cgi;
    
    # Do some error checking.
    my $err = {};
    
    $err->{Null_Title} = 1       unless (flo::plugin::explorer::Check::document_add_has_title());
    $err->{Null_Description} = 1 unless (flo::plugin::explorer::Check::document_add_has_description());
    $err->{Null_Keywords} = 1    unless (flo::plugin::explorer::Check::document_add_has_keywords());
    $err->{Null_Language} = 1    unless (flo::plugin::explorer::Check::document_add_has_language());
    $err->{Null_Template} = 1    unless (flo::plugin::explorer::Check::document_add_has_template());
    
    return $err;
}


##
# __PACKAGE__->document_delete_check;
# -----------------------------------
#   Checks wether the document that we are trying to delete can be
#   deleted or not
##
sub document_delete_check
{
    my $class = shift;
    my $editor = shift;
    my $document = shift;
    
    # Do some error checking.
    my $err = {};
    $err->{Has_Children}  = 1 if (flo::plugin::explorer::Check::document_has_children ($document));
    $err->{Last_Document} = 1 if (flo::plugin::explorer::Check::document_is_last ($document, $editor));
    
    return $err;
}


##
# __PACKAGE__->document_modify_metadata_check;
# --------------------------------------------
#   Checks wether the document that we are trying to add is valid
#   or not
##
sub document_modify_metadata_check
{
    my $class = shift;
    my $cgi   = cgi;
    my $document = shift;
    
    # Do some error checking
    my $err = {};
    
    $err->{Null_Title} = 1 unless (flo::plugin::explorer::Check::document_modify_metadata_has_title());

    if (defined $document->{Parent_ID})
    {
	$err->{Null_URI_Name} = 1 unless (flo::plugin::explorer::Check::document_modify_metadata_has_uri_name());
	unless ($err->{Null_URI_Name} or $err->{Invalid_URI_Name})
	{
	    $err->{URI_Name_Exists} = 1  if (flo::plugin::explorer::Check::document_modify_metadata_name_exists ($document));
	}
    }
    $err->{Null_Description} = 1 unless (flo::plugin::explorer::Check::document_modify_metadata_has_description());
    $err->{Null_Keywords} = 1    unless (flo::plugin::explorer::Check::document_modify_metadata_has_keywords());
    $err->{Null_Language} = 1    unless (flo::plugin::explorer::Check::document_modify_metadata_has_language());
    $err->{Null_Template} = 1    unless (flo::plugin::explorer::Check::document_modify_metadata_has_template());

    # templates names are used to open files, we want to check
    # if there's no weird characters in this parameter which could
    # be used as an exploit. (Happy Bruno (c)(tm)(r))
    $err->{Null_Template} = 1 if ($cgi->param ('Template') !~ /^[A-Za-z0-9-_.]+$/);
    
    return $err;
}


sub document_has_children
{
    my $document = shift;
    my @children = $document->Children;
    return scalar @children > 0;
}


sub document_is_last
{
    my $document = shift;
    my $editor   = shift;
    my @children = $document->Children;
    return if (@children);
    
    my $base_document_table = table ('Base_Document');
    my @docs = $base_document_table->select ( Editor_ID => $editor->{ID} )->fetch_all;
    return (@docs == 1);
}


sub document_modify_metadata_invalid_uri_name
{
    my $document = shift;
    my $cgi = cgi;
    return flo::plugin::explorer::Admin->urlize ($cgi->param ('Name')) ne $cgi->param ('Name');
}


sub document_modify_metadata_name_exists
{
    my $document = shift;
    my $document_table = table ('Document');
    my $cgi = cgi;
    my $val = flo::plugin::explorer::Admin->urlize ($cgi->param ('Name'));
    
    # grab the document that would have the same URI as this document
    # and check that this document has the same ID. If not, it means
    # that the user if trying to change to URI name that would conflict
    # with another document
    my $other_document = $document_table->get ( Parent_ID => $document->{Parent_ID},
						Name      => $val );
    
    if (defined $other_document)
    {
	return ($other_document->{ID} != $document->{ID});
    }
    else
    {
	return;
    }
}


sub document_modify_metadata_has_uri_name
{
    my $cgi = cgi;
    my $val = $cgi->param ('Name');
    return (defined $val and $val ne '' );
}


sub document_modify_metadata_has_title
{
    my $cgi = cgi;
    my $val = $cgi->param ('Title');
    return (defined $val and $val ne '' );
}


sub document_modify_metadata_has_description
{
    my $cgi = cgi;
    my $val = $cgi->param ('Description');
    return (defined $val and $val ne '' );
}


sub document_modify_metadata_has_keywords
{
    my $cgi = cgi;
    my $val = $cgi->param ('Keywords');
    return (defined $val and $val ne '' );
}


sub document_modify_metadata_has_language
{
    my $cgi = cgi;
    my $val = $cgi->param ('Lang');
    return (defined $val and $val ne '' );
}


sub document_modify_metadata_has_template
{
    my $cgi = cgi;
    my $val = $cgi->param ('Template');
    return (defined $val and $val ne '' );
}


sub document_add_has_title
{
    my $cgi = cgi;
    my $val = $cgi->param ('Title');
    return (defined $val and $val ne '' );
}


sub document_add_has_description
{
    my $cgi = cgi;
    my $val = $cgi->param ('Description');
    return (defined $val and $val ne '' );
}


sub document_add_has_keywords
{
    my $cgi = cgi;
    my $val = $cgi->param ('Keywords');
    return (defined $val and $val ne '' );
}


sub document_add_has_language
{
    my $cgi = cgi;
    my $val = $cgi->param ('Lang');
    return (defined $val and $val ne '' );
}


sub document_add_has_template
{
    my $cgi = cgi;
    my $val = $cgi->param ('Template');
    return (defined $val and $val ne '' );
}


1;







