Adding an Institution Field to authors

From EPrints Documentation
Revision as of 13:21, 5 October 2010 by Pm705 (talk | contribs)
Jump to: navigation, search


Note to admin people: move this to the appropriate place, I can't work out how to add it

I wanted to have an institution associated with each author, however the same thing applies whatever the field would be

Alter the Workflow

  • Edit <EPROOT>/archives/<ARCHIVE_ID>/cfg/cfg.d/eprint_fields.pl
  • Add in the field you want for each of the people-type fields:
         {
           'name' => 'creators',
           'type' => 'compound',
           'multiple' => 1,
           'fields' => [
                         {
                           'sub_name' => 'name',
                           'type' => 'name',
                           'hide_honourific' => 1,
                           'hide_lineage' => 1,
                           'family_first' => 1,
                         },
                         {
                           'sub_name' => 'id',
                           'type' => 'text',
                           'input_cols' => 20,
                           'allow_null' => 1,
                         },
                          {
                           'sub_name' => 'institution',
                           'type' => 'text',
                           'input_cols' => 20,
                         }
                       ],
           'input_boxes' => 4,
         },
  • Update the database: <EPROOT>/bin/epadmin update_database_structure <ARCHIVE_ID>

Altering the lookup script

The name fields come with an auto-completer, which (obviously) doesn't know about your new field.

use EPrints;

use strict;

my $session = EPrints::Session->new();

# security?

my $content = "text/xml";
$session->send_http_header( content_type=>$content );
my $family = $session->param( "_name_family" );
my $given = $session->param( "_name_given" );
my $id = $session->param( "_id" );
my $institution = $session->param( "_institution" ); 

my $database = $session->get_database;
my $dataset = $session->get_repository->get_dataset( "eprint" );
my $name_field = $dataset->get_field( "creators_name" );
my $id_field = $dataset->get_field( "creators_id" );
my $institution_field = $dataset->get_field( "creators_institution" ); 

my @fields = ($name_field->get_sql_names, $id_field->get_sql_names, $institution_field->get_sql_names); 

my $Q_table = $database->quote_identifier($dataset->get_sql_table_name);
my $Q_name_table = $database->quote_identifier($dataset->get_sql_sub_table_name($name_field));
my $Q_id_table = $database->quote_identifier($dataset->get_sql_sub_table_name($id_field));
my $Q_institution_table = $database->quote_identifier($dataset->get_sql_sub_table_name($institution_field));

my $Q_eprintid = $database->quote_identifier( "eprintid" );
my $Q_pos = $database->quote_identifier( "pos" );
my $Q_num_matches = $database->quote_identifier( "num_matches" );
my $Q_eprint_status = $database->quote_identifier( "eprint_status" ); 

my $sql = "SELECT COUNT($Q_table.$Q_eprintid) AS $Q_num_matches,".
       join(",", map { $database->quote_identifier($_) } @fields).
       " FROM $Q_table".
       " LEFT JOIN $Q_name_table".
       " ON $Q_table.$Q_eprintid=$Q_name_table.$Q_eprintid".
       " LEFT JOIN $Q_id_table".
       " ON $Q_name_table.$Q_eprintid=$Q_id_table.$Q_eprintid ".
       " AND $Q_name_table.$Q_pos=$Q_id_table.$Q_pos ".
      " LEFT JOIN $Q_institution_table".
      " ON $Q_institution_table.$Q_pos = $Q_id_table.$Q_pos". 
      " AND $Q_institution_table.$Q_eprintid = $Q_id_table.$Q_eprintid ".
       " WHERE ".
       " $Q_table.$Q_eprint_status=".$database->quote_value( "archive" );
if( EPrints::Utils::is_set( $family ) )
{
   $sql .= " AND ".$database->quote_identifier("creators_name_family")
                ." ILIKE "
                .$database->quote_value( '%' . EPrints::Database::prep_like_value($family).'%');
}
if( EPrints::Utils::is_set( $given ) )
{
    $sql .= " AND ".$database->quote_identifier("creators_name_given")
                ." ILIKE "
                .$database->quote_value( '%' . EPrints::Database::prep_like_value($given).'%');
}
if( EPrints::Utils::is_set( $id ) )
{
    $sql .= " AND ".$database->quote_identifier("creators_id")
                ." ILIKE "
                .$database->quote_value( '%' . EPrints::Database::prep_like_value($id).'%');
}
$sql .= "GROUP BY ".join(",",map { $database->quote_identifier($_) } @fields) .
        " ORDER BY $Q_num_matches DESC," .
        $database->quote_identifier("creators_name_family").",".
        $database->quote_identifier("creators_name_given");
my @rows;
my $sth = $session->get_database->prepare_select( $sql, 'limit' => 40 );
$session->get_database->execute( $sth , $sql );
while( my @row = $sth->fetchrow_array )
{
   my $cnt = shift @row;
   my $name = $name_field->value_from_sql_row( $session, \@row );
   my $id = $id_field->value_from_sql_row( $session, \@row );
   my $institution = $institution_field->value_from_sql_row( $session, \@row );
   my $item = {};
   push @rows, $item;
   my $frag = $session->make_doc_fragment;
   $frag->appendChild( $name_field->render_single_value( $session, $name ) );
   if( EPrints::Utils::is_set( $id ) )
   {
       $frag->appendChild( $session->make_text( " " ) );
       $frag->appendChild( $id_field->render_single_value( $session, $id ) );
   }
   my $small = $session->make_element( "small" );
   $frag->appendChild( $small );
   if( EPrints::Utils::is_set( $institution ) )
   {
      $small->appendChild( $session->make_text( " [" ) );
      $small->appendChild( $id_field->render_single_value( $session, $institution ) );
      $small->appendChild( $session->make_text( "] " ) );
   }
   $small->appendChild( $session->make_text( " (author of ".$cnt." item".($cnt>1?"s":"")." in this repository)" ) );
   $item->{xhtml} = $frag;
   $item->{values} = [
     "for:value:relative:_name_family" => $name->{family},
     "for:value:relative:_name_given" => $name->{given},
     "for:value:relative:_name_honourific" => $name->{honourific},
     "for:value:relative:_name_lineage" => $name->{lineage},
     "for:value:relative:_id" => $id,
     "for:value:relative:_institution" => $institution,
   ];
}
$sth->finish();

my $ul = EPrints::Extras::render_lookup_list( $session, \@rows );

$session->send_http_header( content_type => "text/xml; charset=UTF-8" );

binmode(STDOUT,":utf8");
print <<END;
<?xml version="1.0" encoding="UTF-8" ?>
END

print EPrints::XML::to_string( $ul, "utf-8", 1 );

EPrints::XML::dispose( $ul );

$session->terminate;