<?php
/**
 * The page generated by this template depends on the publication type.  Each
 * publication type may appear differently. You can find the publication
 * templates for each type in the pub_types folder. The files have the same
 * name as the type.  If a publication type is not found in that file then the
 * generic.inc template is used for display of the publication.
 */

$pub = $variables['node']->pub;

// expand the title
$pub = chado_expand_var($pub, 'field', 'pub.title');
$pub = chado_expand_var($pub, 'field', 'pub.volumetitle');

// get the citation
$values = array(
  'pub_id' => $pub->pub_id,
  'type_id' => array(
    'name' => 'Citation',
  ),
);
$citation = chado_generate_var('pubprop', $values);
$citation = chado_expand_var($citation, 'field', 'pubprop.value');

// get the abstract
$values = array(
  'pub_id' => $pub->pub_id,
  'type_id' => array(
    'name' => 'Abstract',
  ),
);
$abstract = chado_generate_var('pubprop', $values);
$abstract = chado_expand_var($abstract, 'field', 'pubprop.value');
$abstract_text = '';
if ($abstract) {
  $abstract_text = htmlspecialchars($abstract->value);
}

// get the author list
$values = array(
  'pub_id' => $pub->pub_id,
  'type_id' => array(
    'name' => 'Authors',
  ),
);
$authors = chado_generate_var('pubprop', $values);
$authors = chado_expand_var($authors, 'field', 'pubprop.value');
$authors_list = 'N/A';
if ($authors) {
  $authors_list = $authors->value;
}

// get the first database cross-reference with a url
$options = array('return_array' => 1);
$pub = chado_expand_var($pub, 'table', 'pub_dbxref', $options);
$dbxref = NULL;
if ($pub->pub_dbxref) {
  foreach ($pub->pub_dbxref as $index => $pub_dbxref) {
    if ($pub_dbxref->dbxref_id->db_id->urlprefix) {
      $dbxref = $pub_dbxref->dbxref_id;
    }
  }
}

// get the URL
// get the author list
$values = array(
  'pub_id' => $pub->pub_id,
  'type_id' => array(
    'name' => 'URL',
  ),
);
$options = array('return_array' => 1);
$urls = chado_generate_var('pubprop', $values, $options);
$urls = chado_expand_var($urls, 'field', 'pubprop.value');
$url = '';
if (count($urls) > 0) {
  $url = $urls[0]->value;
} ?>
<div class="tripal_pub-data-block-desc tripal-data-block-desc"></div> <?php

// To simplify the template, we have a subdirectory named 'pub_types'.  This directory
// should have include files each specific to a publication type. If the type is
// not present then the base template will be used, otherwise the template in the
// include file is used.
$inc_name = strtolower(preg_replace('/ /', '_', $pub->type_id->name)) . '.inc';
$inc_path = DRUPAL_ROOT . '/' . drupal_get_path('module', 'tripal_pub') . "/theme/templates/pub_types/$inc_name";
if (file_exists($inc_path)) {
  require_once "pub_types/$inc_name";
}
else {
  require_once "pub_types/default.inc";
}